var currentAnchor = null;  

function setFlash(flash){
	var noextfile = flash[0];
	var width = flash[1];
	var height = flash[2];
	var temp = flash[0].split(".");
	var ext = "";
	if (temp.length>1){
		ext = temp[temp.length-1]
		temp = temp.splice(0,temp.length-1);
		noextfile = temp.join(".");
	}
	var value = AC_FL_RunContent2( "codebase","http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0","width",width,"height",height,"src",noextfile,"quality","high","pluginspage","http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash","movie",noextfile ); //end AC code
	$("#inside_photo").html(value);
	$("#inside_photo").css({height:(height+10)+"px"});
	$("#inside_photo").css({"background-image":"none"});
}

function setImages(image1,image2,image3){
	var displayedImages1;
	var displayedImages2;
	var displayedImages3;
	var temp;
	var reload = false;
	
	

	temp = $(".slideshowImage1");
	if (temp.length!=image1.length) reload = true;
	for (var i = 0; i < temp.length;i++){
		for (var j = 0 ; j < image1.length; j++)
			if (image1[j]==$(temp[i]).attr("src")) break;
		if (j==image1.length) reload = true;
	}
		
	temp = $(".slideshowImage2");
	if (temp.length!=image2.length) reload = true;
	for (var i = 0; i < temp.length;i++){
		for (var j = 0 ; j < image2.length; j++)
			if (image2[j]==$(temp[i]).attr("src")) break;
		if (j==image2.length) reload = true;
	}
		
	temp = $(".slideshowImage3");
	if (temp.length!=image3.length) reload = true;
	for (var i = 0; i < temp.length;i++){
		for (var j = 0 ; j < image3.length; j++)
			if (image3[j]==$(temp[i]).attr("src")) break;
		if (j==image3.length) reload = true;
	}

	if (reload){		
		var holder = document.createElement("div");

		var home_slideshow_1 = document.createElement("div");
		$(home_slideshow_1).attr("id","home_slideshow_1");
		$(home_slideshow_1).attr("style","position:relative");
		for (var i = 0 ; i < image1.length; i++){
			var img = document.createElement("img");
			$(img).addClass("slideshowImage1").attr("src",image1[i]).attr("alt","slideshow image");
			if (i!=0) $(img).attr("style","display:none");
			$(home_slideshow_1).append(img);
		}
		$(holder).append(home_slideshow_1);
		
		var home_slideshow_2 = document.createElement("div");
		$(home_slideshow_2).attr("id","home_slideshow_2");
		$(home_slideshow_2).attr("style","position:relative");
		for (var i = 0 ; i < image2.length; i++){
			var img = document.createElement("img");
			$(img).addClass("slideshowImage2").attr("src",image2[i]).attr("alt","slideshow image");
			if (i!=0) $(img).attr("style","display:none");
			$(home_slideshow_2).append(img);
		}
		$(holder).append(home_slideshow_2);
		
		var home_slideshow_3 = document.createElement("div");
		$(home_slideshow_3).attr("id","home_slideshow_3");
		$(home_slideshow_3).attr("style","position:relative");
		for (var i = 0 ; i < image3.length; i++){
			var img = document.createElement("img");
			$(img).addClass("slideshowImage3").attr("src",image3[i]).attr("alt","slideshow image");
			if (i!=0) $(img).attr("style","display:none");
			$(home_slideshow_3).append(img);
		}
		$(holder).append(home_slideshow_3);

		$("#inside_photo").html("");
		$("#inside_photo").append(holder);
		$("#inside_photo").css({height:"235px"});
		$("#inside_photo").css({"background-image":"url(images/crg_bg_insidephoto.jpg)"});
		startSlideShow();
		
	}

}

