﻿$(document).ready(function () {
    var w = 0;
    var h = 0;
    var dOn = "";
    var dOff = "";

    //Lightbox
    $('a[rel*=lightbox]').lightBox();
    $('a[rel*=lightbox2]').lightBox();

    //HTML Helper
    $('.sidebarItem:last').attr('style', 'border-bottom:0;');
    $('.articleList li:last').attr('style', 'border-bottom:0;');
    $('#gb_msg').focus(function () { $(this).val(""); });

    //Bild Zentrieren
    $('.centerImage img').each(function () {
        var imgHeight = $(this).height();
        var aHeight = $(this).parent().height();
        if (imgHeight < aHeight) {
            // $(this).css('margin-top', (aHeight - imgHeight) / 2);
        }
    });

    //Navigation ausklapp Effekt
    $('ul#navigation li').hover(
		function (e) {
		    $(this).children('ul').slideDown(100);
		},
		function (e) {
		    $(this).children('ul').slideUp(50);
		}
	);

    //Datepicker Calls
    callDatePicker('#tb_ankunft1', 0);
    callDatePicker('#tb_abreise1', 1);
    callDatePicker('#tb_ankunft', 0);
    callDatePicker('#tb_abreise', 1);

    //Tabs
    $("#content ul.tabs").tabs("#content div.item");
    $("#sidebar ul.tabs").tabs("#sidebar div.item");

    //Facebook einblenden
    $('ul#lang a#facebook').click(function (e) {
        e.preventDefault();
        $(this).parent().toggleClass('active');
        $('#fbLike').fadeToggle();
    });

    //Teaser Text Startseite auf und zu klappen
    $('.o').show();
    $('.c').hide();
    $('a#action').click(function (e) {
        e.preventDefault();
        if ($('#teaser').hasClass('close') == true) {
            $('#teaser').animate({
                top: '+=230'
            }, 1500, function () {
            });
            $('#teaser').removeClass("close");
            $('.o').show();
            $('.c').hide();

        } else {
            $('#teaser').addClass("close");

            $('#teaser').animate({
                top: '-=230'
            }, 1500, function () {
            });
            //$('#teaser').removeClass("close");
            $('.o').hide();
            $('.c').show();
        }

    });

    //Startseite Banner Rotation
    if ($('.s3 img').size() > 1) {
        $('.s3').cycle({
            fx: 'fade',
            speed: 700,
            timeout: 3000,
            next: '.s3',
            pause: 1
        });
    }

    //Form Stuff
    $('textarea').click(function () { $(this).text(""); });

    //Form Stuff: Newsletter
    $("#newsletter").validate({
        rules: {
            nl_name: "required",
            nl_mail: {
                required: true,
                email: true
            }
        },
        messages: {
            nl_name: $('#h_nname').val(),
            nl_mail: $('#h_mail').val()
        }
    });

    //Form Stuff: Newsletter
    $("#form1").validate({
        ignore: ".ignore",
        submitHandler: function (form) {
            form.submit();
        },
        rules: {
            tb_vorname: "required",
            tb_name: "required",
            tb_email: {
                required: true,
                email: true
            }
        },
        messages: {
            tb_vorname: $('#h_vname').val(),
            tb_name: $('#h_nname').val(),
            tb_email: $('#h_mail').val()
        }
    });

    //Close ExMessage Wrapper
    $('#exMessage.erfolg').delay(10000).slideUp(500);
    $('#exMessage a.close').click(function (e) {
        e.preventDefault();
        $('#exMessage').slideUp(500);
    });

    //Zebra Preisliste
    $(".zebra tr:nth-child(even)").addClass('zebraStyle');

    //Zebra Sidbar Liste
    $(".sidebarList li:nth-child(even)").addClass('zebraStyleSide');

    //Accordion
    $(".accordion h2:first").addClass("active");
    $(".accordion div.tableItem:not(:first)").hide();

    $(".accordion h2").click(function () {
        $(this).next("div.tableItem").slideToggle("slow").siblings("div.tableItem:visible").slideUp("slow");
        $(this).toggleClass("active");
        $(this).siblings("h2").removeClass("active");
    });

    //Pauschalen Select Redirect
    $("#ddlpauschalen").change(function () {
        var value = $(this).val();
        var index = value.indexOf("-");
        var siid = value.substring(0, index)
        var arid = value.substring(index + 1, value.length);

        if (arid != 1) {
            $("#form1 input, #form1 select").addClass("ignore");
            $("#form1").attr("action", "default.aspx?SIid=" + siid + "&LAid=" + $.getUrlVar('LAid') + "&ARid=" + arid + "&JIid=" + $.getUrlVar('JId') + "&a=t");
            $("#form1").submit();
        }
    });

    //Pauchalen Scroll to
    $('a.buchpauschale').click(function () {
        var buchpauschale = $(this).attr("alt");
        $("#ddlpauschalen option").attr("selected", false);
        $("#ddlpauschalen option").each(function () {
            var value = $(this).val()
            var index = value.indexOf("-");
            var arid = value.substring(index + 1, value.length);
            if (arid == buchpauschale) {
                $(this).attr("selected", "selected");
                //$("#ddlpauschalen").val(removeHTMLTags($(this).text()));
                if (navigator.userAgent.match(/iPad/i) == null) {
                    $.scrollTo($('#AnfrageForm'), 2000, { offset: -1 });
                }
            }
        });
        return false
    });
    if (navigator.userAgent.match(/iPad/i) == null) {
        if ($.getUrlVar('a') == "t") {
            $.scrollTo($('#AnfrageForm'), 2000, { offset: -1 });
        }
    }

    //Pauschalen Detail Anfrage richtig auswählen
    if ($(".mainContent").hasClass("isDetail")) {
        var getSelection = $("h1").attr("rel");
        $('#ddlpauschalen option').each(function () {
            if ($(this).val() == getSelection) {
                $(this).attr("selected", "selected");
            }
        });
    }

    //Social 2 Klicks
    if ($('#socialshareprivacy').length > 0) {
        $('#socialshareprivacy').socialSharePrivacy();
    }

    //Overlay
    $("a.overlay[rel]").overlay({
        mask: '#c1bcad',
        effect: 'apple'
    });

    //Guestbook Validate
    $("form#guestbook").validate({
        rules: {
            gb_12: "required",
            gb_34: {
                required: true,
                email: true
            }
        },
        messages: {
            gb_12: $('#h_nname').val(),
            gb_34: $('#h_mail').val()
        }
    });

    $('form#guestbook').submit(function () {
        $("#recap").val('assigned');
    });

});

