var flashLoaded = false;
var flashImages = Array();
var flashImagesCurrent = 0;

$(document).ready(function(){
	//initialize navigation
	init_navigation();
	init_buttons();
	init_longtext();
	init_search();
	//setTimeout(init_image,1000);
	setTimeout(init_image2,500);

});

function init_navigation(){
	$("li.topnav:not(.active)").hover(
		function(){$(this).animate({backgroundColor:"#FFFFFF"},200)},
		function(){$(this).animate({backgroundColor:"#F0F0F0"},200)}
	);
	
	$(".subnav1 li:not(.active)").hover(
		function(){$(this).animate({backgroundColor:"#F0F0F0"},200)},
		function(){$(this).animate({backgroundColor:"#FFFFFF"},200)}
	);
	
	//reset subnav
	$(document).click(function(){
		$("DIV.subnav1").hide();//.parent().css("borderColor","#047dc2");
		$("#IMAGE_LOADER").css("visibility","visible");
	});
	
	//find subnav elements
	$(".hassub a.topnav").click(function(e){
		//set bgimage
		if($("#IMAGE_LOADER").css("display")!="none" && $("#IMAGE_LOADER").html() != null){
			//set background image
			var flashObj = Ext.get("IMAGE_LOADER").dom;
			if(flashLoaded){
			//if(!typeof flashObj.setCurrentImage == 'undefined'){
				flashObj.setCurrentImage();
			}
			//Ext.get("IMAGE_LOADER").dom.setCurrentImage();
			//hide flash if it's there
			$("#IMAGE_LOADER").css("visibility","hidden");
			//$("#IMAGE_LOADER").hide();
		}
		
		$("DIV.subnav1").hide();//.parent().css("borderColor","#047dc2");
		$(this.parentNode.lastChild).show();//.parent().css("borderColor","#FFFFFF");
		this.blur();
		return false;
	});
	
	//fix subnav shade
	$("DIV.subnav1").each(function(){
		var ul = $(this.lastChild);
		$(this.firstChild)
			.width(ul.width())
			.height(ul.height())
			.css("marginTop","5px")
			.css("marginLeft","5px")
			.css("display","block");
	});
	
	//hide subnavs
	$("DIV.subnav1").css("visibility","visible").hide();
}
function init_buttons(){
	//fix hovering
	$(".button").hover(
		function(){$(this).addClass("button_over");},
		function(){$(this).removeClass("button_over");}
	);
	//fix linking
	$(".dynbtn").click(function(){
		//alert($(this).attr('title'));
		window.open($(this).attr('title'));	
	});
}
function init_search(){
	//fix focus-blanking
	$("#INP_SEARCH").focus(function(){
		$(this).val("");
	});
	$("#INS_SEARCH").focus(function(){
		this.select();
	});
	$("#BTN_SEARCH").click(function(){
		$("#searchform").submit();
	});
}

		
function init_longtext(){
	$("div.longtext").each(function(i){
		var tease = $(this).children("div.tease");
		var full = $(this).children("div.full");
		full.width(tease.width());
		var height = tease.height();
		var newheight = full.height();
		tease
			.html('<p>' + Ext.util.Format.ellipsis(full.text(),100)+'</p>')
			.css("overflow","hidden");
			
		$(this).children("a.more").click(function(){
			tease
				.animate({height:newheight},500)
				.html(full.html());
			$(this).hide();
			return false;
		});
		/*
		$(this.firstChild)
			.append(Ext.util.Format.ellipsis($(this.lastChild).text(),100));
		*/
		//var tease = this.html();
		
	});
}
function init_image2(){
	//kolla om det är array eller singel
	flashImages = topAreaList.split(",");
	//alert('Array loaded: '+tmpA.length);
	if(flashImages.length == 1 && topAreaType == "image"){
		//ladda in bilden direkt
		$("#BODY_IMAGE").html('<img src="'+topAreaPath+flashImages[0]+'">');
	}else{
		//ladda in bilderna/flashen i flash-holdern
		var flashvars = {
			dir:topAreaPath,
			images:topAreaList,
			speed:3
		};
		swfobject.embedSWF("/__shared/media/flash/image.swf", "IMAGE_LOADER", "640", "255", "8.0.0","/__shared/media/flash/expressInstall.swf", flashvars, {}, {});
		//kolla om swfObject lyckades.. annars skapar vi ett javascript-bildspel
		setTimeout(check_if_flash_loaded,500);
		
	}
}
function set_current_image(imgstr){
	imgstr = imgstr.replace(".swf",".jpg");
	$("#BODY_IMAGE").css({
		backgroundImage:"url("+imgstr+")",
		//backgroundPosition:"0 5px",
		backgroundRepeat:"no-repeat"
	});
}
function check_if_flash_loaded(){
	flashLoaded = Boolean(typeof Ext.get("IMAGE_LOADER").dom.setCurrentImage != 'undefined');
	if(!flashLoaded && topAreaType == "image"){
		$("#BODY_IMAGE").html('<img src="'+topAreaPath+flashImages[0]+'" id="top_area_image">');
		bildspel = setInterval(nextImage,3000);
	}
}
function nextImage(){
	flashImagesCurrent++;
	flashImagesCurrent = flashImagesCurrent >= flashImages.length ? 0:flashImagesCurrent;
	$("#top_area_image").attr("src",topAreaPath+flashImages[flashImagesCurrent]);
}