if (!window.ND) { window.ND = {}; window.NextDigital = window.ND; }

(function ($) {
    // fix for ie6 selectbox cover dropdown menu issue
    ND.slectboxIE6bug = function () {
        if ($.browser.msie == true && $.browser.version == "6.0") {

            $('DIV.header-menu UL:eq(0)').attr({ id: 'nav' });

            ieHover = function () {
                var ieLIs = document.getElementById('nav').getElementsByTagName('li');
                for (var i = 0; i < ieLIs.length; i++) if (ieLIs[i]) {
                    ieLIs[i].onmouseover = function () {
                        var ieUL = this.getElementsByTagName('ul')[0];
                        if (ieUL) {
                            var ieMat = document.createElement('iframe');
                            ieMat.style.width = ieUL.offsetWidth + "px";
                            ieMat.style.height = ieUL.offsetHeight + "px";
                            ieUL.insertBefore(ieMat, ieUL.firstChild);
                            ieUL.style.zIndex = "99";
                        }

                        this.className += " iehover";
                    }
                    ieLIs[i].onmouseout = function () {
                        var ieUL = this.getElementsByTagName('ul')[0];
                        if (ieUL) { ieUL.removeChild(ieUL.firstChild); }
                        this.className = this.className.replace(' iehover', '');
                    }
                }
            }
            if (window.attachEvent) window.attachEvent('onload', ieHover);


        }

    }

    // Adds :hover functionality to IE6
    ND.ieHover = function () {
        if ($.browser.msie && $.browser.version.substr(0, 3) == "6.0") {
            var ul_array = $("ul");
            for (var i = 0; i < ul_array.length; i++) {
                if ($(ul_array[i].parentNode).hasClass("hoverable")) {
                    //Add 'hover' classes
                    var listItems = $(ul_array[i]).children("li");
                    for (var k = 0; k < listItems.length; k++) {
                        $(listItems[k]).hover(function () { $(this).addClass("hover"); }, function () { $(this).removeClass("hover"); });
                    }
                }
            }

            // Dave Bui 19/10/2009
            var buttonArray = $("button");
            for (var i = 0; i < buttonArray.length; i++) {
                //Add 'hover' classes
                $(buttonArray[i]).hover(function () { $(this).addClass("hover"); }, function () { $(this).removeClass("hover"); });
            }

            var rowArray = $("table.hoverable tr");
            for (var i = 0; i < rowArray.length; i++) {
                $(rowArray[i]).hover(function () { $(this).addClass("hover"); }, function () { $(this).removeClass("hover"); });
            }

        }
    };

    // Calendar using jQuery UI's date picker - Dave Bui 16/10/2009
    ND.addCalendar = function () {
        $(".linkedDatepicker").datepicker(
		{
		    dateFormat: "dd/mm/yy",
		    yearRange: "2009:2010",
		    beforeShow: function () {
		        $(this).val($(this).closest(".booknow-row, .schedules-row").find(".booknow-day, .schedules-day").val() + "/" +
					$(this).closest(".booknow-row, .schedules-row").find(".booknow-month, .schedules-month").val());
		    },
		    onSelect: function (date) {
		        $(this).closest(".booknow-row, .schedules-row").find(".booknow-day, .schedules-day").val(date.substring(0, 2));
		        var my = date.substring(3);
		        if (my.substring(0, 1) == "0") {
		            my = my.substring(1);
		        }
		        $(this).closest(".booknow-row, .schedules-row").find(".booknow-month, .schedules-month").val(my);
		        doChangeDay($(this).closest(".booknow-row, .schedules-row").find(".booknow-month, .schedules-month"), $(this).closest(".booknow-row, .schedules-row").find(".booknow-day, .schedules-day"), true);
		    },
		    showOn: "both",
		    showAnim: "fadeIn",
		    buttonImageOnly: true,
		    gotoCurrent: true,
		    buttonImage: "/themes/default/img/icon-calendar.gif",
		    buttonText: "Show calendar"
		});
    }

    // Toggle returing date option
    ND.toggleReturing = function () {
        $("input[rel='travelOneway']").click(function () {
            $(this).closest("form").find("div[rel='travelReturn']").slideUp();
        });
        $("input[rel='travelReturn']").click(function () {
            $(this).closest("form").find("div[rel='travelReturn']").slideDown();
        });
        $("input[rel='travelOneway']:checked").closest("form").find("div[rel='travelReturn']").hide(); // Hide after refreshing in Firefox
    }

    // Home page slideshow
    ND.slideshow = function () {
        var links = "";
        if ($("#linkURL").length > 0) {
            links = $("#linkURL").html().split("\n");
            if ($.browser.msie&&($.browser.version != "9.0")) {
                links = $("#linkURL").html().split(" ");
            }

            $("#slideshow .slide").each(function (i, data) {
                if (links[i] != null && links[i].toLowerCase() != "n/a" && links[i].toLowerCase() != "") {
                    $(this).css("cursor", "pointer").click(function () {
                        window.location.href = links[i];
                    });
                }
            });
        }

        slideButtonClick = function (interval, goingBack) {
            slideSwitch(goingBack);
            clearInterval(interval);
            return setInterval("slideSwitch()", 5000);
        }

        slideSwitch = function (goingBack) {
            var $active = $('#slideshow DIV.slide.active');

            if ($active.length == 0) $active = $('#slideshow DIV.slide:last');

            if (!goingBack)
                var $next = $active.next().length ? $active.next()
					: $('#slideshow DIV.slide:first');
            else
                var $next = $active.prev().length ? $active.prev()
					: $('#slideshow DIV.slide:last');

            $active.addClass('last-active');

            $next.css({ opacity: 0.0 })
		        .addClass('active')
		        .animate({ opacity: 1.0 }, 400, function () {
		            $active.removeClass('active last-active');
		        })
        }


        if ($("#slideshow").find("DIV.slide").length > 1) {
            $(".slideshow-buttons").show();

            var currentInterval = setInterval("slideSwitch()", 5000);

            $("button.slideshow-next").click(function () {
                currentInterval = slideButtonClick(currentInterval, false);
            });
            $("button.slideshow-back").click(function () {
                currentInterval = slideButtonClick(currentInterval, true);
            });
        }

    }


    // On load
    $(function () {
        ND.slectboxIE6bug();
        ND.ieHover();
        ND.addCalendar();
        ND.toggleReturing();
        ND.slideshow();
    });

})($);

