$(window).resize(function(){
	//setBackground();
});

function setHeight(unique_class,height){
	height += 10;
	$("#"+unique_class).height(height)
}

function rgb2hex(rgb) {

	//generates the hex-digits for a colour.
	function hex(x) {
		hexDigits = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8","9", "A", "B", "C", "D", "E", "F");
		return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x% 16];
	}
	return "#" + hex(rgb[0]) + hex(rgb[1]) + hex(rgb[2]);
} 

function fixRBG(rgb){
	
	rgb = rgb.replace("rgb(","");
	rgb = rgb.replace(")","");
	arr = rgb.split(",");

	return rgb2hex(arr);
}

function showSearch(){

	$("#search_div").show();
	setHeaders("#search_div h2");

	topPadding = 78;

	windowHeight = $(window).height();
	searchHeight = $("#search_div").height();

	windowHeight -= topPadding;
	searchHeight -= topPadding;

	if (windowHeight > searchHeight){
		$("#search_div").height(windowHeight);
	}

	$("#q").focus();
}

allowHide = true;
function hideSearch(){
	if (allowHide){
		$("#search_div").hide();
	}
	allowHide = true;	
}
function blockHide(){
	allowHide = false;
}




$(document).ready(function(){

	setBackground();		
	
	swf_text_i = 0;
	
	setHeaders("h2,h3, a.current");


	slide_shows = new Array();
	slide_delay = 10;
	slide_speed = 1000;
	slide_time = 4000;
	slide_time_slow = 10000;
	
	i = 0;
	$(".el_image_list").each(function(){

		slide_show = new Object();
		slide_show.target = this;
		slide_show.my_id = this.id;
		slide_show.current = 0;
		slide_show.num_slides = numChildren(this);
		if (slide_show.num_slides > 1){
			slide_show.timer = window.setTimeout('start_interVal('+i+')', slide_delay);			
		}else{
			$("#"+this.id+"_nav").hide();
		}
		slide_shows.push(slide_show);
		i++;	

	});

});	

function setHeaders(selector){

	$("#content_holder "+selector).each(function(){
		
		unique_class = "flash_text_"+swf_text_i;
		this.id = unique_class;
		htmlWidth = $(this).width();
		htmlHeight = $(this).height();
		myText = $(this).html();

		if (myText != ""){
			var flashvars = {};
			flashvars.txt = escape(myText);
			flashvars.unique_class = unique_class;
			flashvars.size = 34;				
			flashvars.twidth = htmlWidth;

			myColor = $(this).css("color");

			if (myColor.length != 7){
				myColor = fixRBG(myColor);
			}

			if(myColor != "#000000"){
				flashvars.color = "0x"+myColor.replace("#","");
			}else{
				flashvars.color = 0x222222;
			}

			mySize = $(this).css("font-size");
			if(mySize != "21px"){
				flashvars.size = mySize.replace("px","");
			}else{
				flashvars.size = 34;
			}

			if($(this).hasClass("current")){
				flashvars.align = "right";
				flashvars.txt = myText;
			}

			var params = {};
			params.menu = "false";
			params.wmode = "transparent";
			params.scale = "noscale";
			params.salign = "lt";
			
			swfobject.embedSWF("swf/caption_cs4.swf", unique_class, htmlWidth, htmlHeight, "9.0.0","", flashvars, params);			
		}
		swf_text_i++;
	});	
}

function alert_r(myObj){
	str = ""
	for ( var attr in myObj ) {
		str += attr + " = " + myObj[attr] + "\n";
	}
	alert(str);
}

function setBackground(){

		var flashvars = {};
		flashvars.image = $("#bg").attr("title");
		var params = {};
			params.menu = "false";
			params.scale = "exactfit";
			params.wmode = "transparent";

		
		var rand = Math.random();
		//swfobject.embedSWF("swf/bg.swf?r="+rand, "bg", "100%", "100%", "9.0.0","", flashvars, params);

		str = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="100%" height="100%" id="swf/bg_test" align="middle">';
		str += '<param name="allowScriptAccess" value="sameDomain" />';
		str += '<param name="allowFullScreen" value="false" />';
		str += '<param name="movie" value="swf/bg.swf?image='+flashvars.image+'&r='+rand+'" /><param name="scale" value="exactfit" /><param name="wmode" value="transparent" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" />	<embed src="swf/bg.swf?image='+flashvars.image+'&r='+rand+'" quality="high" bgcolor="#ffffff" wmode="transparent" scale="exactfit" width="100%" height="100%" name="swf/bg_test" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" />';
		str += '</object>';
		$("#bg").html(str);

}


/*------------------------------------*/
/*  slideshow	*/
/*------------------------------------*/

	function numChildren(node){
		if (typeof node != 'undefined') {
		  if (typeof node.children == 'undefined') {
			var num = 0;
			for(elem in node.childNodes){
				if(node.childNodes[elem].nodeType == 1){
					num ++;
				};
			}
			return num;
		  }else{					
			return node.children.length;
		  }
		} 
	}


	function start_interVal(num){				

		SlideshowNext(num);
		slide_shows[num].timer = window.setInterval('SlideshowNext('+num+')', slide_time);
	}

	slideTimes = 0; 
	lastSlideFunction = "";
	function SlideshowNext(show_num){

		if (lastSlideFunction == "user"){
			clearTimeout(slide_shows[show_num].timer);
			slide_shows[show_num].timer = window.setInterval('SlideshowNext('+show_num+')', slide_time);
		}
		
		showInfo = slide_shows[show_num];

		if (showInfo.current >= showInfo.num_slides){
			showInfo.current = 0;
		}	

		tar_id = showInfo.target.id;
				
		
		$("#"+tar_id+" > img").css("z-index",1);				
		$("#"+tar_id+" :eq("+(showInfo.current-1)+")").css("z-index",14);
		if (slideTimes != 0){
			$("#"+tar_id+" :eq("+showInfo.current+")").hide();
		}
		$("#"+tar_id+" :eq("+showInfo.current+")").css("z-index",20);
		$("#"+tar_id+" :eq("+showInfo.current+")").fadeIn(slide_speed);


		$(".image_num").css("background-color","#999999");
		$("#image_num_"+showInfo.current).css("background-color","#000000");			

		showInfo.current ++;
		slideTimes ++;
		lastSlideFunction = "auto";
	}

	function slideShowNum(show_num,num){
		
		clearTimeout(slide_shows[show_num].timer);
		slide_shows[show_num].timer = window.setInterval('SlideshowNext('+show_num+')', slide_time_slow);
	
		showInfo = slide_shows[show_num];

		lastCurrent = showInfo.current-1;
		showInfo.current = num;

		tar_id = showInfo.target.id;
		
		$("#"+tar_id+" > img").css("z-index",1);				

		$("#"+tar_id+" :eq("+lastCurrent+")").css("z-index",50);
		$("#"+tar_id+" :eq("+showInfo.current+")").hide();
		$("#"+tar_id+" :eq("+showInfo.current+")").css("z-index",100);
		$("#"+tar_id+" :eq("+showInfo.current+")").fadeIn(slide_speed);


		$(".image_num").css("background-color","#999999");
		$("#image_num_"+showInfo.current).css("background-color","#000000");

		showInfo.current ++;
		lastSlideFunction = "user";

	}

 
/*------------------------------------*/
/*  cookies	*/
/*------------------------------------*/


	function setCookie(name,data)
	{
		var date = new Date();
		date.setTime(date.getTime()+(24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
		document.cookie = name+"="+data+expires+"; path=/";
	}

	function getCookie(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 false;
	}
