
Cufon.replace('#topnav .sub a, .princess #top a, #zn1 .sub a, #zn2 .sub a, #b label.booking', { hover: true })
Cufon.replace('#mainMenu a, h1, h2, h3, .newsModTitle, #body_0 #content, #feedback', { hover: true, textShadow: '1px 1px 1px #000' })
Cufon.replace('#footer ul', {hover: true, letterSpacing: '-1px'})

$(document).ready(function () {
    setPackagesLink()
    resize();
    cycle();
    carousel();
    //pageCurl();
    news();
    events();
    packages();
    adminUsers();

    showHideImg()
    setBookingHeight()

//    $('#top').click(function (evt) {
//        if ($(evt.target).attr("id") == "top") {
//            closeBooking();
//        }
//    })

});

$(window).resize(function () {
    resize();
    setBookingHeight();
    if ($("#body_0").length == 0) {
        moveDDLPersons() //needed for quickbooking function on LHBQuickBooking.js
    }
});

function setPackagesLink() {
    $("#subMenuNotSel21_1 > a").attr("href", "javascript:openPackages('de')")
    $("#subMenuNotSel21_2 > a").attr("href", "javascript:openPackages('en')")
    $("#subMenuNotSel21_3 > a").attr("href", "javascript:openPackages('fr')")
}

function Y2K(the_date) {
    if (the_date < 1000) {
        the_date = the_date + 1900;
    } return the_date;
}

function openSinglePackage(packageId, lan) {
        $("#bookingIframe").attr("src", "")
        $("#packagesBox").removeClass("hide");
        $("#pckgsSettings").addClass("hide")
        $.post("/ajax/getPackages.aspx?idLan=" + lan,
        {
            packageId: packageId

        },
        function (data) {
            $("#pckgsWrapper").html(data)
            Cufon.replace('#pckgsWrapper h2.white', { hover: true, textShadow: '1px 1px 1px #000' })
            showDetails(packageId)
        }
    );

        openBookingContainer()
        
}

