/**
 * Init javascript libs
 *
 * @category   Javascript
 * @package    Dessign
 * @copyright  Copyright (c) 2009 Dessign GmbH
 * @author     Wolfram Huesken <wolframhuesken@dessign.de>
 * @filesource $HeadURL$
 * @date       $Date$
 * @version    $Id$
 */

/**
 * Cookie vars
 */
var CookieName = "Layout";
var ExpireDays = 7;


/**
 * DOM Ready
 */
jQuery(document).ready(function(){ 
	// Fire up lightbox
	init_lightbox();
	
	// Fill weather container
	fetch_weather_data();	

	// Make external links open in new window
	jQuery("a[rel='external']").attr('target', 'blank');

	// Init picture slider
	jQuery("#Scroller").simplyScroll({
		autoMode: 'loop',
		speed: 1,
		loopOverload: 1
	});		
	
	// Init video box
	jQuery("a[rel^='prettyPhoto']").prettyPhoto({
		theme: 'dark_rounded' /* light_rounded / dark_rounded / light_square / dark_square */
	});

	// Init newsticker
	jQuery('.newsTicker').vTicker({
		speed: 500,
		pause: 6000,
		showItems: 1
	});	
	
	// Init Overs
	init_hover_pics();
	
	// Init accordions
	init_accordions();
	
	// Fix PNGs for IE6
	jQuery(document).pngFix(); 
});



/**
 * Fetch weather data and insert into weather container
 * @return void
 */
function fetch_weather_data() {
	if (location.href.substr(7).indexOf('/') != -1) {
	    var url = location.href.substr(7, location.href.substr(7).indexOf('/'));
	} else {
		return;
	}
	
	if (document.getElementById('WeatherContainer')) {
		jQuery("#WeatherContainer div").load("http://" + url + "/resources/php/weather/weather.php");
	}
	
//	if (document.getElementById('WeatherImage')) {
//		jQuery("#WeatherImage").load("/resources/php/weather/weatherImage.php");
//	}	
}

/**
 * Start lightbox for rel=lightbox and rel=lightbox_site
 * @return void
 */
function init_lightbox() {
	// Fire up lightbox
	jQuery('[rel=lightbox]').lightBox({
		imageLoading:  'resources/images/lightbox/lightbox-ico-loading.gif',
		imageBtnClose: 'resources/images/lightbox/lightbox-btn-close.gif',
		imageBtnPrev:  'resources/images/lightbox/lightbox-btn-prev.gif',
		imageBtnNext:  'resources/images/lightbox/lightbox-btn-next.gif',
		imageBlank:    'resources/images/lightbox/lightbox-blank.gif',
		txtImage:      'Bild',
		txtOf:         'von'
	});

	// Lightbox for additional pics on site
	jQuery('[rel=lightbox_site]').lightBox({
		imageLoading:  'resources/images/lightbox/lightbox-ico-loading.gif',
		imageBtnClose: 'resources/images/lightbox/lightbox-btn-close.gif',
		imageBtnPrev:  'resources/images/lightbox/lightbox-btn-prev.gif',
		imageBtnNext:  'resources/images/lightbox/lightbox-btn-next.gif',
		imageBlank:    'resources/images/lightbox/lightbox-blank.gif',
		txtImage:      'Bild',
		txtOf:         'von'
	});
}	

/**
 * Save the season as cookie
 * @param season
 * @return
 */
function changeSeason(season) {
	setCookie(CookieName, season, ExpireDays);
	setTimeout("location.reload()", 250);
}


/**
 * Helper function that sets a cookie with the desired style
 * @param name
 * @param value
 * @param expdays
 * @return
 */
function setCookie(name, value, expdays) {
	var now = new Date();
 	var exp = new Date(now.getTime() + (1000*60*60*24*expdays));
 	document.cookie = name + "=" + escape(value) + ";" + "expires=" + exp.toGMTString() + ";" + "path=/";
}


/**
 * Check if there are accordions and init them
 * @return
 */
function init_accordions() {
	if (jQuery('#Accordion')) {
		jQuery('#Accordion').accordion({
				header: 'h3',
				autoHeight: false
		});
	}

	if (jQuery('#Accordion2')) {
		jQuery('#Accordion2').accordion({
				header: 'h3',
				autoHeight: false
		});
	}	
	
	if (jQuery('#Accordion3')) {
		jQuery('#Accordion3').accordion({
				header:     'h3',
				autoHeight: false
		});
	}	
}


/**
 * Read a cookie
 * @param string name
 * @return string
 */
function getCookie(name) {
 	var cname = name + "=";
 	var dc = document.cookie;
 	if (dc.length > 0) {
 		var start = dc.indexOf(cname);
 		if (start != -1) {
 			start += cname.length;
 			var stop = dc.indexOf(";", start);
 			if (stop == -1) stop = dc.length;
 			return unescape(dc.substring(start,stop));
 		}
 	}
 	return null;
}
