
function bindRating() {
	$('input[type=radio].star2').rating({
		callback: function() {
			//console.log('sadsad');
			$(this.form).get(0).onsubmit();
		}
	});
}


$(document).ready(function () {

	$('a#fileSelector').click(function(){
		$(this).next('input[type=file]').trigger("click");
	});


/* Cufon
------------------------------------------------------------------------------*/

	Cufon.replace('#content h1, p.visual small, .cuf, #contestImgWrap p, .cufPink', {fontWeight: 400, hover: true, fontFamily: 'MetaPlusCE'});
	Cufon.replace('.cufBold, p.visual strong, #slideshow h2, #contestImgWrap h2', {fontWeight: 500, hover: true, fontFamily: 'MetaPlusCE'});
	Cufon.replace('.cufMatto', {fontFamily: 'Matto'});



/* Login show/hiden forms
------------------------------------------------------------------------------*/

	if ($("input#user_competition").attr('checked')) {
		$(".contestLogin").show();
	} else {
		$(".contestLogin").hide();
	}

	$("input#user_competition").click(function() {
		$(".contestLogin").toggle();
	});
	
	$("#content #columnLogin a#lostPassword").click(function(){
		$('form#lostPasswordBox').toggleClass('hide');
	});

/* Hide input labels on focus, hover submit button
------------------------------------------------------------------------------*/

	var formInputs = $("input[type=text]:not(.noHide)");
	formInputs.each(function() {
		if ($(this).val() != "") {
			$("label[for=" + $(this).attr("id") + "]").addClass("hide");
		}
	});
	formInputs.focus(function() {
		if ($(this).val() == '') {
			$("label[for=" + $(this).attr("id") + "]").addClass("hide");
		}
	});
	formInputs.blur(function() {
		if ($(this).val() == '') {
			$("label[for=" + $(this).attr("id") + "]").removeClass("hide");
		}
	});

	$("button[type=submit], #whatToBake > li").hover(function() {
		$(this).addClass("hover");
	}, function() {
		$(this).removeClass("hover");
	});



/* Custom forms: checkbox, radio, select
------------------------------------------------------------------------------*/

	$('input[type="checkbox"]:not(.noReplace)').checkbox({ cls:'jquery-checkbox'});
	$('input[type="radio"]:not(.noReplace)').checkbox({ cls:'jquery-radio'});
	$(".jqSelect:not(.noReplace)").jqTransform();






/* Colorbox
------------------------------------------------------------------------------*/

	$("a[rel='lightboxGallery']").colorbox({
		opacity: 0.5,
		onOpen: function() {
			$("#cboxContent h1").remove();
			$("#cboxContent p.subTitle", "#colorbox").remove();
			$("<h1/>").html($(this).attr("rev")).appendTo($("#cboxContent", "#colorbox"));
			Cufon.replace('#cboxContent h1', {fontWeight: 400, hover: true, fontFamily: 'MetaPlusCE'});
			
		}
	});
	$("a[rel='lightboxForm']").colorbox({
		opacity: 0.5,
		onOpen: function() {
			$("#cboxContent h1", "#colorbox").remove();
			$("#cboxContent p.subTitle", "#colorbox").remove();
			$("<h1/>").html($(this).attr("rev")).appendTo($("#cboxContent", "#colorbox"));
			Cufon.replace('#cboxContent h1', {fontWeight: 400, hover: true, fontFamily: 'MetaPlusCE'});
		},
		onComplete: function() {
			$('input[type="checkbox"]:not(.noReplace)').checkbox({ cls:'jquery-checkbox'});
			$('input[type="radio"]:not(.noReplace)').checkbox({ cls:'jquery-radio'});
		}
	});
	$("a[rel='lightboxFormSubTitle']").colorbox({
		opacity: 0.5,
		title: false,
		onOpen: function() {
			$("#cboxContent h1", "#colorbox").remove();
			$("#cboxContent p.subTitle", "#colorbox").remove();
			$("<h1/>").html($(this).attr("rev").split("|")[0]).appendTo($("#cboxContent", "#colorbox"));
			$("<p/>").addClass("subTitle").html($(this).attr("rev").split("|")[1]).appendTo($("#cboxContent", "#colorbox"));
			Cufon.replace('#cboxContent h1', {fontWeight: 400, hover: true, fontFamily: 'MetaPlusCE'});
		},
		onComplete: function() {
			$("#cboxLoadedContent", "#colorbox").addClass("formTitle");
			$.fn.colorbox.resize();
			$('input[type="checkbox"]:not(.noReplace)').checkbox({ cls:'jquery-checkbox'});
			$('input[type="radio"]:not(.noReplace)').checkbox({ cls:'jquery-radio'});
		}
	});
	$("a[rel='lightboxCheck']").colorbox({
		opacity: 0.5,
		title: false,
		onOpen: function() {
			$("#cboxContent h1", "#colorbox").remove();
			$("#cboxContent p.subTitle", "#colorbox").remove();
			//$("<h1/>").html($(this).attr("rev").split("|")[0]).appendTo($("#cboxContent", "#colorbox"));
			//$("<p/>").addClass("subTitle").html($(this).attr("rev").split("|")[1]).appendTo($("#cboxContent", "#colorbox"));
			//Cufon.replace('#cboxContent h1', {fontWeight: 400, hover: true, fontFamily: 'MetaPlusCE'});
			$("a.btn3").live("click", function(e){
				e.preventDefault();
				$.fn.colorbox.close();
			});
		},
		onComplete: function() {
			$("#cboxLoadedContent", "#colorbox").addClass("formTitle2");
			$.fn.colorbox.resize();
			//$('input[type="checkbox"]:not(.noReplace)').checkbox({ cls:'jquery-checkbox'});
			//$('input[type="radio"]:not(.noReplace)').checkbox({ cls:'jquery-radio'});
		},
		onClosed: function() {
			//document.location = "/clanky";
			document.location = $(this).attr('rev');
		}
	});



/* Other
------------------------------------------------------------------------------*/

	$(".newCat").click(function() { $('#newCategories').toggle() });

	$(".tabs", "#content").each(function() {
		var tab = this;
		$(".tab", tab).hide();
		if(window.location.hash) {
			var tabId = window.location.hash.split("#")[1];
			$("#" + tabId).show();
			$(".tabsNavi li a[href$=#" + tabId + "]", tab).addClass("active");

		} else {
			$(".tab:first", tab).show();
			$(".tabsNavi li:first a", tab).addClass("active");
		}

		$(".tabsNavi a", tab).click(function (event) {
			event.preventDefault();
			$(".tabsNavi .active", tab).removeClass("active");
			$(this).addClass("active");
			$(".tab", tab).hide();
			$($(this).attr("href")).show();
		});

	});


	if ($("#article_perex").length) {
		$("#article_perex").bind("keyup", function() {
			if (this.value.length <= 230) {
				$("label[for=" + this.id + "]").find("b").html(230 - this.value.length);
			}
			if (this.value.length > 230) {
				this.value = this.value.substring(0,229);
			}
		});
	}

	if ($("#article_content").length) {
		$("#article_content").bind("keyup", function() {
			//if (this.value.length <= 600) {
				//console.log($("label[for=" + this.id + "]"));
				$("label[for=article_text]").find("b").html(Math.max(600 - this.value.length, 0));
			//}
			/*
			if (this.value.length > 230) {
				this.value = this.value.substring(0,229);
			}*/
		});
	}


	/* Rating
	------------------------------------------------------------------------------*/
	bindRating();
	
});


