var strDomain = window.location.hostname.replace("www.", "").replace("fin.", "");
document.pre = document.FIN ? "http://" + strDomain : "";	
document.strDomain = strDomain;

Window.addEvent('domready',writeRouterFlash);
//Window.addEvent('domready',ieFirstChildren);

/* 
	look for list items that are the first child, and give 'em a class 
	This is for ie6's benefit, 'cos it doesn't understand 'first-child' in css
*/
function ieFirstChildren()
{
	var kids = new Array();
	
	var breadcrumb 	= document.getElementById('breadcrumb');
	var kid 		= breadcrumb.firstChild.nextSibling; // there's text before this list item
	kids.push(kid);
	
	var misc	 	= document.getElementById('misc');
	var kid 		= misc.firstChild.nextSibling;
	kids.push(kid);
	
	var credits	 	= document.getElementById('credits');
	var kid 		= credits.firstChild.nextSibling;
	kids.push(kid);
	
	for(var i=0; i<kids.length; i++)
	{
		kids[i].className 	= 'firstchild';
	}
}


/* flash stuff */

function writeRouterFlash() 
{
	setTimeout("writeFlashContent()", 500);
}

function writeFlashContent()
{
	if(!document.FIN)
	{
		var strDomain = window.location.hostname.replace("www.", "").replace("fin.", "");	
		document.pre = document.FIN ? "http://" + strDomain : "";
		//var xmlPath = "http://" + strDomain + "/z_includes/inc_indexlessrouter.php"; // this should work!
		var xmlPath = "http://fin." + strDomain + "/modules/pagemaker/extra/out_structure.php?ignorefirst=1";  
		var foR = new SWFObject(document.pre+"/z_swf/cjg_router.swf", "flashb", "825", "65", "8.0", "", true);
		 
		foR.addVariable("xmlPath", xmlPath);
		foR.addVariable("navCurr", document.navCurr);
		foR.addVariable("subCurr", document.subCurr);
		foR.addParam("wmode", "transparent");
		foR.write("flashrouter");
	}
}

function getThisURL(url)
{
	document.location.href(url);
}


function writeFlashFace()
{
	var strDomain = window.location.hostname.replace("www.", "").replace("fin.", "");	
	document.pre = document.FIN ? "http://" + strDomain : "";
	var arrSWFs = new Array("cjg_john.swf", "cjg_johnk.swf");
	var foF = new SWFObject("/z_swf/cjg_loader.swf", "flashtest", "510", "435", "8.0", "", true);
	
	foF.addVariable("swfPath", document.pre+"/z_swf/"+arrSWFs[Math.floor(Math.random()*2)]);
	foF.addParam("wmode", "opaque");
	
	if(getCookie('FlashCookie') != "false")
	{
		foF.write("flashface");
	}

}

function writeFlashFaceNarrow()
{
	var strDomain = window.location.hostname.replace("www.", "").replace("fin.", "");	
	document.pre = document.FIN ? "http://" + strDomain : "";
	var arrSWFs = new Array("cjg_john.swf", "cjg_johnk.swf");
	var foF = new SWFObject("/z_swf/cjg_loader.swf", "flashtest", "550", "435", "8.0", "", true);
	
	foF.addVariable("swfPath", document.pre+"/z_swf/"+arrSWFs[Math.floor(Math.random()*2)]);
	foF.addParam("wmode", "opaque");
	
	if(getCookie('FlashCookie') != "false")
	{
		foF.write("flashface");
	}
	else 
	{
		$('flashface').setHTML('<img src="/z_images/content/rightimages/rightimages_1.jpg" alt="Garlands Employee"/>');		
	}
}

function writeFlashFaceHome()
{
	var strDomain = window.location.hostname.replace("www.", "").replace("fin.", "");	
  	document.pre = document.FIN ? "http://" + strDomain : "";
	var foF = new SWFObject(document.pre+"/z_swf/cjg_loader.swf", "flashtest", "550", "435", "8.0", "", true);
	foF.addVariable("swfPath", document.pre+"/z_swf/cjg_janine.swf");
	foF.addParam("wmode", "opaque");
	
	if(getCookie('FlashCookie') != "false")
	{
		foF.write("flashface");
	}
	else
	{
		$('flashface').setHTML('<img src="/z_images/content/rightimages/rightimages_3.jpg" alt="Garlands Employee"/>');		
	}
	
}

function writeHomeAni()
{
	var strDomain = window.location.hostname.replace("www.", "").replace("fin.", "");	
  	document.pre = document.FIN ? "http://" + strDomain : "";
	var foF = new SWFObject(document.pre+"/z_swf/cjg_loader.swf", "flashintro", "550", "300", "8.0", "", true);
	
	foF.addVariable("swfPath", document.pre+"/z_swf/cjg_homeanimation_09.swf");
	
	if(getCookie('FlashCookie') != "false")
	{
		foF.write("flashintro");
	}
	else 
	{
		$('flashintro').setHTML('<img src="/z_images/content/non-flash/home_animation.jpg" alt="Now recruiting"/><a href=\"/careers/current_vacancies.php\" id=\"home_applynow\">Now Recruiting</a><br/><a href=\"/home/changing_attitudes.php\" id=\"home_learnmore\">Learn More</a>');	
	}
}


function setActiveStyleSheet(name) 
{
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel") && a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == name) a.disabled = false;
    }
  }
}

function changeStyleSheet(name,value)
{
	setProfileCookie(name,value);
	setActiveStyleSheet(value);
}


function setProfileCookie(name,value) 
{
  	var expiration_date = new Date("January 1, 2010");
 	expiration_date = expiration_date.toGMTString();
	setCookie(name, value, expiration_date);
}

function setCookie(name, value, expires)
{
	 document.cookie= name + "=" + escape(value) +
     ((expires) ? "; expires=" + expires : "") + "; path=/" ;
}

function openURL(url, name, width, height, scrollbars)
{
	var str 	= "height=" + height + ",innerHeight=" + height + ",width=" + width + ",innerWidth=" + width + ",statusbar=no,scrollbars=" + (scrollbars?"yes":"no");
	var xpos	= (screen.availWidth-width)/2;
	var ypos	= (screen.availHeight-height)/2;
	newWindow 	= window.open(url,"pic",str);
	newWindow.focus();
}

if(window.ie && !document.FIN)
{
	window.addEvent('load',insertBullets);
}

function insertBullets()
{
	if(document.title == "Garlands - Home")
	{
		var insert		= new Element('span');
		insert.addClass('ieFixImg');
		var listItems = $$("#latest li");
		for(var i = 0; i < listItems.length; i++)
			if(i > 0)
			{
				var tmp = insert.clone();
				tmp.injectBefore(listItems[i]);
			}
				
	}
}


/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}
