function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

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");
}

//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)/8;
		if (totalVideoRows > 1){
			$('#vidPaging').append("<a href='#prev'>&lt;</a>");
		}
		for (var i=0;i<totalVideoRows; i++){
			var firstTen = $('#dlVideos #vidThumbs a:lt(8)');
			$('#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 class="cssShadow" 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 class="cssShadow" src="',thumb,'" alt="" /><span class="vidTitle">', title, '</span><span class="vidDesc noSeeUm"></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();
}

$(document).ready(function(){
	$('a[href$="pdf"]').addClass('pdf');
	$('a[href$="wmv"]').addClass('wmv');
	$('a[href$="mov"]').addClass('mov');


});

	/* 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);
}

/*video pages*/
	$("#vidPages .page a").live("click", function(event){
		event.preventDefault();
		LoadVideo($(this).attr("href"), $("span.vidTitle", this).html(), $(" span.vidDesc", this).html());
	});
	
	
/* jscrollPane */
(function(A){A.jScrollPane={active:[]};A.fn.jScrollPane=function(C){C=A.extend({},A.fn.jScrollPane.defaults,C);var B=function(){return false};return this.each(function(){var O=A(this);O.css("overflow","hidden");var X=this;if(A(this).parent().is(".jScrollPaneContainer")){var Ac=C.maintainPosition?O.position().top:0;var L=A(this).parent();var d=L.innerWidth();var Ad=L.outerHeight();var M=Ad;A(">.jScrollPaneTrack, >.jScrollArrowUp, >.jScrollArrowDown",L).remove();O.css({top:0})}else{var Ac=0;this.originalPadding=O.css("paddingTop")+" "+O.css("paddingRight")+" "+O.css("paddingBottom")+" "+O.css("paddingLeft");this.originalSidePaddingTotal=(parseInt(O.css("paddingLeft"))||0)+(parseInt(O.css("paddingRight"))||0);var d=O.innerWidth();var Ad=O.innerHeight();var M=Ad;O.wrap(A("<div></div>").attr({className:"jScrollPaneContainer"}).css({height:Ad+"px",width:d+"px"}));A(document).bind("emchange",function(Ae,Af,p){O.jScrollPane(C)})}if(C.reinitialiseOnImageLoad){var N=A.data(X,"jScrollPaneImagesToLoad")||A("img",O);var G=[];if(N.length){N.each(function(p,Ae){A(this).bind("load",function(){if(A.inArray(p,G)==-1){G.push(Ae);N=A.grep(N,function(Ag,Af){return Ag!=Ae});A.data(X,"jScrollPaneImagesToLoad",N);C.reinitialiseOnImageLoad=false;O.jScrollPane(C)}}).each(function(Af,Ag){if(this.complete||this.complete===undefined){this.src=this.src}})})}}var o=this.originalSidePaddingTotal;var l={height:"auto",width:d-C.scrollbarWidth-C.scrollbarMargin-o+"px"};if(C.scrollbarOnLeft){l.paddingLeft=C.scrollbarMargin+C.scrollbarWidth+"px"}else{l.paddingRight=C.scrollbarMargin+"px"}O.css(l);var m=O.outerHeight();var i=Ad/m;if(i<0.99){var H=O.parent();H.append(A("<div></div>").attr({className:"jScrollPaneTrack"}).css({width:C.scrollbarWidth+"px"}).append(A("<div></div>").attr({className:"jScrollPaneDrag"}).css({width:C.scrollbarWidth+"px"}).append(A("<div></div>").attr({className:"jScrollPaneDragTop"}).css({width:C.scrollbarWidth+"px"}),A("<div></div>").attr({className:"jScrollPaneDragBottom"}).css({width:C.scrollbarWidth+"px"}))));var z=A(">.jScrollPaneTrack",H);var P=A(">.jScrollPaneTrack .jScrollPaneDrag",H);if(C.showArrows){var g;var Ab;var S;var r;var j=function(){if(r>4||r%4==0){y(u+Ab*b)}r++};var K=function(p){A("html").unbind("mouseup",K);g.removeClass("jScrollActiveArrowButton");clearInterval(S)};var Z=function(){A("html").bind("mouseup",K);g.addClass("jScrollActiveArrowButton");r=0;j();S=setInterval(j,100)};H.append(A("<a></a>").attr({href:"javascript:;",className:"jScrollArrowUp"}).css({width:C.scrollbarWidth+"px"}).html("Scroll up").bind("mousedown",function(){g=A(this);Ab=-1;Z();this.blur();return false}).bind("click",B),A("<a></a>").attr({href:"javascript:;",className:"jScrollArrowDown"}).css({width:C.scrollbarWidth+"px"}).html("Scroll down").bind("mousedown",function(){g=A(this);Ab=1;Z();this.blur();return false}).bind("click",B));var Q=A(">.jScrollArrowUp",H);var J=A(">.jScrollArrowDown",H);if(C.arrowSize){M=Ad-C.arrowSize-C.arrowSize;z.css({height:M+"px",top:C.arrowSize+"px"})}else{var s=Q.height();C.arrowSize=s;M=Ad-s-J.height();z.css({height:M+"px",top:s+"px"})}}var w=A(this).css({position:"absolute",overflow:"visible"});var D;var Y;var b;var u=0;var V=i*Ad/2;var a=function(Ae,Ag){var Af=Ag=="X"?"Left":"Top";return Ae["page"+Ag]||(Ae["client"+Ag]+(document.documentElement["scroll"+Af]||document.body["scroll"+Af]))||0};var f=function(){return false};var v=function(){n();D=P.offset(false);D.top-=u;Y=M-P[0].offsetHeight;b=2*C.wheelSpeed*Y/m};var E=function(p){v();V=a(p,"Y")-u-D.top;A("html").bind("mouseup",T).bind("mousemove",h);if(A.browser.msie){A("html").bind("dragstart",f).bind("selectstart",f)}return false};var T=function(){A("html").unbind("mouseup",T).unbind("mousemove",h);V=i*Ad/2;if(A.browser.msie){A("html").unbind("dragstart",f).unbind("selectstart",f)}};var y=function(Ae){Ae=Ae<0?0:(Ae>Y?Y:Ae);u=Ae;P.css({top:Ae+"px"});var Af=Ae/Y;w.css({top:((Ad-m)*Af)+"px"});O.trigger("scroll");if(C.showArrows){Q[Ae==0?"addClass":"removeClass"]("disabled");J[Ae==Y?"addClass":"removeClass"]("disabled")}};var h=function(p){y(a(p,"Y")-D.top-V)};var q=Math.max(Math.min(i*(Ad-C.arrowSize*2),C.dragMaxHeight),C.dragMinHeight);P.css({height:q+"px"}).bind("mousedown",E);var k;var R;var I;var t=function(){if(R>8||R%4==0){y((u-((u-I)/2)))}R++};var Aa=function(){clearInterval(k);A("html").unbind("mouseup",Aa).unbind("mousemove",e)};var e=function(p){I=a(p,"Y")-D.top-V};var U=function(p){v();e(p);R=0;A("html").bind("mouseup",Aa).bind("mousemove",e);k=setInterval(t,100);t()};z.bind("mousedown",U);H.bind("mousewheel",function(Ae,Ag){v();n();var Af=u;y(u-Ag*b);var p=Af!=u;return !p});var F;var W;function c(){var p=(F-u)/C.animateStep;if(p>1||p<-1){y(u+p)}else{y(F);n()}}var n=function(){if(W){clearInterval(W);delete F}};var x=function(Af,p){if(typeof Af=="string"){$e=A(Af,O);if(!$e.length){return}Af=$e.offset().top-O.offset().top}H.scrollTop(0);n();var Ae=-Af/(Ad-m)*Y;if(p||!C.animateTo){y(Ae)}else{F=Ae;W=setInterval(c,C.animateInterval)}};O[0].scrollTo=x;O[0].scrollBy=function(Ae){var p=-parseInt(w.css("top"))||0;x(p+Ae)};v();x(-Ac,true);A("*",this).bind("focus",function(Ah){var Ag=A(this);var Aj=0;while(Ag[0]!=O[0]){Aj+=Ag.position().top;Ag=Ag.offsetParent()}var p=-parseInt(w.css("top"))||0;var Ai=p+Ad;var Af=Aj>p&&Aj<Ai;if(!Af){var Ae=Aj-C.scrollbarMargin;if(Aj>p){Ae+=A(this).height()+15+C.scrollbarMargin-Ad}x(Ae)}});if(location.hash){x(location.hash)}A(document).bind("click",function(Ae){$target=A(Ae.target);if($target.is("a")){var p=$target.attr("href");if(p.substr(0,1)=="#"){x(p)}}});A.jScrollPane.active.push(O[0])}else{O.css({height:Ad+"px",width:d-this.originalSidePaddingTotal+"px",padding:this.originalPadding});O.parent().unbind("mousewheel")}})};A.fn.jScrollPane.defaults={scrollbarWidth:10,scrollbarMargin:5,wheelSpeed:18,showArrows:false,arrowSize:0,animateTo:false,dragMinHeight:1,dragMaxHeight:99999,animateInterval:100,animateStep:3,maintainPosition:true,scrollbarOnLeft:false,reinitialiseOnImageLoad:false};A(window).bind("unload",function(){var C=A.jScrollPane.active;for(var B=0;B<C.length;B++){C[B].scrollTo=C[B].scrollBy=null}})})(jQuery);
