var isAanimating = false;

function showIt(selector)
{
    if(typeof ClickTaleExec=='function')
        ClickTaleExec("showIt("+selector+")");
    $(selector).show();
}


function hideIt(selector)
{
    if(typeof ClickTaleExec=='function')
        ClickTaleExec("hideIt("+selector+")");
    $(selector).hide();
}

function makeScrollable(parent, prev, next, window, counter)
{
    $(parent+" .scrollable .items").css("width", "2000em");
    $(parent+" .scrollable").css("overflow", "hidden");
    $(next).click(function(){ $(counter).text(scrollElement(window, "next"));});
	$(prev).click(function(){ $(counter).text(scrollElement(window, "prev"));});
}

function BFService()
{
    service_date = $("form").serialize();
    BFServiceDate(service_date);
    BFServicePayment(service_date);
}

function BFServiceDate(service_date)
{
    params=service_date;
    $.ajax({
        type: "POST",
        url: "/inc_show_page.php?page=inc_booking_service_form_date",
        data: params,
        success: function(html)
        {
            $("#service_date").html(html);
        }
    });
}

function BFServicePayment(service_date)
{
    params=service_date;
    $.ajax({
        type: "POST",
        url: "/inc_show_page.php?page=inc_booking_service_payment",
        data: params,
        success: function(html)
        {
            $("#payment_list").html(html);
        }
    });
}



function checkNavigatorCheckbox(el)
{
    if($(el).is(':checked'))
    {
        $(el).attr('checked', false);
        clickedCategory = $(el).val();
        if (clickedCategory == "hotels")
        {
            $("#cat_7").attr('checked', false);
            $("#cat_8").attr('checked', false);
            $("#cat_9").attr('checked', false);
            $("#cat_10").attr('checked', false);
            $("#cat_13").attr('checked', false);
            $("#cat_14").attr('checked', false);
            $("#cat_16").attr('checked', false);
        }
        if (clickedCategory == "pensions")
        {
            $("#cat_11").attr('checked', false);
            $("#cat_12").attr('checked', false);
        }
        if (clickedCategory == "apartments")
        {
            $("#cat_15").attr('checked', false);
            $("#cat_17").attr('checked', false);
            $("#cat_19").attr('checked', false);
        }

        $(el).parent().removeClass("active");
    } else
    {
        clickedCategory = $(el).val();
        if (clickedCategory == "hotels")
        {
            $("#cat_7").attr('checked', true);
            $("#cat_8").attr('checked', true);
            $("#cat_9").attr('checked', true);
            $("#cat_10").attr('checked', true);
            $("#cat_13").attr('checked', true);
            $("#cat_14").attr('checked', true);
            $("#cat_16").attr('checked', true);
        }
        if (clickedCategory == "pensions")
        {
            $("#cat_11").attr('checked', true);
            $("#cat_12").attr('checked', true);
        }
        if (clickedCategory == "apartments")
        {
            $("#cat_15").attr('checked', true);
            $("#cat_17").attr('checked', true);
            $("#cat_19").attr('checked', true);
        }

        $(el).attr('checked', true);
        $(el).parent().addClass("active");
    }
}

function scrollElement(elName, direction)
{
    if (isAanimating == false)
    {
        elName = $("#"+elName).find(".items");
        items = $(elName).children().size();
        currentItem = ($(elName).position().left)*(-1)/$(elName).children().width();
        currentItem = currentItem+1;
        if (direction == "next")
        {
            if (currentItem < items)
            {
                isAanimating = true;
                scrollAmount = ($(elName).position().left)*(-1)+($(elName).children().width()+parseInt($(elName).children().css("padding-left"),10));
                currentItem = currentItem+1;
                
                $(elName).animate({left:-scrollAmount}, 300, function()
                {
                    isAanimating = false;
                });
            }
        }
        else
        {
            if (currentItem > 1)
            {
                isAanimating = true;
                scrollAmount = ($(elName).position().left)*(-1)-($(elName).children().width()+parseInt($(elName).children().css("padding-left"),10));
                currentItem = currentItem-1;
                $(elName).animate({left:-scrollAmount}, 300, function()
                {
                    isAanimating = false;
                });
            }
        }
        currentItem = Math.floor(currentItem);
        return currentItem;
    }
}

