$(document).ready(function(){
	$.stadtwerke.init();
	
	$(".slideShowContainer").each(function() {

		var containerId=$(this).attr("id");
		var slideWidth=$(this).attr("rev");
		var sleepTime=$(this).attr("rel");
		var slideTime=$("#"+containerId+" .slideShowControls").attr("rel");
		
		$("#"+containerId+" .slideShowControls a").click(function() {
			
			clickId=$(this).attr("rel");
			if (!clickId) return;
			
			marginLeft=clickId*slideWidth*(-1);
			
			$("#"+containerId+" .slideShowHolder").attr("rel", clickId);
			$("#"+containerId+" .slideShowControls a").removeClass("selected");		
			$("#"+containerId+" .slideShowControls a[rel="+clickId+"]").addClass("selected");

		    if (slideTime==0) $("#"+containerId+" .slideShowHolder").css("margin-left", marginLeft+"px")
		    else
		    {
		    	$("#"+containerId+" .slideShowHolder").animate({ 
		        marginLeft: marginLeft+"px"
		    	}, slideTime*1);
		    }

		});
		
		var sleep=sleepTime*1000;
		
		window.setTimeout("mdlSlideshow.slideshowDamon('"+containerId+"', '"+sleep+"', '"+slideTime+"')", sleep);
		
	});
	
	if (page_id==7 || page_id==174)
	{
		$(".news .box").addClass("newTabClosed");
		$(".news h3").removeClass("active");
		$(".news h3").first().addClass("active");
		$(".news .box").first().removeClass("newTabClosed");
	}
});

var mdlSlideshow = new function()
{
	this.nextSlide = function (containerId, slideTime) {
		
		var slideWidth=$("#"+containerId).attr("rev");
		var currentImg=$("#"+containerId+" .slideShowHolder").attr("rel");
		var maxImg=$("#"+containerId+" .slideShowHolder").attr("rev");
		
		if (currentImg<maxImg-1) currentImg++;
		else currentImg=0;
	
		var marginLeft=currentImg*slideWidth*(-1);
		
	    if (slideTime==0) $("#"+containerId+" .slideShowHolder").css("margin-left", marginLeft+"px")
	    else
	    {
	    	$("#"+containerId+" .slideShowHolder").animate({ 
	        marginLeft: marginLeft+"px"
	    	}, slideTime*1);
	    }	
	    
		$("#"+containerId+" .slideShowHolder").attr("rel", currentImg);	
		$("#"+containerId+" .slideShowControls a").removeClass("selected");
		$("#"+containerId+" .slideShowControls a[rel="+currentImg+"]").addClass("selected");		
	};
	
	this.previousSlide = function (containerId, slideTime) {
		
		var slideWidth=$("#"+containerId).attr("rev");
		var currentImg=$("#"+containerId+" .slideShowHolder").attr("rel");
		var maxImg=$("#"+containerId+" .slideShowHolder").attr("rev");
		
		if (currentImg>0) currentImg--;
		else currentImg=maxImg-1;
	
		var marginLeft=currentImg*slideWidth*(-1);
		
	    if (slideTime==0) $("#"+containerId+" .slideShowHolder").css("margin-left", marginLeft+"px")
	    else
	    {
	    	$("#"+containerId+" .slideShowHolder").animate({ 
	        marginLeft: marginLeft+"px"
	    	}, slideTime*1);
	    }
	    
		$("#"+containerId+" .slideShowHolder").attr("rel", currentImg);
		$("#"+containerId+" .slideShowControls a").removeClass("selected");		
		$("#"+containerId+" .slideShowControls a[rel="+currentImg+"]").addClass("selected");
		
	};
	
	this.slideshowDamon = function (containerId, sleep, slideTime)
	{
		mdlSlideshow.nextSlide(containerId, slideTime);
		window.setTimeout("mdlSlideshow.slideshowDamon('"+containerId+"', '"+sleep+"', '"+slideTime+"')", sleep);
	};
}

