(function($) {
	$.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(!$.support.opacity)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(!$.support.opacity)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeTo = function(speed,to,callback) {
		return this.animate({opacity: to}, speed, function() {
			if (to == 1 && jQuery.browser.msie)
				this.style.removeAttribute('filter');
			if (jQuery.isFunction(callback))
				callback();
		});
	};
})(jQuery);

$(function() {

    /* Vyhledavaci policko
    -------------------------------------------------------------- */
    var $searchInput = $('input[name=search]');
	var $searchValue = $searchInput.val();

	$searchInput.focus(function(){
		if ((this.value == '') || (this.value == $searchValue)) {
			$(this).val('');
		}
	});

	$searchInput.blur(function(){
		if (this.value == '') {
			$(this).val($searchValue);
		}
	});

    /* Rozbalovatka
    -------------------------------------------------------------- */
	$(".doc-block-subcontainer h3").toggle(function(){
	$(this).attr("class", "minus");
	}, function () {
	$(this).attr("class", "plus");
	});


   $(".doc-block-subcontainer h3").click(function(){
	$(this).next().removeClass("hide1");
	$(this).next().addClass("doc-text");
    $(this).next().slideToggle("slow,");

   });


    /* vsem obrazkum co jsou v acku pridat rel jquerz ..
    -------------------------------------------------------------- */
    var $a = $("a").not("a.nolightbox").find("img");
    $a.parent("a").attr("rel", "lightbox-fotos");


    $(".hp-news > .image > a").removeAttr("rel");



    /* odstraneni posledniho .hr ve vyhledavani
    -------------------------------------------------------------- */
    if ($("div.hr").size())
    {
        $("div.hr:last").remove();
    }

    /* Colorbox
    -------------------------------------------------------------- */
    if ($("a[rel='lightbox-fotos']").size())
    {
        $("a[rel='lightbox-fotos']").colorbox({current: "obrázek {current} z {total}"});
    }



    /* Tooltip
    -------------------------------------------------------------- */
    $("div[id^=tooltip]").hide();

    $('area').hover(function(e) {
        //$('body').append(html).children('#info').show();
		//$('#info').css('top', e.pageY + -20).css('left', e.pageX + 40);

        var html = '<div id="fvAjax"></div>';
        $('body').append(html).children('#fvAjax').hide();


        var href   = $(this).attr("href");
        var number = href.match(/\/(\d+).html/);
        tooltipID  = number[1];

        if (number[1] == '')
        {
            $("div[id^=tooltip]").hide();
            $('#fvAjax').remove();
            return;
        }

        //alert("#tooltip"+tooltipID);

        $('#fvAjax').append($("#tooltip"+tooltipID).html());
        $('#fvAjax').css('top', e.pageY + -20).css('left', e.pageX + 40).fadeIn(300);


	}, function() {
        $("#tooltip"+tooltipID).hide();
        $('#fvAjax').remove();
	});


    $('area').mousemove(function(e) {
        $('#fvAjax').css('top', e.pageY + -20).css('left', e.pageX + 40);
    });


});
$(document).ready(function(){
    if($("#message").length){
        $.colorbox({inline:true, href:"#message", opacity: 0, height: '500px', width: '600px'});
        $(document).bind('cbox_complete', function(){
          //$("#colorbox").css("padding-right","0");
        })
    }
})



/* Ankety
------------------------------------ */
var poll = {};
poll.vote = function(pollID)
{
    var allAnswers,
        allAnswersArray,
        formData,
        canSave,
        OBJ;

    OBJ = this;
    formData = $("#voting").serialize();
    canSave  = true;


    $(".answer-vote-content").each(function(index){

        var obj = $(this);

        if (!obj.find(":radio").is(':checked'))
        {
            canSave = false;
            alert("Vyplňte prosím všechny otázky");
            return false;
        }


    });


    if (canSave === false)
        return false;


    // ulozeni
    $.post('/!/voting.php', {postData: formData, pollID: pollID}, function(response){

        //alert(response);
        OBJ.finish();

    });



};


poll.finish = function() {

    $("#voting").remove();
    $("#finish").css("display", "block");

};

poll.feedback = function(formObject) {

    var jmeno = $("input[name=jmeno]", formObject);
    var email = $("input[name=email]", formObject);
    var telefon = $("input[name=telefon]", formObject);
    var formData = $(formObject).serialize();

    if (jmeno.val() == '' || email.val() == '')
    {
        alert('Vyplňte prosím povinné položky (Jméno a E-mail)');
        return false;
    }
    else if (email.val().indexOf("@") == -1)
    {
        alert('Emailová adresá není ve správném formátu');
        return false;
    }

    /* Odeslani formulare
    ------------------------------- */
    $.post('/!/voting.feedback.php', {postData: formData}, function(response){
        alert("Děkujeme za odeslání formuláře");
        $.colorbox.close();
    });




}
