/* snippets*/




/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

/**
 * Create a cookie with the given name and value and other optional parameters.
 *
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @desc Create a cookie with all available options.
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
 *       used when the cookie was set.
 *
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
 *                             when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 *                        require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};


//*******************************
/*  Tabs */
var Tabs = function() {
	return {
		/* Alle vorhandenen Tabs werden per jQueryTools gestaltet */
		"prepare" : function() {
			$('ul.tabs').tabs('div.panes > div.tabcontent', { 
				// active klasse wird entfernt, wenn ein anderes tab>li geklickt wird
			    onClick: function(event, tabIndex) { 
					this.getCurrentTab().parent().removeClass('active');
			    }
			}).history();
			
			// 'active' klasse zum li hinzufügen, wenn tab>li geklickt wird
			$('ul.tabs li').click(function () { 
				$(this).addClass('active'); 
		    });
			$('ul.tabs li a').click(function () { 
				$(this).parent().addClass('active'); 
		    });
			
			
		}
	};
}();



//*******************************
/*  Slider */
var Slider = function() {
	return {
	
		/* Alle vorhandenen Tabs werden per jQueryTools gestaltet */
		"normalSlider" : function() {

			$("div.jQuery_slider").each(function(){

				var parentWidth = $(this).parent('.content').width();
				var slideWidth = parentWidth - 74;
				var slideTabsMargin = parentWidth / 2 - 35;

				// set width of slides and slidetabs, according to its parent box
				$(this).children('.slides').width(slideWidth);
				$(this).children('.slidetabs').css({
					'margin-left' : slideTabsMargin+'px'
				});

				// set min-height for sliderbox
				var slideHeight = $(this).children('.slides').height();
				if (slideHeight < 200) {		
					$(this).children('.slides').height(200);
				}

				// get width of parent content element
				$(this).width(parentWidth);
			});


			$("div.slidetabs").tabs(".slides > div.slide", { 
		        // start from the beginning after the last tab 
				effect: 'fade',
				rotate: true,
		        relative: true
		    }).slideshow({
		    	// slider not clickable
		    	clickable: false
		    });
		},

		/* Slider für Bider mit Autoplay und Fadeeffekt */
		"autoplaySlider" : function() {
			// activate slideshow
			$(".autoSlides").tabs("div.autoSlide", {
				// enable "cross-fading" effect
		        effect: 'fade',
		        fadeOutSpeed: 2500,
		        fadeInSpeed: 2500,
		        // start from the beginning after the last tab
		        rotate: true,
		        relative: true,
		        event: 'dblclick'
		    }).slideshow({
		    	// slider not clickable
		    	clickable: true,
		    	autoplay: true,
		    	next: '',
		        prev: '',
		        interval: 8000
		    });
		},
		
		/* Slider für Bider mit Autoplay und Fadeeffekt */
		"productSlider" : function() {
			// activate slideshow
			$(".productSlides").tabs("div.productSlide", {
				// enable "cross-fading" effect
		        effect: 'fade',
		        fadeOutSpeed: 2500,
		        fadeInSpeed: 2500,
		        // start from the beginning after the last tab
		        rotate: true,
		        relative: true,
		        event: 'dblclick'
		    }).slideshow({
		    	// slider not clickable
		    	clickable: true,
		    	autoplay: true,
		    	next: '',
		        prev: '',
		        interval: 9000
		    });
		},

		/* Slider für Bilder mit Autoplay und Fadeeffekt */
		"accordion" : function(newInitialIndex) {
			// new effect for independent accordion toggle
			$.tools.tabs.addEffect("toggle", function(tabIndex, done) { 
				done.call(); 
		    });
			
			// activate accordion
			var api = $(".accordion").tabs(".content", {
				tabs: 'div.accordiontitle',
			    effect: 'toggle',
			    current: '',
			    initialIndex: newInitialIndex,
			    api: true,
			    onBeforeClick: function(event, tabIndex) {
			        // toggle class on accordion title
					$(this.getTabs().eq(tabIndex)).toggleClass('current');
			        // toggle class on footer, to show background when active
			        $(this.getTabs().eq(tabIndex).next().next('.footer')).toggleClass('show');
			        this.getPanes().eq(tabIndex).toggle();
			    }
			});
			// hide empty accordion elements
			$(".accordiontitle").each(function(){
				if ($(this).html().length > 0 || $(this).next('.content').html().length > 0){
					$(this).css({'display' : 'block'});
					$(this).next('.content').next('.footer').css({'display' : 'block'});
				}
			});

			// open all accordion panes if class="allOpen" is set
			$('.jQuery_Accordion').children().filter('.allOpen').children('.accordiontitle').filter(':visible').each(function (i) {
			          $(this).addClass('current');
			          $(this).next('.content:hidden').next('.footer').addClass('show');
			          $(this).next('.content:hidden').toggle();
	        });
			
			// close all panes, if class="allClosed" is set
			$('.jQuery_Accordion').children().filter('.allClosed').children('.accordiontitle').filter(':visible').each(function (i) {
			          $(this).removeClass('current');
			          $(this).next('.content:visible').next('.footer').removeClass('show');
			          $(this).next('.content:visible').toggle();
	        });
			
			
			
			// add toggler to each accordion with toggleMe class 
			$('.jQuery_Accordion').children().prepend('<a href="#" class="toggler">toggle</a>');
			
			
			// click toggler, to toggle accordion panes open/close
			$(".toggler").click(function () {
				var allVisible = $(this).nextAll('.accordiontitle:visible');
				var allVisibleSize = $(this).nextAll('.accordiontitle:visible').size();
				var allCurrent = $(this).nextAll('.current');
				var allCurrentSize = $(this).nextAll('.current').size();
				
				// close all accordion panes, if toggler is clicked
				if (allVisibleSize == allCurrentSize) {
					$(allCurrent).each(function (i) {
						$(this).removeClass('current');
					    $(this).next('.content:visible').next('.footer').removeClass('show');
					    $(this).next('.content:visible').toggle();
			        });
				} else {
					// open all panes
					$(allVisible).each(function (i) {		        		
		        		  $(this).addClass('current');
				          $(this).next('.content:hidden').next('.footer').addClass('show');
				          $(this).next('.content:hidden').toggle();
					});
				}
			});
		}
	};
}();




