var strOriginalColor;
var intFontSize;
var blnHiCon;

function initialise() {

	// show share link
	if ( document.getElementById( 'social' ) ) {
		document.getElementById( 'social' ).style.display		= 'block';
	}
	document.getElementById( 'font_size' ).style.display	= 'block';
	document.getElementById( 'view_options' ).style.display = 'block';

	retrieveFontSize();

}

/* http://www.w3schools.com/js/js_cookies.asp */
function setCookie() {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+999);
	document.cookie= "intFontSize=" +escape(intFontSize)+ ";path=/;expires="+exdate.toGMTString();
	document.cookie= "blnHiCon=" + blnHiCon + ";=" +escape(intFontSize)+ ";path=/;expires="+exdate.toGMTString();
}

function getCookie( strVariable ){

	if(document.cookie.length>0){
		c_start=document.cookie.indexOf( strVariable + "=");
		if (c_start!=-1){
			c_start=c_start + strVariable.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 false;
}

function resizeText( objLink, intFontSizeSelected ) {

	intFontSize = intFontSizeSelected;
	document.body.style.fontSize = intFontSize + "em";

	// select correct image
	document.getElementById( 'textNormal' ).className		= (document.getElementById( 'textNormal' ) == objLink ? 'selected' : '' );
	document.getElementById( 'textLarge' ).className		= (document.getElementById( 'textLarge' ) == objLink ? 'selected' : '' );
	document.getElementById( 'textExtraLarge' ).className	= (document.getElementById( 'textExtraLarge' ) == objLink ? 'selected' : '' );

	setCookie();
    Cufon.replace('h1');
    Cufon.replace('h2');

	return false;
}

function retrieveFontSize() {

	intFontSizeRetrieved = getCookie( 'intFontSize' );
	intFontSize = ( intFontSizeRetrieved ? intFontSizeRetrieved : '0.8' );

	if( intFontSize ){
		switch ( intFontSize ) {
			case '0.8':
				var strIdTextImage = 'textNormal';
				break;
			case '1':
				var strIdTextImage = 'textLarge';
				break;
			case '1.2':
				var strIdTextImage = 'textExtraLarge';
				break;
		}

		resizeText( document.getElementById( strIdTextImage ), intFontSize );
	}
}

function retrieveContrast() {
	blnHiCon = getCookie( 'blnHiCon' );
	toggleHighContrast();
}

/**
 * toggles high contrast stylesheet
 */
function toggleHighContrast() {

	// ensure font color changes without load so is ready for Cufon
	objBodyStyle = document.getElementsByTagName('body')[0].style;

	objSheet = getHiConStyleSheet();

	if ( !blnHiCon ) {

		if ( objSheet ) {
			objSheet.disabled = false;
		} else {
			loadStylesheet( 'hicon' );
		}

		strLogoSearch	= 'dark';
		strLogoReplace	= 'light';
		strCaptionLink  = 'Standard View';

		strOriginalColor = objBodyStyle.color;
		objBodyStyle.color = '#FF0';

	} else {
		objSheet.disabled = true;
		strLogoSearch	= 'light';
		strLogoReplace	= blnLightLogo ? 'light' : 'dark';
		strCaptionLink  = 'High Contrast';

		objBodyStyle.color = strOriginalColor;
	}

	document.getElementById('aContrast').innerHTML = strCaptionLink;

	// replace borough img with white one
	if ( document.getElementById( 'imgBoroughLogo' ) ) {
		document.getElementById( 'imgBoroughLogo' ).src = document.getElementById( 'imgBoroughLogo' ).src.replace( strLogoSearch, strLogoReplace );
	}

	// update Cufon to replace headers in appropriate color
    Cufon.replace('h2');
    Cufon.replace('h1');

	blnHiCon = !blnHiCon;

	setCookie();

	return false;

}

function loadStylesheet( strSheetName ) {
	var strStylesheet = 'http://upgrade2010.bitedev.co.uk/css/' + strSheetName + '.css';
	
	// switch to high contrast sheet
	if(document.createStyleSheet) {
		document.createStyleSheet(strStylesheet);
	} else {
		var styles = "@import url(' " + strStylesheet + " ');";
		newSS=document.createElement('link');
		newSS.rel='stylesheet';
		newSS.href='data:text/css,'+escape(styles);
		document.getElementsByTagName("head")[0].appendChild(newSS);
	}

}

function getHiConStyleSheet() {

	for (i=0; i< document.styleSheets.length; i++) {
		if ( document.styleSheets[i].href && document.styleSheets[i].href.indexOf( 'hicon' ) > 0 ) {
			return document.styleSheets[i];
		}
	}
}