$(document).ready(function () {
    $(".openinwindow").click(function () {
        window.open($(this).parent().attr("href"), "mywin", "left=20,top=20,width=800,height=450,toolbar=0,resizable=0,scrollbars=yes");
        return false;
    });
});

function chgDay(sel, d) {
    sel.change(function () {
        doChangeDay(sel, d, false);
    });
}

function rewriteMonthYear(obj) {
    if (obj.size() > 0) {
        var 
            dayList = obj.prev(),
            month = obj.val().substring(0, obj.val().indexOf("/")),
            year = obj.val().substring(obj.val().indexOf("/") + 1),
            currentDay = dayList.find("option").index(dayList.find("option:selected"));

        max = month == "1" | month == "3" | month == "5" | month == "7" | month == "8" | month == "10" | month == "12" ? 29 : 28;

        if (month != "2") {
            max += 2;
        } else {
            if (!year % 4)
                max += 1;
        }

        dayList.html("");
        for (var i = 1; i <= max; i++) {
            dayList.append("<option>" + (i < 10 ? "0" + i : i) + "</option>");
        }
        dayList.find("option").eq(currentDay).attr("selected", "selected");
    }
}

function doChangeDay(sel, d, reset) {
    var val = sel.val();
    var dop = d.find("option");

    if (val.indexOf("4/") + val.indexOf("6/") + val.indexOf("9/") + val.indexOf("11/") > -4) {
        if (dop.size() == 31) {
            dop.eq(30).remove();
        } else {
            if (dop.size() == 28) {
                d.append("<option>29</option>");
                d.append("<option>30</option>");
            }
            if (dop.size() == 29) {
                d.append("<option>30</option>");
            }
        }
    } else if (val.indexOf("2/") > -1 && val.indexOf("12/") == -1) {
        if (dop.size() == 31) {
            dop.eq(30).remove();
            dop.eq(29).remove();
        }
        if (dop.size() == 30) {
            dop.eq(29).remove();
        }
        var year = parseInt(val.substring(val.indexOf("/") + 1));
        if (year % 4 > 0 || (year % 4 == 0 && year % 100 == 0 && year % 400 > 0)) {
            dop.eq(28).remove();
        }
    } else {
        if (dop.size() == 28) {
            d.append("<option>29</option>");
            d.append("<option>30</option>");
            d.append("<option>31</option>");
        }
        if (dop.size() == 29) {
            d.append("<option>30</option>");
            d.append("<option>31</option>");
        }
        if (dop.size() == 30) {
            d.append("<option>31</option>");
        }
    }
    if (reset) {
        if (d.attr("id").indexOf("Depart") > 0) {
            var 
            id = d.attr("id").replace("Depart", "Returning"),
            index = d.find("option").index(d.find("option:selected")),
            returnMonthYear = d.parent().next().find("select:eq(1)"),
            departMonthYear = d.parent().find("select:eq(1)"),
            currentMonthYear = departMonthYear.find("option").index(departMonthYear.find("option:selected"));

            if (index < d.find("option").size() - 1) {
                returnMonthYear.find("option").eq(currentMonthYear).attr("selected", "selected");
            } else {
                returnMonthYear.find("option").eq(currentMonthYear + 1).attr("selected", "selected");
                index = -1;
            }
            rewriteMonthYear(returnMonthYear);

            $("#" + id + " option:eq(" + (index + 1) + ")").attr("selected", "selected");
        }
    }
}