function removeHTMLTags(inputcode) {
    var strInputCode = inputcode;
    strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1) {
        return (p1 == "lt") ? "<" : ">";
    });
    var strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
    return strTagStrippedText;
}

function callDatePicker(id, date) {
    $(id).datepicker({
        showOn: "both",
        buttonImage: "App_Themes/images/icons/cal.gif",
        buttonImageOnly: true,
        monthNames: ['J&auml;nner', 'Februar', 'M&auml;rz', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'],
        dayNamesMin: ['So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa'],
        dateFormat: 'dd.mm.yy',
        firstDay: 1,
        minDate: date,
        onSelect: function (dateText, inst) {
            dOff = $('#tb_abreise').val();
            dOn = $('#tb_ankunft').val();
            if (!dOn == 0 && !dOff == 0) {
                dOn = new Date(dOn.split(".").reverse().join(","));
                dOff = new Date(dOff.split(".").reverse().join(","));
                if (((dOff - dOn) / 86400000) <= 0) {
                    dOff = $('#tb_abreise').val("");
                    dOn = $('#tb_ankunft').val("");
                    $('#tb_abreise').parent().append("<label class='error'>Abfahrt darf nicht vor Ankunft sein!</label>");
                    $('#tb_ankunft').parent().append("<label class='error'>Abfahrt darf nicht vor Ankunft sein!</label>");
                } else {
                    $('#tb_abreise').parent().find('label.error').remove();
                    $('#tb_ankunft').parent().find('label.error').remove();
                    $('#ddlnaechte').val((dOff - dOn) / 86400000);
                }

            }
        }
    });
}

function vollbild(page) {
    window.open(page, 'Panorama', 'toolbar=0,menubar=0,location=0,scrollbars=0,resizable=0,width=800,height=600');
}

$.extend({
    getUrlVars: function () {
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for (var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    },
    getUrlVar: function (name) {
        return $.getUrlVars()[name];
    }
});
