﻿function initializeEnta() {

    var urlx = encodeURI('http://www.operaen.no/');
    var nosimple = ['1132'];

    var caboo = ($('#Calendar') == null) ? false : true;
    if (caboo) {
        getCalendar();
    }

    var plxsboo = ($('#ProductionListXS') == null) ? false : true;
    if (plxsboo) {
        getEventsToday();
    }

    var login = ($('entaLogin') == null) ? false : true;
    if (login) {
        getEntaLogin('');
    }

    var baboo = ($('EntaBasketXS') == null) ? false : true;
    if (baboo) {
        getBasket();
    }

    var elboo = ($('EventList') == null) ? false : true;
    if (elboo) {
        //var shoid = ($('EventList').get('title') == null) ? '' : $('EventList').get('title');
        var shoid = ($("#EventList").attr("title") == null) ? "0" : $("#EventList").attr("title");
        getEventsByShow(shoid);
    }
	        

}

function getEventsByShow(id) {
    var haseventlist = ($("#EventList") == null) ? false : true;
    if (haseventlist) {
        var txt = $.get("http://www.operaen.no/bpElementProvider.aspx?template=dnoevetinybyshow&shoid="+id, function (data) {
            $('#EventList').html(data);
            initializeEventListTriggers();
        });
    }
}

function initializeEventListTriggers() {
    $("#EventList").undelegate("div", "click");
    $("#EventList").delegate("div", "click", function () {
        var id = (this.id == null) ? '' : this.id;
        //$('#EventList').children().css("background-color", "#000000");
        $(this).parent().children().css("background-color", "#343434");

        // Does the performance have Seating Plan access?
        var x = id.indexOf('{1}');
        var noseatplan = (x == -1) ? true : false;

        // Check if PriceBands and Prices exists on the page.
        var pr = ($('#PriceBands') == null) ? false : true;
        var pb = ($('#Prices') == null) ? false : true;

        if (id.length > 1) {
            if (pr && pb && noseatplan) {
                var per = (this.title == null) ? '' : this.title;
                $(this).css("background-color", "#ff6600");
                getPriceBands(per);
                //Missing Denne skal ha en annen trigger
                id = id.replace('{0}', 'EventPrices.aspx').replace('{1}', 'EventFlashPlan.aspx').replace('+', '&');
                window.location = ('http://www.operaen.no/bpElementProvider.aspx?template=dnolink&' + id)
                //window.location = ('http://www.operaen.no/Default.aspx?ID=27362&' + id)
            } else {
                id = id.replace('{0}', 'EventPrices.aspx').replace('{1}', 'EventFlashPlan.aspx').replace('+', '&');
                window.location = ('http://www.operaen.no/bpElementProvider.aspx?template=dnolink&' + id)
                //window.location = ('http://www.operaen.no/Default.aspx?ID=27362&' + id)
            }
        }
    });
}

function getPriceBands(id) {
    $("#Prices").html("");
    $("#PriceBands").html("");
    var txt = $.get("bpElementProvider.aspx?template=dnopricebands&perindex=" + id, function (data) {
        $("#PriceBands").html(data);
        var prg = ($("#PriceBands > div").attr("id") == null) ? '' : $("#PriceBands > div").attr("id");

        if (prg != '') {
            getPrices('perindex=' + id + '&mclid' + prg);
        } else {
            $('#Prices').html("");
        }
    });
}

function getPrices(id) {
    var txt = $.get("bpElementProvider.aspx?template=dnoprices&" + id, function (data) {
        $("#Prices").html(data);
        //MISSING Trigger for å bestille unummerert::::
    });
}

function getCalendar() {
    var hascalendar = ($("#Calendar") == null) ? false : true;
    if (hascalendar) {
        var txt = $.get("http://www.operaen.no/bpElementProvider.aspx?template=dnocalendar", function (data) {
            $('#Calendar').html(data);
            initializeCalendarTriggers();
        });
    }
}

function getCalendar(qs) {
    var hascalendar = ($("#Calendar") == null) ? false : true;
    if (hascalendar) {
        var txt = $.get("http://www.operaen.no/bpElementProvider.aspx?template=dnocalendar&" + qs, function (data) {
            $('#Calendar').html(data);
            initializeCalendarTriggers();
        });
    }
}