function checkAnchor(){  
    if(currentAnchor != document.location.hash){  
        currentAnchor = document.location.hash;  
        if(!!currentAnchor)  
			$("a.active").filter(".ajaxLink").removeClass("active");
			$("a[href="+currentAnchor+"]").addClass("active");
			$.get(currentAnchor.slice(1)+".html.ajax",{},function(data){
				eval("var parsedData = " + data);
				if (parsedData.text!=undefined)
					if (parsedData.text.length>0)
						$("#inside_text").html(parsedData.text[0]);
				if (parsedData.META_description!=undefined)
					if (parsedData.META_description.length>0)
						$("meta[name=description]").removeAttr("content").attr("content",parsedData.META_description[0]);
				if (parsedData.META_keywords!=undefined)
					if (parsedData.META_keywords.length>0)
						$("meta[name=keywords]").removeAttr("content").attr("content",parsedData.META_keywords[0]);
				if (parsedData.HTML_title!=undefined)
					if (parsedData.HTML_title.length>0)
						document.title = parsedData.HTML_title[0];
				if (parsedData.headerfile!=undefined)
					if (parsedData.headerfile.length>0)
					{
						if (parsedData.link!=undefined){
							if (parsedData.link.length>0)
								setPromo(parsedData.headerfile[0],parsedData.link[0],false);
						} else if (parsedData.headerlinkfile!=undefined){
							if (parsedData.headerlinkfile.length>0)
								setPromo(parsedData.headerfile[0],parsedData.headerlinkfile[0],true);
						} else setPromo(parsedData.headerfile[0],"",false);
					}
				if (parsedData.flash!=undefined){
					if (parsedData.flash.length>0)
						setFlash(parsedData.flash);
				} else if (parsedData.image1!=undefined && parsedData.image2!=undefined && parsedData.image3!=undefined){
					if (parsedData.image1.length>0 && parsedData.image2.length>0 && parsedData.image3.length>0)
						setImages(parsedData.image1,parsedData.image2,parsedData.image3);
				}
				
        	});  
    }  
}

function setPromo(promofile,url,newwindow){
	var noextfile = promofile;
	var temp = promofile.split(".");
	var ext = "";
	if (temp.length>1){
		ext = temp[temp.length-1]
		temp = temp.splice(0,temp.length-1);
		noextfile = temp.join(".");
	}
	var value = "";
	if (ext.toLowerCase()=="swf")
		value = AC_FL_RunContent2( "codebase","http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0","width","530","height","80","src",noextfile,"quality","high","pluginspage","http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash","movie",noextfile ); //end AC code
	else {
		if (url!="" && !newwindow) value = "<a href='"+url+"'>";
		if (url!="" && newwindow) value = "<a target='_blank' href='"+url+"'>";
		value =value +  '<img border="0" src="'+promofile+'" alt="Zylo Promo" width="530" height="80" />'
		if (url!="") value = value + "</a>";
	}
	$("#promo").html(value);
	
}

function nextImage(klasa) {
	if ($("."+klasa).length<2) return;
	var img1 = $("."+klasa+":visible");
	if (img1.length==0){
		$("."+klasa+":first").fadeIn(3000);
	} else {
		var img1next = img1.next("img");
		if (img1next.length==0)
			img1next = $("."+klasa+":first");
		img1.fadeOut(3000);
		img1next.fadeIn(3000);
	}
	return;
}

function startSlideShow()
{
  clearInterval(slideshowinterval);
  clearTimeout(delay)
  delay = setTimeout('slideShow(); slideshowinterval = setInterval("slideShow();",9000);',3000);
}

function slideShow(){
	setTimeout('nextImage("slideshowImage2")',1000);
	setTimeout('nextImage("slideshowImage3")',2000);
	setTimeout('nextImage("slideshowImage4")',3000);
	nextImage("slideshowImage1");
}

function manageAjaxLinks(){
	$(".ajaxLink").each(function(){
		var h = $(this).attr("href");
		h= h.replace(/\.html/,"");
		$(this).attr("href","#"+h);
		
	})
	$(".ajaxLink").click(function(){

		return true;
	});
}

var delay;
var slideshowinterval;

$(document).ready(function(){
	$.ajaxSetup({
	  cache: false
	});
	currentAnchor ="";
	manageAjaxLinks();
	setInterval("checkAnchor()", 300);
});

/*
$(window).load(function(){
	startSlideShow();
});
*/