﻿var picView = new Array();
var picTimeOut = null;
var picCurrent = 0;
var eventInfo = new Array();

function OpenEventInfo(id) {
    var i = 0;
    var text = "";
    var cid = 0;
    var sid = 0;
    for (i = 0; i < eventInfo.length; i++) {
        if (eventInfo[i][0] == id) {
            text = eventInfo[i][1];
            cid = eventInfo[i][2];
            sid = eventInfo[i][3];
            isBuy = eventInfo[i][4];
            break;
        }
    }
    $('#event-info').html('<div style="padding-bottom:10px;">' + text + '</div>').show();
    $('#panelScroll').height(203);
    $('.jScrollPaneContainer').height($('.jScrollPaneContainer').height() - 35);
    $('#scheme').show();
    $('#eventsList').hide();
    $('#panelScroll').jScrollPane();
    if (sid > 0) {
        $('.schemeLink').attr('href', 'Scheme.aspx?id=' + sid);
        $('.schemeLink').children('img').attr('src', 'img/scene-active.png');
        $("a.zoom").fancybox({
            callbackOnStart: FancyOnShow,
            callbackOnClose: FancyOnClose,
            frameWidth: 1009,
            frameHeight: 588
        });
    } else {
        $('.schemeLink').children('img').attr({
            'src': 'img/scene-not-active.png',
            'href': ''
        }).unbind('click').unbind('click.fb').bind('click', function() { return false; });
    }
    if (isBuy == "True") {
        $('.bookTicked').attr('href', 'Ticket.aspx?id=' + id).show();
    } else {
     $('.bookTicked').hide();
    }
}

function CloseEventInfo() {
    $('#event-info').html('').hide();
    $('#panelScroll').height(238);
    $('.jScrollPaneContainer').height($('.jScrollPaneContainer').height() + 35);
    $('#scheme').hide();
    $('#eventsList').show();
    $('#panelScroll').jScrollPane();
}

function ChangePicture() {
    SetPicture(picView[picCurrent]);
    picCurrent++;
    picCurrent = picCurrent < picView.length ? picCurrent : 0;
}

function SetPicture(link) {
    var img = document.getElementById("picAfisha");
    img.src = link ? link : img.src;
    img.style.display = img.src ? "" : "none";
}

function OnPicture(link) {
    SetPicture(link);
    window.clearTimeout(picTimeOut);
}

function InitAfisha(src) {
    if (src) {
        $('#picAfisha').attr('src', src);
        $('#picAfisha').show();
    } else {
        ChangePicture();
        picTimeOut = window.setInterval("ChangePicture();", 2000);
    }
}

function FancyOnShow() {
    $('#flash').hide();
    $('a.myPlayer').css('visibility', 'hidden');
}
function FancyOnClose() {
    $('#flash').show();
    $('a.myPlayer').css('visibility', 'visible');
}
function SwapContainer(val) {
    switch (val) {
        case 0:
            $('#tours-list-conteiner').show();
            $('#promoters').hide();
            $('#listProm').hide();
            break;
        case 1:
            $('#tours-list-conteiner').hide();
            $('#promoters').show();
            $('#listProm').hide();
            break;
        case 2:
            $('#tours-list-conteiner').hide();
            $('#promoters').hide();
            $('#listProm').show();
            break;
    }
}
function SetPid(id) {
    $('#' + pid).val(id);
}

var CurrentRow = 0;
function PrevNextGallery(val) {
    var count = Math.round($('div.galleryScroll').children('div').length / 2);
    if (count > 0) {
        if (val) {
            CurrentRow++;
            if (count < CurrentRow) {
                CurrentRow--;
            } else {
                $('div.galleryScroll').trigger('next');
            }
        } else {
            CurrentRow--;
            if (CurrentRow < 0) {
                CurrentRow++;
            } else {
                $('div.galleryScroll').trigger('prev');
            }
        }
    }
}


var ContactsMap = {
    map: null,
    marker: null,
    title: {
        "english version": "<p class='bold-black-14'>Наш Офис</p><p class='black-14'>ул. Комсомольская 54</p>",
        "русская версия": "<p class='bold-black-14'>Our office</p><p class='black-14'>st. Komsomolskaya 54</p>"
    },
    init: function() {
        this.map = new GMap2($("#map_canvas").get(0));
        this.map.setCenter(new GLatLng(48.468056, 35.031736), 17);
        this.map.setUIToDefault();
        this.map.setMapType(G_HYBRID_MAP);
        this.marker = new GMarker(new GLatLng(48.468056, 35.031736));
        GEvent.addListener(this.marker, "click", $.proxy(this, 'openMarker'));
        this.map.addOverlay(this.marker);
    },
    openMarker: function() {
        this.marker.openInfoWindowHtml(this.title[GetLang()]);
    }
};