//*******************************
/*  Suchbox */
var Searchbox = function() {
	return {
		/* Defaultvalue der Suchbox wird beim Focus geleert */
		"emptySearchBoxOnFocus" : function() {

			$("#searchboxInput").focus(function () {
		         $(this).attr('value', '');
		    });
		}
	};
}();


//*******************************
/*  tt_news Gallery */
var NewsGallery = function() {
	return {
		/* Defaultvalue der Suchbox wird beim Focus geleert */
		"initializeGallery" : function() {
			// initialize scrollable
		    $("div.scrollable").scrollable();
		    
		    $(".items img").click(function() {
		        // calclulate large image's URL based on the thumbnail URL (flickr specific)
		        var url = $(this).attr("src");
		        // get handle to element that wraps the image and make it semitransparent
		        var wrap = $("#image_wrap");
		        // the large image from flickr
		        var img = new Image();
		        // call this function after it's loaded
		        img.onload = function() {
		            // change the image
		            wrap.find("img").attr("src", url);
		        }; 
		        // begin loading the image from flickr
		        img.src = url;
		        
		        // when page loads simulate a "click" on the first image
		    }).filter(":first").click();
		}
	};
}();



$(document).ready(function () {
	Tabs.prepare();
	Slider.normalSlider();
	Slider.autoplaySlider();
	Slider.productSlider();
	Slider.accordion('0');
	Searchbox.emptySearchBoxOnFocus();
	NewsGallery.initializeGallery();
	
	if (typeof makeMap == 'function') { 
		makeMap();
	}
});
