/* Load all (document).ready() and dom:loaded events at the bottom */
function launchBackorder(pro){
	window.open('/resource/templates/products/backorder.cfm?pro='+pro, 'Backorder', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=350,height=280');
}
			
function setVisibilityBySelector(selector, newState){
	var topAry = $$(selector);
	
	for (i=0; i<topAry.length; i++){
		topAry[i].style.visibility = newState;
		//visible,hidden
	}
}

function openOverLay(value, type, headerText, width, height,ytB,Burl){
	if(!headerText)
		headerText = '';
	if(!Burl){
		ytB = '';
		Burl = '';
	}
	
	if (type == "iframe"){
		if(jQuery(document).height() < 2100)
			jQuery('#outerContainer').height(2100);
		insertOverLay('overlay', 'overlayIframe');
	}
	else{
		insertOverLay('overlay', 'overlay');
	}
	
	insertOverLayContentContainer('overlayContentContainer', 'overlayContentContainer', headerText);
	
	type = type.toLowerCase();
	switch(type)
	{
		case "youtube":
		  insertYoutube('overlayContentContainer', 'videoContainer', value, width, height,ytB,Burl);
		  break;
		case "video":
		  insertVideo('overlayContentContainer', 'videoContainer', value, width, height);
		  break;    
		case "image":
		  insertImage('overlayContentContainer', 'imageContainer', value);
		  break;
		case "flash":
		  insertFlash('overlayContentContainer', 'imageContainer', value, width, height);
		  break;
		case "iframe":
		  insertiFrame('overlayContentContainer', 'iFrameContainer', value, width, height);
		  break;
		case "iframe2":
		  insertiFrame2('overlayContentContainer', 'iFrameContainer', value, width, height);
		  break;
		default:
	} 

	recenterContentContainer('overlayContentContainer');
}

function insertFlash(containerId, id, src, width, height){
	
	htmlOutput = '<div style="font-weight: bold; text-align: right;"><a href="javascript:removeOverLay()">close [x]</a></div><div style="padding:8px; background-color:#FFFFFF"><div id="' + id + '"><div id="playerdiv"></div></div></div>';	
	//$(containerId).insert(htmlOutput);
	$(containerId).innerHTML = htmlOutput;
	
	var so = new SWFObject(src, "main", width, height, 8, "#FFFFFF");
	so.addParam("allowFullScreen", "true");
	so.write("playerdiv");
}

function insertiFrame(containerId, id, src, width, height){
	height = 2100;
	htmlOutput = '<div style="font-weight: bold; text-align: right;"></div><div style="z-index:10000; overflow:hidden;"><iframe frameborder="no" height="'+ height +'" width="'+ width +'" src="' + src + '" style="overflow:hidden;"></iframe>';	
	//$(containerId).insert(htmlOutput);
	//$(containerId).setStyle({width:'950px'});
	//$(containerId).setStyle({height:'2000px'});
	$(containerId).setStyle({width:width + 'px'});
	$(containerId).setStyle({height:height + 'px'});
	$(containerId).setStyle({backgroundColor:'#FFFFFF'});
	$(containerId).innerHTML = htmlOutput;
	
}

function insertiFrame2(containerId, id, src, width, height){
	htmlOutput = '<div style="font-weight: bold; text-align: right;"><a href="javascript:removeOverLay()">close [x]</a></div>';
	htmlOutput += '<div style="z-index:10000; overflow:hidden;"><iframe id="'+id+'" frameborder="no" height="'+ height +'" width="'+ width +'" src="' + src + '" style="overflow:hidden;"></iframe>';
	$(containerId).innerHTML = htmlOutput;
}

function recenterContentContainer(id){
	
	contentContainerWidth = $(id).getWidth();	
	contentContainerHeight = $(id).getHeight();
	tmpLeft = (document.viewport.getWidth()/2) - (contentContainerWidth/2);
	tmpTop = (document.viewport.getHeight()/2) - (contentContainerHeight/2);
	
	if(tmpLeft < 0){
		tmpLeft = 0;
	}
	
	if(tmpTop < 0){
		tmpTop = 0;
	}
	
	if(contentContainerHeight > document.body.getHeight()){
		document.body.setStyle({height:contentContainerHeight + 'px'});
	}
	
	topOffset = document.viewport.getScrollOffsets().top;
	topFinal = tmpTop + topOffset; //Stephen
	$(id).style.left = tmpLeft + "px";
	$(id).style.top = topFinal + "px"; //Stephen
	//$(id).style.top = tmpTop + topOffset + "px";
	//$(id).style.top = tmpTop + "px"; //Stephen: commented out
}

function resizeOverLay(id, outterContainerId){
	
	tmpHeight1 = $(outterContainerId).getHeight();
	tmpHeight2 = $(document.body).getHeight();

	if(tmpHeight1 > tmpHeight2){
		tmpHeight = tmpHeight1;
	}else{
		tmpHeight = tmpHeight2;
	}
	
	$(id).style.height = tmpHeight + "px";
}

function insertOverLay(id, className){
	
	if( $('hideContent') ) {
		$('hideContent').toggle();
	}
	if( $('hideContent2') ) {
		$('hideContent2').toggle();
	}
	
	$(document.body).insert("<div class='" + className + "' id='" + id + "'>&nbsp;</div>");
	
	resizeOverLay('overlay', 'outerContainer');
	
	//setVisibilityBySelector('select', 'hidden');
}

function insertOverLayContentContainer(id, className, headerText){
	htmlOutput  = '<div class="' + className + '" id="' + id + '">';
	htmlOutput += "<div class='header'>" + headerText + "</div>";
	htmlOutput += '<div style="font-weight:bold; text-align:right;"><a href="javascript:removeOverLay()">close [x]</a></div>';
	htmlOutput += '</div>';
	$(document.body).insert(htmlOutput);
}

function removeOverLay(){
	if( $('hideContent') ) {
		$('hideContent').toggle();
	}
	if( $('hideContent2') ) {
		$('hideContent2').toggle();
	}
	if($('videoContainer')){
		$('videoContainer').remove();
	}
	
	$('overlayContentContainer').remove();
	$('overlay').remove();
	
	setVisibilityBySelector('select', 'visible');
	//document.body.style.overflow = "";
}

function insertYoutube(containerId, id, myFlashVars, width, height, ytB, Burl){
	if(width == undefined)
		width = 435;
	
	if(height == undefined)
		height = 354;
	/* Extra button code, if the URL exists */
	if(!Burl || ytB == 'hide')
		xB = '';
	else
		xB = '<div style="padding:8px;background-color:#FFFFFF"><div style="background:url(/resource/images/ytBtnBg2.jpg) repeat-x;height:60px;text-align:center;"><a href="' + Burl + '" target="_blank"><img style="margin-top:8px;"src="/resource/images/' + ytB + '.png" /></a></div></div></div>';
	if(myFlashVars.loadedContent == undefined)
		htmlOutput = '<div style="font-weight: bold; text-align: right;"><a href="javascript:removeOverLay()">close [x]</a></div><div style="padding:8px; background-color:#FFFFFF;"><div id="' + id + '">' + myFlashVars + '</div> </div>' + xB;
	else
		htmlOutput = '<div style="font-weight: bold; text-align: right;"><a href="javascript:removeOverLay()">close [x]</a></div><div style="padding:8px; background-color:#FFFFFF;"><div id="' + id + '">' + myFlashVars.loadedContent + '</div> </div>' + xB;
	
	$(containerId).innerHTML = htmlOutput;
}

function insertVideo(containerId, id, myFlashVars, width, height){
	htmlOutput = '<div style="padding:8px; background-color:#FFFFFF"><div id="' + id + '"><div id="playerdiv"></div></div></div>';	
	$(containerId).insert(htmlOutput);

	if(width == undefined){
		width = 360;
	}
	
	if(height == undefined){
		height = 266;
	}
	
	var so = new SWFObject("/resource/swf/videoplayer.swf", "main", width, height, "8", "#FFFFFF");
	so.addParam("allowFullScreen", "true");
	so.addVariable("videoLink", myFlashVars.loadedContent); 
	so.addVariable("videoVolume", "75");
	so.addVariable("autoplay", "true");
	so.write("playerdiv");
}

function insertImage(containerId, id, src){
	
	htmlOutput = '<div style="padding:8px; background-color:#FFFFFF"><div id="' + id + '"><img src="' + src + '" alt="" onload="recenterContentContainer(\'' + containerId + '\');" /></div></div>';	
	$(containerId).insert(htmlOutput);
}

if(typeof Prototype != "undefined"){
	Event.observe(window, "resize", function() {
		
		if($('overlay')){
			resizeOverLay('overlay', 'outerContainer');						 
			recenterContentContainer('overlayContentContainer');
		}
	});
}

function fixURLsToOpenInANewWindow(objContainer){
	for(i = 0; i < objContainer.childNodes.length; i++){
		objNode = objContainer.childNodes[i]
		if(objNode.tagName == 'A'){
			// Set the node's target to _blank to cause it to open a new window...
			objNode.target="_blank";
			objNode.className = "blue";
		}
	}
}

function popUpWindow(strURL, intWidth, intHeight){
	intLeft = (window.screen.width - (intWidth))/2;
	intTop = (window.screen.height - (intHeight))/2;
	objWindow = window.open(strURL,'_blank', 'width=' + intWidth + ', height=' + intHeight + ', left=' + intLeft +  ', top=' + intTop + ', location=yes, titlebar=yes, toolbar=yes, fullscreen=yes, scrollbars=yes, resizable=yes, menubar=yes, status=yes');
}

blnIsIE = navigator.appName.indexOf("Internet Explorer") == -1?false:true;
function insertAtCursor(myField, myValue){
	//IE support
	if(blnIsIE){
		if (document.selection){
			myField.focus();
			sel = document.selection.createRange();
			sel.text = myValue;
		}
	}
	else{
		if(myField.selectionStart || myField.selectionStart == 0){
			var startPos = myField.selectionStart;var endPos = myField.selectionEnd;
			myField.value = myField.value.substring(0, startPos)+ myValue+ myField.value.substring(endPos, myField.value.length);
		} 
		else{
			myField.value += myValue;
		}
	}
}
function contentExpand(show, content){
	Effect.toggle(show,'appear');
	Effect.SlideDown(content);
}
function contentDexpand(show, content){
	Effect.toggle(show,'appear');
	Effect.SlideUp(content);
}
function showContent(button, content){
	Effect.SlideDown(content);
	$(button).innerHTML = '<div id="' + button + '"></div>';
}
function hideContent(button, content){
	Effect.SlideUp(content);
	$(button).innerHTML = '<div id="' + button + '"><a href="javascript:void(0)" onclick="showContent( \'' + button + '\', \'' + content + '\')">read more &raquo;</a></div>';
}
function checkInputZip(){
	action = $F('wtbAction');
	paging = $F('paging');
	tempZip = $F('inputZip').replace(/[^0-9-]/g,"");
	if(tempZip != ''){
		zip = tempZip;
	}
	else{
		zip = 'all';
	}
	$('ZipForm').writeAttribute('action','/customerservice/where-to-buy/' + action + '/' + zip + '/' + $F('selectDistance') + '/' + paging);
}
function checkInputZipRC(){
	action = $F('wtbAction');
	paging = $F('paging');
	tempZip = $F('inputZip').replace(/[^0-9-]/g,"");
	if(tempZip != ''){
		zip = tempZip;
	}
	else{
		zip = 'all';
	}
	if($F('sto_state') != ''){
		state = $F('sto_state');
	}
	else{
		state = 'n';
	}
	if($F('sto_inwarranty') != ''){
		inwarranty = $F('sto_inwarranty');
	}
	else{
		inwarranty = 'n';
	}
	/*if($F('sto_preferred') != ''){
		preferred = $F('sto_preferred');
	}
	else{
		preferred = 'n';
	}*/
	//$('ZipForm').writeAttribute('action','/customerservice/where-to-buy/' + action + '/' + zip + '/' + $F('selectDistance') + '/' + paging + '/' + state + '/' + inwarranty + '/' + preferred);
	$('ZipForm').writeAttribute('action','/customerservice/where-to-buy/' + action + '/' + zip + '/' + $F('selectDistance') + '/' + paging + '/' + state + '/' + inwarranty);
}

/* BEGIN Help Balloon JS */
// Override the default settings to point to the parent directory
HelpBalloon.Options.prototype = Object.extend(HelpBalloon.Options.prototype, {
	icon: "/resource/javascript/HelpBalloon/images/icon.gif",
	button: "/resource/javascript/HelpBalloon/images/button.gif?x=1500",
	balloonPrefix: "/resource/javascript/HelpBalloon/images/balloon-"
});
function switchImage_HelpBalloon(idToSwitch, newSrc) {
	$(idToSwitch).src = newSrc;
}
/* END Help Balloon JS */
