/**
 * 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 : cookie_encode(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;
};

function cookie_encode(string){
	//full uri decode not only to encode ",; =" but to save uicode charaters
	var decoded = encodeURIComponent(string);
	//encod back common and allowed charaters {}:"#[] to save space and make the cookies more human readable
	var ns = decoded.replace(/(%7B|%7D|%3A|%22|%23|%5B|%5D)/g,function(charater){return decodeURIComponent(charater);});
	return ns;
};
var slides = {};
slides.init = function(){
	jQuery("#block-views-home-page-slides-titles-block .view-content a:eq(0)").addClass("active");
	
	jQuery("#block-views-home-page-slides-titles-block .view-content a").click(function(e){
		e.preventDefault();
		
		var index = jQuery(this).closest("li").index();
		
		jQuery("#block-views-home-page-slides-titles-block .view-content a.active").removeClass("active");
		jQuery(this).addClass("active");
		jQuery("#block-views-home-page-slides-content-block .view-content .views-row:visible").hide();
		jQuery("#block-views-home-page-slides-content-block .view-content .views-row:eq(" + index + ")").show();
		
		slides.stop();
	});
	
	slides.rotate();
}
slides.next = function(){
	var currentTitle = jQuery("#block-views-home-page-slides-titles-block .view-content a.active");
	var currentSlide = jQuery("#block-views-home-page-slides-content-block .view-content .views-row:visible");
	var nextTitle = (currentTitle.closest('li').next().find('a').length > 0) ? currentTitle.closest('li').next().find('a') : jQuery("#block-views-home-page-slides-titles-block .view-content a:eq(0)");
	var nextSlide = (currentSlide.next().length > 0) ? currentSlide.next() : jQuery("#block-views-home-page-slides-content-block .view-content .views-row:eq(0)");
	
	currentTitle.removeClass('active');
	currentSlide.hide();
	nextTitle.addClass('active');
	nextSlide.show();
}
slides.rotate = function(){
	clearInterval(slides.interval);
	slides.interval = setInterval("slides.next();", 8000);
}
slides.stop = function(){
	clearInterval(slides.interval);
}

jQuery(function(){
	if(jQuery("#showcase").length > 0){
		slides.init();
	}
	
	if(jQuery("#bottom").length > 0){
		var height = 0;
		jQuery("#bottom .content .view").each(function(element){
			if(jQuery(this).innerHeight() > height){
				height = jQuery(this).innerHeight() - 30;
			}
		});
		
		if(height > 265){
			jQuery("#bottom .content .view").css("height", height);
		}
		else {
			jQuery("#bottom .content .view").css("height", 265);
		}
	}
	
	/*jQuery("#location .submit").click(function(e){
		e.preventDefault();
		jQuery(this).closest("form").submit();
	});*/
	
	jQuery("#location-input").val("Postal Code");
	jQuery("#location-input").focus(function(e){
		if(jQuery(this).val() == "Postal Code"){
			jQuery(this).val("");
		}
	});
	jQuery("#location-input").blur(function(e){
		if(jQuery(this).val() == ""){
			jQuery(this).val("Postal Code");
		}
	});
	
	jQuery("#edit-search-block-form--2").val("Search");
	jQuery("#edit-search-block-form--2").focus(function(e){
		if(jQuery(this).val() == "Search"){
			jQuery(this).val("");
		}
	});
	jQuery("#edit-search-block-form--2").blur(function(e){
		if(jQuery(this).val() == ""){
			jQuery(this).val("Search");
		}
	});
	
	jQuery("#join-input").val("Email");
	jQuery("#join-input").focus(function(e){
		if(jQuery(this).val() == "Email"){
			jQuery(this).val("");
		}
	});	
	jQuery("#join-input").blur(function(e){
		if(jQuery(this).val() == ""){
			jQuery(this).val("Email");
		}
	});	
	
	if(jQuery.browser.mozilla && jQuery.browser.version.search(/1.9.0/) != -1){
		jQuery("#search #edit-submit").css({position: "relative", top: "-22px"});
	}
	
	jQuery('a[href*=http://www.youtube.com]').not('.youtube').addClass('iframe').removeAttr('target').fancybox({'transitionIn': 'elastic', 'transitionOut': 'elastic', 'easingIn': 'easeOutBack', 'easingOut': 'easeInBack'});
});

;