function showPricelist(el, scroll, currency)
{
    if ($("#pricelist_per_day").val() == 0)
        ajaxpage="inc_detail_pricelist_new_merged";
    else
        ajaxpage="inc_detail_pricelist_new";
    if (!currency)
        currency = "CZK";
    send = $(el).parents().filter('form').serialize();
    if (scroll)
        send = send+"&scroll="+scroll+"&currency="+currency;     
    $.ajax({
            url: "/inc_show_page.php?page="+ajaxpage,
            cache: false,
            type: "post",
            data: send,
                success: function(response){
                    $("#pricelist-window").html(response);
                    $('input.date-picker').datepicker({
                        dateFormat: 'dd.mm.yy',
                        minDate: 0,
                        changeMonth: true, 
                        changeYear: true
                    });
                    
                    $('img.date-picker').click(function(){
                        $(this).prev().datepicker('show');
                    });
                    $("#pricelist_from").change(function()
                    {
                        showPricelist($(this));
                    });
                    $("#pricelist_next").click(function()
                    {
                        showPricelist($(this), "1");
                        return false;
                    });
                    $("#pricelist_prev").click(function()
                    {
                        showPricelist($(this),"-1");
                        return false;
                    });
                    $("#setCurrency .change_currency").click(function()
                    {
                        showPricelist($(this), "0", $(this).attr("id"));
                        return false;
                    });
                    
                }
        });
}



function ProcessBooking()
{
    send = $(document).find('form').serialize();
    //alert(send);
    $.ajax({
        url: "/inc_show_page.php?page=inc_show_payment",
        cache: false,
        type: "post",
        data: send,
            success: function(response){
                //alert(response);
                $("#payment_result").html(response);
                $("input").hover(
                    function()
                	{
                        if ($(this).attr("paymentTitle") != "" && $(this).attr("paymentTitle") != null)
                        {
                            height = $(this).height();
                            width = $(this).position().left+$(this).width()+20;
                            //width = width+150;
                            topY = $(this).offset().top;   
                            $(this).after("<div id='hoverHint' class='hide' style='left:"+width+"px;top:"+topY+"px;'><img src='/layout/hint-arrow.png'>"+$(this).attr("paymentTitle")+"</div>");
                            $("#hoverHint").fadeIn("slow");
                        }
                    },
                    function()
                	{
                        if ( $(this).next().attr("id") == "hoverHint")
                        {
                            $(this).next().hide();
                            $(this).next().remove();
                        }
                            
                    }       
                );
                $("label").hover(
                    function()
                	{
                        if ($(this).attr("paymentTitle") != "" && $(this).attr("paymentTitle") != null)
                        {
                            height = $(this).height();
                            width = $(this).position().left+$(this).width()+20;
                            //width = width+150;
                            topY = $(this).offset().top;
                            $(this).after("<div id='hoverHint' class='hide' style='left:"+width+"px;top:"+topY+"px;'><img src='/layout/hint-arrow.png'>"+$(this).attr("paymentTitle")+"</div>");
                            $("#hoverHint").fadeIn("slow");
                        }
                    },
                    function()
                	{
                        if ( $(this).next().attr("id") == "hoverHint")
                        {
                            $(this).next().hide();
                            $(this).next().remove();
                        }
                            
                    }       
                );
                $(".processBooking").click(function()
                {
                    ProcessBooking();
                });                
            }
    });
    $.ajax({
        url: "/inc_show_page.php?page=inc_calculation",
        cache: false,
        type: "post",
        data: send,
            success: function(response){
                $("#calculation_result").html(response);
            }
    });
    
}

function newChatWindow(page,params,width,height) {
  if(width<1) {width=500;}
  if(height<1) {height=600;}
  window.open("/online-chat/","CHAT","height="+height+",width="+width+",scrollbars=yes,location=no,resizable=yes,left=300,top=100");
}

function zobraz_transfer_other(id)
{
    if (id == 99)
        $("#transfer_other").show();
    else
        $("#transfer_other").hide();
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
    window.open(theURL,winName,features);
}

function zobrazSkryj_pos(idecko,action){
//alert(idecko);
div = document.getElementById(idecko);
if (div)
{
    var el=document.getElementById(idecko).style; 
    if (action=='hide'){ el.display='none';}
    if (action=='show') {el.display='block';}
}
}


