// JavaScript Document
var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline = 0;
var rotate_time = 5000;		//time in milliseconds
// slideshow
var slide_show_speed = 1500;
var slide_duration = 6000;
var slide_interval;

/* Google Analytics */
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

// jquery functions
$(document).ready(function() {
	 
	/* highlight current page */
	$('#menu_side a').not(':first').each(function(){
		if (window.location.href.indexOf($(this).attr("href")) > 0) {
			$(this).addClass('selected');
			/* $(this).parent().addClass('selected'); */
		}
	});
	
	/*
	// define sections
	var noSections = $("#menu_side > ul > li > ul > li").size();
	var section = new Array();
	
	// read cookies and show / hide sections
	for (i = 0; i <= noSections; i++) {
		// collapse all
		section[i] = "collapsed";
		
		// read cookies
		if ($.cookie("section" + i)) {
			section[i] = $.cookie("section" + i);
		}
		
		// show appropriate menus
		if (section[i] == "expanded") {
			$("#section" + i + " > ul").show();
		} else {
			$("#section" + i + " > ul").hide();
		}
	}
	*/
	
	$("#menu_side .current").each(function() {
		$(this).parent().show("fast");
		// var elid = $(this).parent().parent().attr("id");
		// alert(elid);
		// $.cookie(elid, 'expanded');
	});
	
	$("#menu_side .active").each(function() {
		$(this).show("fast");
		// var elid = $(this).parent().parent().attr("id");
		// alert(elid);
		// $.cookie(elid, 'expanded');
	});
													 
	/* slideshow */
	var $first = $('#slideshow li:first');
	$first.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, slide_show_speed);
	
	
	/* special offers */
	headline_count = $("div.headline").size();
	$("div.headline:eq("+current_headline+")").css('top','6px');
	
	headline_interval = setInterval(headline_rotate,rotate_time);
	
	
	/* functions */
	
	/* special offers */
	$('#special_offers').hover(function() {
		 clearInterval(headline_interval);
	}, function() {
		 headline_interval = setInterval(headline_rotate,rotate_time);
		 // headline_rotate();
	});
	
	// $("#menu_side > ul > li > ul > li > a").click(function(event) {
		// event.preventDefault();
		// var elid = $(this).parent().attr("id");
		// alert(elid);
		/*
		if ($("#" + elid + " > ul").is(':visible')) {
			$.cookie(elid, 'collapsed');
		} else {
			$.cookie(elid, 'expanded');
		}
		*/
		// $("#" + elid + " > ul").slideToggle();
	// });
	
	/* Google Analytics */
	try {
		var pageTracker = _gat._getTracker("UA-6033135-53");
		pageTracker._trackPageview();
	} catch(err) {}
});

$(window).load(function() {
	slide_interval = setInterval( "slideSwitch()", slide_duration );
});

function slideSwitch() {
	var $active = $('#slideshow li.active');

	// if ( $active.length == 0 ) $active = $('#slideshow img:last');

	// use this to pull the images in the order they appear in the markup
	var $next =  $active.next().length ? $active.next() : $('#slideshow li:first');

	$active.addClass('last-active');

	// $active.animate({opacity: 0}, slide_show_speed, function() {
		
		$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, slide_show_speed, function() {
				$active.removeClass('active last-active');
			});
			
	// });
}

function headline_rotate() {
	current_headline = (old_headline + 1) % headline_count; 
	$("div.headline:eq(" + old_headline + ")").animate({top: -125},"slow", function() {
		$(this).css('top','125px');
	});
	$("div.headline:eq(" + current_headline + ")").show().animate({top: 6},"slow");  
	old_headline = current_headline;
}
	