jQuery(function($){
	
	noIE6();
	
	//Highlight current page
	try{
		if(currentPage>0)
		$("#navigation > li").eq(currentPage-1).addClass("active");
	}
	catch(e){
	}
	
	//Thumb Dom
	$("#banner .thumb a").append('<span class="hit"></span>');
	
	//Navigation Dom
	$("#navigation ul").wrap('<div class="drop-down"><div class="bottom"></div></div>').parents(".drop-down").prepend('<div class="top"></div>');
	$("#navigation ul li:last-child").addClass("last-child");
	
	//Dropdown
	$("#navigation > li").hover(function(){
		$(this).find(".drop-down").delay(100).height("auto").slideDown(300);
		$(this).find("> a").addClass("active");
	}, function(){
		$(this).find(".drop-down").stop().slideUp(100);
		$(this).find("> a").removeClass("active");
	});		
	
	//Search
	$("#search-hit").click(function(){
		$(this).toggleClass("active");
		$("#search").toggle();
		return false;
	});
	
	//chain
	$("#chain-box").customChain({
		previous: ".arrow3-left",
		next: ".arrow3-right",
		target: ".chain li"
	});
	$("#chain-box-2").customChain({
		previous: ".arrow3-left",
		next: ".arrow3-right",
		target: ".chain li",
		totalDisplay: 4
	});
	$(".chain-box").mouseover(function(e){
		$target = $(e.target);
		if($target.is(".image img")){
			$target.animate({"opacity":0.6});
		}
	});
	$(".chain-box").mouseout(function(e){
		$target = $(e.target);
		if($target.is(".image img")){
			$target.animate({"opacity":1});
		}
	});
	
	//theme-selector
	$("#theme-selector a").click(function(){
		setCookie("theme", $(this).attr("href"));
		window.location.reload();
		return false;
	});
	
	
});

$(window).load(function(){
	$("body").width("auto");
	
});

// Cookiehandling
if(getCookie("theme")!=undefined){
	var themeCSS = document.createElement("link");
	themeCSS.setAttribute("href",getCookie("theme"));
	themeCSS.setAttribute("rel","stylesheet");
	themeCSS.setAttribute("type","text/css");
	document.getElementsByTagName("head")[0].appendChild(themeCSS);
};

//setCookie
function setCookie(c_name,value,exdays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
};
//getCookie
function getCookie(c_name){
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++){
	  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
	  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
	  x=x.replace(/^\s+|\s+$/g,"");
	  if (x==c_name){
		return unescape(y);
	  }
	}
}


//customChain
(function($){	
	$.fn.customChain = function(opt){
		
		var defaults = {
			target: "li",
			previous: ".previous",
			next: ".next",
			speed: 400,
			totalDisplay: 3
		};
		
		var o = $.extend(defaults, opt);
		
		return this.each(function(){
			
			var $this = $(this);
			var $next = $(o.next, $this);
			var $previous = $(o.previous, $this);
			var $target = $(o.target, $this);
			var total  = $target.length;
			var animation = true;
			var current = 0;
			var prev = 0;
			
			stimulateIn(current);
			
			$next.click(function(){
				if(animation){
					return false;
				}
				current += o.totalDisplay;
				if(current>=total){
					current = 0;
				}
				showFrom();
				return false;
			});
			$previous.click(function(){
				if(animation){
					return false;
				}
				current -= o.totalDisplay;
				if(current<0){
					var mod = total%o.totalDisplay;
					if(mod>0){
						current = total-(total%o.totalDisplay);
					}else{
						current = total-o.totalDisplay;
					}
				}
				showFrom();
				return false;
			});
			
			
			function showFrom(){
				animation = true;
				for(i = prev,j = 0,k = 0; j<o.totalDisplay; i++, j++){
					$target.eq(i).delay(j*120).animate({"top":220}, o.speed, function(){
						k++;
						//alert(total);
						if(k>=o.totalDisplay || i > total){
							stimulateIn();
						};
					});
				};				
			};
			
			function stimulateIn(){
				$target.hide();
				for(i = current,j =0; j<o.totalDisplay; i++, j++){
						$target.eq(i).delay(j*120).animate({"top":0, "opacity":"show"}, o.speed);
				}
				prev = current;
				animation = false;
			}
			
		});
		
	}
	
})(jQuery);


function noIE6(){
    if($.browser.msie && $.browser.version == 6){
		 $("body").html("<p style='color:#fff'>This browser doesn't support some elements on this page. Please, try other modern browser.</p>");
	};
}