$(document).ready(function(){

jQready();
    
});
    
    
    //replaceChecks();
    function jQready() {
    
    if (typeof ClickTaleExec == 'function') {
        ClickTaleExec('jQready()');
    }
         
    var inputDatePickers = $('input.date-picker'); 
    inputDatePickers.datepicker({
        dateFormat: 'dd.mm.yy',
        minDate: 0,
        firstDay: 1,
        changeMonth: true, 
        changeYear: true
    });
    inputDatePickers.next().click(function(){
        $(this).prev().datepicker('show');
    });
    
    
    $('#my-accommodation-button').click(function()
    {
        $('#my-accommodation').fadeToggle();
        return false;
    });
                 
    $('#date-to-button').click(function()
    {
        $('#date-to').datepicker('show');
    }); 
    
    /*
    if ( !$.browser.msie && ($.browser.version != 6) ) {
        $('select').selectmenu({
            maxHeight: 200,
            style:'dropdown'
        });
    } */
    
	
	
	/*
    $('.availability_free').click(function()
	{
        return false;
    });
	$('.availability_occupied').click(function()
	{
        return false;
    });
	$('.availability_free').tipsy({gravity: 's', status:'free'});
	$('.availability_occupied').tipsy({gravity: 's', status:'occupied'});
	*/
    
	
	var mainSearchCheckboxes = $('#mainSearch input.nav-checkbox'); 
    mainSearchCheckboxes.each(function() {
        if($(this).is(':checked'))
        {
            $(this).parent().addClass("active");
        } else
        {
            $(this).parent().removeClass("active");
        }
    });
     
    var clickCheckbox = false;
    mainSearchCheckboxes.click(function (event) {
        clickCheckbox = true;
        clickedCategory = $(this).val();
        if($(this).is(':checked'))
        {
            if (clickedCategory == "hotels")
            {
                $("#cat_7").attr('checked', true);
                $("#cat_8").attr('checked', true);
                $("#cat_9").attr('checked', true);
                $("#cat_10").attr('checked', true);
                $("#cat_13").attr('checked', true);
                $("#cat_14").attr('checked', true);
                $("#cat_16").attr('checked', true);
            }
            if (clickedCategory == "pensions")
            {
                $("#cat_11").attr('checked', true);
                $("#cat_12").attr('checked', true);
            }
            if (clickedCategory == "apartments")
            {
                $("#cat_15").attr('checked', true);
                $("#cat_17").attr('checked', true);
                $("#cat_19").attr('checked', true);
            }
            $(this).parent().addClass("active");
        } else
        {
            if (clickedCategory == "hotels")
            {
                $("#cat_7").attr('checked', false);
                $("#cat_8").attr('checked', false);
                $("#cat_9").attr('checked', false);
                $("#cat_10").attr('checked', false);
                $("#cat_13").attr('checked', false);
                $("#cat_14").attr('checked', false);
                $("#cat_16").attr('checked', false);
            }
            if (clickedCategory == "pensions")
            {
                $("#cat_11").attr('checked', false);
                $("#cat_12").attr('checked', false);
            }
            if (clickedCategory == "apartments")
            {
                $("#cat_15").attr('checked', false);
                $("#cat_17").attr('checked', false);
                $("#cat_19").attr('checked', false);
            }
            $(this).parent().removeClass("active");
        }
    });  
    
    
    $('.checkable').click(function () {
        if (clickCheckbox != true)
        {
            checkNavigatorCheckbox($(this).find('input[type="checkbox"]'));
        } else
        {
            clickCheckbox = false;            
        }
    }); 
    
	/*
    $('.menu-checkbox').each(function() {
        if($(this).is(':checked'))
        {
            $(this).parent().removeClass("silverBtnBorder");
            $(this).parent().addClass("blueBtnBorder");
        } else
        {
            $(this).parent().removeClass("blueBtnBorder");
            $(this).parent().addClass("silverBtnBorder");
        }
    });

    $('.menu-checkbox').click(function () {
        if($(this).is(':checked'))
        {
            $(this).parent().removeClass("silverBtnBorder");
            $(this).parent().addClass("blueBtnBorder");
        } else
        {
            $(this).parent().removeClass("blueBtnBorder");
            $(this).parent().addClass("silverBtnBorder");
        }
    });
    */
    
    var navigatorButton = $("#navigator-left li.navigator-button");
    navigatorButton.click(function () {
        navigatorButton.removeClass("active");
        navigatorButton.removeClass("navigator-button-active");
        $("#categorymenu .categorymenus").hide();
        $(this).addClass("active");
        $(this).addClass("navigator-button-active");
        btnId = $(this).attr("id");
        switch(btnId) {
            case "nav-accommodation" :
                $("#navigator-pointer").css("top","32px");
                $("#navigator-main .search-windows").hide();
                $("#navigator-search").show();
                $("#accommodation-category").show();
            break;
            case "nav-services" :
                $('#navigator-pointer').css("top","117px");
                $("#navigator-main .search-windows").hide();
                $("#navigator-services-search").show();
                $("#service-category").show();
            break;
            case "nav-transfers" :
                $("#navigator-pointer").css("top","202px");
                $("#navigator-main .search-windows").hide();
                $("#navigator-transfers-search").show();
                $("#accommodation-category").show();
            break;
            default : $("#navigator-pointer").css("top","0px");
        }                
        return false;
    });
    
    var leftMenuButtons = $('#submenu li.main-button');
    leftMenuButtons.click(function () {
        leftMenuButtons.removeClass("no-margin");
        leftMenuButtons.removeClass("blueBtn");
        $(this).addClass("blueBtn");
        $(this).addClass("no-margin");
        btnId = $(this).attr("id");
        $('#submenu li.main-button-content').each(function() {
            btnContentId = btnId+'-content';
            if($(this).is(':visible') && $(this).attr('id') != btnContentId)
            {
                $(this).slideUp("slow");
            }
        });
        $('#'+btnId+'-content').slideDown("slow");
        return false;
    });
    
    var contentFilterTrigger = $('#content .filter-trigger');
    var Filter = $("#filter");
    contentFilterTrigger.click(function () {
        if(Filter.is(':visible'))
        {
            contentFilterTrigger.attr("src","/layout/icons/filter-down.gif");
        } else
        {
            contentFilterTrigger.attr("src","/layout/icons/filter-up.gif");
        }
        Filter.slideToggle("slow");
        return false;
    });
    
    var hotelMenuItems = $('#hotel-menu li.hotel-menu-item');
    hotelMenuItems.click(function () {
        id = $(this).attr("id").split("-");
        clickHotelMenu(id[0]);
        if (typeof ClickTaleExec == 'function') {
            ClickTaleExec('clickHotelMenu('+id[0]+')');
        }
        return false;
        
    });
    

    //IKONY WIFI/SNIDANE/VZDALEDNOST U UBYTOVANI
    $(".hoverInfoIcons").mouseover(function() {
        $(this).parent().find('.hoverInfoIcons').each(function() {
            var src1 = $(this).attr("src").replace("-hover", "");
            $(this).attr("src", src1);
        });
        
        thisId = $(this).attr("class").split(/\s+/);
        //alert(thisId[1]);
        
        $(this).parent().next().find(".distance-text-info").hide();
        $(this).parent().next().find("."+thisId[1]+"Text").show(); 
        //alert(thisId);
        //alert($('#'+thisId+' ~ siblings').attr("id"));
        //alert(textId);
        var src = $(this).attr("src").match(/[^\.]+/) + "-hover.gif";
        $(this).attr("src", src); 
    });    

    $('#content .moreRooms').click(function () {
        $(this).parent().find('.more').toggle();
        return false;
    });
    
	$("#rooms table .moreRooms").click(function()
	{
        $(this).parent().parent().parent().next().toggle();
        return false;
    });
	
	//$(document).pngFix();
	
	//ODSTRANIT!!!!!!
    //$(".hideByJS").hide();
	//$(".onlyJS").show();

    tabid = location.href.split('#');
    if (tabid[1])
    {
        hotelMenuItems.removeClass("active-menu");
        $("#"+tabid[1]+"-menu").addClass("active-menu");
        $(".detail-item").hide();
        $("#"+tabid[1]).show();
    }

	/*
	$("input.ui-state-default").focus(function()
	{
        $(this).addClass("ui-state-active");
        if ($(this).attr("title") != "")
        {
            height = $(this).height();
            width = $(this).position().left+$(this).width()+20;
            //width = width+150;
            topY = $(this).offset().top;
            $(this).after("<div id='hoverHint' class='hide' style='left:"+width+"px;top:"+topY+"px;'><img src='/layout/hint-arrow.png'>"+$(this).attr("title")+"</div>");
            $("#hoverHint").fadeIn("slow");
        }
    });
	$("input.ui-state-default").blur(function()
	{
        $(this).removeClass("ui-state-active");
        if ( $(this).next().attr("id") == "hoverHint")
        {
            //$(this).next().hide("slow", function() { $(this).remove()});
            $(this).next().hide();
            $(this).next().remove();
        }
            
    });
    */

    $("img.pictogram").hover(
        function()
    	{
            if ($(this).attr("alt") != "")
            {
                height = $(this).height();
                width = $(this).position().left+$(this).width();
                //width = width+150;
                topY = $(this).offset().top;
                $(this).after("<div id='hoverHint' class='hide' style='left:"+width+"px;top:"+topY+"px;'><img src='/layout/hint-arrow.png'>"+$(this).attr("alt")+"</div>");
                $("#hoverHint").fadeIn("slow");
            }
        },
        function()
    	{
            if ( $(this).next().attr("id") == "hoverHint")
            {
                //$(this).next().hide("slow", function() { $(this).remove()});
                $(this).next().hide();
                $(this).next().remove();
            }
                
        }       
    );
    
    $("img.pictogram_checkbox").hover(
        function()
    	{
            if ($(this).next().attr("title") != "")
            {
                height = $(this).height();
                width = $(this).offset().left+$(this).width();
                //width = width+150;
                topY = $(this).offset().top;
                $(this).after("<div id='hoverHint' class='hide' style='left:"+width+"px;top:"+topY+"px;'><img src='/layout/hint-arrow.png'>"+$(this).next().attr("title")+"</div>");
                $("#hoverHint").fadeIn("slow");
            }
        },
        function()
    	{
            if ( $(this).next().attr("id") == "hoverHint")
            {
                //$(this).next().hide("slow", function() { $(this).remove()});
                $(this).next().hide();
                $(this).next().remove();
            }
                
        }       
    );
    
	
    $("a[rel='singleBox']").colorbox({maxWidth:"100%", maxHeight:"100%",scalePhotos:true,rel:'nofollow'});
    $("a[rel='gallery']").colorbox({maxWidth:"100%", maxHeight:"100%",scalePhotos:true});
    //$("a[rel='gallery']").click(function(){alert('ahoj');})

    /*
    $(".linkInside").click(function()
    {
        window.location = $(this).children().attr("href");
    });
    */    
  
    
    
    //var ElServicePictograms = $(".servicePictogram");
    
    $('.servicePictogram').each(function(index)
    {
       
        pict_id = $(this).attr("id");
        pict_title = $(this).attr("title");
        div_width = $(this).attr("checkbox_width");
        div_height = $(this).attr("checkbox_height");
        if($(this).is(':checked'))
            predpona = "";
        else
            predpona = "no_";
            
        
        obsah = '<div style="position:relative; width:'+div_width+'px; height:'+div_height+'px; margin:1px;" class="left"><div class="left pointer" style="position:absolute; margin:2px;"><img class="servicePictogram" checkbox_id="'+pict_id+'" alt="'+pict_title+'" src="/img/pictograms/'+predpona+pict_id+'.gif">';
        //if (pict_title)
            //obsah=obsah+'<div style="position:absolute;top:50px; left:0px;width:100%;text-align:center; font-size:11px;">'+pict_title+'</div>'
        obsah = obsah+'</div></div>';
        $(this).after(obsah);
        $(this).hide();
    });
    
    $('.servicePictogram').hover(
        function()
    	{
            //alert($(this).next().attr("id"));
            if ($(this).attr("alt") != "")
            {
                height = $(this).height();
                width = $(this).position().left+$(this).width();
                //width = width+150;
                topY = $(this).position().top;
                $(this).after("<div id='hoverHint' class='hide' style='z-index:10000;left:"+width+"px;top:"+topY+"px;'><img src='/layout/hint-arrow.png'>"+$(this).attr("alt")+"</div>");
                $("#hoverHint").fadeIn("slow");
            }
        },
        function()
    	{
            if ( $(this).next().attr("id") == "hoverHint")
            {
                //$(this).next().hide("slow", function() { $(this).remove()});
                $(this).next().hide();
                $(this).next().remove();
            }
                
        }       
    );
    
    
    //var ElServicePictograms = $(".servicePictogram");
    $(".servicePictogram").click(function()
    {
        id = $(this).attr("checkbox_id");
        if($("#"+id).is(':checked'))
        {
            $("#"+id).attr('checked', false);
            $(this).attr("src", "/img/pictograms/no_"+id+".gif");
            $(this).parent().removeClass("white");
        } else
        {
            $("#"+id).attr('checked', true);
            $(this).attr("src", "/img/pictograms/"+id+".gif");
            $(this).parent().addClass("white");
        }
    });
    
    // close chat
    $("#site-bottom-bar img.close").click(function()
    {                                                   
        $.ajax({
            type: "POST",
            url: "/inc_show_page.php?page=ajax_chat_disable",
            success: function(html)
            {
                $("#site-bottom-bar .submenuBox").hide('slow');
            }
        });
                
    })  
    
}    
    

function clickHotelMenu(thisEl)
{
    $('#hotel-menu li.hotel-menu-item').removeClass("active-menu");
    $("#"+thisEl+"-menu").addClass("active-menu");
    //alert("#"+thisEl+"-menu");
    //id = $(thisEl).attr("id").split("-");
    //alert(id[0]);
    $("#content .detail-item").hide();
    $("#"+thisEl).fadeIn();
    
    if(thisEl == "map"){   
        initialize();
        google.maps.event.trigger(map, 'resize');
        map.setZoom( map.getZoom() );
    }
    
    return false;
}
