//detect IE 6 or less
var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);
var isIE = itsAllGood && (itsAllGood < 7);
var ready = false;
var listHeight;
//detect IE Mac and suggest different browser
var isMac = (navigator.userAgent.indexOf("Mac") != -1);
if((navigator.appName.indexOf("Microsoft") != -1) && isMac){alert("The web browser you are using, Microsoft Internet Explorer Mac, is no longer supported by Microsoft. They recommend that you use Safari, which is already on your computer, or that you use Firefox. If you continue to use Internet Explorer Mac, you will experience technical difficulties on this website, among others.");}

/****** $ shortcut from Prototype JS Library *******/

function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string') {
			if (document.getElementById) {
				element = document.getElementById(element);
			} else if (document.all) {
			element = document.all[element];
			}
		}
		if (arguments.length == 1) {
			return element;
		}
		elements.push(element);
	}
	return elements;
}

//
/******cookie functions******/
//
var theImage, seenIt, chosenImage, imageLibrary, numberOfImages, newNumberOfImages;

/*****************
Let's Bake Some Cookies!
*****************/

// Cookie Functions

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	var ck = name+"="+value+expires+"; path=/";
	document.cookie = ck;
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i<ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}
//

/*************
Fix inline PNGs

Based on (but somewhat modified) SLEIGHT.JS CODE
by YoungPup

**************/

function fnLoadPngs() {
        for (var i = document.images.length - 1, img = null; (img = document.images[i]); i--) {
                if (img.src.match(/\.png$/i) != null) {
                        var src = img.src;
                        img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')"
                        img.src = src.slice(0,src.indexOf(".png"))+"_clear.gif";
                }
        }
}

/***End inline PNG fix***/

/*****************/

var winResize = function(){
	//Get Values
	winW = (isIE) ? document.documentElement.clientWidth : window.innerWidth;
	winH = (isIE) ? document.documentElement.clientHeight : window.innerHeight;
	winH = (winH<540) ? 540 : winH;
	//
	//Home Behaviour
	//
	if (document.body.id == "home"){
		if ($("bluebg")){
			$("bluebg").style.height = (winH-128)+"px";
			$("bluebg").style.width = (winW-8)+"px";
		}
		//
		var fooditems = Math.round((winW-4)/(121+4));
		var foodboxes = Math.floor(fooditems/2);
		var gapW = (winW-4)-(fooditems*(121+4));
		var boxW = Math.ceil(gapW/foodboxes)+121+"px";
		for (i=1;i<=foodboxes;i++){
			$("box"+i).style.width = boxW;
		}
	}
	//
	//Sec Behaviour
	//
	 else if (document.body.id == "sec"){
		//
		var fooditems = Math.round((winH-4)/(120+4));
		var foodboxes = Math.floor(fooditems/2);
		var gapH = (winH-4)-(fooditems*(120+4));
		var boxH = Math.ceil(gapH/foodboxes)+120+"px";
		for (i=1;i<=foodboxes;i++){
			$("box"+i).style.height = boxH;
		}
	}  else if (document.body.id == "products"){
		if ($("bluebg")){
			$("bluebg").style.height = (winH-194-4)+"px";
			$("bluebg").style.width = (winW-4-4)+"px";
		}
		$("bodytext").style.height = (winH-194-4)+"px";
	}
	/* else if (document.body.id == "products"){
		var bodyH = $("products").clientHeight;
		if (listHeight > (bodyH-194-18)){
			bodyH = listHeight+194+18;
		}
		if (winH > $("products").clientHeight){
			if ($("bluebg")){
				$("bluebg").style.height = (winH-194-4)+"px";
				$("bluebg").style.width = (winW-4-4)+"px";
				
			}
			$("bodytext").style.height = (winH-194-4)+"px";
		} else {
			
			if ($("bluebg")){
				$("bluebg").style.height = (bodyH-194-4)+"px";
				$("bluebg").style.width = (winW-4-4)+"px";
			}
			$("bodytext").style.height = (bodyH-194-4)+"px";	
			$("products").style.height = (bodyH)+"px";
		}
	}*/
}

window.onresize = winResize;

/********** create array of random images ***********/

// Image Series Setup

var imageLibrary = new Array("1","2","3","4","5","6","7","8","9","10","11","12","13","14");

var seenIt = readCookie("hollimeximgs");

//
if (!seenIt){
	seenIt = "0t,";
} else if (seenIt.indexOf(imageLibrary.length+"t") != -1){
	seenIt = "0t,";
}
//
var pickAnImage = function(){
	if (seenIt.indexOf(imageLibrary.length+"t") != -1){
		seenIt = "0t,";
	}
	var chosenImage = Math.round(Math.random()*(imageLibrary.length-1));
	while (seenIt.indexOf(","+chosenImage+",") != -1){
		chosenImage = Math.round(Math.random()*(imageLibrary.length-1));
	}
	seenIt += chosenImage+",";
	numberOfImages = parseInt(seenIt.slice(0,seenIt.indexOf("t")));
	newNumberOfImages = numberOfImages+1;
	seenIt = newNumberOfImages + seenIt.slice(seenIt.indexOf("t"));
	var theImgUrl = imageLibrary[chosenImage];
	return theImgUrl;
}

//