var gallInit = false;


$(function () {
    
    if (!gallInit) {
    /* Slideshows
    ------------------------------------------------------------------------------*/

        var indexSlideshow = $("#slideshow");
        var indexSlideshowSlides = $("#slides", "#slideshow");
        var indexSlideshowThumbs = $(".thumbs a", "#slideshow");
        if (indexSlideshow[0]) {
            indexSlideshow.animate({opacity: 1.0}, 4000).animate({top: "122px"}, 800);
            indexSlideshowSlides.cycle({
                fx: "fade",
                speed: "500",
                timeout: 4000
            });
            indexSlideshowThumbs.click(function(event){
                event.preventDefault();
                var slideNumber = parseFloat($(this).attr("rel"));
                indexSlideshowSlides.cycle(slideNumber);
            });
        }

        var recipeSlideshow = $(".slideImages", "#content");
        var recipeSlidesLength = recipeSlideshow.children().length;
        if (recipeSlideshow[0] && recipeSlidesLength > 1) {
            recipeSlideshow.cycle({
                fx: "fade",
                speed: "500",
                timeout: 40000000,
                prev: '.prev',
                next: '.next',
                pager: '.photos',
                pagerAnchorBuilder: function pagerFactory(idx, slide) {
                        return '<a class="replace" href="" title="">' + (idx + 1) + '&middot;<span></span></a>';
                    }
            });
        }
        if (recipeSlidesLength == 1) {
            recipeSlideshow.parent().parent().addClass("slideshowOff");
        }

        gallInit = true;
        
    }
});