function openPackages(lan) {
    if ($("#bookingContainer").css("display") == "none") {
        $("#bookingIframe").attr("src", "")
            $("#pckgToDate").datepicker({ dateFormat: 'dd M yy', minDate: 1, maxDate: '+12M', onSelect: function () { refreshPackages(lan) } });
            $("#pckgFromDate").datepicker({
                dateFormat: 'dd M yy',
                minDate: 0,
                maxDate: '+12M',
                onSelect: function (selectedDate) {
                    var arrDate = $("#pckgFromDate").datepicker("getDate")
                    arrDate.setDate(arrDate.getDate() + 1)
                    $("#pckgToDate").datepicker("option", "minDate", arrDate)
                    $("#pckgToDate").datepicker("setDate", arrDate)
                    refreshPackages(lan)
                }
            });

            $("#slider-days").slider({
                range: true,
                min: lowerNights,
                max: higherNights,
                values: [lowerNights, higherNights],
                slide: function (event, ui) {
                    $("#days").html(+ui.values[0] + ' - ' + ui.values[1]);
                    $("#daysFrom").val(ui.values[0])
                    $("#daysTo").val(ui.values[1])
                },
                stop: function () {
                    refreshPackages(lan)
                }
            });
            $("#days").html($("#slider-days").slider("values", 0) + ' - ' + $("#slider-days").slider("values", 1));
            $("#slider-price").slider({
                range: true,
                min: LowerPackagePrice,
                max: HigherPackagePrice,
                values: [LowerPackagePrice, HigherPackagePrice],
                slide: function (event, ui) {
                    $("#price").html(ui.values[0] + ' - ' + ui.values[1]);
                    $("#priceFrom").val(ui.values[0])
                    $("#priceTo").val(ui.values[1])
                },
                stop: function () {
                    refreshPackages(lan)
                }
            });
            $("#price").html($("#slider-price").slider("values", 0) + ' - ' + $("#slider-price").slider("values", 1));
            $("#packagesBox").removeClass("hide");
            $("#pckgsSettings").removeClass("hide")
        refreshPackages(lan)
        openBookingContainer()
    } else {
        $("#bookingShadow").fadeOut();
        $("#bookingContainer").slideFadeToggle("fast")
    }
}
function refreshPackages(lan) {
    var DateA = $("#pckgFromDate").datepicker("getDate")
    var DateD = $("#pckgToDate").datepicker("getDate")
    var sDateA = ""
    var sDateD = ""
    var arrivalYear = Y2K(DateA.getYear())
    var departureYear = Y2K(DateD.getYear())
    if (DateA != null)
        var sDateA = arrivalYear + "/" + (DateA.getMonth() + 1) + "/" + DateA.getDate()
    if (DateD != null)
        var sDateD = departureYear + "/" + (DateD.getMonth() + 1) + "/" + DateD.getDate()
    $.post("/ajax/getPackages.aspx?idLan=" + lan,
        {
            daysFrom: $("#daysFrom").val(),
            daysTo: $("#daysTo").val(),
            priceFrom: $("#priceFrom").val(),
            priceTo: $("#priceTo").val(),
            startDate: sDateA,
            endDate: sDateD,
            endDateValue: $("#pckgToDate").val()

        },
        function (data) {
            $("#pckgsWrapper").html(data)
            Cufon.replace('#pckgsWrapper h2.white', { hover: true, textShadow: '1px 1px 1px #000' })
        }
    );
}
function openBookingContainer() {
    if ($("#bookingContainer").css("display") == "none") {
        $("#mainnav").addClass("hide");
        $("#bookingShadow").fadeIn(function () {
            if (jQuery.browser.msie) { this.style.removeAttribute("filter") }
        });
        $("#topnav").css("-moz-border-radius-bottomright", "0")
        $("#topnav").css("-moz-border-radius-bottomleft", "0")
        $("#topnav").css("-webkit-border-bottom-left-radius", "0")
        $("#topnav").css("-webkit-border-bottom-right-radius", "0")
        $("#topnav").css("border-bottom-left-radius", "0")
        $("#topnav").css("border-bottom-right-radius", "0")
        $("#bookingContainer").slideFadeToggle("fast")
    }
}
function closeBooking() {
    if ($("#bookingShadow").css("display") != "none") {
        $("#mainnav").removeClass("hide");
        $("#bookingShadow").fadeOut();
        $("#topnav").css("-moz-border-radius-bottomright", "6px")
        $("#topnav").css("-moz-border-radius-bottomleft", "6px")
        $("#topnav").css("-webkit-border-bottom-left-radius", "6px")
        $("#topnav").css("-webkit-border-bottom-right-radius", "6px")
        $("#topnav").css("border-bottom-left-radius", "6px")
        $("#topnav").css("border-bottom-right-radius", "6px")
        $("#bookingContainer").slideFadeToggle("fast")
    }
}
function openBooking(link) {
	_gaq.push(function() {
	  var pageTracker = _gat._getTrackerByName();
	  link = pageTracker._getLinkerUrl(link);
	});
    if ($("#packagesBox").hasClass("hide") != true)
        $("#packagesBox").addClass("hide");
    
    $("#bookingIframe").removeClass("hide")
    $("#bookingIframe").attr("src", link)
    openBookingContainer()    
}

function setBookingHeight() {
    $("#bookingContainer").height("0px")
    $("#bookingContainer").height(($(document).height() - $("#topnav").outerHeight() - 24) + "px");
    $("#bookingHolder").height(($(document).height() - $("#topnav").outerHeight() - 58) + "px");
    $("#bookingShadow").height(($(document).height()) + "px");
}
function showDetails(pkgId) {
    $("#pkgDetails-" + pkgId).slideFadeToggle("slow")

}
function adminUsers() {
    if ($('#floatdiv').length == 0) { //not admin
        $('#pane').jScrollPane();
    } else {
        clearInterval(refreshIntervalId)
        $('#footer').remove()
        $('#canvas').css('overflow', 'auto')
        $('#top').css('top', '38px')
    }
}

