﻿/*
'--------------------------------------------------------------------------------------------------
' Title			: Bows/Products VIEW PRODUCT JavaScript
' Description	: JavaScript functions and declarations for the products View Product sections
'--------------------------------------------------------------------------------------------------
' History
' 11/11/2008	: DPE - Created Page
'--------------------------------------------------------------------------------------------------
*/

var swfPlayerReady = false;

// Function to initiate switching between product tabs
function initViewProdTabs(){
	
	// Lets start with a clean slate
	$(".box").hide();
	$("#primaryGraphic").hide();
	$("#videoPlayerBox").html("");
	
	// Then lets find out which tab is requested
	var reqTab = window.location.hash.replace(/#/,"")
	if(reqTab == "") {reqTab = "overview";}
	
	// Next, add the action to each button
	$("#bowNav li[class!='retailer'] a").click(function(){
		switchTabs($(this).attr("rel"));
	});
	
	switchTabs(reqTab); // Finally, setup the initial state
}

// Actually performs the tab switching
function switchTabs(reqTab) {
	var arr_reqTab = reqTab.split("-");
	// Set set the selected button
	$("#bowNav li[class!='retailer']").each(function(){
		if($(this).find("a").attr("rel") == arr_reqTab[0]){
			$(this).addClass("sel");
		} else {
			$(this).removeClass("sel");		
		}
	});
	
	// Turn everything off
	$(".box").hide();
	$("#primaryGraphic").hide();
	$("#videoPlayerBox").html("");	
	
	
	// The turn on what we need
	switch(arr_reqTab[0]) {
		case "overview":
			$("#overview").show();
			$("#primaryGraphic").show();
			break;
		case "specs":
			$("#specs").show();
			$("#primaryGraphic").show();
			break;
		case "gallery":
			//Added by Jake Holman on 08/04/09
			if(arr_reqTab[1] && arr_reqTab[2]) {
				switch(arr_reqTab[1]) {
					case "videos":
						if(IsNumeric(arr_reqTab[2])) {
							$("#gallery").show();	
								
								$("#largeImgBox").hide();
								$("#videoPlayerBox").show();	
								SwitchVid_auto($("#video_" + arr_reqTab[2]));
								break;
						}
						else {
							$("#bowNav li[class!='retailer']").each(function(){
								if($(this).find("a").attr("rel") == "overview"){
									$(this).addClass("sel");
								} else {
									$(this).removeClass("sel");		
								}
							});
		
						
							$("#overview").show();
							$("#primaryGraphic").show();
							break;
						}
											
					default:
						$("#bowNav li[class!='retailer']").each(function(){
							if($(this).find("a").attr("rel") == "overview"){
								$(this).addClass("sel");
							} else {
								$(this).removeClass("sel");		
							}
						});
					
						$("#overview").show();
						$("#primaryGraphic").show();
						break;


				}

			}
			else {
				$("#gallery").show();
				if($("#gallery .images")){
					$("#videoPlayerBox").html("");
					$("#largeImgBox").show();
				} else {
					$("#largeImgBox").hide();
					$("#videoPlayerBox").show();			
				}
			}
			
			break;
		case "threeD":
			$("#overview").show();
			$("#threeD").show();
			break;
		case "testimonials":
			$("#testimonials").show();
			//$("#primaryGraphic").show();
			break;
		default:
			$("#overview").show();
			$("#primaryGraphic").show();
			break;
	}
}

function initSwitchImg(){

	// Apply the action to the thubnail links	
	$("#imgNav li a").click(function(){
		//get the large image attributes
		var largeImgSrc 	= $(this).find("img").attr("src").replace(/w70/,"w365");
		var largeImgAlt 	= $(this).find("img").attr("alt");
		largeImgSrc 		= largeImgSrc.replace(/_sq/,"");
		var closeUpImgSrc	= largeImgSrc.replace(/w365_/,"");
		
		// Remove any videos that are playing
		$("#videoPlayerBox").html("");
		
		// Set the large image to the requested one display it
		$("#largeImg").attr({src:largeImgSrc,alt:largeImgAlt,jqimg:closeUpImgSrc});
		$("#largeImgBox").show();

	});
	
	// Preload the large images
	largeImgs	= new Array(); // Array to hold the LARGE preloaded images
	closeUpImgs	= new Array(); // Array to hold the SUPER/MAGNIFY preloaded images

	$("#imgNav li a img").each(function(i){
		var largeImgSrc		= $(this).attr("src").replace(/w70/,"w365");
		largeImgSrc			= largeImgSrc.replace(/_sq/,"");
		largeImgs[i]		= new Image;
		largeImgs[i].src	= largeImgSrc;
		var closeUpImgSrc	= largeImgSrc.replace(/_w365/,"");
		closeUpImgs[i]		= new Image;
		closeUpImgs[i].src	= closeUpImgSrc;
	});
	
}

function initSwitchVid() {
	$("#vidNav li a").click(function(){
		
		// Turn off any large images...
		$("#largeImgBox").hide();
		
		var videoObj	= document.getElementById("videoPlayer");
		var flvInfo		= $(this).attr("rel");
		if(flvInfo.indexOf("viddler") > -1) {
			var strUrl		= "/site/layouts/viddler/getVideo_viewProduct.php";
		} else {
			var strUrl		= "/site/layouts/products/ajax/setVideoObject.asp";
		}
		
		$.ajax({
			url: strUrl,
			data: "flvInfo=" + flvInfo + "",
     		error: function (xhr, desc, exceptionobj) {
				alert(xhr.responseText);
			},
			success: function(output){
				if(output.error) {
					alert(output.error);
					return;
				}
				$("#videoPlayerBox").html(output)
		   }
		 });
		 				
	});
}

function SwitchVid_auto(video) {
		// Turn off any large images...
		$("#largeImgBox").hide();
		
		var videoObj	= document.getElementById("videoPlayer");
		var flvInfo		= video.attr("rel");
		if(flvInfo.indexOf("viddler") > -1) {
			var strUrl		= "/site/layouts/viddler/getVideo_viewProduct.php";
		} else {
			var strUrl		= "/site/layouts/products/ajax/setVideoObject.asp";
		}

		$.ajax({
			url: strUrl,
			data: "flvInfo=" + flvInfo + "",
     		error: function (xhr, desc, exceptionobj) {
				alert(xhr.responseText);
			},
			success: function(output){
				if(output.error) {
					alert(output.error);
					return;
				}
				$("#videoPlayerBox").html(output)
		   }
		 });

}

function initVideoBtns(){
	$(".videoBtn").click(function(){

		reqTab = $(this).attr("href").replace(/.*#/,"");
		switchTabs(reqTab);
		
		// Turn off any large images...
		$("#largeImgBox").hide();
		
		var videoObj	= document.getElementById("videoPlayer");
		var flvInfo		= $(this).attr("rel");
		if(flvInfo.indexOf("viddler") > -1) {
			var strUrl		= "/site/layouts/viddler/getVideo_viewProduct.php";
		} else {
			var strUrl		= "/site/layouts/products/ajax/setVideoObject.asp";
		}

		
		$.ajax({
			url: strUrl,
			data: "flvInfo=" + flvInfo + "",
     		error: function (xhr, desc, exceptionobj) {
				alert(xhr.responseText);
			},
			success: function(output){
				if(output.error) {
					alert(output.error);
					return;
				}
				$("#videoPlayerBox").html(output)
			}
		 });
		 			
	});
}

function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }

function initSifr(){
	if(typeof sIFR == "function"){ // Fancy Text for the page title...
		if($("body").hasClass("Monster") || $("body").hasClass("MonsterXLR8")){
			sIFR.replaceElement("h1", named({sFlashSrc:"/site/swf/sifr-myriadProCond.swf",sColor:"#E72A11",sBgColor:"#000000",sWmode:"opaque"}));
		} else if($("body").hasClass("Passion")) {
			sIFR.replaceElement("h1", named({sFlashSrc:"/site/swf/sifr-myriadProCond.swf",sColor:"#d31aba",sBgColor:"#0a000b",sWmode:"opaque"}));
		//} else if($("body").hasClass("Z7")) {
			//sIFR.replaceElement("h1", named({sFlashSrc:"/site/swf/sifr-myriadProCond.swf",sColor:"#a97c25",sBgColor:"#1a0c02",sWmode:"opaque"}));
		//} else if($("body").hasClass("Monster70")) {
			//sIFR.replaceElement("h1", named({sFlashSrc:"/site/swf/sifr-myriadProCond.swf",sColor:"#d2b686",sBgColor:"#070603",sWmode:"opaque"}));
		} else {
			sIFR.replaceElement("h1", named({sFlashSrc:"/site/swf/sifr-myriadProCond.swf",sColor:"#a97c25",sWmode:"opaque"}));
		}
	};
}


// Replaces tradtional body onLoad="" functionality throughout the site
$(document).ready(function(){
	initViewProdTabs(); // initiates switching between product tabs
	initSwitchImg();	// initiates image switching on the gallery tab
	initSwitchVid();	// initiates video switching on the gallery tab
	initVideoBtns();	// initiates the View Video btn in the overview
	initSifr();			// initiates the Flash text replacement for the page title
	$("#gallery .jqzoom").jqueryzoom({xzoom:400,yzoom:400,offset:0,position:"left",preload:0}); // Initiates the magnifier/close-up image functions
	$("#testimonials .jqzoom").jqueryzoom({xzoom:365,yzoom:220,offset:2,position:"left",preload:0}); // Initiates the magnifier/close-up image functions

});
