var flashPlayerReady = false;
function setFlashPlayerReady() {flashPlayerReady = true; }
$(document).ready(function() {
	function videoDef (url, title, htmlDuration, flashDuration, type, description, thumburl, actionurl, actiontext) {
	  this.url = url;
	  this.title = title;
	  this.htmlDuration = htmlDuration;
	  this.flashDuration = flashDuration;
	  this.type = type;
	  this.description = description;
	  this.thumburl = thumburl;
	  this.actionurl = actionurl;
	  this.actiontext = actiontext;
	}

	function videoType (name, firstvid) {
	  this.name = name;
	  this.firstvid = firstvid;
	}

	function sortByTitle(a, b) {
		var x = a.title.toLowerCase();
		var y = b.title.toLowerCase();
		return ((x < y) ? -1 : ((x > y) ? 1 : 0));
	}
	
	// getting the flash object on the page
	function getFlashMovieObject(movieName){
		if (window.document[movieName]){return window.document[movieName];}
		if (navigator.appName.indexOf("Microsoft Internet")==-1){
			if (document.embeds && document.embeds[movieName]){return document.embeds[movieName];}
		}
		else{return document.getElementById(movieName);  }
	}

	// checking if flash is ready
	function checkForFlash(){
		setTimeout(playVideo, "100");
	}

	// passing calls to flash
	function callFlash(videoUrl, videoDuration){
    // Strip protocol and domain (if exists) from videoUrl before passing to flash
    if (videoUrl.indexOf("http") != -1) {
      var toStrip = "http://" + document.domain;
      videoUrl = videoUrl.replace(toStrip,"");
    }
    var videoWidth = 480;
    var videoHeight = 360;
    if(!flashPlayerReady){setTimeout(checkForFlash, "100");}
		else{
			getFlashMovieObject("videoPlayer").playVideoInFlashPlayer(videoUrl, videoWidth, videoHeight, videoDuration);
		}
	}

	// Array of all of the videos
	var videos = new Array();
	videos[0] = new videoDef("/video/Transformation.flv","Our Latest Commercial","0:00:31","31","tv","Transform your home with Hunter Douglas window treatments.", "/images/video_gallery/thumbs/HunterDouglas_Commercial.jpg","/about-us.jsp","Visit About Us");
	videos[1] = new videoDef("/video/gtkop_sheers_480x360.swf","Get to Know: Hunter Douglas Sheers","0:00:47","47", "ttc","Enjoy a brief overview of our sheers collection.", "/images/video_gallery/thumbs/Sheers_GS-GTK.jpg","/our-products-category.jsp?id=1","View Our Sheers Collection");
	videos[2] = new videoDef("/video/gtkop_shades_480x360.swf","Get to Know: Hunter Douglas Shades","0:00:45","45","ttc","Enjoy a brief overview of our shades collection.", "/images/video_gallery/thumbs/Shades_GS-GTK.jpg","/our-products-category.jsp?id=2","View Our Shades Collection");
	videos[3] = new videoDef("/video/gtkop_honeycombs_480x360.swf","Get to Know: Hunter Douglas Honeycombs","0:00:38","38","ttc","Enjoy a brief overview of our honeycombs collection.", "/images/video_gallery/thumbs/Honeycombs_GS-GTK.jpg","/our-products-category.jsp?id=3","View Our Honeycombs Collection");
	videos[4] = new videoDef("/video/gtkop_blinds 480x360.swf","Get to Know: Hunter Douglas Blinds","0:00:48","48","ttc","Enjoy a brief overview of our blinds collection.", "/images/video_gallery/thumbs/Blinds_GS-GTK.jpg","/our-products-category.jsp?id=4","View Our Blinds Collection");
	videos[5] = new videoDef("/video/gtkop_shutters_480x360.swf","Get to Know: Hunter Douglas Shutters","0:00:47","47","ttc","Enjoy a brief overview of our shutters  collection.", "/images/video_gallery/thumbs/Shutters_GS-GTK.jpg","/our-products-category.jsp?id=5","View Our Blinds Collection");
	videos[6] = new videoDef("/flash/mainsite/videos/starterguide/ttc_lightcontrol_privacy.swf","Consider: Privacy &amp; Light Control","0:00:46","46","ttc","A brief overview of how Hunter Douglas window fashions provide limitless options for controlling natural light and privacy.", "/images/video_gallery/thumbs/Privacy-Light_GS-TTC.jpg","/our-products.jsp","View Our Products Page");
	videos[7] = new videoDef("/flash/mainsite/videos/starterguide/ttc_viewthrough_clearview.swf","Consider: Clear View-Through","0:00:31","31","ttc","A brief overview of the benefits of window fashions that allow you to enjoy a clear outside view when lowered.", "/images/video_gallery/thumbs/ClearView_GS-TTC.jpg","/our-products.jsp","View Our Products Page");
	videos[8] = new videoDef("/flash/mainsite/videos/starterguide/ttc_viewthrough_softenedview.swf","Consider: Softened View-Through","0:00:45","45","ttc","A brief overview of the benefits of window fashions that beautifully diffuse light while providing a softened view to the outside. ", "/images/video_gallery/thumbs/SoftenView_GS-TTC.jpg","/our-products.jsp","View Our Products Page");
	videos[9] = new videoDef("/flash/mainsite/videos/starterguide/ttc_viewthrough_shadedview.swf","Consider: Shaded View-Through","0:00:45","40","ttc","A brief overview of the benefits of window fashions that block light, reduce noise and offer optimal privacy when lowered.", "/images/video_gallery/thumbs/ShadedView_GS-TTC.jpg","/our-products.jsp","View Our Products Page");
	videos[10] = new videoDef("/flash/mainsite/videos/starterguide/ttc_operatingsystems_manual.swf","Consider: Manual Operating Systems","0:00:40","40","ttc","Learn more about our superior manual lifting systems.", "/images/video_gallery/thumbs/Manual_GS-TTC.jpg","/our-products.jsp","View Our Products Page");
	videos[11] = new videoDef("/flash/mainsite/videos/starterguide/ttc_operatingsystems_motorized.swf","Consider: Motorized Operating Systems","0:00:41","41","ttc","Learn more about how motorized operating systems from Hunter Douglas set new standards in convenience and flexibility.", "/images/video_gallery/thumbs/Motorized_GS-TTC.jpg","/our-products.jsp","View Our Products Page");
	videos[12] = new videoDef("/video/Lift_Systems.flv","Lifting Systems Overview","0:02:52","172","product","Discover Hunter Douglas' remarkable range of innovative lifting and operating systems.", "/images/video_gallery/thumbs/LiftingSys_ProdVid.jpg","/our-products.jsp","View Our Products Page");
	videos[13] = new videoDef("/flash/mainsite/videos/starterguide/ttc_energyefficiency_maximum.swf","Consider: Energy Efficiency","0:00:52","52","ttc","Learn more about how Hunter Douglas window fashions make a big energy savings difference at the window&mdash;and throughout your home.", "/images/video_gallery/thumbs/EnergyEff_GS-TTC.jpg","/our-products.jsp","View Our Products Page");
	videos[14] = new videoDef("/video/Alustra.flv","Alustra&reg; Collection Video","0:00:52","52","product","Learn more about our exclusive Alustra Collection.", "/images/video_gallery/thumbs/Alustra_ProdVid.jpg","/our-products-alustra-collection.jsp","Visit The Alustra Collection Overview Page");
	videos[15] = new videoDef("/video/Shutters.flv","Custom Shutters Collection Video","0:01:37","97","product","Learn more about how the Hunter Douglas Custom Shutter Collection offers time-honored craftsmanship, superior performance and exceptional versatility.", "/images/video_gallery/thumbs/Shutters_ProdVid.jpg","/our-products-category.jsp?id=5","View Shutters Overview Page");
	videos[16] = new videoDef("/video/Vertical.flv","Custom Vertical Blinds Video","0:01:12","72","product","Learn more about how Hunter Douglas Custom Vertical Blinds are the perfect solution for sliding windows and specialty shapes.", "/images/video_gallery/thumbs/VertScreen_ProdVid.jpg","/our-products-category.jsp?id=4","View Vertical Blinds Overview Page");
	videos[17] = new videoDef("/video/DesignerScreens.flv","Designer Screen Shades Video","0:01:27","87","product","Learn more about how Designer Screen Shades offer a winning combination of light control, UV protection and elegant design.", "/images/video_gallery/thumbs/DesScreen_ProdVid.jpg","/our-products-detail.jsp?id=47","View Designer Screen Shades Product Page");
	videos[18] = new videoDef("/video/Duette.flv","Duette&reg; Product Video","0:01:14","74","product","Learn more about how Duette honeycomb shades are an ideal solution to cover any window or door.", "/images/video_gallery/thumbs/Duette_ProdVid.jpg","/our-products-detail.jsp?id=6","View Duette Product Page");
	videos[19] = new videoDef("/video/Horizontal.flv","Horizontal Blinds Video","0:01:47","107","product","Learn more about how Hunter Douglas horizontal blinds offer warmth, style, unsurpassed light-blocking and years of worry-free operation.", "/images/video_gallery/thumbs/Horiz_ProdVid.jpg","/our-products-category.jsp?id=15","View Horizontal Blinds Overview Page");
	videos[20] = new videoDef("/video/Luminette.flv","Luminette&reg; Product Video","0:01:16","76","product","Learn more about how Luminette Privacy Sheers marry the beauty of sheers with the privacy of soft draperies for the widest range of light control possible.", "/images/video_gallery/thumbs/Luminette_ProdVid.jpg","/our-products-detail.jsp?id=4","View Luminette Product Page");
	videos[21] = new videoDef("/video/Pirouette.flv","Pirouette&reg; Product Video","0:01:26","86","product","Learn more about how Pirouette window shadings control light in an entirely new way.", "/images/video_gallery/thumbs/Pirouette_ProdVid.jpg","/our-products-detail.jsp?id=51","View Pirouette Product Page");
	videos[22] = new videoDef("/video/Woven_Woods.flv","Provenance&reg; Product Video","0:01:18","78","product","Learn more about how Provenance woven wood shades allow you to easily vary the amount of light entering your room.", "/images/video_gallery/thumbs/WovWood_ProdVid.jpg","/our-products-detail.jsp?id=8","View Provenance Product Page");
	videos[23] = new videoDef("/video/Silhouette.flv","Silhouette&reg; Product Video","0:02:23","143","product","Learn more about how Silhouette window shadings offer you superb control over the lighting and privacy of your room.", "/images/video_gallery/thumbs/Silhouette_ProdVid.jpg","/our-products-detail.jsp?id=1","View Silhouette Product Page  ");
	videos[24] = new videoDef("/video/Skyline.flv","Skyline&trade; Product Video","0:01:22","82","product","Learn more about how Skyline Gliding Window Panels dress even the largest windows in style.", "/images/video_gallery/thumbs/Skyline_ProdVid.jpg","/our-products-detail.jsp?id=50","View Skyline Product Page");
	videos[25] = new videoDef("/video/Vignette.flv","Vignette&reg; Product Video","0:01:18","78","product","Learn more about how Vignette Modern Roman Shades softly filter the light while letting you choose from nearly a hundred colors and dozens of semi-sheer, translucent and room-darkening fabrics.", "/images/video_gallery/thumbs/Vignette_ProdVid.jpg","/our-products-detail.jsp?id=3","View Vignette Product Page");

	// sorts the array alphabetically
	videos.sort(sortByTitle); 
	
	$("#video_list_contents").html(
		'<div class="scroll-pane"><ul id="video_list_ul"></ul></div><ul id="video_list_links"></ul>'
	);// creates the list holders
	for (var i=0; i< videos.length; i++){ // loops through and appends the items to the page
		$(
			'<li class="' + videos[i].type + '" id="videoLi_' + i + '">' +
			'<a href="' + videos[i].url + "&" + videos[i].flashDuration + '" class="flash_link">' + 
			'<img src="' + videos[i].thumburl + '" alt="" border="0" />' + 
			'</a>' +
			'<div>' + 
			'<a href="' + videos[i].url + "&" + videos[i].flashDuration + '" class="flash_link">' + 
			videos[i].title + '</a>' + 
			'<span>Time: ' + videos[i].htmlDuration + '</span>' +
			'<p>' + videos[i].description + '</p></div>' + 
			'</li>'
		).appendTo("#video_list_ul");
		
		$(
			'<li id="videoLink_' + i + '"><a href="' + videos[i].actionurl + '">' +
			videos[i].actiontext + ' <img src="/images/common/small_arrow_right.png" alt="" border="0" /></a></li>'
		).appendTo("#video_list_links")
	}
	
	$("li a.flash_link").hover(
		function(){
			$(this).parent().addClass("over");
		},
		function(){
			$(this).parent().removeClass("over");	
		}
	);

	// setting the numbers that appear in parentheses next to each category link
	$("#all_tab").append(" (" + $("#video_list_ul li").length + ")");
	$("#prod_video_tab").append(" (" + $("li.product").length + ")");
	$("#things_to_consider_tab").append(" (" + $("li.ttc").length + ")");
	$("#tv_tab").append(" (" + $("li.tv").length + ")");
	
	// setting the order of which item is listed first per category
	$("li.ttc:first div").addClass("ttc_first"); // things to consider 
	$("li.product:contains('Lifting Systems Overview') div").addClass("product_first"); // product
	$("li.tv:contains('Our Latest Commercial') div").addClass("tv_first"); // tv and all
	$("li.tv:contains('Our Latest Commercial')").addClass("on");
	var theId = $("div.tv_first").parent().attr("id").split("_"); // setting the corresponding link for commercial
	$("#videoLink_" + theId[1]).addClass("on"); // setting the corresponding link for commercial
	
	// setting "all" to be the default category turned on
	$("#video_list_ul").attr("class", "all");
	// setting the tv commercial to be first
	$("#video_list_ul>li").tsort("div[class*=tv_first]");
	
	// set the item and corresponding link to be on, as well as pass the call to flash
	$("a.flash_link").click(function(event){
		$("#video_list_ul li").removeClass("on");
		$("#video_list_links li").removeClass("on");
		if($(this).parent().parent().attr("id") == "video_list_ul"){
			var theLI = $(this).parent();
		}
		else{var theLI = $(this).parent().parent();}
		var theId = theLI.attr("id").split("_");
		$("#videoLink_" + theId[1]).addClass("on");
		theLI.addClass("on");
		var theHref = $(this).attr('href');
		theHref = theHref.split("&");
		event.preventDefault();
		if(flashPlayerReady){callFlash(theHref[0], theHref[1]);}
	});
	
	// show the approriate items when changing categories
	$("#buttons a").click(function(event){
		event.preventDefault();
		$("#buttons a").removeClass("on");
		var theClass = $(this).attr("class");
		// resort everything alphbetically and set the correct first item
		$("#video_list_ul>li").tsort();
		$("#video_list_ul>li").tsort("div[class*="+theClass+"_first]");
		// set the corresponding link to be on when you change tabs
		if($(this).attr("id") != "all_tab"){
			//var theID = $("div." + theClass + "_first").parent().attr("id").split("_");
			//$("#videoLink_" + theID[1]).addClass("on");
		}
		else{
			var theID = $("div.tv_first").parent().attr("id").split("_");
			//$("#videoLink_" + theID[1]).addClass("on");
			$("#video_list_ul>li").tsort("div[class*=tv_first]");
		}
		$("#video_list_ul").attr("class", theClass);
		$(this).addClass("on");
		
		$('.scroll-pane').jScrollPane({showArrows:true, scrollbarWidth: 15, scrollbarMargin: 0, dragMaxHeight: 86, dragMinHeight: 86, arrowSize: 18});
	});
	
	setTimeout(function(){
		$('.scroll-pane').jScrollPane({showArrows:true, scrollbarWidth: 15, scrollbarMargin: 0, dragMaxHeight: 86, dragMinHeight: 86, arrowSize: 18});
	}, "200");
});