var init = function(){
	
	var totalimgsH = Math.ceil(Math.floor(screen.width/(121+4))/2);
	var totalimgsV = Math.ceil(Math.floor(screen.height/(120+4))/2);
	
	if (document.body.id == "sec") {
		var totalImgs = totalimgsV;
	} else if (document.body.id == "home") {
		var totalImgs = totalimgsH;
	} else {
		var totalImgs = null;
	}
	
	//
	
	if (totalImgs) {
		for (i=1;i<=totalImgs;i++){
			var curRandImg = pickAnImage()
			var foodimgt = document.createElement("img");
			foodimgt.id = "food"+i;
			foodimgt.src = "images/food/food"+curRandImg+".jpg";
			$("foodbar").getElementsByTagName("div")[0].getElementsByTagName("span")[0].appendChild(foodimgt);
			var boximgt = document.createElement("img");
			boximgt.id = "box"+i;
			boximgt.src = "images/food/box"+curRandImg+".gif";
			$("foodbar").getElementsByTagName("div")[0].getElementsByTagName("span")[0].appendChild(boximgt);
		}
	}
	
	createCookie("hollimeximgs",seenIt,300);
	//
	var bluebgimg = document.createElement("img");
	bluebgimg.id = "bluebg";
	bluebgimg.src = "images/blue_gradient.jpg";
	if (document.body.id == "home") {
		document.body.appendChild(bluebgimg);
	} else if (document.body.id == "sec"){
		var bluebgimg2 = document.createElement("img");
		bluebgimg2.id = "bluebg2";
		bluebgimg2.src = "images/blue_gradient.jpg";
		document.body.appendChild(bluebgimg2);
		$("content").appendChild(bluebgimg);
	} else if (document.body.id == "products"){
		//listHeight = $("bodytext").getElementsByTagName("ul")[0].clientHeight;
		var middlecolimg = document.createElement("img");
		middlecolimg.id = "bluebg2";
		middlecolimg.src = "images/blue_gradient-flip.jpg";
		document.body.appendChild(bluebgimg);
		$("middlecolbg").appendChild(middlecolimg);
		$("middlecolbg").style.visibility = "visible";
		for (i=0;i<$("bodytext").getElementsByTagName("ul")[0].childNodes.length;i++){
			$("bodytext").getElementsByTagName("ul")[0].childNodes[i].id = i;
		}
		var duplicateList =  $("bodytext").getElementsByTagName("ul")[0].cloneNode(true);
			$("middlecolbg").appendChild(duplicateList);
		}
	/**/
	
	/**/
	winResize();
	ready = true;
}


/*****************
Element Behaviours
******************/
var currentMenu1 = false;
var currentMenu2 = false;
var openMenu = function(menuItem,menuLevel){
	var level1 = (menuLevel == 1) ? true : false;
	if((currentMenu1 && level1) || (currentMenu2)){
		if (menuLevel == 1){
			var deadLiA = currentMenu1.parentNode.getElementsByTagName("a")[0];
		} else {
			var deadLiA = currentMenu2.parentNode.getElementsByTagName("a")[0];
		}
		var deadUl = deadLiA.parentNode.getElementsByTagName("ul")[0];
		if (level1){
			currentItem.style.display = "none";
		}
		if (!isIE){
			if (level1){
				deadLiA.style.backgroundImage = "url(images/products_list_bg-up.png)";
			} else {
				//deadLiA.style.backgroundImage = "";	
			}
		} else {
			if (level1){
				deadLiA.style.backgroundImage = "url(images/products_list_bg-up.gif)";
			} else {
				//deadLiA.style.backgroundImage = "";	
			}	
		}
		deadLiA.style.color = "#008DD5";
	}
	var listItem = parseInt(menuItem.parentNode.id);
	var curUl = $("middlecolbg").getElementsByTagName("ul")[0].childNodes[listItem].getElementsByTagName("ul")[0];
	if (level1){
		curUl.style.display = "block";
		menuItem.style.backgroundImage = "url(images/products_list_bg-over.gif)";
		currentMenu1 = menuItem;
		currentItem = curUl;
		menuItem.style.color = "#262626";
	} else {
		///menuItem.style.backgroundImage = "url(images/products_list-level2_bg-over.gif)";
		//menuItem.style.backgroundRepeat = "repeat-x";
		currentMenu2 = menuItem;
	}
	
}
var menuRoll = function(menuItem,menuLevel){
	var listLength = menuItem.parentNode.parentNode.childNodes.length;
	/*for (i=0;i<listLength;i++){
		var deadLiA = menuItem.parentNode.parentNode.childNodes[i].getElementsByTagName("a")[0];
		if (deadLiA.style.color != "#008DD5"){
			if (menuLevel == 1){
				if (currentMenu1 != deadLiA){
					deadLiA.style.color = "#008DD5";
				}
			} else {
				if (currentMenu2 != deadLiA){
					deadLiA.style.color = "#008DD5";
				}
			}
		}
	}*/
	if (menuLevel == 1){
		menuItem.style.color = "#262626";
	}
}


/*************/
var myrules = {
	'#bodytext ul li a' : function(el){
		el.onclick = function(){
			openMenu(this,1);
		}
		el.onfocus = function(){
			this.blur();
		}
		el.onmouseover = function(){
			menuRoll(this,1);
		}
		el.onmouseout = function(){
			if (currentMenu1 != this){
				this.style.color = "#008DD5";
			}
		}
	},
	'#bodytext ul li ul li a' : function(el){
		el.onclick = function(){
			openMenu(this,2);
		}
		el.onfocus = function(){
			this.blur();
		}
		el.onmouseover = function(){
			menuRoll(this,2);
		}
		el.onmouseout = function(){
			if (currentMenu2 != this){
				this.style.color = "#008DD5";
			}
		}
	}
};
if(Behaviour){
	Behaviour.register(myrules);
}
/*************/

if(isIE){Behaviour.addLoadEvent(fnLoadPngs);}
Behaviour.addLoadEvent(init);
Behaviour.addLoadEvent(winResize);