function news() {
    if ($('#specialNews').length > 0) {
        $('#specialNews').before('<div id="commPagerNews">')
        .cycle({
            fx: 'fade',
            speed: 400,
            timeout: 5000,
            pager: '#commPagerNews',
            cleartype: true, cleartypeNoBg: true //ie7/8 bug
        });
        $("#commPagerNews").append("&nbsp;")
        $("#commPagerNews a").html("&nbsp;");
        $("#commPagerNews a").click(function() {
            $('#specialNews').cycle('pause');
        })
    }
}

function events() {
    if ($('#specialEvents').length > 0) {
        $('#specialEvents').before('<div id="commPagerEvents">')
        .cycle({
            fx: 'fade',
            speed: 400,
            timeout: 5000,
            pager: '#commPagerEvents',
            cleartype: true, cleartypeNoBg: true //ie7/8 bug
        });
        $("#commPagerEvents").append("&nbsp;")
        $("#commPagerEvents a").html("&nbsp;");
        $("#commPagerEvents a").click(function() {
            $('#specialEvents').cycle('pause');
        })
    }
}

function packages() {
    if ($('#homePackages').length > 0) {
        $('#homePackages').before('<div id="commPagerPackages">')
        .cycle({
            fx: 'fade',
            speed: 400,
            timeout: 5000,
            pager: '#commPagerPackages',
            cleartype: true, cleartypeNoBg: true //ie7/8 bug
        });
        $("#commPagerPackages").append("&nbsp;")
        $("#commPagerPackages a").html("&nbsp;");
        $("#commPagerPackages a").click(function () {
            $('#homePackages').cycle('pause');
        })
    }
}

function pageCurl() {
    if ($('#pageflip').length == 1) {
        $("#pageflip").hover(function() {
            $("#pageflip img , .msg_block").stop()
		    .animate({
		        width: '307px',
		        height: '319px'
		    }, 250);
        }, function() {
            $("#pageflip img").stop()
		    .animate({
                width: '100px',
                height: '104px'
		    }, 220);
            $(".msg_block").stop()
		    .animate({
                width: '100px',
                height: '100px'
		    }, 100);
        });
    }
}

var refreshIntervalId;
var interval = 6000;
if ($('.princess').length != 0) {
    interval = 10000;
}

function cycle() {  
   
    if ($('#cycle a:first').length == 1) {
        var $first = $('#cycle a:first').attr('href');
        $('#loader').fadeIn(200);
        //$('<img src="' + $first + '" />').load(start($first));

        var img = new Image();
        $(img).load(start($first)).attr('src', $first);

    }
    
    function start(src) {
        $('#loader').fadeOut(200);
        $("#cycle").append('<img class="hide active" src="' + src + '" />')
        $("#cycle").find('.hide').fadeIn(400, function() {
            //show the first
            $(this).removeClass('hide');
            //start to load all images
            $('#cycle a').not(':first').each(function() {

                //$('<img src="' + this.href + '" />').load(next);
                var img = new Image();
                $(img).load(next).attr('src', this.href);

            })
        })
    }

    var actions = $('#cycle a').not(':first').length;
    function next() {
        if (--actions) return;
        $('#cycle a').not(':first').each(function() {
            $("#cycle").append('<img src="' + this.href + '" class="hide" />');
        })


        if ($('.princess').length == 0) {
            refreshIntervalId = setInterval(function () { imgsCarousel('nxtcycle', false); }, interval); //setInterval("imgsCarousel('nxtcycle')", interval);

        }

        cycleBtnTrigger();
        //resize all appended images
        resize();
        
    }   
    
    //resize the first loaded image    
    resize(); 
  
}

function cycleBtnTrigger() {
    $('.btncycle').fadeIn(300).one('click', function() {
        clearInterval(refreshIntervalId)
        imgsCarousel($(this).attr('id'), true)
    });

//    $(document).keydown(function(e) {
//        if (e.keyCode == 37) { //left
//            $("#prvcycle").trigger('click').addClass('trigged');
//            return false;
//        }
//        if (e.keyCode == 39) { //right
//            $("#nxtcycle").trigger('click').addClass('trigged');
//            return false;
//        }
//    });

//    $(document).keyup(function(e) {
//        if (e.keyCode == 37) {
//            $("#prvcycle").removeClass('trigged');
//            return false;
//        }
//        if (e.keyCode == 39) {
//            $("#nxtcycle").removeClass('trigged');
//            return false;
//       }
//   })

}


