﻿/// <reference path="jquery-1.3.2.js" />
/// <reference path="Config.js" />

var welcome = 'Benvenuto';

$(document).ready(function() {

    $('body').ajaxStart(function() { $('#loading').show(); }).ajaxStop(function() { $('#loading').hide(); });

    $('#LoginSelector').click(function() {
        //var sinistra = $('#Login').css('left');
        $('#Login').animate({ right: 0 }, 500);
    });

    $('#Login').mouseleave(function() {
        //var sinistra = $('#Login').css('left');
        $('#Login').animate({ right: -200 }, 500);
    });

    $('#AssistantSelector').click(function() {
        //var sinistra = $('#Login').css('left');
        $('#Assistant').animate({ right: 0 }, 500);
    });

    $('#Assistant').mouseleave(function() {
        //var sinistra = $('#Login').css('left');
        $('#Assistant').animate({ right: -200 }, 500);
    });

    $('#mainLogo').attr('src', 'ImgManager.aspx?ImgId=' + idLogoDefault + '&WMax=' + wLogo + '&HMax=' + hLogo);

    $('#ita').click(function() {
        cambiaLingua(this.id);
        $('#eng').animate({ width: 24 }, 900, 'linear', function() { });
        $('#ita').animate({ width: 32 }, 900, 'linear', function() { });
        $('#miniLogo').hide();

    });

    $('#eng').click(function() {
        cambiaLingua(this.id);
        $('#eng').animate({ width: 32 }, 900, 'linear', function() { });
        $('#ita').animate({ width: 24 }, 900, 'linear', function() { });
        $('#miniLogo').hide();
    });

    $('#btCerca').click(function() {
        var html, stile;
        $.getJSON("Prodotti.aspx?w=Search&Text=" + $('#txtCerca').val(), "",
        function(result) {
            $('#risultati').empty();
            $('#txtCerca').val('')
            $.each(result, function(entryIndex, entry) {
                if (entryIndex % 2 == 1) stile = 'ricerca_alt';
                else stile = 'ricerca'
                html = '<div class="' + stile + '" id="src_' + entry['idProdotto'] + '" onclick="ShowProdottoTab(' + entry['idProdotto'] + ');" >' + entry['Nome'] + '</div>';
                $('#risultati').append(html);
            });
        });
    });

    $('.BtnCerca').attr('disabled', 'disabled');
    if (isUserLogged) {
        login();
    }
});


function invia(txtPass, txtUser, lblErrore, _lblUser, _lblPassword, _button1, _btnLogOut) {
    //alert('ciao');
    //$.getJSON("Main.aspx?w=Login", "",
    var user = $('#' + txtUser);
    var pass = $('#' + txtPass);
    var err = $('#' + lblErrore);
    var lblUsr = $('#' + _lblUser);
    var lblPass = $('#' + _lblPassword);
    var Button1 = $('#' + _button1);
    var btnLogOut = $('#' + _btnLogOut);
    $.post("Main.aspx", { 'action': 'login', 'user': user.val(), 'pass': pass.val() },
        function(result) {

            if (result.search('yes') > 0) {
                login();
            } else {
                err.text('Login failed. Please check your user name and password and try again.');
                user.show();
                pass.show();
                lblUsr.show();
                lblPass.show();
                Button1.show();
                btnLogOut.hide();
                $('#CarrelloSelector').hide();
                $('#welcome').text('');
            }
        });
}

function logOut(txtPass, txtUser, lblErrore, _lblUser, _lblPassword, _button1, _btnLogOut) {
    var user = $('#' + txtUser);
    var pass = $('#' + txtPass);
    var err = $('#' + lblErrore);
    var lblUsr = $('#' + _lblUser);
    var lblPass = $('#' + _lblPassword);
    var Button1 = $('#' + _button1);
    var btnLogOut = $('#' + _btnLogOut);
    $.post("Main.aspx", { 'action': 'logout' },
        function(result) {
            user.show();
            pass.show();
            lblUsr.show();
            lblPass.show();
            Button1.show();
            btnLogOut.hide();
            err.text('');
            $('#CarrelloSelector').hide();
            $('#welcome').text('');
            $('.BtnCerca').attr('disabled', 'disabled');
            if ($("#IframeDesign").attr("src").toLowerCase().indexOf("prodotti.aspx") != -1) {
                $('#IframeDesign').attr('src', $('#IframeDesign').attr('src'));
            }
            if ($("#IframeDesign").attr("src").toLowerCase().indexOf("carrello") != -1) {
                $("#IframeDesign").attr("src", "CommonPage.aspx?Type=Azienda&Lang=1040")
            }

        });
}

