/******************************************************
       Project:  Webloz'10

       Company:  BSH
     Developer:  Martin Metodiev

    Started on:  18/01/2010
    Updated on:  20/01/2010

    Sheet type:  DEFAULT JavaScript
******************************************************/



/* .............. Random image */
	function randomNum(num){
	num1 = Number(num)
	num = Math.ceil(Math.random()*num1)
		str = "<div id=\"wrapper\" class=\"centered\" style=\"background: url(images/header/header_img_" + num + ".jpg) no-repeat center top\">"
		document.write(str)
	}
	
	function setCookie(name, value, expires, path, domain, secure) {
	  var curCookie = name + "=" + escape(value) +
		  ((expires) ? "; expires=" + expires.toGMTString() : "") +
		  ((path) ? "; path=" + path : "") +
		  ((domain) ? "; domain=" + domain : "") +
		  ((secure) ? "; secure" : "");
	  document.cookie = curCookie;
	}
	
	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));
	}
	
	function checkRandomNum(base, array){
		num = Math.ceil(Math.random()*base)
		for(var i=0; i<array.length; i++){
			if(Number(array[i])==num){
				checkRandomNum(base, array)
			}
			
		}
		return(num)
	}
	
	function randomNumber(){
		num = 8 //total number of images
		casheLimit = 8 //how many to keep in memory
		
		if(getCookie("randomCookie")){
			cookie_array = getCookie("randomCookie")
			cookie_array = cookie_array.split("|")
			if(cookie_array.length>=casheLimit){
				cookie_array.shift()
			}
			randomNum = checkRandomNum(num, cookie_array)
			
		}else{
			
			randomNum = Math.ceil(Math.random()*num)
			cookie_array = Array()
		}
		
		cookie_array[cookie_array.length]=randomNum
		//cookie_array.push(randomNum)
		cookiestring=""
		
		for(var i=0; i<cookie_array.length; i++){
			cookiestring +=cookie_array[i]
			if(i<cookie_array.length-1){
				cookiestring +="|"
			}
		}
		
		setCookie("randomCookie", cookiestring)
		//alert(cookie_array.toString())
		str = "<div id=\"wrapper\" class=\"centered\" style=\"background: url(images/header/header_img_" + randomNum + ".jpg) no-repeat center top\">"
		document.write(str)
	}
/* ........................... */


	jQuery(document).ready(function() {

/* ............. Content width */
		function resizeFunc() {
			var mainWidth = $("div#main").width()
			var rest = mainWidth - 310;
			var contentWidth = rest * 0.96
			$("div#content").css("width", contentWidth)
		}

		/* ........................... */

		var accentWidth = $("div#content").width();
		var imgWidth = $("div.inner img.full").width();
		var imgHeight = $("div.inner img.full").height();
		var objWidth = $("div.inner object").width();
		var objHeight = $("div.inner object").height();
		var embWidth = $("div.inner embed").width();
		var embHeight = $("div.inner embed").height();

		function keepImgRatio() {
			var accentImgHeight, imgCoef;
			accentWidth = $("div#content").width();
			$("div.inner img.full").css("width", "100%")
			imgCoef = imgHeight / imgWidth;
			accentImgHeight = accentWidth * imgCoef;
			$("div.inner img.full").css("height", accentImgHeight);
		}

		function keepObjRatio() {
			var accentObjHeight, accentEmbHeight, objCoef, embCoef;
			accentWidth = $("div#content").width();
			$("div.inner object").attr("width", "100%")
			$("div.inner embed").attr("width", "100%")
			objCoef = objHeight / objWidth;
			embCoef = embHeight / embWidth;
			accentObjHeight = accentWidth * objCoef;
			
			if($.browser.msie) {
				accentEmbHeight = accentWidth * embCoef;
				$("div.inner embed").attr("height", accentEmbHeight);
			}
			else {
				accentEmbHeight = accentWidth * objCoef;
				$("div.inner object").attr("height", accentObjHeight);
				$("div.inner embed").attr("height", accentObjHeight);
			}
		}

		/* ........................... */

		resizeFunc();
		keepImgRatio();
		keepObjRatio();

		jQuery(window).resize(function() {
			resizeFunc();
			keepImgRatio();
			keepObjRatio();
		});
/* ........................... */
	

/* ................. Accordion */
		$('div#navigation div').accordion({
			autoheight: false,
			alwaysOpen: false,
			active: '.selected',
			selectedClass: 'active',
			header: ".section"
		});

		$("div#navigation h2").mouseover(function() {
			$(this).css("background-position", "0px 12px");
		})

		$("div#navigation h2").mouseout(function() {
			$(this).css("background-position", "-10px 12px");
		})
/* ........................... */

	});



/*****************************************************/