function showHideImg() {
    if (($('.welcome').length == 0) && (($('.princess').length == 0))) {

       if ($.browser.msie) {
            $('.inner #canvas').mousemove(function (e) {
                if ($(e.target).attr('id') == 'canvas') {
                    $('#mycursorOn').css('left', e.clientX + 2).css('top', e.clientY + 2).show();
                }
            });
        } else {
            $('#canvas').css('cursor', 'url(/images/view_text_de.png), pointer');
        }

        $('#canvas').click(function (evt) {

            if (evt.target == evt.currentTarget) {

                if ($('#top, #opacity, #content, #ddlPersons').is(':visible')) {
                    $("#top, #opacity, #content, #ddlPersons").fadeOut('slow', function () {

                        if ($.browser.msie) {
                            $('.inner #canvas').mousemove(function (e) {
                                if ($(e.target).attr('id') == 'canvas') {
                                    $('#mycursorOn').hide();
                                    $('#mycursorOff').css('left', e.clientX + 2).css('top', e.clientY + 2).show();
                                }
                            });
                        } else {
                            $('#canvas').css('cursor', 'url(/images/view_image_de.png), pointer');
                        }

                        clearInterval(refreshIntervalId)
                    });
                } else {
                    $("#top, #opacity, #content, #ddlPersons").fadeIn('slow', function () {
                        if (jQuery.browser.msie) { this.style.removeAttribute("filter") }

                        if ($.browser.msie) {
                            $('.inner #canvas').mousemove(function (e) {
                                if ($(e.target).attr('id') == 'canvas') {
                                    $('#mycursorOff').hide();
                                    $('#mycursorOn').css('left', e.clientX + 2).css('top', e.clientY + 2).show();
                                }
                            });
                        } else {
                            $('#canvas').css('cursor', 'url(/images/view_text_de.png), pointer');
                        }


                    });
                }
            }
        })  
    }  
     
}


function imgsCarousel(direction, isButton) {
   
    var $active = $('#cycle img.active');
    var $next;
    if (direction == 'nxtcycle') {
        $next = $active.next().length ? $active.next() : $('#cycle img:first');
    }
    if (direction == 'prvcycle') {
        $next = $active.prev()
        if ($active.attr('src') == $('#cycle img:first').attr('src')) {
            $next = $('#cycle img:last');
        }
    }
          
    $next.removeClass('hide')
    $active.addClass('last-active');

    if ($('.princess').length != 0) {
        $('#princessUl li:visible').fadeOut(function() {
            $('#princessUl li').eq($('#cycle img').index($next)).fadeIn(500)
        });
    }

    $next.css({ opacity: 0.0 })
    .addClass('active')
    .animate({ opacity: 1.0 }, 1000, function () {
        $active.removeClass('active last-active').addClass('hide');

        if (isButton) {
            $('#' + direction).one('click', function () { imgsCarousel(direction, true) })
        }

    })
    
}

function resize() {
    var wh = $(window).height();
    var ww = $(window).width() + 20;
    var minh = $('#container').height();  //600; //height container CSS
    $('#loader').css('top', (wh / 2) - ($('#loader').height() / 2));
    $('#loader').css('left', (ww / 2) - ($('#loader').width() / 2));
    var h = wh < minh ? minh : wh;
    var ratio = 1.6; //1440x900
    var imgw = wh * ratio;
    if (imgw <= ww) {
        $('#cycle img').each(function() {
            $(this).height(ww / ratio);
            $(this).width(ww);
        });
    } else {
        $('#cycle img').each(function() {
            $(this).height(wh);
            $(this).width(imgw);
        });
    }
    var ct = (wh / 2) - (minh / 2);
    var ht = ct < 0 ? 0 : ct;
    ht=ht-90 //TRICK PER TOP BANNER PACKAGE
    $('#container').css('top', ht).fadeIn(1200);
    
    //show footer when possible
    if ((((wh - minh) / 2) >= $('#footer').height())) {
        $('#footer').fadeIn('slow')
    } else {
        $('#footer').fadeOut('fast')
    }
    
}

