var maxMobileDevice = 550;
if (typeof(window.innerWidth) == "number"){
	windowWidth = window.innerWidth;
}else {
	windowWidth = document.documentElement.clientWidth;
}
function IsTablet(){
	var android =/android/i;
	var ios =/(iphone|ipod|ipad)/i;
	var mobile =/mobile/i;
	if ((android.test(navigator.userAgent) || ios.test(navigator.userAgent) && mobile.test(navigator.userAgent)==false)){
		return true;
	}
	return false;
}
function IsMobileorTablet(){
	var android =/android/i;
	var ios =/(iphone|ipod|ipad)/i;
	var mobile =/mobile/i;
	if (android.test(navigator.userAgent) || ios.test(navigator.userAgent) || mobile.test(navigator.userAgent)){
		return true;
	}
	return false;
}
function IsIElt9(){
	var ie8 = /MSIE 8/i;
	var ie7 = /MSIE 7/i;
	var ie6 = /MSIE 6/i;
	if(ie8.test(navigator.userAgent) || ie7.test(navigator.userAgent)){
		return true;
	}
	return false;	
}

function popUp(pPath, pWidth, pHeight) 
{
	window.open(pPath, "HobbyZonePopUp", "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=" + pWidth + ",height=" + pHeight + ",left=50,top=50");
}
	/* Cookies */
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
	
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) {
			return c.substring(nameEQ.length,c.length);
		}
	}
	
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

//for Blade Chooser
function DisplayHeliItem(featureHeli){
	$("div.chooseBladeDetail").removeClass("featured").fadeOut(200);
	setTimeout("fadeFeature()", 200);
	$('#' + featureHeli).addClass("featured");
	$("div#helisBladeChooser").pngFix();	
	//var featHeliImg = $('#' + featureHeli + ' div.img').css("background-image");
	//track
	try{
	var thisPage = "Home Page";
	var strUrl = document.location.href.toLowerCase();
	if (strUrl.match("/helis") != null){
		thisPage = "Helis Page";
	}
	pageTracker._trackEvent("Blade Chooser", featureHeli, "Load - " + thisPage);
	}catch(err){}
}
function fadeFeature(){
	$('.featured').fadeIn(700);
}

