// JavaScript for CF Triple R
IsMSIE  = navigator.appVersion.indexOf('MSIE') != -1;
IsMSIE7 = navigator.appVersion.indexOf('MSIE 7') != -1;
IsMSIE8 = navigator.appVersion.indexOf('MSIE 8') != -1;
TransitionSpeed_Overlays = 250;


// JavaScript for ChoiceTech

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
			} 
		while (obj = obj.offsetParent);
	}

	return [curleft,curtop];
}

function showDropDown() {
	var productMenu = document.getElementById('productmenu');
	var productTab = document.getElementById('producttab');
	var posLeft = findPos(productTab);
	//alert(posLeft[0]);
	productMenu.style.left = posLeft[0] - 42;
	productMenu.style.top = posLeft[1] + 27;
	productMenu.style.display = 'block';
}




// Sets the "visibility" css property of an object to "hidden" or "visible"
function toggleCSSDisplay(objID, displayOverride) {
	
	if (displayOverride)
		document.getElementById(objID).style.display = displayOverride;
	
	else {
		d = document.getElementById(objID).style.display;
		if (d == "block" || d == "")
			document.getElementById(objID).style.display = "none";
		else
			document.getElementById(objID).style.display = "block";
	}
}

// Returns left-of-center values
function placeObjFromCenter_coords(LeftValueWhenEvenBrowserWidth) {

	BrowserWidth = document.body.clientWidth;

	if (String(BrowserWidth/2).match(/\.[0-9]{1,}/))
		return Math.floor(BrowserWidth/2) + LeftValueWhenEvenBrowserWidth;
	else
		return BrowserWidth/2 + (LeftValueWhenEvenBrowserWidth);
}

// Places an image off-center
function placeObjFromCenter(ImgID, l, t) {
	use_position = "absolute";
	use_left     = placeObjFromCenter_coords(l);
	use_top      = (document.all)?t:(t-3);

	document.getElementById(ImgID).style.visibility = "visible";
	document.getElementById(ImgID).style.position = use_position;
	document.getElementById(ImgID).style.left = use_left; // 84
	document.getElementById(ImgID).style.top  = use_top; // 18;
}

// Renames the SRC and size of an image
function renameSRCandSizeOfImg(ImgID, NewSrc, NewX, NewY) {
	document.getElementById(ImgID).src    = NewSrc;
	document.getElementById(ImgID).width  = NewX;
	document.getElementById(ImgID).height = NewY;
}

// Generates email address "safe" from harvesters
function generateAntiSpiderEmail(Username, DomainName, TLD) {
	EmailAddress = Username + "&#64;" + DomainName + "." + TLD;
	document.write("<a href=\"ma" + "il" + "to:" + EmailAddress + "\">" + EmailAddress + "</a>");
}

// OPENS A DYNAMIC WINDOW
function openDynamicWindow(output, WinName, WinParams) {
	DynWin = window.open("", WinName, WinParams);
	
	with (DynWin.document) {
		open();
		write(output);
		close();
	}
}

// ROTATES OUTPUT, MARQUEE-STYLE
function rotateOutput(OutputArray, PlaceInObj) {
	
	if (typeof ArrayCounter == 'undefined')
		ArrayCounter = 0;
	
	if (typeof OutputArray[ArrayCounter] == 'undefined')
		ArrayCounter = 0;
	
	if (navigator.appVersion.indexOf('MSIE') != -1) {
		PlaceInObj.style.filter = "revealTrans(Duration=1,Transition=4)";
		PlaceInObj.filters[0].Apply();
		PlaceInObj.filters[0].Play();
	}
	
	PlaceInObj.innerHTML = OutputArray[ArrayCounter];
	ArrayCounter++;
}

