/* --------------------------------------------------------------------------------------------------------
AUTHOR:			Ryan Marincovich
DATE CREATED:	2008.07.22
File Purpose:	Versare Website Family Common Javascript File
-------------------------------------------------------------------------------------------------------- */


// OVERLAY FUNCTIONS ----------------------------------------------
var isOverlayLoaded = false;

// build overlay objects (run onload)
function buildOverlay() {
	mainOverlayObj = eval(doc + '"mainOverlay"' + sty);
	overlayBGObj = eval(doc + '"overlayBG"' + sty);
	overlayGalleryObj = eval(doc + '"photoGallery"' + sty);
	overlayVideoPlayerObj = eval(doc + '"videoPlayer"' + sty);
	overlayViewerWindowObj = document.getElementById('mainOverlay');
	overlayViewerBodyObj = document.getElementById('bodyContainer');
	windowHeight = overlayViewerWindowObj.offsetHeight;
	contentHeight = overlayViewerBodyObj.offsetHeight;
	overlayBGObj.height = Math.max(windowHeight, contentHeight) + "px";
	// set var to note objects are built and ready
	isOverlayLoaded = true;
}

// function to open photo gallery overlay
function photoGalleryOpen(intResourceID) {
	scroll(0, 0);
	mainOverlayObj.visibility = 'visible';
	overlayGalleryObj.visibility = 'visible';
	ajaxOpenPhotoGallery(intResourceID);
}

// function to close photo gallery overlay
function photoGalleryClose() {
	mainOverlayObj.visibility = 'hidden';
	overlayGalleryObj.visibility = 'hidden';
	eval(doc + '"galleryMain' + currGalleryImage + '"' + sty + ".visibility = \'hidden\'");
	eval(doc + '"tinyThumbOn' + currGalleryImage + '"' + sty + ".visibility = \'hidden\'");
	eval(doc + '"descriptionMain' + currGalleryImage + '"' + sty + ".visibility = \'hidden\'");
}

