//<![CDATA[
var IBSRAH = IBSRAH || {};
IBSRAH = {
    slideShow: function () {
        $("#slides").slides({
            preload: true,
            preloadImage: "/images/ajax-loader.gif",
            play: 5000,
            pause: 1500,
            width: 580,
            height: 360
        });
    },
    pageScroll: function () {
        $(".regionLink, .top, .enquirytop").click(function () {
            var a = $(this.hash), b;
            if (a.length) {
                b = a.offset().top;
                $("html,body").animate({ scrollTop: b }, 1000);
                return false;
            }
        });
    },
    iconKey: function () {
        $("#imgKey").hide();
        $("a.showKey").click(function () {
            var img = $("#imgKey");
            if (img.is(":visible")) {
                img.hide("medium");
            } else {
                img.show("slow");
            }
            return false;
        });
    },
    addTabs: function () {
        var tabber1 = new Yetii({
            id: "info-tabs"
        });
        $("#info-tabs-nav a").click(function () {
            $(this).blur();
        });
        $("#demo-tabs a[href^='http']").attr({
            "target": "_blank",
            "title": "This link opens in a new window"
        });
    },
    valEnq: function (bttn) {
        $(bttn).click(function () {
            var OK = true,
            errArr = [],
            errId, i;
            $("label").removeClass("err");
            $(".req").each(function () {
                if (!$(this).val()) {
                    OK = false;
                    errId = "#" + $(this).attr("id");
                    errArr.push(errId);
                }
            });
            if (!$("#tcConfirmed").is(":checked")) {
                OK = false;
                errArr.push("#tcConfirmed");
            }
            for (i = 0; i < errArr.length; i += 1) {
                $(errArr[i]).prev().addClass("err");
            }
            return OK;
        });

    },
    makeBooking: function (elem) {
        $(elem).click(function () {
            var clickedElem = $(this),
            rahDate = clickedElem.attr("id"),
            HostelId = $("#HostelId").val(),
            rahParts,
            url = "https://www.hostelbookings.net/ibsv2/IBSStart.aspx?itemid=" + HostelId,
            qStr = "&StartDay={Day}&StartMonth={Month}&StartYear={Year}",
            link;
            rahParts = rahDate.split("/");
            if (+rahParts.length === 3) {
                link = qStr.supplant({ "Day": rahParts[0], "Month": rahParts[1], "Year": rahParts[2] });
                location.href = url + link;
            }
        });
    },
    enquiryForm: function (elem) {

        $(elem).click(function () {

            var clickedElem = $(this),
            rahDate = clickedElem.attr("id"),
            tableElem = clickedElem.closest("table"),
            mainElem = $("#main"),
            formElem = $("#FrmWrap"),
            formVisible = formElem.is(":visible"),
            hostel = $("#HostelName").val(),
            tableId = tableElem.attr("id"),
            tableTop = 0,
            tableLeft = 0,
            tableWidth = 0,
            formTop = 0,
            formLeft = 0,
            mainTop = mainElem.offset().top,
            mainLeft = mainElem.offset().left;


            // remove any error highlighting
            $("label").removeClass("err");


            //change displayed date in <thead>
            $(".tblHead").empty().append(hostel + " on <span class=\"enquiryDate\">" + rahDate  + "</span>");


            // startdate hidden field
            $("#Start").val(rahDate);


            // add clicked table id to form wrapper as a class     
            formElem.addClass(tableId);

            tableTop = tableElem.offset().top;
            tableLeft = tableElem.offset().left;
            tableWidth = tableElem.outerWidth();
            formTop = tableTop - mainTop;
            formLeft = (tableLeft - mainLeft) + tableWidth + 40;

            formElem.hide().css({ "top": formTop, "left": formLeft });
            $("html,body").animate({ scrollTop: tableTop }, 500, function () {
                formElem.fadeIn("fast");
            });

        });
    },
    showTerms: function (elem) {
        $(elem).click(function () {
            var win_URL = $(this).attr("href");
            $("#dialog").load(win_URL + " #text").dialog({
                height: 550,
                width: 750,
                modal: true,
                title: "RentaHostel Terms &amp; Conditions"
            });
            return false;
        });
    },

    hostelClick: function () {

        $(".RAHostels li").click(function () {
            var href = $(this).find("a").attr("href");
            if (href) {
                window.location = href;
            }
        });
    }
};
// http://javascript.crockford.com/remedial.html
if (typeof String.prototype.supplant !== "function") {
    String.prototype.supplant = function (o) {
        return this.replace(/{([^{}]*)}/g,
            function (a, b) {
                var r = o[b];
                return typeof r === "string" || typeof r === "number" ? r : a;
            });
    };
}
//]]> 