jQuery.cookie = function (key, value, options) {
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }
        
        value = String(value);
        
        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

jQuery.stadtwerke = new function ()
{
	this.init = function()
	{
		$.stadtwerke.clicks();
		$.stadtwerke.flyoutMenu();
	}
}

$.stadtwerke.clicks = function ()
{
	$(".news h3").click(function(){
		  
		if ($(this).hasClass("active"))
		{
			//$.cookie("newTabOpened"+$(this).attr("rel"), "0");
			$(this).removeClass("active");
			$(this).parent().children(".box").animate({
				height: "0px",
				paddingTop: "0px",
				paddingBottom: "0px"
			}, 500, function () {
				
			});
		}
		else
		{	
			thisId=$(this).attr("rel");
			$(".news h3").each(function(){
				if ($(this).attr("rel")!=thisId)
				{
					$.cookie("newTabOpened"+$(this).attr("rel"), "0");
					$(this).removeClass("active");
					$(this).parent().children(".box").animate({
						height: "0px",
						paddingTop: "0px",
						paddingBottom: "0px"
					}, 500, function () {
						
					});
				}
			});
			$(this).addClass("active");
			//$.cookie("newTabOpened"+$(this).attr("rel"), "1");
			$(this).parent().children(".box").animate({
				height: "154px",
				paddingTop: "10px",
				paddingBottom: "10px"				
			}, 500, function () {
				
			});
		}

	});
	
	$(".arrow_control").click(function(){
		  
		if ($(".arrow_control").hasClass("arrow_control_up"))
		{
			$(".arrow_control").addClass("arrow_control_down");
			$.cookie("bannerClosed", "1");
			$(".arrow_control").removeClass("arrow_control_up");
			$("#banner").animate({
				height: "0px"
			}, 500, function () {
				$("#banner").addClass("closedBanner");
			});
			window.setTimeout('$(".slideShowControls").addClass("slideShowControlsHiddenTemp");', 75);
		}
		else
		{
			$("#banner").removeClass("closedBanner");
			$(".arrow_control").addClass("arrow_control_up");
			$.cookie("bannerClosed", "0");
			$(".arrow_control").removeClass("arrow_control_down");
			$("#banner").animate({
				height: "400px"
			}, 500, function () {
				
			});
			window.setTimeout('$(".slideShowControls").removeClass("slideShowControlsHiddenTemp");', 400);
		}

	});	
}

$.stadtwerke.flyoutMenu = function()
{
	$("#mainnav ul.rootMenu > li > a").mouseover(function(){
		//$(this).parent().children("ul").css("display", "block");
	});
	
	$("#mainnav ul.rootMenu > li > a").mouseout(function(){
		$(this).parent().children("ul").css("display", "none");
	});	
	
	$("#mainnav ul.rootMenu > li > ul").mouseover(function(){
		//$(this).css("display", "block");
		$(this).parent().children("a").addClass("childrenSelected");
	});
	
	$("#mainnav ul.rootMenu > li > ul").mouseout(function(){
		$(this).css("display", "none");
		$(this).parent().children("a").removeClass("childrenSelected");
	});
	
	$("#mainnav ul.rootMenu > li > ul").each(function(){
		$(this).parent().children("a").addClass("hasChildren");
		$(this).parent().addClass("hasChildren");
	});
	
	$("#mainnav ul.rootMenu > li > ul > li > ul").each(function(){
		$(this).parent().addClass("arrow");
	});	
}

$.stadtwerke.keinBanner = function ()
{
	$(".arrow_control").addClass("arrow_control_down");
	$.cookie("bannerClosed", "1");
	$(".arrow_control").removeClass("arrow_control_up");
	$("#banner").css("height", "0px");
	$("#banner").addClass("closedBanner");
	$(".slideShowControls").addClass("slideShowControlsHiddenTemp");
}
