var maxIndex = 9;

var backgroundInfos = new Array('LEMAYMICHAUD, QUÉBEC, QUÉBEC, CANADA',
								'LEMAYMICHAUD, QUÉBEC, QUÉBEC, CANADA',
								'LEMAYMICHAUD, QUÉBEC, QUÉBEC, CANADA',
								'LEMAYMICHAUD, QUÉBEC, QUÉBEC, CANADA',
								'LEMAYMICHAUD, MONTRÉAL, QUÉBEC, CANADA',
								'LEMAYMICHAUD, MONTRÉAL, QUÉBEC, CANADA',
								'LEMAYMICHAUD, MONTRÉAL, QUÉBEC, CANADA',
								'LEMAYMICHAUD, MONTRÉAL, QUÉBEC, CANADA',
								'LEMAYMICHAUD, MONTRÉAL, QUÉBEC, CANADA');

checkVersion();




/**
 * Check the version and redirect if it's IE7, IE6 or IE5.5
 */
function checkVersion() {
	var browser = navigator.appName;
	var ver = navigator.appVersion;
	var thestart = parseFloat(ver.indexOf("MSIE"))+1;
	var brow_ver = parseFloat(ver.substring(thestart+4,thestart+7));

	if (navigator.appName){
		var ver = navigator.appVersion;
		var thestart = parseFloat(ver.indexOf("MSIE"))+1;
		var brow_ver = parseFloat(ver.substring(thestart+4,thestart+7));
		
		if (brow_ver < 8) {
			window.location="redirect.html";
		}
	} 
}

/**
 * Random the background
 */
function random(level) {
	var index = Math.ceil(Math.random()*maxIndex);
	changeBackGround(index, level);
}

/**
 * Change the background with the previous image
 * 
 * @param level the actual level
 */
function previous(level) {
	changeBackGround(getPreviousIndex(Get_Cookie('index')), level);
}

/**
 * Get the previous index
 * 
 * @param index the actual index
 * @returns the previous index
 */
function getPreviousIndex(index) {
	if(index > 1) {
		return (index*1) - 1;
	}

	return maxIndex;
}

/**
 * Change the background with the next image
 * 
 * @param level the actual level
 */
function next(level) {
	changeBackGround(getNextIndex(Get_Cookie('index')), level);
}

/**
 * Get the next index
 * 
 * @param index the actual index
 * @returns the next index
 */
function getNextIndex(index) {
	if(index < maxIndex) {
		return (index*1) + 1;
	}
	
	return 1;
}

/**
 * Set the cookie
 * 
 * @param name the cookie name
 * @param value the cookie value
 */
function setCook(name, value) {
	document.cookie = name + "=" + escape(value);
}
	
/**
 * Get the cookie
 * 
 * @param name the cookie name
 * @returns the cookie value
 */
function getCook(name) {
	var deb = document.cookie.indexOf(name + "=");
        
	if (deb >= 0) {
		deb += name.length + 1;
		fin = document.cookie.indexOf(";",deb);
        
		if (fin < 0) fin = document.cookie.length;
		return unescape(document.cookie.substring(deb,fin));
	}
	
	return "";
}

/**
 * Change the background
 * 
 * @param index the index of the background to show
 * @param level the directory level 
 */
function changeBackGround(index, level) {
	Set_Cookie('index',index, '', '/', '', '');
	var prefix = getBaseUrl(level);

	document.getElementById("background").src = prefix + "images/background/BKG_" + index + ".jpg";

	new Image().src = prefix + "images/background/BKG_" + getNextIndex(index) + ".jpg";
	new Image().src = prefix + "images/background/BKG_" + getPreviousIndex(index) + ".jpg";
	
	var greyBar = document.getElementById('greyBar');
	if (greyBar) {
		greyBar.innerHTML = '<span>' + backgroundInfos[index-1] + '</span>'
	}
}

/**
 * Get the base url base of the directory level
 * 
 * @param level the directory level 
 * @returns "../" * level
 */
function getBaseUrl(level) {
	var prefix='';
	if (level) {
		for (var i=0;i<level;i++) {
			prefix+='../';
		}
	}	
	return prefix;
}

/**
 * Apply the background
 * 
 * @param level the level of the background to display
 */
function applyBackGround(level) {
	var index = Get_Cookie('index');
	
	if (index != null && index != 'null') {	
		changeBackGround(index, level);
	} else {
		random(level);
	}
}

/**
 * This fixes an issue with the old method, ambiguous values with this test document.cookie.indexOf( name + "=" );
 * 
 * @param check_name the cookie name
 * @returns the cookie value
 */
function Get_Cookie(check_name) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for (i = 0; i < a_all_cookies.length; i++) {
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );

		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if (cookie_name == check_name) {
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if (a_temp_cookie.length > 1) {
				cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}

	if (!b_cookie_found) {
		return null;
	}
}

/**
 * Set the cookie
 * 
 * @param name cookie name
 * @param value cookie value
 * @param expires cookie expire value
 * @param path cookie path
 * @param domain cookie domain
 * @param secure cookie secure value
 */
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" : "");
}

/**
 * Build the footer. Replace the div (id=footer)
 * 
 * @param lang the language of the footer 
 * @param destination the href to switch to
 */
function buildFooter(lang, destination, level) {
	
	var footerDiv = document.getElementById('footer');
	if (footerDiv) {	
		var footer = '<div id="footer">';
		footer += '<p id="copyright">';

		var prefix = getBaseUrl(level);
		
		if (lang=='fr') {
			footer +='TOUS DROITS RÉSERVÉS © LEMAYMICHAUD 2010';
			footer +='</p>';
			footer +='<a id="linkJob_fr" target="_blank" href="' + prefix + 'emplois/emplois.php"><span class="linkTxtPos" style="cursor:pointer;">EMPLOIS</span></a>';
			footer +='<a id="linkContact_fr" href="javascript:showContact();"><span class="linkTxtPos" style="cursor:pointer;">COORDONNÉES</span></a>';
			/*footer +='<a id="linkFrDisabled"><span class="">FRANÇAIS</span></a>';
			footer +='<a id="linkEn" href="javascript:changeLang(\'' + destination + '\');"><span class="" style="cursor:pointer;">EN</span></a>';*/
		} else {
			footer +='COPYRIGHTS © LEMAYMICHAUD 2010';
			footer +='</p>';
			footer +='<a id="linkJob_en" target="_blank" href="' + prefix + 'jobs/jobs.php"><span class="linkTxtPos" style="cursor:pointer;">JOBS</span></a>';
			footer +='<a id="linkContact_en" href="javascript:showContact();"><span class="linkTxtPos" style="cursor:pointer;">CONTACT</span></a>';
			/*footer +='<a id="linkFr" href="javascript:changeLang(\'' + destination + '\');"><span class="linkTxtPos" style="cursor:pointer;">FRANÇAIS</span></a>';
			footer +='<a id="linkEnDisabled"><span class="linkTxtPos" >ENGLISH</span></a>';*/
		}	
	
		footer += '</div>';

		
		
		footerDiv.innerHTML=footer;
	}
	
	
}

/**
 * Change the language of the site
 * 
 * @param url the url to redirect the site
 */
function changeLang(url) {
	if (lastBox != null) {
		url+='?box=' + lastBox;
	}
	window.location = url;
}


