var mediaServerUrl = "http://media.covergirl.com";

function trimstring(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

ChatWindow = Class.create();

ChatWindow.prototype = {
	initialize:function() {
		
	},
	show:function(pars) {
		//url='http://cghelp.custhelp.com/cgi-bin/cghelp.cfg/php/enduser/live_param.php?p_mode=chat&p_firstname=Brad&p_lastname=Yuan&p_email=byuan@g2.com&interface_id=1&p_sid=1'
		url = ''	
		window.open(url, 'chatWindow', 'width=200,height=200,location=no')
	}
}

var ChatWindow = new ChatWindow();

FloatingBox = Class.create();

FloatingBox.prototype = {
	initialize:function() {
	},
	
	generateBox:function(displayFunc, width, height) {
		window.pageYOffset ? scroll_top=window.pageYOffset : scroll_top=document.documentElement.scrollTop;
		
		if (parseInt(navigator.appVersion)>3) {
			if(BrowserDetect.browser=='Explorer') {
				winW = document.documentElement.clientWidth-20;
				winH = document.documentElement.clientHeight-20;
			} else {
				winW = window.innerWidth-16;
				winH = window.innerHeight-16;
			}
		}
		
		displayCode = '<div id="floatingBoxContainer" style="height:100%;width:100%;">'
		+'<iframe height="100%" width="100%" frameborder="0" scrolling="no"></iframe>'
		+'<div style="height:'+document.body.clientHeight+'px;width:100%;background-color:#000000;filter:alpha(opacity=15);-moz-opacity:.15;opacity:.15;position:absolute"></div>'
		+'<div id="floatingBox" style="width:'+width+'px;height:'+height+'px;display:none;">'
		+'<div align="right" onclick="killFloatBox()" style="cursor:pointer;">[X]</div>'
		+'<div id="floatBoxContent"></div>'
		+'</div>'
		+'</div>'
		try {
			if (!$('floatingBoxContainer')) new Insertion.Top('mainBody', displayCode);
			$('floatingBox').style.left=Math.ceil((winW-$('floatingBox').style.width.replace('px',''))/2)+'px';
			$('floatingBox').style.top=Math.ceil((winH-$('floatingBox').style.height.replace('px',''))/2)+scroll_top+'px';
			
			$('floatingBoxContainer').style.display='block';
			
			if($('floatingBox').style.display=='none') {
				Effect.Appear('floatingBox', {duration:.5});
				$('floatBoxContent').innerHTML = eval(displayFunc+'()');
			}
		} catch (err) {
			alert('Error: Please assign the body tag with the ID of \'mainBody\'');
		}
	}
}

var FloatingBox = new FloatingBox();
var floatBoxPointer;

function startFloatListener() {
	FloatingBox.generateBox();
}

function newFloatBox(displayFunc, width, height) {
	FloatingBox.generateBox(displayFunc, width, height);
	floatBoxPointer = startFloatListener.bindAsEventListener();
	Event.observe(window, 'scroll', floatBoxPointer);
}

function killFloatBox() {
	Event.stopObserving(window, 'scroll', floatBoxPointer);
	
	Effect.Fade('floatingBox', {duration:.5});
	//setTimeout(function() {$('floatingBox').style.display='none';$('floatingBoxContainer').style.display='none';}, 500);
	setTimeout(function() {$('floatingBoxContainer').style.display='none';}, 500);
}

var tempX = 0;
var tempY = 0;
function getMouseXY(obj) {	
  	if ( obj.offsetParent ) { 
		tempX = obj.offsetLeft;
		tempY = obj.offsetTop;
		while( obj = obj.offsetParent){				
			tempX += obj.offsetLeft;
			tempY += obj.offsetTop;
		}
  	}  
}

function displayMobile(obj) {
	var message = getSkuInfo();
	if (message != '') {
		getMouseXY(obj);
		var skuListInfo = getSkuInfo().split(",");
		if(skuListInfo.length == 3) {
			tempY -= 560;
		}else{
			tempY -= 233;
		}
		$('sendToMobileContainer').style.top=tempY + 'px';	
		$('sendToMobileContainer').style.display='block';
		hideCGOverlayContainer();
	}
	else {		
		displayCGOverlayContainer(obj);
		AjaxMobile.onEmptyHandler();			
	}
}

var playspeed = 5000;  //change this to the speed you want
var slideTimer;
function displayCGOverlayContainer(obj) {
	if (!obj) {
		tempX = mouseX - 70;
		tempY = mouseY - 108;		
	}
	else {
		getMouseXY(obj);
		tempY -= 100;
	}
	
	$('cgOverlayBody').innerHTML = '';	
	$('cgOverlayContentContainer').style.top=tempY + 'px';	
	$('cgOverlayContentContainer').style.left=tempX + 'px';
			
	if ($('cgOverlayContentContainer').style.display == 'none') {
		$('cgOverlayContentContainer').style.display='block';
	}
	clearTimeout(slideTimer);
		
}

function setHideTimer() {
	slideTimer = setTimeout("hideCGOverlayContainer()", playspeed); //settime out
}

function hideCGOverlayContainer() {	
	$('cgOverlayContentContainer').style.display='none';
	$('cgOverlayBody').innerHTML = '';
	clearTimeout(slideTimer); //clear the time out		
}


if (!document.all) document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = getMousePosition;
		
var mouseX = 0;
var mouseY = 0;

function getMousePosition(e) {
  e = e || window.event;
  if (document.all) { // grab the x-y pos.s if browser is IE
     var de = document.documentElement;
     var b = document.body;
     mouseX = e.clientX + (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
     mouseY = e.clientY + (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
  } else {  // grab the x-y pos.s if browser is NS
    mouseX = e.pageX;
    mouseY = e.pageY;
  }  
  // catch possible negative values in NS4
  if (mouseX < 0){mouseX = 0;}
  if (mouseY < 0){mouseY = 0;}  
}

var ajaxTimer;
function ajaxSetTimeOut(func) {	
	ajaxTimer = setTimeout("eval(" + func + ")",120000); //for 2 mins
}

var inputs = null;
var flashdiv = null;
function sl_close() {
	$('sl_container').style.display='none';
	$('slContent').innerHTML = '';
	if(flashdiv != null){
		flashdiv.innerHTML = '';
		flashdiv = null;
	}	
}

function sl_open(slID) {
	$('sl_container').style.height = findWinHeight() + 'px';
	if (slID != '') {
		$('slContent').innerHTML = $('slink'+slID).innerHTML;
		
		/*search for flash piece to insert upon opening. information to be held in a hidden input with id beginning with 'flash_'*/
		inputs = $('slink'+slID).getElementsByTagName('input');
		for(var x=0;x<inputs.length;x++){
			if(inputs[x].id.substring(0,6) == "flash_"){
				var strArr = inputs[x].value.split(",");
				outputSwatchFlash("flashmovie" + slID, strArr[1], strArr[2], "", mediaServerUrl + strArr[3], "", strArr[0]);
				flashdiv = $(strArr[0]);
			}
		}
		
	}
	$('sl_container').style.display='block';
}

function findWinHeight() {
    var h; //height
	
	if (typeof( window.innerHeight ) == 'number') { //except ie
		h = window.innerHeight + window.scrollMaxY;
	}
    else if (document.documentElement && document.documentElement.clientHeight) { //
        h = document.documentElement.scrollHeight; 
    }
	else if (document.body) {
		h = document.body.clientHeight + document.body.scrollHeight;
	}
    
	return (h-140);
	
}

function sl_close_look(slID) {
	$('sl_container_look').style.display='none';
	$('slink'+slID).innerHTML = $('slContent_look').innerHTML;
	$('slContent_look').innerHTML = '';
}

function sl_open_look(slID) {
	
	$('sl_container_look').style.height = findWinHeight() + 'px';
	if (slID != '') {
		$('slContent_look').innerHTML = $('slink'+slID).innerHTML;
		$('slink'+slID).innerHTML = '';
	}
	$('sl_container_look').style.display='block';
}

var sels = null;
function sl_close_howto() {
	if(BrowserDetect.browser == "Explorer" && BrowserDetect.version <= 6){
		//var sels = document.getElementsByTagName('select');
		for(var x=0;x<sels.length;x++){
			sels[x].style.visibility = "visible";
		}
	}
	$('howto_flashdiv').innerHTML = '';
	$('sl_container_howto').style.display='none';	
}

function sl_open_howto(slID) {	
	//alert("here comes the popup " + slID);
	if(BrowserDetect.browser == "Explorer" && BrowserDetect.version <= 6){
		if(sels == null){
			sels = document.getElementsByTagName('select');
		}
		for(var x=0;x<sels.length;x++){
			sels[x].style.visibility = "hidden";
		}
	}
	$('sl_container_howto').style.height = findWinHeight() + 'px';
	if(slID != ''){
		try {
			$('howto_title').lastChild.src = "images/ot_" +slID+ ".gif";
			$('howto_title').lastChild.alt = $('howto_tipTitle'+slID).innerHTML;
		}catch(err){}
		$('howto_desc_text').innerHTML = $('howto_tipText'+slID).innerHTML;		
		//var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
		//if (hasReqestedVersion) {
			outputSwatchFlash('howtoMovie', 344, 299, "", mediaServerUrl + "/tips/face/flash/Tip_00"+(parseInt(slID)+1)+".swf", "", "howto_flashdiv");
		//}
	}
	$('sl_container_howto').style.display='block';
}

function sl_close_antmvideo() {
	if(BrowserDetect.browser == "Explorer" && BrowserDetect.version <= 6){
		//var sels = document.getElementsByTagName('select');
		for(var x=0;x<sels.length;x++){
			sels[x].style.visibility = "visible";
		}
	}
	$('antmvideo_flashdiv').innerHTML = '';
	$('sl_container_antmvideo').style.display='none';	
}

function sl_open_antmvideo(slID) {	
	if(BrowserDetect.browser == "Explorer" && BrowserDetect.version <= 6){
		if(sels == null){
			sels = document.getElementsByTagName('select');
		}
		for(var x=0;x<sels.length;x++){
			sels[x].style.visibility = "hidden";
		}
	}
	$('sl_container_antmvideo').style.height = findWinHeight() + 'px';
	if(slID != ''){
		$('antmvideo_title').lastChild.src = "images/ot_" +slID+ ".gif";
		$('antmvideo_title').lastChild.alt = $('antmvideo_tipTitle'+slID).innerHTML;
		$('antmvideo_desc_text').innerHTML = $('antmvideo_tipText'+slID).innerHTML;		
		//var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
		//if (hasReqestedVersion) {
			outputSwatchFlash('antmvideoMovie', 344, 299, "", mediaServerUrl + "/antm/flash/Video_00"+(parseInt(slID)+1)+".swf", "", "antmvideo_flashdiv");
		//}
	}
	$('sl_container_antmvideo').style.display='block';
}

function sl_close_standard(theId) {
	if(BrowserDetect.browser == "Explorer" && BrowserDetect.version <= 6){
		//var sels = document.getElementsByTagName('select');
		for(var x=0;x<sels.length;x++){
			sels[x].style.visibility = "visible";
		}
	}	
	if($(theID + "_flashdiv")){
		$(theID + "_flashdiv").innerHTML = '';			
	}
	$(theID).style.display='none';	
}

function sl_open_standard(theID) {	
	if(BrowserDetect.browser == "Explorer" && BrowserDetect.version <= 6){
		if(sels == null){
			sels = document.getElementsByTagName('select');
		}
		for(var x=0;x<sels.length;x++){
			sels[x].style.visibility = "hidden";
		}
	}
	$(theID).style.height = findWinHeight() + 'px';
	if($(theID + "_flashdiv") && $(theID + "_flashstring")){
		var str = $(theID + "_flashstring").value;
		var strArr = str.split(",");
		alert(strArr[0] + " " + strArr[1] + " " + strArr[2]);
		//var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
		//if (hasReqestedVersion) {
			//outputSwatchFlash(theID + "movie", strArr[0], strArr[1], "", mediaServerUrl + strArr[2] +".swf", "", theId + "_flashdiv");
		//}
	}	
	$(theID).style.display='block';
}