// fadeObj() and changeOpac() functions from http://brainerror.net/scripts/javascript/blendtrans/
function fadeObj(id, opacStart, opacEnd, millisec) {
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        
        for(var i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
        
    } else if(opacStart < opacEnd) {
    
        for(var i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

// change the opacity for different browsers
function changeOpac(setOpac, id) {
	with (document.getElementById(id).style) {
		opacity		= (setOpac / 100);
		MozOpacity	= (setOpac / 100);
		KhtmlOpacity	= (setOpac / 100);
		filter		= "alpha(opacity=" + setOpac + ")";
	}
}

// Function to create image overlay
// Supports embedding a youtube video with VIDEOSRC:http://www.youtube.com/...
function overlay_OPEN(ImgSrc, ImgLabel, ImgText, OverrideWidthForAlternate) {
	
	IsYoutubeVideo = ImgSrc.substr(0,9) == 'VIDEOSRC:';
	IsHTML         = ImgSrc.substr(0,5) == 'HTML:';

	with (document) {
		NewContainerDiv	= createElement('div');
		NewDisableDiv	= createElement('div');
		NewLoadingDiv   = createElement('div');
		NewImg		= createElement('img');
		NewXImg		= createElement('img');
		NewTextContainerDiv = createElement('div');
	}
	
	with (NewImg) {
		src		= ImgSrc;
		style.cursor	= 'pointer';
		style.display	= 'block';
	
		TmpLabel = ((ImgLabel)?ImgLabel + ' ':'') + '[click to close]';		
		setAttribute('alt', TmpLabel);
		setAttribute('title', TmpLabel);
		setAttribute('id', 'MainImg');
	}
	
	with (NewXImg) {
		src = '/img/x.gif';
		
		style.position	= 'absolute';
		style.margin	= '-500px 0px 0px ' + ((IsMSIE7 && IsYoutubeVideo)?'5':NewImg.width-10) + 'px';
		style.cursor	= 'pointer';
		
		setAttribute('width', '30');
		setAttribute('height', '35');
		setAttribute('alt', 'close');
		setAttribute('title', 'close');
		setAttribute('id', 'XImg');
	}
	
	setEvent(NewImg, 'onClick', 'overlay_CLOSE()');
	setEvent(NewXImg, 'onClick', 'overlay_CLOSE()');
	
	if (NewImg.width == 0 && !IsYoutubeVideo && !IsHTML) {
		setTimeout('overlay_OPEN("' + ImgSrc + '", "' + ImgLabel + '", "' + ImgText + '")', 500);
		return;
	}
	else if (IsYoutubeVideo)
		NewImg.width = (OverrideWidthForAlternate)?OverrideWidthForAlternate:425;
		
	else if (IsHTML)
		NewImg.width = OverrideWidthForAlternate;
	
	with (NewTextContainerDiv.style) {
		width  = (NewImg.width - 20) + 'px';
		margin = '10px 10px 5px 10px';
	}	

	setLeft = (Math.round(document.body.clientWidth / 2)*1)/1 - ((NewImg.width + 10) / 2);
	setLeft = ((String(setLeft).indexOf('.') == -1)?String(setLeft):String(setLeft).substr(0, String(setLeft).lastIndexOf('.'))) + 'px';
	
	setTop = String(document.body.scrollTop + 35) + 'px';
	
	// IE doesn't get a loading div because it's retarded (doesn't set "left" property, for no obvious reason)
	if (!IsMSIE) {
		with (NewLoadingDiv) {
			style.position        = 'absolute';
			style.border          = '1px solid #A0A0A0';
			style.backgroundColor = '#000000';
			style.color           = '#A0A0A0';
			style.padding         = '20px';
			style.width           = '200px';
			style.left            = (Math.round(document.body.clientWidth / 2) - 110) + 'px';
			style.textAlign       = 'center';
			style.zIndex          = '100';
			style.top             = setTop;
			
			setAttribute('id', 'LoadingContainer');
			innerHTML = '[loading]';
		}
	}
	
	with (NewContainerDiv) {
		setAttribute('id', 'MainImgContainer');
		
		style.position	= 'absolute';
		style.top	= setTop;
		style.left	= setLeft;
		style.zIndex	= '100';
		style.background= '#FFFFFF';
		style.fontFamily= 'Arial';
		style.fontSize	= '12px';
		style.padding   = '5px';
		
		// IE's too retarded to display the XImg properly when the overlay fades in, so IE doesn't get a fade.  Boo-hoo.
		if (!IsMSIE) {
			style.opacity      = 0
			style.MozOpacity   = 0
			style.KhtmlOpacity = 0
			style.filter       = "alpha(opacity=0)";
			
			style.display = 'none';
		}
	}
	
	with (NewDisableDiv.style) {
		top        = '0px';
		left       = '0px';
		width      = '100%';
		position   = 'absolute';
		height     = String(document.documentElement.scrollHeight) + 'px';
		background = '#000000';
		zIndex     = '99';
		
		if (IsMSIE) {
			//toggleSelectMenuVisibility(true, 'hidden');
			filter = 'alpha(opacity=50)';
		}
		else {
			setProperty('-moz-opacity', '.50', ''); 
			setProperty('opacity', '.50', '');			
		}			
	}
	
	NewDisableDiv.setAttribute('id', 'DisableDiv');
	
	with (document.body) {
		if (!IsMSIE)
			appendChild(NewLoadingDiv);
			
		appendChild(NewDisableDiv);
		appendChild(NewContainerDiv);
	}
	
	if (IsYoutubeVideo) {
		NewContainerDiv.innerHTML
		= '<object width="425" height="344" style="margin-bottom: 20px"><param name="movie" value="' + ImgSrc.substr(9) + '"></param>'
		+ '<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param>'
		+ '<embed src="' + ImgSrc.substr(9) + '" type="application/x-shockwave-flash" width="425" height="344" allowscriptaccess="always" '
		+ 'allowfullscreen="true" style="margin-bottom: 20px"></embed></object>';
		
		//NewContainerDiv.appendChild(NewXImg);
	}
	else if (IsHTML) {
		NewContainerDiv.innerHTML = ImgSrc.substr(5);
		//NewContainerDiv.appendChild(NewXImg);
	}	
	else {
		//NewContainerDiv.appendChild(NewXImg);
		NewContainerDiv.appendChild(NewImg);
	}
	
	NewContainerDiv.appendChild(NewTextContainerDiv);
	NewTextContainerDiv.innerHTML = ImgText +
	'<p style="margin-top: 15px; font-size: 11px; font-weight: bold; text-align: center"><a href="javascript: overlay_CLOSE();">Return to Products Page</a></p>';
	
	if (!IsMSIE)
		setTimeout("document.getElementById('MainImgContainer').style.display = 'block'; fadeObj('MainImgContainer', 0, 100, TransitionSpeed_Overlays); " +
		           "document.body.removeChild(document.getElementById('LoadingContainer')); ", TransitionSpeed_Overlays * 5);
}

// Function to close overlay
function overlay_CLOSE() {
	DeleteCodeExec = "document.body.removeChild(document.getElementById('DisableDiv')); document.body.removeChild(document.getElementById('MainImgContainer'));";
	
	if (!IsMSIE) {
		fadeObj('MainImgContainer', 100, 0, TransitionSpeed_Overlays);
		setTimeout(DeleteCodeExec, TransitionSpeed_Overlays + 100)
	}
	else
		eval(DeleteCodeExec);
}



// ASSIGNS A JAVASCRIPT EVENT TO A SPECIFIED OBJECT ACCORDING TO THE BROWSER
function setEvent(Obj, Event, Action) {
	
	// For IE, use attachEvent
	if (IsMSIE) {
		eval("Obj.attachEvent('" + Event.toLowerCase() + "', function() { " + Action + " });");
	}
	
	// For proper browsers, simply use setAttribute
	else
		Obj.setAttribute(Event, Action);
}