var ConcertMap = {
    button: null,
    text: {
        open: {
            "english version": "Карта",
            "русская версия": "Map"
        },
        close: {
            "english version": "Презентация",
            "русская версия": "Presentation"
        }
    },
    map: null,
    mapContainer: null,
    flash: null,
    markers: {},
    info: {},
    geocoder: null,
    init: function() {
        this.geocoder = new GClientGeocoder();
        this.button = $('#map_tour');
        this.flash = $('div.flash');
        this.mapContainer = $('div.map');
        this.button.bind({
            click: $.proxy(this, 'openMap')
        });

    },
    showMap: function() {
        this.map = new GMap2($("#map_concert").get(0));
        this.map.setCenter(new GLatLng(49.28214, 32.01416), 5);
        this.map.setUIToDefault();
        this.map.setMapType(G_HYBRID_MAP);
        for (var i in this.info) {
            var self = this;
            this.geocoder.getLatLng(
                    this.info[i].city,
                    function(index, info) {
                        return function(point) {
                            if (point) {
                                self.markers[index] = new GMarker(point);
                                self.map.addOverlay(self.markers[index]);
                                GEvent.addListener(self.markers[index], "click", function() {
                                    self.markers[index].openInfoWindowHtml('<p class="bold-black-14">' + self.info[index].city + '</p><p class="black-14">' + self.info[index].date + '</p>')
                                });
                            }
                        }
                    } (i, this.info));
        }
    },
    openMap: function() {
        this.flash.hide();
        this.mapContainer.show();
        if (!this.map) {
            this.showMap();
        }
        this.button.text(this.text.close[GetLang()])
                   .unbind('click')
                   .bind({
                       click: $.proxy(this, 'closeMap')
                   });
        return false;
    },
    closeMap: function() {
        this.mapContainer.hide();
        this.flash.show();
        this.button.text(this.text.open[GetLang()])
                   .unbind('click')
                   .bind({
                       click: $.proxy(this, 'openMap')
                   });
        return false;
    }
};

function GetLang() {
    return $('#languageControl a').text();
}

var Zoom = {
    button: null,
    img: null,
    plus: null,
    zoomed: false,
    init: function () {
        this.button = $('#zoom');
        this.plus = this.button.find('img');
        this.img = $('#scene');
        this.button.click($.proxy(this.onClick, this));
        this.img.dblclick($.proxy(this.onClick, this));
    },
    onClick: function () {
        if (this.zoomed) {
            this.onZoomOut();
        } else {
            this.onZoomIn();
        }
        return false;
    },
    onZoomIn: function () {
        this.zoomed = true;
        this.img.panFullSize(null, 411, $.proxy(this.onPan, this));
        this.plus.attr('src', 'img/minus.png');
    },
    onZoomOut: function () {
        this.zoomed = false;
        this.img.normalView();
        this.plus.attr('src', 'img/plus.png');
    },
    onPan: function () {
        $('#panscene').dblclick($.proxy(this.onClick, this));
    }
};