function login() {
    $('#txtUser').hide();
    $('#txtPass').hide();
    $('#lblUser').hide();
    $('#lblPassword').hide();
    $('#Button1').hide();
    $('#btnLogOut').show();
    $('#lblErroreLogin').text('');
    $('#CarrelloSelector').show();
    $('.BtnCerca').attr('disabled', '');
    $('#welcome').text(welcome + ' ' + $('#txtUser').val());
    $('#txtUser').val('');
    $('#txtPass').val('');
    if ($("#IframeDesign").attr("src").toLowerCase().indexOf("prodotti.aspx") != -1) {
        $('#IframeDesign').attr('src', $('#IframeDesign').attr('src'));
    }
}

function GoToPage(tag, lang) {
    var path;

    if (tag.id == 'Azienda' || tag.id == 'DoveSiamo') path = 'CommonPage.aspx?Type=' + tag.id + '&Lang=' + lang;
    else if (tag.id == 'Prodotti') path = 'Prodotti.aspx?Lang=' + lang;
    else if (tag.id == 'Contatti') path = 'Contacts.aspx?Lang=' + lang;
    else if (tag.id == 'CarrelloSelector') path = 'Carrello.aspx?Lang=' + lang;
    else if (tag.id == 'news') path = 'News.aspx?Lang=' + lang;
    else if (tag.id == 'Regali') path = 'Gifts.aspx?Lang=' + lang;
    //$('#' + $('#idIframeDesign').val()).attr('src', path);
    $('#IframeDesign').attr('src', path);
    //alert(path);
    //window.top.IframeDesign.window.location.replace(path, true);
}

function ShowLoading() {
    $('#loading').show();
}

function HideLoading() {
    $('#loading').hide();
}

function cambiaLingua(lingua) {
    var codiceLingua1, codiceLingua2;
    var srcImgCarrello = 'Images/assistent_carrello';
    var srcImgRicerca = 'Images/assistent_ricerca';
    var azienda = 'Iniziativa', prodotti = 'Proposte', doveSiamo = 'Dove Siamo', contatti = 'Contatti', regali = 'Regali';
    if (lingua == 'eng') {
        codiceLingua1 = 2057;
        codiceLingua2 = 1040;
        azienda = 'Company';
        prodotti = 'Products';
        doveSiamo = 'Where we are';
        contatti = 'Contacts';
        regali = 'Gifts';
        srcImgCarrello = srcImgCarrello + '_en';
        srcImgRicerca = srcImgRicerca + '_en';
        $('#btCerca').val('Search');
        welcome = 'Welcome';
        $('#welcome').text($('#welcome').text().replace('Benvenuto', 'Welcome'));
    } else {
        codiceLingua1 = 1040;
        codiceLingua2 = 2057;
        $('#btCerca').val('Cerca');
        welcome = 'Benvenuto';
        $('#welcome').text($('#welcome').text().replace('Welcome', 'Benvenuto'));
    }

    srcImgCarrello = srcImgCarrello + '.png';
    srcImgRicerca = srcImgRicerca + '.png';
    
    var html = '<div id="AreaTopMenu">' +
                    '<a href="#" id="Azienda" onclick="GoToPage(this,' + codiceLingua1 + ');" class="MenuItem">' + azienda + '</a>' +
                    '<a href="#" id="Prodotti" onclick="GoToPage(this,' + codiceLingua1 + ');" class="MenuItem">' + prodotti + '</a>' +
                    '<a href="#" id="news" onclick="GoToPage(this,' + codiceLingua1 + ');" class="MenuItem">News</a>' +
                    '<a href="#" id="DoveSiamo" onclick="GoToPage(this,' + codiceLingua1 + ');" class="MenuItem">' + doveSiamo + '</a>' +
                    '<a href="#" id="Contatti" onclick="GoToPage(this,' + codiceLingua1 + ');"  class="MenuItem" >' + contatti + '</a>' +
                    '<a href="#" id="Regali" onclick="GoToPage(this,' + codiceLingua1 + ');"  class="MenuItem" >' + regali + '</a>' +
                '</div>';

    //cambio l'evento onclick aggiornando la lingua all'assistent del carrello
    $('#CarrelloSelector').click(function() {
        GoToPage(this, codiceLingua1)
    });
    //cambio l'immagine dell'assistente
    $('#CarrelloSelector').attr('src', srcImgCarrello);
    $('#AssistantSelector').attr('src',srcImgRicerca);


    
    var url = $('#' + $('#idIframeDesign').val()).attr('src');



    if (url.search('Lang') > 0) url = url.replace(codiceLingua2, codiceLingua1);
    else {

        if (url.indexOf('?') < 0) url = url + '?'; else url = url + '&';
        url = url + 'Lang=' + codiceLingua1;
    }
    $('#TopMenu').empty();
    $('#TopMenu').append(html);
    //alert(url);
    $('#' + $('#idIframeDesign').val()).attr('src', url);
}

