$(function() {

    var blokken = $('#categoryBlocks .block');

	if($("#carousel").length) {
		$("#carousel").jCarouselLite({
			btnNext: ".next",
			btnPrev: ".prev",
			visible:1,
			scroll:1,
			auto: 4000,
			pauseOnHover: true
		});
	}

    blockAnimation(blokken);
    textResize();
	bottomNavResize();
	
	if (window.onbeforeprint !== undefined && $.browser.msie && $.browser.version != "9.0") {
		window.onbeforeprint = ShowLinks;
		window.onafterprint = HideLinks;
	}
		
});

function ShowLinks() {
	$("a").each(function() {
		if($(this).attr('href').substr(0,4) == "http") {    
			$(this).data("linkText", $(this).text());
			$(this).append(" (" + $(this).attr("href") + ")");
		}
	});
}

function HideLinks() {
	$("a").each(function() {
		$(this).text($(this).data("linkText"));
	});
}

function blockAnimation(selector){

    selector.each(function(i){
        var block = $(this);
        var list = $('ul',this);
        var close = $('.closebtn',this);


        block.mouseenter(function(){

            $(this).addClass('block-over');

        }).mouseleave(function(){

            $(this).removeClass('block-over');

        }).click(function(e){

            if($(e.target).attr('class') != 'closebtn'){
                
                list.slideDown(500);
                $(this).addClass('block-active');
                close.show();
                $(selector).not(this).find('ul').slideUp(400,
                    function(){
                        $(this).parent('.block').removeClass('block-active');
                    });
                $(selector).not(this).find('.closebtn').hide();                
                
            }
            else{
                return;
            }
            
        });

        close.click(function(e){

            e.preventDefault();
            var btn = $(this);
            btn.parent('.block').find('ul').stop().slideUp(400);
           btn.hide();
        });        

    });
}

function textResize(){
    
        var cookieSize = readCookie('text_size');

        if(cookieSize){
          $('body').addClass(cookieSize);
		  $('.resize-links a').removeClass('active');
		  $('.resize-links a#'+cookieSize).addClass('active');
        }

        $('.resize-links a').click(function(e) {
                e.preventDefault();
                var textSize = $(this).attr('id');
                $('body').removeClass('small medium big');
                $('body').addClass(textSize);
                setCookie('text_size',textSize);
		  		$('.resize-links a').removeClass('active');
		  		$(this).addClass('active');
        });
}


function readCookie(cookieName) {

	 var theCookie=""+document.cookie;
	 var ind=theCookie.indexOf(cookieName);
	 if (ind==-1 || cookieName=="") return "";
	 var ind1=theCookie.indexOf(';',ind);
	 if (ind1==-1) ind1=theCookie.length;
	 var cookieReturned = unescape(theCookie.substring(ind+cookieName.length+1,ind1));
	 return cookieReturned;
}

function setCookie(cookieName, cookieValue) {
		var today = new Date();
		var expire = new Date();
		var cookieLength = 365;
		//expire.setTime(today.getTime() + 3600000*24*cookieLength);
		//document.cookie = cookieName + "="+cookieValue+";expires="+expire.toGMTString();
		document.cookie = cookieName + "="+cookieValue;
}

function bottomNavResize() {
	$('#bottomNav a').each(function() {
		//alert($(this).height());
		if($(this).height() > 20) {
			var i = $('#bottomNav a').index($(this));
			if($(this).hasClass('last')) {
				$('#bottomNav a').eq(i-1).height($(this).height());
			} else {
				$('#bottomNav a').eq(i+1).height($(this).height());
			}
		}
	});
}