var PreOrder = {
    deliveries: {},
    selected: [],
    totalLine: {},
    totalInt: 0,
    totalHolder: null,
    courier: null,
    courierCost: 0,
    courierCostHolder: null,
    controls: {
        s1: null,
        s2: null,
        s3: null,
        s4: null,
        bGotoOrder: null,
        bDelivery: null,
        bPreOrder: null,
        bOrder: null,
        bPreOrderBack: null,
        bPreDelivery: null,
        bPayment: null,
        bPay: null,
        preorder: null,
        order: null,
        fio: null,
        city: null,
        phone: null,
        email: null,
        address: null,
        orderTotal: null,
        places: null
    },
    init: function () {
        this.totalHolder = $('#total');
        this.courier = $('#courier');
        this.courierCostHolder = $('#courier-cost');
        this.controls.s1 = $('#step-1');
        this.controls.s2 = $('#step-2').hide();
        this.controls.s3 = $('#step-3').hide();
        this.controls.s4 = $('#step-4').hide();
        this.controls.bGotoOrder = $('#bGotoOrder').click(this.onOrder).hide();
        this.controls.bDelivery = $('#bDelivery').hide().click(function () {
            PreOrder.controls.s2.show();
            PreOrder.controls.bDelivery.hide();
        });
        this.controls.bPreDelivery = $('#bPreDelivery');
        this.controls.bPayment = $('#bPayment').click(function () {
            PreOrder.controls.s3.show();
            PreOrder.controls.bPayment.hide();
            PreOrder.controls.bPreOrder.hide();
        });
        this.controls.bPreOrder = $('#bPreOrder');
        this.controls.bOrder = $('#bOrder').click(function () {
            if ($('#aspnetForm').valid()) {
                PreOrder.controls.s4.show();
                PreOrder.onOrder();
                PreOrder.controls.bPreDelivery.hide();
                PreOrder.controls.bOrder.hide();
            }
        });
        this.controls.bPreOrderBack = $('#bPreOrderBack').click(function () {
            PreOrder.controls.bGotoOrder.show();
        });

        this.controls.fio = $('input.tFio');
        this.controls.city = $('input.tCity');
        this.controls.phone = $('input.tPhone');
        this.controls.email = $('input.tEmail');
        this.controls.address = $('input.tAddress');

        this.controls.phone.mask("+38(999) 999-9999");

        this.controls.orderTotal = $('#order-total');

        $('#aspnetForm').validate();

        this.controls.bPay = $('#bPay').click(function () {
            PreOrder.controls.places.val(PreOrder.selected.toString());
        });
        this.controls.preorder = $('#preorder tbody');
        this.controls.order = $('#order tbody');
        this.controls.places = $('#places');

        $('.courier-dd').change($.proxy(this.onCourier, this)).change();
        $('.ticket-item').click($.proxy(this.onItem, this));
    },
    onOrder: function () {
        PreOrder.controls.order.html(PreOrder.controls.preorder.html());
        PreOrder.controls.order.find('.ticket-item:not(.select)').remove();
        PreOrder.controls.order.find('tr td.places-td:not(:has(.ticket-item))').parent().remove();
        $('div.tAddress').text(PreOrder.controls.address.val());
        $('div.tCity').text(PreOrder.controls.city.val());
        $('div.tPhone').text(PreOrder.controls.phone.val());
        $('div.tFio').text(PreOrder.controls.fio.val());
        PreOrder.controls.orderTotal.text(PreOrder.totalInt + PreOrder.courierCost);
    },
    onItem: function (e) {
        var el = $(e.currentTarget);
        var id = el.attr('data-id');
        var cost = parseFloat(el.attr('data-cost'));
        var line = el.attr('data-line');
        if (!this.totalLine[line]) {
            this.totalLine[line] = 0;
        }
        if (el.hasClass('select')) {
            this.selected = $.grep(this.selected, function (value) {
                return value != id;
            });
            this.totalInt -= cost;
            this.totalLine[line] -= cost;
            el.removeClass('select');
        } else {
            this.selected.push(id);
            this.totalInt += cost;
            this.totalLine[line] += cost;
            el.addClass('select');
        }
        this.totalHolder.html(this.totalInt == 0 ? "&nbsp;" : this.totalInt);
        el.closest('tr').find('.line-total').html(this.totalLine[line] == 0 ? "&nbsp;" : this.totalLine[line]);
        if (this.totalInt > 0) {
            this.controls.bDelivery.show();
            if (this.controls.s4.is(':visible')) {
                this.controls.bGotoOrder.show();
                this.controls.bPay.show();
                this.controls.bDelivery.hide();
            }
        } else {
            this.controls.bDelivery.hide();
            if (this.controls.s4.is(':visible')) {
                this.controls.bGotoOrder.hide();
                this.controls.bPay.hide();
            }
        }
    },
    fillCourier: function (id) {
        if (this.deliveries && this.deliveries[id]) {
            this.courier.text(this.deliveries[id].name);
            this.courierCost = parseFloat(this.deliveries[id].cost);
            this.courierCostHolder.text(this.deliveries[id].cost);
            this.controls.orderTotal.text(this.totalInt + this.courierCost);
        }
    },
    onCourier: function (e) {
        var el = $(e.currentTarget);
        this.fillCourier(el.val());
    }
};

var Requis = {
    print: function () {
        window.print();
    },
    printView: function () {
        $('body').addClass('print');
    }
}