function ajaxOpenPhotoGallery(intResourceID) {
	var xmlHttp = GetXmlHttpObject();
	var objGalleryContainer = document.getElementById("photoGallery");
	xmlHttp.onreadystatechange=function() {
		if (xmlHttp.readyState==4) {
			objGalleryContainer.innerHTML = xmlHttp.responseText;
		}
	}
	var url = "products-photos.php?intResourceID="+intResourceID;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

var currGalleryImage = null;

function turnOnFirstImage(which) {
	if (currGalleryImage == null) {
		currGalleryImage = which;
	}
	eval(doc + '"galleryMain' + currGalleryImage + '"' + sty + ".visibility = \'visible\'");
	eval(doc + '"tinyThumbOn' + currGalleryImage + '"' + sty + ".visibility = \'visible\'");
	eval(doc + '"descriptionMain' + currGalleryImage + '"' + sty + ".visibility = \'visible\'");
}

function galleryNavOver(which) {
	if ((currGalleryImage != null) && (currGalleryImage != which)) {
		eval(doc + '"galleryMain' + currGalleryImage + '"' + sty + ".visibility = \'hidden\'");
		eval(doc + '"tinyThumbOn' + currGalleryImage + '"' + sty + ".visibility = \'hidden\'");
		eval(doc + '"descriptionMain' + currGalleryImage + '"' + sty + ".visibility = \'hidden\'");
	}
	currGalleryImage = which;
	eval(doc + '"galleryMain' + currGalleryImage + '"' + sty + ".visibility = \'visible\'");
	eval(doc + '"tinyThumbOn' + currGalleryImage + '"' + sty + ".visibility = \'visible\'");
	eval(doc + '"descriptionMain' + currGalleryImage + '"' + sty + ".visibility = \'visible\'");
	window.status = " ";
}

// function to open video overlay
function videoPlayerOpen() {
	scroll(0, 0);
	mainOverlayObj.visibility = 'visible';
	overlayVideoPlayerObj.visibility = 'visible';
}

// define function to start playing a video in a div obj without reloading the page
function playVideo(strVideoTitle, strFlashSRC) {
	// open the overlay
	videoPlayerOpen();
	// get frame objects
	var objVideoFrame = document.getElementById('videoFrame');
	// init html var and fill with content
	var strHTML = '';
	strHTML += '<div style="text-align:center; margin-bottom: 10px; display:none;">Playing: ' + strFlashSRC + '</div>';
	strHTML += '<script type="text/javascript">';
	strHTML += '	AC_FL_RunContent(\'writeToID\',\'videoFrame\',\'codebase\',\'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0\',\'width\',\'400\',\'height\',\'345\',\'wmode\',\'transparent\',\'title\',\'Video\',\'src\',\'product_video?strVideoFilename=/_uls/resources/' + strFlashSRC + '\',\'quality\',\'high\',\'pluginspage\',\'http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash\',\'movie\',\'product_video?strVideoFilename=/_uls/resources/' + strFlashSRC + '\');';
	strHTML += '<\/script>';
	strHTML += '<noscript>';
	strHTML += '	<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="400" height="345" title="Video">';
	strHTML += '		<param name="movie" value="/product_video.swf?strVideoFilename=/_uls/resources/' + strFlashSRC + '" />';
	strHTML += '		<param name="quality" value="high" />';
	strHTML += '		<param name="wmode" value="transparent" />';
	strHTML += '		<embed src="/product_video.swf?strVideoFilename=/_uls/resources/' + strFlashSRC + '" quality="high" wmode="transparent" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="400" height="345"></embed>';
	strHTML += '	</object>';
	strHTML += '</noscript>';
	// set video frame html
	objVideoFrame.innerHTML = strHTML;
	// eval all script that was set in html
	var x = objVideoFrame.getElementsByTagName("script");
	for (var i=0; i < x.length; i++) {
		eval(x[i].text);
	}
}

// function to stop a video (clears innerHTML of video frame)
function stopVideo() {
	mainOverlayObj.visibility = 'hidden';
	overlayVideoPlayerObj.visibility = 'hidden';
	var objVideoFrame = document.getElementById('videoFrame');
	objVideoFrame.innerHTML = '';
}
// END OVERLAY FUNCTIONS ------------------------------------------

// MAIN NAVIGATION FUNCTIONS/DATA ------------------------------------------

function navOn(which) {
	showNav(which);
	return true;
}
function navOff(which) {
	hideNav(which);
	return true;
}

// function to roll a nav button on
function navStayOn(which) {
	changeImages("nav_"+which,"nav_"+which+"_roll");
	return true;
}
// function to roll a nav button off
function navStayOff(which, state) {
	changeImages("nav_"+which,"nav_"+which+"_"+state);
	return true;
}

var currCartTab = "cart";
var currSizeTab = "sizes";
var currRelatedBlock = "documents";

function cartTabOn(which, tabType) {
	if (tabType == "cart") {
		if ((currCartTab != null) && (currCartTab != which)) {
			cartTabOff(currCartTab, tabType);
		}
	} else if (tabType == "size") {
		if ((currSizeTab != null) && (currSizeTab != which)) {
			cartTabOff(currSizeTab, tabType);
		}
	}
	changeImages(tabType + "_title_" + which, tabType + "_title_" + which + "_on");
	eval("document.getElementById('module_" + which + "').style.display = 'block'");
	eval("document.getElementById('tab_" + which + "').className = '" + tabType + "_tab'");
	if (tabType == "cart") {
		currCartTab = which;
	} else if (tabType == "size") {
		currSizeTab = which;
	}
	return true;
}
function cartTabOff(which, tabType) {
	changeImages(tabType + "_title_" + which, tabType + "_title_" + which + "_off");
	eval("document.getElementById('module_" + which + "').style.display = 'none'");
	eval("document.getElementById('tab_" + which + "').className = '" + tabType + "_tab_off'");
	return true;
}

function shiftRelatedContent(which) {
	clearTimeout(navTimer);
	navTimer = null;
	//if ((currRelatedBlock != null) && (currRelatedBlock != which)) {
	//	closeRelatedContent(currRelatedBlock);
	//}
	blockOpenRelatedObj = eval("document.getElementById('module_" + which + "').style");
	blockOpenRelatedObj.display = 'block';
	currRelatedBlock = which;
	return true;
}
function closeRelatedContent(which) {
	clearTimeout(navTimer);
	navTimer = null;
	blockCloseRelatedObj = eval("document.getElementById('module_" + which + "').style");
	blockCloseRelatedObj.display = 'none';
	return true;
}
function toggleRelatedContent(which) {
	clearTimeout(navTimer);
	navTimer = null;
	blockToggleRelatedObj = eval("document.getElementById('module_" + which + "').style");
	if (blockToggleRelatedObj.display == 'none') {
		shiftRelatedContent(which);
	} else {
		closeRelatedContent(which);
	}
	return true;
}

// UTILITY FUNCTIONS/DATA --------------------------------------------------

// warns about changing language once in shopping cart
function cartLanguageWarning(strISOCode) {
	var strMsg = "Sorry, the language and currency cannot be changed once you have added items to your shopping cart.";
	if (strISOCode == 'fre') {
		var strMsg = "[FR] Sorry, the language and currency cannot be changed once you have added items to your shopping cart.";
	}
	alert(strMsg);
}

// sets cursor focus on the field given by param (formID.fieldID)
function formFocus(strFormAndFieldIDs) {
	if (isPageLoaded) {
		eval("document.forms." + strFormAndFieldIDs + ".focus();");
	} else {
		setTimeout("formFocus('" + strFormAndFieldIDs + "')", 100);
	}
}

// vars for dropdown menus
var navTimer = null;
var currNav = null;
var navHideDelay = 500;
var isNavLoaded = false;

// build dropdown objects (run onload)
function buildNav() {
	productsDropNavObj = eval(doc + '"productsDropNav"' + sty);
	accessoriesDropNavObj = eval(doc + '"accessoriesDropNav"' + sty);
	customer_serviceDropNavObj = eval(doc + '"customer_serviceDropNav"' + sty);
	isNavLoaded = true;
}

// shows a dropdown menu, hides any other dropdown that is currently showing
function showNav(which) {
	if (isNavLoaded) {
		clearTimeout(navTimer);
		navTimer = null;
		if ((currNav != null) && (currNav != which)) {
			eval(currNav + "DropNavObj.visibility = 'hidden'");
		}
		if ((which == "products") || (which == "accessories") || (which == "customer_service")) {
			currNav = which;
			eval(currNav + "DropNavObj.visibility = 'visible'");
		}
	}
}
// hides a dropdown menu on a time delay
function hideNav(which) {
	if (isNavLoaded) {
		clearTimeout(navTimer);
		navTimer = null;
		if ((which == "products") || (which == "accessories") || (which == "customer_service")) {
			navTimer = setTimeout(which + "DropNavObj.visibility = 'hidden'", navHideDelay);
		}
	}
}

// swap one or more images
function changeImages() {
	if (document.images) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			var argArray = arguments[i].split('.');
			var objName;
			if ((document.layers && argArray.length > 1) || argArray.length <= 1) {
				objName = eval("document." + arguments[i]);
			} else {
				objName = eval("document." + argArray[argArray.length - 1]);
			}
			objName.src = eval(changeImages.arguments[i+1] + ".src");
		}
	}
}

// move element
function moveObjectUp(which, intMargin) {
	var intMoveMargin = intMargin;
	divElementMoveObj = eval(doc + '"move" + which + "Image"' + sty);
	eval("divElementMoveObj.top = '" + intMoveMargin + "px'");
	
}

// dhtml layer variables for cross-browser compatability
if (document.layers) {	// Netscape 4
	doc = "document[";
	conDoc = "document";
	sty = "]";
	htm = ".document";
	cls = "].className";
} else if (document.getElementById) {	// NS 6 or IE 5 and up (W3C compliant browsers).
	doc = "document.getElementById(";
	conDoc = "document.getElementById(";
	sty = ").style";
	htm = ".document";
	cls = ").className";
} else if (document.all) {	// IE 4
	doc = "document.all[";
	conDoc = "document.all[";
	sty = "].style";
	htm = "";
	cls = "].className";
}