function initializeCalendarTriggers() {
    $("#Calendar").undelegate("span", "click");
    $("#Calendar").delegate("span", "click", function () {
        getCalendar("fromdate="+this.id);
    });

    $("#Calendar").undelegate("td", "click");
    $("#Calendar").delegate("td", "click", function () {
        getEventsByDate(this.id);
        $("#Calendar").find("td").css("background-color", "transparent");
        $(this).css("background-color", "rgb(247, 95, 19)");
        $(this).css("color", "white");
    });

}

function getEventsToday() {
    var hasprdlsxs = ($("#ProductionListXS") == null) ? false : true;
    if (hasprdlsxs) {
        var txt = $.get("http://www.operaen.no/bpElementProvider.aspx?template=dnoevetinybydate", function (data) {
            $('#ProductionListXS').html(data);
            getEventsByDateTriggers();
        });
    }
}

function getEventsByDate(dt) {
    var hasprdlsxs = ($("#ProductionListXS") == null) ? false : true;
    if (hasprdlsxs) {
        var txt = $.get("http://www.operaen.no/bpElementProvider.aspx?template=dnoevetinybydate&fromdate=" + dt, function (data) {
            $('#ProductionListXS').html(data);
            getEventsByDateTriggers();
        });
    }
}

function getEventsByDateTriggers() {
    $("#ProductionListXS").undelegate("div", "click");
    $("#ProductionListXS").delegate("div", "click", function () {
        var id = (this.id == null) ? '' : this.id;

        if (id.length > 1) { 
            id = id.replace('{0}', 'EventPrices.aspx').replace('{1}', 'EventFlashPlan.aspx').replace('+', '&');
            window.location = ('http://www.operaen.no/bpElementProvider.aspx?template=dnolink&' + id)
            //window.location = ('http://www.operaen.no/Default.aspx?ID=27362&' + id)
        }
    });
}

function getBasket() {
    var txt = $.get("http://www.operaen.no/bpElementProvider.aspx?template=dnotinybasket", function (data) {
        $('#EntaBasketXS').html(data);
        enumTinyBasketTriggers();
    });
}

function enumTinyBasketTriggers() {
    $("#EntaBasketXS").undelegate("a", "click");
    $("#EntaBasketXS").delegate("a", "click", function () {
        window.location = ('http://www.operaen.no/bpElementProvider.aspx?template=dnolink&QL=GBasicBasket.aspx')
        //window.location = ('http://www.operaen.no/Default.aspx?ID=27362&QL=GBasicBasket.aspx')
    });
}

function getEntaLogin(id) {
    var txt = $.get("http://www.operaen.no/bpElementProvider.aspx?template=dnocustomerlogon&" + id, function (data) {
        $('#entaLogin').html(data);
    });
    enumLoginTriggers();
}

function enumLoginTriggers() {
    $("#entaLogin").undelegate("div", "click");
    $("#entaLogin").delegate("div", "click", function () {
        var id = (this.id == null) ? '' : this.id;
        if (id == 'userlogin') {
            getEntaLogin('emuemail=' + $('#userid').val() + '&emupwd=' + $('#userpwd').val());
        }
        if (id == 'userlogout') {
            getEntaLogin('agtrefno=0');
        }
        if (id == 'useredit') {
            window.location = ('http://www.operaen.no/bpElementProvider.aspx?template=dnolink&QL=GNewEmailLogon.aspx')
            //window.location = ('http://www.operaen.no/Default.aspx?ID=27362&QL=GNewEmailLogon.aspx')
        }
        if (id == 'usernew') {
            window.location = ('http://www.operaen.no/bpElementProvider.aspx?template=dnolink&QL=GNewEmailLogon.aspx')
            //window.location = ('http://www.operaen.no/Default.aspx?ID=27362&QL=GNewEmailLogon.aspx')
        }
        if (id == 'usersendpwd') {
            window.location = ('http://www.operaen.no/bpElementProvider.aspx?template=dnolink&QL=GPasswordRequest.aspx')
            //window.location = ('http://www.operaen.no/Default.aspx?ID=27362&QL=GPasswordRequest.aspx')
        }
        if (id == 'userbookings') {
            window.location = ('http://www.operaen.no/bpElementProvider.aspx?template=dnolink&QL=GBookingList.aspx')
            //window.location = ('http://www.operaen.no/Default.aspx?ID=27362&QL=GBookingList.aspx')
        }
    });

}