function carousel() {

    carouselHover()
    carouselBtnTrigger()
}

function carouselHover() {
    
    //x carousel li
    $("#carousel li").hover(function() {
        //menu
        $('.main #zn1 .sub li').eq($('#carousel li').index(this)).addClass('selected')
        Cufon.replace('#zn1 .sub a', { hover: true })
        //images
        $("#carousel").find("li").not(this).find('.cover img').stop().fadeTo('fast', 0.5)
    }, function() {
        //menu
        $('.main #zn1 .sub li').eq($('#carousel li').index(this)).removeClass('selected')
        Cufon.replace('#zn1 .sub a', { hover: true })
        //images
        $("#carousel").find("li").not(this).find('.cover img').stop().fadeTo('normal', 1)
    });

    //x sub li
    $("'.main #zn1 .sub li a").hover(function() {
        $("#carousel li").not("#carousel li:eq(" + $('.main #zn1 .sub li a').index(this) + ")").find('.cover img').stop().fadeTo('fast', 0.5)
    }, function() {
        $("#carousel li").not("#carousel li:eq(" + $('.main #zn1 .sub li a').index(this) + ")").find('.cover img').stop().fadeTo('normal', 1)
    });
    
}

function nextCarousel(maxValue, moveValue) {
    if ($('#nxtmain').is(':visible')) {
        $('#prvmain').fadeIn();
        $("#carousel li .cover img").stop().fadeTo('fast', 0.5)
        if ($('#carousel ul').position().left == ('-' + maxValue)) {
            $('#nxtmain').hide();
        }
        $('#carousel ul').stop().animate({
            left: '-=' + moveValue
        }, 300, function() {
            $("#carousel li .cover img").stop().fadeTo('normal', 1)
            $('#nxtmain').one('click', function() { nextCarousel(maxValue, moveValue) });
        });
    }
}

function previousCarousel(moveValue) {
    if ($('#prvmain').is(':visible')) {
        $('#nxtmain').fadeIn();
        $("#carousel li .cover img").stop().fadeTo('fast', 0.5)
        if ($('#carousel ul').position().left == '-' + moveValue) {
            $('#prvmain').hide();
        }
        $('#carousel ul').stop().animate({
            left: '+=' + moveValue
        }, 300, function() {
            $("#carousel li .cover img").stop().fadeTo('normal', 1)
            $('#prvmain').one('click', function() { previousCarousel(moveValue) });
        })
    }
}

function carouselBtnTrigger() {
    var maxSize = 4;
    var moveValue = 221;
    var size = $('#carousel ul li').size();
    var maxValue = ((size - maxSize)-1) * moveValue;
    if (size > maxSize) {
        
        $('#nxtmain').fadeIn();
        $('#nxtmain').one('click', function() {
            nextCarousel(maxValue, moveValue)
        });
        $('#prvmain').one('click', function() {
            previousCarousel(moveValue)
        });

//        $(document).keydown(function(e) {
//            if (e.keyCode == 37) { //left
//                if ($('#prvmain').is(':visible')) { $("#prvmain").trigger('click').addClass('trigged'); }
//                return false;
//            }
//            if (e.keyCode == 39) { //right
//                if ($('#nxtmain').is(':visible')) { $("#nxtmain").trigger('click').addClass('trigged'); }
//                return false;
//            }
//        });

//        $(document).keyup(function(e) {
//            if (e.keyCode == 37) {
//                $("#prvmain").removeClass('trigged');
//                return false;
//            }
//            if (e.keyCode == 39) {
//                $("#nxtmain").removeClass('trigged');
//                return false;
//            }
//        })
    }
}

jQuery.fn.slideFadeToggle = function (speed, easing, callback) {
    return this.animate({ opacity: 'toggle', height: 'toggle' }, speed, easing, callback);
};