function ShowProdottoTab(idArticolo) {
    //$('#loading').show();
    var element = $('#' + $('#idIframeDesign').val()).contents().find('#ProdottoTab');
    element.css('width', $('body').width() - margineSinistroAnimazione);
    if ($('#' + $('#idIframeDesign').val()).attr('src').search('Prodotti.aspx') < 0) {
        $('#' + $('#idIframeDesign').val()).attr('src', 'Prodotti.aspx').load(function() {
            loadProdottoTab(idArticolo);
        });
    } else {
        if (element.is(':visible')) hideProdottoTab(idArticolo);
        else loadProdottoTab(idArticolo);
    }

    //$('#loading').hide();

    //

}

function loadProdottoTab(idArticolo) {

    $('#' + $('#idIframeDesign').val()).contents().find('#ProdottoTab').css('marginLeft', $('body').width());
    var element = $('#' + $('#idIframeDesign').val()).contents().find('#ProdottoTab');
    var titolo = $('#' + $('#idIframeDesign').val()).contents().find('#titolo');
    var testo = $('#' + $('#idIframeDesign').val()).contents().find('#testo');
    var header = $('#' + $('#idIframeDesign').val()).contents().find('#header');
    var detail = $('#' + $('#idIframeDesign').val()).contents().find('#detail');
    var cartone = $('#' + $('#idIframeDesign').val()).contents().find('#cartone');
    var strati = $('#' + $('#idIframeDesign').val()).contents().find('#strati');
    var pallet = $('#' + $('#idIframeDesign').val()).contents().find('#pallet');
    var peso = $('#' + $('#idIframeDesign').val()).contents().find('#peso');
    var prezzo = $('#' + $('#idIframeDesign').val()).contents().find('#prezzo');
    var mainPhoto = $('#' + $('#idIframeDesign').val()).contents().find('#mainPhoto');
    var smallPhoto1 = $('#' + $('#idIframeDesign').val()).contents().find('#smallPhoto1');
    var smallPhoto2 = $('#' + $('#idIframeDesign').val()).contents().find('#smallPhoto2');
    var smallPhoto3 = $('#' + $('#idIframeDesign').val()).contents().find('#smallPhoto3');
    var smallPhoto4 = $('#' + $('#idIframeDesign').val()).contents().find('#smallPhoto4');
    var testo_chiudi = $('#' + $('#idIframeDesign').val()).contents().find('#testo_chiudi');

    var pos = ($('body').width() - 900) / 2;


    $('#Assistant').animate({ right: -200 }, 500, '', function() {



        $.getJSON("Prodotti.aspx?w=ProdottoTab&idArticolo=" + idArticolo, "",
           function(result) {
               imgCount = 0;
               imgTot = 1;

               testo_chiudi.text('chiudi ');
               if (result['StyleMain'] != '') element.attr('style', result['StyleMain']);
               if (result['StyleTitolo'] != '') titolo.attr('style', result['StyleTitolo']);
               if (result['StyleTesto'] != '') testo.attr('style', result['StyleTesto']);
               if (result['StyleHeaderTabella'] != '') header.attr('style', result['StyleHeaderTabella']);
               if (result['StyleDetailTabella'] != '') detail.attr('style', result['StyleDetailTabella']);

               element.width($('body').width() - margineSinistroAnimazione);
               titolo.text(result['Titolo']);
               testo.append(result['Testo']);
               cartone.text(result['NumPerCartone']);
               strati.text(result['StratiPerCartone']);
               pallet.text(result['Pallet']);
               peso.text(result['Peso']);
               prezzo.html(result['Prezzo']);
               mainPhoto.attr("src", result['PathImg1'] + '&HMax=' + hImgSchedaProdotto + '&WMax=' + wImgSchedaProdotto).load(checkImgLoad(element)); ;

               if (result['PathImg2'] != '') { imgTot++; smallPhoto1.attr("src", result['PathImg2'] + '&HMax=' + hImgSchedaProdotto + '&WMax=' + wImgSchedaProdotto).load(checkImgLoad(element)).show(); }
               else { smallPhoto1.hide(); }
               if (result['PathImg3'] != '') { imgTot++; smallPhoto2.attr("src", result['PathImg3'] + '&HMax=' + hImgSchedaProdotto + '&WMax=' + wImgSchedaProdotto).load(checkImgLoad(element)).show(); }
               else { smallPhoto2.hide(); }
               if (result['PathImg4'] != '') { imgTot++; smallPhoto3.attr("src", result['PathImg4'] + '&HMax=' + hImgSchedaProdotto + '&WMax=' + wImgSchedaProdotto).load(checkImgLoad(element)).show(); }
               else { smallPhoto3.hide(); }
               if (result['PathImg5'] != '') { imgTot++; smallPhoto4.attr("src", result['PathImg5'] + '&HMax=' + hImgSchedaProdotto + '&WMax=' + wImgSchedaProdotto).load(checkImgLoad(element)).show(); }
               else { smallPhoto4.hide(); }
           });

        //element.css('marginLeft', $('body').width());

        //element.css('display', 'block');
        //element.show(10);
        //element.animate({ marginLeft: margineSinistroAnimazione }, 2000, 'linear');

        //alert(element.attr('id'));
    });


}

