/**
* 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
*
*/
$.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 banner        = 0;
var bannerTimeout = 2000;

function changeBanner() {
	var img = $('.banner-img');
	var current = parseInt(img.attr('id').replace('banner_', ''));
	current++;
	if (current > 5) {
		current = 2;
	}

	$.cookie('banner', current);
	var nextImage = $('<img src="/images/layout/banner'+ current +'.jpg" id="banner_'+current+'" class="banner-img" alt="" width="627" height="203" />');

	img.before(nextImage);

	img.fadeOut(2000, function() {
		img.remove();
		setTimeout(changeBanner, bannerTimeout);
	});
}

function scrollto(scrollTo) {
    if (scrollTo && $(scrollTo)) {
        $('html, body').animate({scrollTop: $(scrollTo).offset().top}, 'slow');
    }
}

$(function() {
    
    $('.title-home h1').html('Briketovací lisy, BRIKLIS, spol. s r.o.');

	setTimeout(changeBanner, bannerTimeout);

    $('a.bullet-open').click(function() {
       $(this).parent().find('.content').toggleClass('hidden');
       return false;
    });

    $('.contacts .tabs a, .product-tabs .tabs a').click(function() {
        var tabs = $(this).parents('.product-tabs, .contacts');
        tabs.find('> .content').addClass('hidden');
        tabs.find('.tabs div').removeClass('selected');
        $(this).parent().addClass('selected');
        $($(this).attr('href')).removeClass('hidden');

        updateHash('product-tabs', $($(this).attr('href')).attr('id'));

        return false;
    });

    var product_tab = getUrlVars()["product-tabs"];
    if (product_tab) {
    	$('.product-tabs > .content').addClass('hidden');
    	$('.product-tabs .tabs div').removeClass('selected').each(function() {
    		if($(this).find('a').attr('href') == '#' + product_tab) {
    			$(this).addClass('selected')
    		}
    	});
    	$('#' + product_tab).removeClass('hidden');
    }
    
    var scrollTo = getUrlVars()['scroll'];
    if (scrollTo && $("#" + scrollTo)) {
        $('html, body').animate({scrollTop: $("#" + scrollTo).offset().top}, 'slow');
    }

    $('.infobox a.opener').click(function() {
        $(this).parents('.infobox').find('.body').toggleClass('hidden');
        return false;
    });

    var menuClosingTimeout = null;
    $('.mainmenu .items > ul > li').mouseenter(function() {
    	clearTimeout(menuClosingTimeout);
    	if (!$(this).hasClass('selected')) {
    		$('.mainmenu .items > ul > li').removeClass('selected');
    		$(this).addClass('selected');
    	}
    	return false;
    }).mouseleave(function() {
    	clearTimeout(menuClosingTimeout);
    	menuClosingTimeout = setTimeout(function() {
    		$('.mainmenu .items > ul > li').removeClass('selected');
    	}, 250);
    	return false;
    });
    
    $('.sidebar .panels .panel').mouseenter(function() {
        $(this).addClass('dark');
    }).mouseleave(function() {
        $(this).removeClass('dark');
    });

    $("a[ref='colorbox']").colorbox({});

    //$.hurl("update", {"id": 15765, "link": "111"});

    //console.debug();
});

function getUrlVars() {
	var vars = {};
	var hash = '&' + window.location.hash.substr(1);
	var parts = hash.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
		vars[key] = value;
	});
	return vars;
}

function updateHash(name, value) {
	var vars = getUrlVars();
	vars[name] = value;
	var hash = [];
	for(var k in vars) {
		hash.push(k + "=" + vars[k]);
	}
	window.location.hash = '#' + hash.join('&');
}


