// Flash debugging code  
var printable;
var popup;
var newwindow = 0;
var lastpageid = "Home";
var NEW_MAIN_FORMAT = "main:pdp//objectid+{0}//{pdp_tab:pdp_overview//objectid+{1}//}";
var NEW_OVERLAY_FORMAT = "overlay:_blank";
var PRODUCTID_LOCATOR = "objectid+";
var PRODUCTID_END_LOCATOR = "//";

if (typeof (console) == 'undefined') {
	console = {};
	console.log = function() { };
}

if(getCookie("referrer") == "" && document.referrer != "") {
	document.cookie="referrer="+escape(document.referrer);	
}

function scrollToTop()
{
	scroll(0,0);
}


function cmTrack()
{
	cmCreatePageviewTag("LOREAL_PAGE", "LOREAL_CATEGORY", "", "");
}

function getCookie(c_name)
{
	if (document.cookie.length > 0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) { 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) {
				c_end=document.cookie.length;
			}
			return unescape(document.cookie.substring(c_start,c_end));
		} 
	}
	return "";
} 


///////////////////////////////////////////////////////////////////////
//////   perform redirects for old-style urls

/*
why?
The new version of swfaddress has changed the URL format to:
http://whatever/#/?whatever
There may and probably will still be users with old bookmarks and
links floating around that look like:
http://whatever/#whatever
or
http://whatever/#?whatever

we need to look for this on the client side because the browser
doesn't send the part after the # to the server.
if we find an incorrect URL, we change it and redirect.

*/

function doRedirectForImproperURLs() {
	var urlstr = '' + document.location;

	var index = urlstr.indexOf('/#/?');
	if (index > -1) return; // correct syntax, no redirect

	// forced seo redirect, no need to fix URL
	if (urlstr.toLowerCase().indexOf('html=1') > -1) return;

	// the order is important
	if (checkForRedirect(urlstr, '/#?', '/#/?')) return;
	if (checkForRedirect(urlstr, '/?#', '/#/?')) return;
	if (checkForRedirect(urlstr, '#?', '#/?')) return;
	if (checkForRedirect(urlstr, '?#', '#/?')) return;
	if (checkForRedirect(urlstr, '/#', '/#/?')) return;
	if (checkForRedirect(urlstr, '/?', '/#/?')) return;
	if (checkForRedirect(urlstr, '#', '#/?')) return;
	if (checkForRedirect(urlstr, '?', '#/?')) return;
}

// why? DRY principle.
// for a given substring, if it is found, replace and redirect.
// returns true if we redirected, otherwise false.
// this function is only used by doRedirect, above.
//
function checkForRedirect(urlstr, sub, repl)
{
	var index = urlstr.indexOf(sub);
	if (index > -1) {
		var s1 = urlstr.substring(0, index);
		var s2 = urlstr.substring(index + sub.length);
		redir =  s1 + repl + s2; // return correct url
		//alert('changing document.location to' + redir)
		window.location = decodeURIComponent(redir);
		return true;
	}
	return false;
}


///////////////////////////////////////////////////////////////////////
//////   sharing

var pagespec='top{media:register}';

function setPagespec(str){
	pagespec=str;		
}			

function shareOnDelicious(url, title)
{
	window.open('http://delicious.com/save?v=5&amp;noui&amp;jump=close&amp;url='+encodeURIComponent(url)+'&amp;title='+encodeURIComponent(title), 'delicious','toolbar=no,width=550,height=550');
	return false;
}


function checkUnload() {				
	var flashMovie = document.getElementById('website');
	flashMovie.navigateFromJS(pagespec);

	var msg = '...';
	return msg;
}

function showMsgBeforeLeaving(on){
	window.onbeforeunload = (on) ? checkUnload : null;
}

showMsgBeforeLeaving(false);


///////////////////////////////////////////////////////////////////////
//// crm tracking

function SetSiebelReaction()
{
	var qs = new Querystring();
	var siebelReaction = qs.get("crm_email");
	var passedID = "";

	if (siebelReaction != null && siebelReaction != "") {
		passedID = siebelReaction;
	}

	if (passedID != "") {
		Set_Cookie("crm_email", passedID, 0 ,"/","","");
	}
}

function Set_Cookie( name, value, expires, path, domain, secure ) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if (expires) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date(today.getTime() + (expires));

	document.cookie = name + "=" + escape(value) +
		((expires) ? ";expires=" + expires_date.toGMTString() : "") + 
		((path) ? ";path=" + path : "") + 
		((domain) ? ";domain=" + domain : "") +
		((secure) ? ";secure" : "");
}