function hideProdottoTab(idArticolo) {
    //$('#ProdottoTab').css("right", $("body").width() + "px");
    //$('#ProdottoTab').animate({ right: $("body").width() }, 1500);
    var element = $('#' + $('#idIframeDesign').val()).contents().find('#ProdottoTab');
    var titolo = $('#' + $('#idIframeDesign').val()).contents().find('#titolo');
    var testo = $('#' + $('#idIframeDesign').val()).contents().find('#testo');
    var header = $('#' + $('#idIframeDesign').val()).contents().find('#header');
    var detail = $('#' + $('#idIframeDesign').val()).contents().find('#detail');
    var cartone = $('#' + $('#idIframeDesign').val()).contents().find('#cartone');
    var strati = $('#' + $('#idIframeDesign').val()).contents().find('#strati');
    var pallet = $('#' + $('#idIframeDesign').val()).contents().find('#pallet');
    var peso = $('#' + $('#idIframeDesign').val()).contents().find('#peso');
    var prezzo = $('#' + $('#idIframeDesign').val()).contents().find('#prezzo');
    var mainPhoto = $('#' + $('#idIframeDesign').val()).contents().find('#mainPhoto');
    var smallPhoto1 = $('#' + $('#idIframeDesign').val()).contents().find('#smallPhoto1');
    var smallPhoto2 = $('#' + $('#idIframeDesign').val()).contents().find('#smallPhoto2');
    var smallPhoto3 = $('#' + $('#idIframeDesign').val()).contents().find('#smallPhoto3');
    var smallPhoto4 = $('#' + $('#idIframeDesign').val()).contents().find('#smallPhoto4');
    var testo_chiudi = $('#' + $('#idIframeDesign').val()).contents().find('#testo_chiudi');

    //$('#loading').hide();
    element.animate({ marginLeft: $("body").width() }, 2000, 'linear', function() {
        titolo.text('');
        testo.text('');
        cartone.text('');
        strati.text('');
        pallet.text('');
        peso.text('');
        prezzo.text('');
        mainPhoto.attr("src", '');
        smallPhoto1.attr("src", '');
        smallPhoto2.attr("src", '');
        smallPhoto3.attr("src", '');
        smallPhoto4.attr("src", '');
        element.hide();

        loadProdottoTab(idArticolo);

    });



}


function checkImgLoad(tag) {
    //if (tag.id == 'mainPhoto') resizeMainPhoto(tag);
    imgCount++;
    if (imgCount == imgTot) {
        $('#loading').hide();
        tag.css('marginLeft', $('body').width());
        tag.show(10);
        tag.animate({ marginLeft: margineSinistroAnimazione }, 2000, 'linear');
    }
}

      