//for YouTube fed Video pages
function gup( name ){//get Url Parameters
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
function PageVideos(){
	$('#vidPaging').html("");
	$('#vidPages').css('left', '0px');
//groups videos by 10 and create a page number link for each group
		var totalVideoRows = ($("#dlVideos #vidThumbs a").length)/10;
		if (totalVideoRows > 1){
			$('#vidPaging').append("<a href='#prev'>&lt;</a>");
		}
		for (var i=0;i<totalVideoRows; i++){
			var firstTen = $('#dlVideos #vidThumbs a:lt(10)');
			$('#vidPages').append("<div class='page' id='page" + (i+1) + "'><div class='pageInner'></div></div><!-- endpage -->");
			$('#page' + (i+1) + ' div.pageInner').html(firstTen);
			$('#vidPaging').append("<a href='#page" + (i+1) + "' id='paging" + (i+1) + "'>" + (i+1) + "</a>");
		}
		if (totalVideoRows > 1){
			$('#vidPaging').append("<a href='#next'>&gt;</a>");
		}
		$("#vidPaging a:nth-child(2)").addClass("active");
		$('#vidPaging a').click(function(event){
			event.preventDefault();
			//detect y value for the group of pages and animate to the next page or to a certain page's value
			var showPage = $(this).attr("href");
			var vidsY = $('#vidPages').position();
			vidsY = vidsY.top;
			
			if((showPage.match("#next") == null)  && (showPage.match("#prev") == null)){
				$('#vidPaging a').removeClass("active");
				$(this).addClass("active");
				var pageNum = $(this).text();
				$('#vidPages').animate({top: (pageNum -1)* -370}, 500);
			}else if (showPage.match("#next") == "#next"){
				if ((totalVideoRows-1)*-370 < vidsY){
				$("#vidPaging a.active").removeClass("active").next().addClass("active");
				$('#vidPages').animate({top: '-=370'}, 500);
				}
			}else{
				if(-50 > vidsY){
				$("#vidPaging a.active").removeClass("active").prev().addClass("active");
				$('#vidPages').animate({top: '+=370'}, 500);
				}
			}
		});

		//test if we are loading a specific video from the URL - else load the first video
		var vidId = gup("vidId");
	if (vidId != "" && $("a#"+vidId).length){
	LoadVideo($("a#"+vidId).attr("href"), $("a#"+vidId+" span.vidTitle").html(), $("a#"+vidId+" span.vidDesc").html());
	}else {
	LoadVideo($("#vidPages a:first-child").attr("href"), $("#vidPages a:first-child span.vidTitle").html(), $("#vidPages a:first-child span.vidDesc").html());
	}

}

function LoadVideo(vidLoc, vidTitle, vidDesc){
//load video through SWFObject and add video data to sidebar
	var swfplayer = vidLoc;
	var so = new SWFObject(swfplayer, "myytplayer", "640", "386", "9", "#121d25");
		so.addParam("allowScriptAccess", "always");
		so.addParam("wmode", "opaque");
		so.write("ytapiplayer");
	var title = vidTitle;
	var desc = vidDesc;
	$("#ytplayerData").html("<h2>" + vidTitle + "</h2><div class='inner'>" + vidDesc + "</div>");
	$('#ytplayerData').jScrollPane({showArrows:true, scrollbarWidth:14, dragMaxHeight:100});
	//$"#ytplayerData').scrollTop(0);
// track
	try{
	var vidPage = "Videos Page";
	var strUrl = document.location.href.toLowerCase();
	if (strUrl.match("/experience/youtubepicks") != null){
		vidPage = "YouTube Picks";
	}else if (strUrl.match("/experience/popculture") != null){
		vidPage = "Pop Culture Sightings";
	}
	pageTracker._trackEvent("VideoLoad", vidTitle);
	}catch(err){} 
}

//showMyVideos for uploads
function showMyVideos_up(data) {
	entries = data.data.items || [];
	var html = [];
	for (var i = 0; i < entries.length; i++) {
		var entry = entries[i];
		var title = entry.title;
		var thumb = entry.thumbnail.sqDefault;
		var vidlocation = entry.id;
		vidlocation = "http://www.youtube.com/v/" + vidlocation + "?enablejsapi=1&playerapiid=ytplayer&color2=0xffffff&autoplay=1&fs=1&rel=0";
		var desc = entry.description;
		var vidId = entry.id;
		desc.replace("\n", "<br /><br />");
		var viewCount = entry.viewCount;
		html.push('<a href="', vidlocation , '" id="', vidId ,'"><img src="',thumb,'" alt="" /><span class="vidTitle">', title, '</span><span class="vidDesc noSeeUm">', desc , '</span><span class="vidViews">',viewCount,' Views</span></a>');
	}
	document.getElementById('vidThumbs').innerHTML = html.join('');
	PageVideos();
}
//showMyVideos for playlists - references to video data is a little different
function showMyVideos_pl(data) {
	entries = data.data.items || [];
	var html = [];
	for (var i = 0; i < entries.length; i++) {
		var entry = entries[i];
		var title = entry.video.title;
		var thumb = entry.video.thumbnail.sqDefault;
		var vidlocation = entry.video.id;
		vidlocation = "http://www.youtube.com/v/" + vidlocation + "?enablejsapi=1&playerapiid=ytplayer&color2=0xffffff&autoplay=1&fs=1&rel=0";
		var desc = entry.video.description;
		var vidId = entry.video.id;
		desc.replace("\n", "<br /><br />");
		var viewCount = entry.video.viewCount;
		html.push('<a href="', vidlocation , '" id="', vidId ,'"><img src="',thumb,'" alt="" /><span class="vidTitle">', title, '</span><span class="vidDesc noSeeUm">', desc , '</span><span class="vidViews">',viewCount,' Views</span></a>');
	}
	document.getElementById('vidThumbs').innerHTML = html.join('');
	PageVideos();
}

function sortByViews(a,b){ 
	var x = a.viewCount;
	var y = b.viewCount;
	return ((x < y) ? -1: ((x>y) ? 1 : 0))
}

function sortByViews_pl(a,b){ 
	var x = a.video.viewCount;
	var y = b.video.viewCount;
	return ((x < y) ? -1: ((x>y) ? 1 : 0))
}
function sortByDate(a,b){ 
	var x = a.uploaded;
	var y = b.uploaded;
	return ((x < y) ? -1: ((x>y) ? 1 : 0))
}
//SortVideos for uploads
function SortVideos(sortType){
	document.getElementById('vidThumbs').innerHTML = '';
	document.getElementById('vidPages').innerHTML = '';
	$('#vidPages').css("top", "0");
	
	if(sortType == 'sortViews'){
	entries.sort(sortByViews);
	entries.reverse();
	}else{
	entries.sort(sortByDate);
	entries.reverse();
	}
	var html = [];
	for (var i = 0; i < entries.length; i++) {
		var entry = entries[i];
		var title = entry.title;
		var thumb = entry.thumbnail.sqDefault;
		var vidlocation = entry.id;
		vidlocation = "http://www.youtube.com/v/" + vidlocation + "?enablejsapi=1&playerapiid=ytplayer&color2=0xffffff&autoplay=1&fs=1&rel=0";
		var desc = entry.description;
		var viewCount = entry.viewCount;
		html.push('<a href="', vidlocation , '"><img src="',thumb,'" alt="" /><span class="vidTitle">', title, '</span><span class="vidDesc noSeeUm">', desc , '</span><span class="vidViews">',viewCount,' Views</span></a>');
	}
	document.getElementById('vidThumbs').innerHTML = html.join('');
	PageVideos();
}
//SortVideos for playlists
function SortVideos_pl(sortType){
	document.getElementById('vidThumbs').innerHTML = '';
	document.getElementById('vidPages').innerHTML = '';
	
	if(sortType == 'sortViews'){
	entries.sort(sortByViews_pl);
	entries.reverse();
	}else{
	entries.sort(sortByDate);
	entries.reverse();
	}
	var html = [];
	for (var i = 0; i < entries.length; i++) {
		var entry = entries[i];
		var title = entry.video.title;
		var thumb = entry.video.thumbnail.sqDefault;
		var vidlocation = entry.video.id;
		vidlocation = "http://www.youtube.com/v/" + vidlocation + "?enablejsapi=1&playerapiid=ytplayer&color2=0xffffff&autoplay=1&fs=1&rel=0";
		var desc = entry.video.description;
		var viewCount = entry.video.viewCount;
		html.push('<a href="', vidlocation , '"><img src="',thumb,'" alt="" /><span class="vidTitle">', title, '</span><span class="vidDesc noSeeUm">', desc , '</span><span class="vidViews">',viewCount,' Views</span></a>');
	}
	document.getElementById('vidThumbs').innerHTML = html.join('');
	PageVideos();
}

//Skin for Surface products/pages
function SkinSurface(){
	$("body").addClass("surface");
	if (windowWidth > 500){
	$("div#header").append("<div id='gofasterBg'></div>");
	}
}

//jquery
$(document).ready(function($){
	
	$('a[href$="pdf"]').addClass('pdf');
	$('a[href$="wmv"]').addClass('wmv');
	$('a[href$="mov"]').addClass('mov');
	
	//dynamic landing pages
	$("div.dynLandingPage div.detailView").hide();
	$("div.dynLandingPage div.prodSection div.prodImage a").mouseenter(function(){
		$(this).parent().parent().children("div.detailView").fadeIn();
	}).mouseleave(function(){
		$(this).parent().parent().children("div.detailView").fadeOut();
	});
	
	//vertCarousel
	//initialize vertCarousel
	$("div.vertCarouselCont").css("overflow", "hidden");
	$("div.vertCarThumbs").prepend("<a href='#' class='galScrollUp disabled' />").append("<a href='#' class='galScrollDown' />");

	//functions
	$("div#prodContent a.galScrollUp").click(function(event){
		event.preventDefault();
		if($("ul.vertCarouselList:animated").length == 0){
			var galTop = $("ul.vertCarouselList").position();
			if(galTop.top < 0){
				if(galTop.top > -320){
					ShowVertButtons();
					$("ul.vertCarouselList").animate({top: 0}, 500);
					$("div.vertCarousel a.galScrollDown").addClass("disabled");
				}else{
					$("ul.vertCarouselList").animate({top: (galTop.top+ 320)}, 500);
					ShowVertButtons();
				}
			}else{$("a.galScrollDown").addClass("disabled");}
		}	
	});
	$("div#prodContent a.galScrollDown").click(function(event){
		event.preventDefault();
		if($("ul.vertCarouselList:animated").length == 0){
			var galTop = $("ul.vertCarouselList").position();
			var galLast = $("ul.vertCarouselList li:last").position();
			var galDiff = (galLast.top + 75)-418;
			if(galTop.top > galDiff*-1){
				if(galDiff + galTop.top < 320){
					ShowVertButtons();
					$("ul.vertCarouselList").animate({top: ((galDiff)*-1)-10}, 500);
					$("a.galScrollDown").addClass("disabled");
				}else{
					$("ul.vertCarouselList").animate({top: (galTop.top-= 320)}, 500);
					ShowVertButtons();
				}
			}else{$("a.galScrollDown").addClass("disabled");}
		}
	});
	$("div#resultsTable div.vertCarousel a.galScrollUp").click(function(event){
		event.preventDefault();
		if($("ul.vertCarouselList:animated").length == 0){
			var galTop = $("ul.vertCarouselList").position();
			if(galTop.top < 0){
				if(galTop.top > -310){
					ShowVertButtons();
					$("ul.vertCarouselList").animate({top: 0}, 500);
					$("div.vertCarousel a.galScrollUp").addClass("disabled");
				}else{
					$("ul.vertCarouselList").animate({top: (galTop.top+ 310)}, 500);
					ShowVertButtons();
				}
			}else{$("a.galScrollUp").addClass("disabled");}
		}	
	});
	$("div#resultsTable div.vertCarousel a.galScrollDown").click(function(event){
		event.preventDefault();
		if($("ul.vertCarouselList:animated").length == 0){
			var galTop = $("ul.vertCarouselList").position();
			var galLast = $("ul.vertCarouselList li:last").position();
			var galDiff = (galLast.top + 75)-400;
			if(galTop.top > galDiff*-1){
				if(galDiff + galTop.top < 310){
					ShowVertButtons();
					$("ul.vertCarouselList").animate({top: ((galDiff)*-1)-10}, 500);
					$("a.galScrollDown").addClass("disabled");
				}else{
					$("ul.vertCarouselList").animate({top: (galTop.top-= 310)}, 500);
					ShowVertButtons();
				}
			}else{$("a.galScrollDown").addClass("disabled");}
		}
	});
	function ShowVertButtons(){
		$("a.galScrollDown").removeClass("disabled");
		$("a.galScrollUp").removeClass("disabled");
	}
	
	//dynamicLanding thumb/preview
	if (windowWidth > 500){
		if ($("div#resultsTable ul.vertCarouselList li").length < 10){
			$("a.galScrollDown").addClass("disabled");
		}
		$("div.detailView").hide();
		$("div#detailView").hide();
		$("div.detailView").each(function(index){
			var detailHtml = $(this).html();
			$("div#detailView").append("<div class='detailItems' id='detailItem" + index + "'>" + detailHtml + "</div>");
			$(this).closest("li.prodSection").attr("id", index);
		});
		$("div#resultsTable li.prodSection div.prodImage a").mouseenter(function(){
			
			var detailIndex = $(this).closest("li.prodSection").attr("id");
			$("div#detailView div.detailItems").hide();
			$("div#detailView div#detailItem" + detailIndex).show();
			$("div#detailView").fadeIn(200);
			detailIndex = "";
		}).mouseleave(function(){
			$("div#detailView").hide();
			$("div#detailView div.detailItems").hide();
		});
	}

	//Static tabs
	$("div.tabContent").not(":eq(0)").hide();
	$("ul#menuTabs a").click(function(event){
		event.preventDefault();
		$("ul#menuTabs li").removeClass("active");
		$(this).parent("li").addClass("active");
		var tabtoOpen = $(this).attr("href");
		//alert(tabtoOpen);
		$("div.tabContent").fadeOut(200).removeClass("openTab");
		$("div" + tabtoOpen).fadeIn(400).addClass("openTab");
		
		
	});
	

	//storelocator zebra rows
	$("div.StoreLocatorRow:even").addClass("alt");
	//related parts zebra 
	$("table.relPartsList tr:even td").addClass("alt");
	//zebra rows for all select boxes
	$("select > option:nth-child(even)").addClass("alt");
	//articles zebra rows
	$("div.articlesList:even").addClass("alt");
	//article pages zebra
	$("div.art_pageNames:odd").addClass("alt");
	
	//mobile header
	if (windowWidth  < maxMobileDevice){
	$("div#quickLinks").prepend('<a href="#quick" class="quickToggle">Quick Links</a>').live("click", function(){
		if ($(this).hasClass("open")){
			$(this).removeClass("open");
		}else {
			$(this).addClass("open");
		}
	});
	$("div#header").append('<a href="#" class="mMore">More</a><div class="menuMore" style="display:none"><div class="menuMore_exp">'+ $('li#btn_main_experience').html() + '</div><div class="menuMore_sup">' + $('li#btn_main_support').html() + '</div></div');
	$("a.mMore").live("click", function(){
		if ($(this).hasClass("open")){
			$(this).removeClass("open")
			$("div.menuMore").slideUp(100);
		}else{
			$(this).addClass("open")
			$("div.menuMore").slideDown(300);
		}
	});
	}//maxMobileDevice
	
	//section or accordion toggles 
	$("div#sensors").hide();
	$("a.secTog").click(function(event){
		event.preventDefault();
		var nextSec = $(this).next("div.secTog");
		if (nextSec.css("display") == "block"){
			$(this).removeClass("open");
			$(this).next("div.secTog").hide();
		}else {
			$(this).addClass("open");
			$(this).next("div.secTog").slideDown(200);
		}
	});



});