///////////////////////////////////////////////////////////////////////
/// omniture tracking

var omnitureaccname;

function setOmnitureAccountName(accname)
{
	if ((accname + '') == 'undefined' || accname == null || accname == '') {
		omnitureaccname = 'lorealuscomdev';
	} else {
		omnitureaccname = accname;
	}
}

function getOmnitureAccountName()
{
	return omnitureaccname;
}


///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
/// atlas tracking

var TrackTag = 'http://switch.atdmt.com/jaction/';

function GetActionTag(URL) { 
	document.action_tag.src = TrackTag + URL;
}

///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
//// I don't know

function findElementWithName(d, name)
{
	if (d.nodeName == name) {
		return d;
	} else {
		for (var c = d.firstChild ; c ; c = c.nextSibling) {
			var t = findElementWithName(c, name);
			if (t != null) return t;
		}
		return null;
	}
}

function showAdditionalFooter(val)
{
	if ('' + val == 'true') {
		var c = document.getElementById('fm2');
		c.style.display = 'block';
	} else {
		var c = document.getElementById('fm2');
		c.style.display = 'none';
	}
}

function resizeFlash(newheight)
{
	var c = document.getElementById('flashcontent');
	c.style.height = '' + newheight  + 'px';
	// for safari we have to resize the embed tag also
	// since we are doing embed we might as well do the object tag as well
	var obj = findElementWithName(c, 'OBJECT');
	if (obj != null) {
		obj.height = newheight;
	}
	var embed = findElementWithName(c, 'EMBED');
	if (embed != null) {
		embed.height = newheight;
	}
}

///////////////////////////////////////////////////////////////////////
//// inserting the swfobject

// why:
// this function takes care of the annoying
// tricky business of adding a potentially
// null string onto the end of a potentially
// null string and making sure you don't get
// stray &'s
function addQSVar(qs, toadd)
{
	if (qs == '' && toadd == '') {
		return '';
	} else if (qs == '') {
		return toadd;
	} else if (toadd == '') {
		return qs;
	} else {
		return qs + '&' + toadd;
	}
}

var ___cid;

function setCID(newcid)
{
	___cid = newcid;
}

// why: pass query string values into flash.
// flashvar syntax is same as the query string format
// so we don't really need to parse the qs.
// The CID is not in the query string but
// everything else is.
function getTheFlashVars()
{
	var qs = '' + window.location;
	var left = qs.indexOf('?') + 1;
	var fv = qs.substring(left);
	var cid = ___cid;
	if (cid != '') {
		cid = 'cid=' + cid;
	}
	fv = addQSVar(fv, cid);
	return fv;
}

function insertSWFObject() {
	return insertSWFObjectWithFlashvars(getTheFlashVars(), '950', '620');
}

function topnavigate(url)
{
	top.location = url;
}

function insertSWFObjectWithFlashvars(fv, w, h) {
	var l_url = (document.location + '').toLowerCase();
	var l_forceFailure = l_url.indexOf('forcehtml=1') > -1;
	if (l_forceFailure) {
		return false;
	}
	var el_flashContent = document.getElementById('flashcontent');
	el_flashContent.style.display = 'none';
	//debugger;
	var so = new SWFObject('swf/lorealparis.swf', 'website', w, h, '9', '#000000');
	var l_success;

	so.addParam('menu', 'false');
	so.addParam('scale', 'noscale');
	so.addParam('quality', 'high');
	so.addParam('flashvars',fv);
	so.addParam('allowScriptAccess','always');
	//so.addParam('wmode','opaque');
	so.addParam('wmode','window');
	l_success = so.write('flashcontent');
	if (l_success) {
		el_flashContent.style.display = 'block';
	}
	return l_success;
}


///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////


function openWindowWithProps (url, target, props){
	window.open("redirect.aspx?url="+url, target, props);   
}

function openWindow (url, target){
	window.open("redirect.aspx?url="+url, target); 
}


function getURL(newWindow){
	window.open(newWindow);
}


function trace(str)
{
	if (newwindow == 0) {
		newwindow = window.open("about:blank", "", "width=400,height=500,scrollbars=yes");
		newwindow.document.write("<html><head></head><body><div id=\"debugspace\"></div></body></html>");
	}
	newwindow.document.all("debugspace").innerHTML += str;
}

function getFlashVars(wcflashvars)
{
	var fv;
	var qs = '' + window.location;
	var left = qs.indexOf("?") + 1;
	qs = qs.substring(left, qs.length);
	fv = qs + ((wcflashvars == '' && qs == '')?'':'&') + wcflashvars;
}

