var wcscroll = {
	intRegex : /^\d+$/,
	all_w : $(".content").width(),
	n_elem : $("#accordion").children(".vid").length,
	closed_w : "10px",
	open_w : "500px",
	arrowClick : false,
	hashes : {"home" : "home", "contact" : "contact", "login" : "login"},
	idsHashes : {"home" : "home", "contact" : "contact", "login" : "login"},
	
	
	init : function(options) {
		wcscroll.loadVids(options, function() {
			$.History.bind(function(state) {
				wcscroll.gotoHash(state);
			});
			
			$(".vid:not(.open)").click(function() {
				if(!wcscroll.arrowClick){
					wcscroll.scrollto(this);
				}
			});
			
			$(".listitem:not(.li_hilight)").click(function() {
				wcscroll.scrollto($("#"+$(this).attr("id").replace(/li/, "t")));
			});
			
			wcscroll.scrollto($("#accordion").children(".vid").first(), true);
			
			$("#details").hover(function() {
				wcscroll.foldUp();
			});
			
			$("#listing").hover(function() {
				wcscroll.foldDown();
			});
			
			$(".vid").hover(function() {
				wcscroll.foldDown();
			});		
			
			$("#accordionFiller").hover(function() {
				wcscroll.foldDown();
			});
		});
	},
	
	gotoHash : function(hash) {
		$("#vidbodydiv").remove();
		if(hash) {
			if(hash == "home") {
				tabs.showStrip($("#work"));
			} else if(hash == "login") {
				tabs.showStrip($("#login"));
			} else if(hash == "contact") {
				tabs.showStrip($("#contact"));
			} else {
				var vidHash = hash;
				if(vidHash != null) {
					var vidHashArray = vidHash.split("/");
					wcscroll.scrollto($("#t_"+wcscroll.hashes[vidHashArray[0]]));
					tabs.showStrip($("#work"));
					if(vidHashArray[1] == "play-video") wcscroll.showVid(wcscroll.hashes[vidHashArray[0]]);
				}
			}
		}
	},
	
	loadVids : function (options, callback) {
		//load the video list
		var vidListSrc = ""
		if(options.vidList)
			vidListSrc = options.vidList;
		else vidListSrc = "videos.xml";
		
		$.ajax({
			url: vidListSrc,
			success: function(data) {
				wcscroll.populate(data, callback);
			},
			error: function() {
				alert("XML error.");
			}
		});
	},
	
	populate : function(xml, callback) {
		var id = 1;
		$(xml).find("video").each(function(){
			var title = $(this).find("name").text();
			var file = $(this).find("file").text();
			var thumb_h = $(this).find("thumb_h").text();
			var thumb_v = $(this).find("thumb_v").text();
			var desc = $(this).find("description").text();
			var client = $(this).find("client").text();
			var url_hash = $(this).find("urlHash").text();
			
			wcscroll.hashes[url_hash] = id;
			wcscroll.idsHashes[id] = url_hash;
			
			var html = ''
				+ '<div class="vid" id="t_'+id+'" style="background:url(\''+thumb_h+'\'); background-position:center center;">'
				+ '<div class="meta" id="c_'+id+'">'+client+'<\/div>'
				+ '<div class="meta" id="d_'+id+'">'+desc+'<\/div>'
				+ '<div class="meta" id="ti_'+id+'">'+title+'<\/div>'
				+ '<div class="meta" id="thb_v_'+id+'"><img src="'+thumb_v+'" \/><\/div>'
				+ '<div class="meta" id="vid_'+id+'">'+file+'<\/div>'
				+ '<\/div>';
				
				
			
			$("#accordion").append(html);
			
			var html = ''
				+ '<div class="listitem" id="li_'+id+'">'+client+'<\/div>';
			
			$("#listcont").append(html);
			
			id++;
		});
		$("#accordion").append('<div id="accordionFiller"><\/div>');
		callback();
	},
	
	updateHist : function(id) {
		var histString = wcscroll.idsHashes[id]+"";
		if($("#vidbodydiv").length) histString += "/play-video";
		$.History.setHash(histString);
		//_gaq.push(['_trackPageView', '/'+histString]);
		pageTracker._trackPageview('/'+histString);
	},
	
	updateHistNoId : function() {
		var id = $(".open").attr("id").replace(/.*_/, "");
		wcscroll.updateHist(id);
	},
	
	updateLR : function() {
		$(".left").click(function() {
			wcscroll.scrollto($(this).parent().prev(".vid"));
			wcscroll.arrowClick = true;
		});
		
		$(".right").click(function() {
			wcscroll.scrollto($(this).parent().next(".vid"));
			wcscroll.arrowClick = true;
		});
	},
	
	scrollto : function(item) {
		scrollto(item, false);
	},
	
	scrollto : function(item, skipUpdateHash) {
		if(!$(item).hasClass("open") && $(item).length) {
			$("#details").children().each(function() {
				$(this).fadeOut(200);
			});
			var id = parseInt($(item).attr("id").replace(/.*_/, ""));
			$("#accordion").children(".vid").removeClass("open anim leftOf");
			$("#accordion").children(".vid").stop(true, false);
			$(item).addClass("anim open");
			$("#accordion").children(".vid").each(function() {
				var myNumID = parseInt($(this).attr("id").replace(/.*_/, ""));
				if (myNumID < id) {
					$(this).addClass("leftOf");
				}
			}); 
			$(item).animate({width: wcscroll.open_w, opacity:1}, {duration:400, queue:false, complete:function() {
				$(item).children(".left, .right").remove();
				if($(".leftOf").length) $(item).append('<div class="left"><\/div>');
				if($(item).next(".vid:not(.open)").length) $(item).append('<div class="right"><\/div>');
					
					
				$(".left, .right").hide()
					.fadeIn(400);
				wcscroll.updateLR();
				if(!skipUpdateHash) wcscroll.updateHist(id);
				wcscroll.updateDetails(id);
				$("#accordion").children().removeClass("anim");
				$("#accordion").children(".vid:not(.open, .leftOf)").css({"width":wcscroll.closed_w, opacity:.5});
				wcscroll.arrowClick = false;
			}});
			$(".left, .right").fadeOut(200, function() {
				$("#accordion").children(".vid").children(".left, .right").remove();
			});
			
			$(".open").each(function() {
				wcscroll.hilightLI($(this).attr("id"));
			});
			$("#accordion").children(".vid:not(.anim), .leftOf:not(.anim)").animate({"width":0, opacity:.5}, {duration:400, queue:false});						
			$("#accordion").children(".vid:not(.anim, .leftOf)").animate({"width":wcscroll.closed_w, opacity:.5}, {duration:400, queue:false, complete:function() {
				if(!$(item).find(".left, .right").length) {
					if($(".leftOf").length) $(item).append('<div class="left"><\/div>');
					if($(item).next(".vid:not(.open)").length) $(item).append('<div class="right"><\/div>');
					wcscroll.updateLR();
					if(!skipUpdateHash) wcscroll.updateHist(id);
				}
			}});
		}
	},
	
	updateDetails : function(id) {
		$(".title").html('"'+($("#ti_"+id).html())+'"');
		$(".client").html($("#c_"+id).html());
		$(".desc").html($("#d_"+id).html());
		$("#imgs").html($("#thb_v_"+id).html());
		
		$("#playBtn").attr('href', 'javascript:wcscroll.showVid('+id+');')
		
		$("#details").children().each(function() {
			$(this).fadeIn(200);
		});
	},
	
	showVid : function(id) {
		var title = $("#ti_"+id).html();
		var client = $("#c_"+id).html();
		var desc = $("#d_"+id).html();
		var vid = $("#vid_"+id).html();
		
		var html = ''
		+ '<div id="vidbodydiv">'
		+ '<div id="vidtathead">'
		+ '<div id="vidprojectInfo">'
		+ '<div id="viddetailText">'
		+ '<span class="vidtitle">"'+title+'"<\/span>'
		+ '<br \/>'
		+ '<span class="vidclient">'+client+'<\/span>'
		+ '<\/div>'
		+ '<\/div>'
		+ '<div id="vidcont">'
		+ '<div id="flashcontent">'
		+ '<\/div>'
		+ '<a id="closebtn" href="javascript:wcscroll.hideVid();"><img id="closebtnimg" src="img/close_btn.png" /><div style="display:none">close<\/div><\/a>'
		+ '<div class="viddesc">'+desc+'<br /><br /><div style="width:100%" align="right"><a style="color:#000" href="javascript:wcscroll.hideVid();">back<\/a><\/div><\/div>'
		+ '<\/div>'
		+ '<\/div>'
		+ '<\/div>'
		
		$("body").prepend(html);
		swfobject.embedSWF("NMD_flashplayer_WIP_3.swf", "flashcontent", "540", "325", "9", "", {"playFile":$("#vid_"+id).html(), "ar":"16:9"}, {}, {}, function(){});
		$("#vidbodydiv").hide()
			.fadeIn(700);
		wcscroll.updateHist(id);
	},
	
	hideVid : function() {
		$("#vidbodydiv").fadeOut(300, function(){
			$("#vidbodydiv").remove();
			wcscroll.updateHistNoId();
		});
	},
	
	hilightLI : function(id) {
		var nId = id.replace(/t/, "li");
		$(".listitem").animate({opacity:.4}, {duration:200, queue:false})
			.removeClass(".li_hilight");
		$("#"+nId).animate({opacity:1}, {duration:200, queue:false})
			.addClass(".li_hilight");
	},
	
	foldUp : function() {
		$("#accordion").animate({height:"40px"},{duration:200, queue:false});
	},
	
	foldDown : function() {
		$("#accordion").animate({height:"150px"},{duration:200, queue:false});
	}
};
