"use strict";

/**
 * jQuery Cookie plugin
 *
 * Copyright (c) 2010 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
 *
 */
jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    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(''));
    }

    // key and possibly options given, get cookie...
    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;
};


var extraImages = function() {
	return {
		init: function() {
			if($("#extraImages li").length == 0) return;
		
			$.getScript("/themes/gartenstadt/assets/jquery.cross-slide.min.js", function() {
				var	$this = $("div.stage img");
					$this.parent().css({width: $this.width(),height: $this.height()});
					$this.css({position: 'absolute', top: 0, left: 0});
				var imgs = $('#extraImages li img').map(function() {
					  	var $this = $(this);
					  return {src: this.src, alt: $this.attr('alt'), from: $this.data('options').from, to: $this.data('options').to, time:  $this.data('options').speed}; 
					  
					}).get();
					
					$this.parent().crossSlide({
				  fade: 1
				}, imgs);
					
				});
			
		}		
	};
}();

function checkHeight() {
   // keep footer visible:
		var hContent = $("div.wrap").height() + 30; 
		var hWindow = $(window).height();
			var wWindow = $(window).width();
			var toggle = false;
			if(hContent>hWindow) {
				if(hContent - $(window).scrollTop() > hWindow) {
				toggle = true;
				}
			} 
			
			$("div.footer").toggleClass("docked", toggle);
			$("#bg").css("display", (wWindow >= 1600) ? "block" : "none");
};

var homeIntro = function() {
	return {
	init: function() {
		var intro = $("div.intro");
		if(intro.length == 0) return false;
	
		if($.cookie("intro") == "done") return false;
	
		var content = $("div.outer");
		
		content.css("display", "none");
		intro.css("display", "block");
		
		setTimeout(function() {
				content.fadeIn(function() {
					$.cookie("intro", "done");

					extraImages.init();
					
					});
				intro.fadeOut();
			}, 1500);
	}
	};
}();

$(function() {
	if(homeIntro.init() == false) {
	extraImages.init();};
	
	if ($.browser.msie && jQuery.browser.version.substr(0, 1) == "6") {
		// no fun for IE6 Users
	} else {
    $(window).scroll(checkHeight);
	$(window).resize(checkHeight);
	checkHeight();
	}
		
	$.getScript("/themes/gartenstadt/assets/jquery-ui-1.8.14.custom.min.js", function() {
		var haeuser = $("div.wohnungen div.haeuser");
		var haeuserImg = haeuser.find("img");
		
		var setHaus = function(index) {
			switch(index) {
				case 1:
					haeuserImg.css("left", "-1071px");
				break;
				case 2:
					haeuserImg.css("left", "-714px");
				break;
				case 3:
					haeuserImg.css("left", "-357px");
				break;
				default:
					haeuserImg.css("left", "0px");
			}
		};
		
		$("#wohnungen").accordion({collapsible:true, active: false, changestart: function(event, ui) { 
			var active = $("#wohnungen").accordion("option", "active");
			if(active === false) active = -1;
			setHaus(active + 1);	
		}});
		
		haeuser.find("div").mouseenter(function() {
			setHaus($(this).index());			
			}).click(function() {
				$("#wohnungen").accordion("option", "active", $(this).index() -1);
		});
		
		haeuser.mouseleave(function() {
			var active = $("#wohnungen").accordion("option", "active");
			if(active === false) active = -1;
			setHaus(active + 1);	
		});
		
		setTimeout(function() {$("#wohnungen").accordion("option", "active", 0)}, 600);
		
		checkHeight();
	});
	
	
	$.getScript("/themes/gartenstadt/assets/fancybox/jquery.fancybox-1.3.4.pack.js", function() {
		$("a.zoom").fancybox();
		$("ul.gallery a").fancybox();
		$("a.video").fancybox();
	});
});