function printview (url, width, height, resizable){
//trace("url: "+url+" width: "+width+" height: "+height)
	if (printable == undefined || (printable && printable.closed)) {
		printable = window.open("","printable", "width="+width+",height="+height+",resizable="+resizable+",scrollbars=yes,toolbar=yes,menubar=yes,status=yes")
	}   
	printable.location.replace(url);
}

function getLocation() {
	return location.toString();
}

function getReferrer()
{
	var cookieReferrer=getCookie("referrer");
	document.cookie =  "referrer=;expires=Thu, 01-Jan-1970 00:00:01 GMT";

	if (document.referrer != "") {
		//alert("document: "+document.referrer);
		return document.referrer;
	} else {
		//alert("cookie: "+cookieReferrer);
		return cookieReferrer;
	}
}

function getElement(nm)
{
	return document.getElementById(nm);
}

function shouldDebugTracking()
{
	var qs = '' + window.location;
	return (qs.search(/showtracking=true/) != -1);
}


/* ADDED TO TEST BACKGROUND CENTERING */

function getWinSize()
{
	var myWidth = 0, myHeight = 0;
	if(typeof(window.innerWidth) == 'number') {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return {w:myWidth,h:myHeight}
}   

function adjustObjectSize(id,w,h)
{
	var winSizes = getWinSize();
	var tmpObject = document.getElementById(id);
	if(winSizes.w < w) {
		tmpObject.style.width = w + "px";
	} else {
		tmpObject.style.width = "100%";
	}
	if(winSizes.h < h) {
		tmpObject.style.height = h + "px";
	} else {
		tmpObject.style.height = (winSizes.h)-2 + "px";
	}
}

function doRedirectForProductDetail()
{
	var urlstr = '' + document.location;
	var index = urlstr.search("productdetail");
	if (index <= -1) return; // not a productdetail page request
	productID = findProductID(urlstr);
	var new_main =  NEW_MAIN_FORMAT.replace("{0}", productID);
	var new_main2 = new_main.replace("{1}", productID);
	var mainReplaced = replaceSubString("main", new_main2, urlstr);
	var overlayReplaced = replaceSubString("overlay", NEW_OVERLAY_FORMAT, mainReplaced);
	document.location = overlayReplaced;
}

function findProductID(urlstr)
{
	var productIDstart = urlstr.indexOf(PRODUCTID_LOCATOR, 0) + PRODUCTID_LOCATOR.length;
	var productIDend = urlstr.indexOf(PRODUCTID_END_LOCATOR, productIDstart);
	var productID = urlstr.substring(productIDstart, productIDend);

	return productID;
}

function replaceSubString( searchString,  newStr, origUrl)
{
	var replacedUrl;
	var Start = origUrl.indexOf(searchString, 0);
	if (Start <= -1 && searchString == "main") {
		replacedUrl = origUrl.substr(0, origUrl.length - 1) + "|" + newStr + "}";
		return replacedUrl;
	}
	var End = origUrl.indexOf("|", Start);
	if (End <= -1) {
		End = origUrl.indexOf("}", Start);
	}
	var oldStr = origUrl.substring(Start, End);
	replacedUrl = origUrl.replace(oldStr, newStr);
	return replacedUrl;
}



function GetHash(p_url)
{
	l_urlSegments = p_url.split('#');
	if (l_urlSegments.length > 1) {
		return l_urlSegments[l_urlSegments.length - 1];
	}
	return '';
}

function GetQueryString(p_url)
{
	l_urlSegments = p_url.split('?');
	if (l_urlSegments.length > 1) {
		return l_urlSegments[l_urlSegments.length - 1];
	}
	return '';
}


function ForceHTMLView() {
	var sURL = window.document.location.toString();
	sURL = sURL.replace('#/#/?', '#/?');
	sURL = sURL.replace('/#/?', '#/?');
	var hash = GetHash(sURL);
	var isSEORedirected = sURL.toLowerCase().indexOf('html=1') > -1
	if (hash + '' != '') {
		hash = hash.replace('/?', '?');
		hash = hash.replace('?', '&');
		sURL = 'default.aspx?html=1' + hash
		window.location = sURL;
	} else if (!isSEORedirected) {
		var l_qs = GetQueryString(sURL);
		if (l_qs + '' != '') {
			l_qs = '&' + l_qs;
		}
		window.location = 'default.aspx?html=1' + l_qs;
	}
}


/* 
for scroll operations:
added by Lyndon
*/

function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
