// JScript File
jQuery(document).ready(function () {
//
    //jQuery("#liMenuShop").append('<div id="menuShop" style="position:absolute;"></div>');
    
    //jQuery("#LICategorie").append('<div id="menuCategorie"></div>');
    jQuery("body").append('<div id="dettaglio"></div>');
    //jQuery("body").prepend('<div id="checkout"></div>');
    jQuery("body").append('<div id="addCarrello"></div>');
    jQuery("body").append('<div id="sendMail"></div>');
    jQuery("body").append('<div id="modPsw"></div>');
    jQuery("body").append('<div id="misureArticolo"></div>');
    jQuery("#pnlLoggato").append('<div id="miniCarrello"></div>');
    jQuery("#pnlLogin").append('<div id="miniCarrello"></div>');

//    jQuery("#dettaglio").dialog({
//        bgiframe: true,
//        autoOpen: false,
//        width: 600,
//        height: 500,
//        closeOnEscape: true,
//        modal: true,
//        resizable: false,
//        draggable: false,
//        buttons: {
//            'Chiudi': function() {
//	                jQuery(this).dialog('close');
//                }
//        },
//        close: function() {
//            
//        }
//    });
    jQuery("#dettaglio").dialog({
        bgiframe: true,
        autoOpen: false,
        width: 900,
        height: 620,
        closeOnEscape: true,
        modal: true,
        resizable: false,
        draggable: false 
    });
    
    jQuery("#misureArticolo").dialog({
        bgiframe: true,
        autoOpen: false,
        width: 900,
        height: 580,
        closeOnEscape: true,
        modal: true,
        resizable: false,
        draggable: false 
    });
//    
//    jQuery("#checkout").dialog({
//        bgiframe: true,
//        autoOpen: false,
//        width: 600,
//        height: 500,
//        closeOnEscape: false,
//        modal: true,
//        resizable: false,
//        draggable: false,
//        buttons: {
//			'Continua': function() {
//			    if (checkForm()) {
//				    var str=jQuery('#registra').serialize();
//                    jQuery("#checkout").load("checkout.aspx?" + str + "&nocache=" + new Date().getTime());
//                }
//			},
//			'Annulla': function() {
//				jQuery(this).dialog('close');
//			}
//		}
//    });
    
    jQuery("#modPsw").dialog({
        bgiframe: true,
        autoOpen: false,
        width: 550,
        height: 500,
        closeOnEscape: true,
        modal: true,
        resizable: false,
        draggable: false 
    });
    
    jQuery("#addCarrello").hide();
    jQuery("#sendMail").hide();
    //jQuery("#menuShop").load("elencoMarche.aspx?nocache=" + new Date().getTime());
    //jQuery("#menuCategorie").load("elencoCategorie.aspx?nocache=" + new Date().getTime());
    miniCarrello();
})


function misure(id){
//    jQuery("#misureArticolo").load("misureArticolo.aspx?id=" + id + "&nocache=" + new Date().getTime(),'',function(){
//        jQuery("#misureArticolo").dialog('open');
//    });
    jQuery("#divMisure").css("top","-400px");
    jQuery("#divMisure").css("left","35px");
    jQuery("#divMisure").show();
    /*var offset = jQuery("#divMisure").offset();
    jQuery('html, body').animate({scrollTop: offset.top }, 'fast');*/
}

function chiudiMisure(){
    //jQuery("#misureArticolo").dialog('close');
    jQuery("#divMisure").hide();
}

function chiudiDettaglio(){
    jQuery("#dettaglio").dialog('close');
}

function dettaglioArticoloCart(id){
    jQuery("#dettaglio").load("dettaglio.aspx?id=" + id + "&cart=SI&nocache=" + new Date().getTime(),'',function(){
        jQuery("#dettaglio").dialog('open');
    });
}

function dettaglioArticolo(id){
    jQuery("#dettaglio").load("dettaglio.aspx?id=" + id + "&nocache=" + new Date().getTime(),'',function(){
        jQuery("#dettaglio").dialog('open');
    });
}

function aggiungiACarrelloDet(id){
    var qta = jQuery("#qtaDettaglio").val();
    if (qta.length==0 || isNaN(qta)){
        alert('Quantit\u00E0 non corretta');
        return;
    }
    var mag = jQuery("#optDettaglio").val();
    var qtaDisp = parseInt(getDisponibilita(mag));
    var qtaACarrello = parseInt(getQtaACarrello(mag));
    if (qta > (qtaDisp - qtaACarrello)) {
        alert("Prodotto non disponibile per queste quantità.");
	qta = qtaDisp - qtaACarrello;
	if (qta > 0){
	   alert("La quantit\u00E0 disponibile - " + qta + " - verr\u00E0 aggiunta a carrello.");
	}
    }
    if (qta > 0){
	    jQuery("#addCarrello").load("addCarrello.aspx?id=" + mag + "&qta=" + qta + "&nocache=" + new Date().getTime(),'',function(){
	        jQuery("#addCarrello").css("top", ( jQuery(window).height() - jQuery("#addCarrello").height() ) / 2+jQuery(window).scrollTop() + "px");
	        jQuery("#addCarrello").css("left", ( jQuery(window).width() - jQuery("#addCarrello").width() ) / 2+jQuery(window).scrollLeft() + "px");    
	        jQuery("#addCarrello").css("margin", 0)
	        jQuery("#addCarrello").fadeIn(300, function () {
	              window.setTimeout('jQuery("#addCarrello").animate({"width": "toggle", "height": "toggle", "opacity": "toggle", "left": jQuery("#miniCarrello").offset().left, "top": jQuery("#miniCarrello").offset().top}, 300,"linear",function(){miniCarrello();jQuery("#addCarrello").fadeOut(300);} );',500);
	             
	        });
	    });
    }
}

function getDisponibilita(mag){
    var q = "?nocache=" + new Date().getTime() + "&id=" + mag;
    var qtaDisp = jQuery.ajax({  url: "getDisponibilita.aspx" + q, async: false }).responseText;
    return qtaDisp;
}

function getQtaACarrello(mag){
    var q = "?nocache=" + new Date().getTime() + "&id=" + mag;
    var qtaACarrello = jQuery.ajax({  url: "getQtaACarrello.aspx" + q, async: false }).responseText;
    return qtaACarrello;
}

function aggiungiACarrelloDaCarrello(id){
	            
    var qta = jQuery("#qta" + id).val();
    if (qta.length==0 || isNaN(qta)){
        alert('Quantita\' non corretta');
        return;
    }
    var mag = jQuery("#opt" + id).val();
    var qtaDisp = parseInt(getDisponibilita(mag));
    var qtaACarrello = parseInt(getQtaACarrello(mag));
    if (qta > (qtaDisp - qtaACarrello)) {
        alert("Prodotto non disponibile per queste quantita\'.");
	    qta = qtaDisp - qtaACarrello;
	    if (qta > 0){
	       alert("La quantita\' disponibile - " + qta + " - verra\' aggiunta a carrello.");
	    }
    }
    if (qta > 0){
        jQuery("#addCarrello").load("addCarrello.aspx?id=" + mag + "&qta=" + qta + "&nocache=" + new Date().getTime(),'',function(){
            jQuery("#addCarrello").css("top", ( jQuery(window).height() - jQuery("#addCarrello").height() ) / 2+jQuery(window).scrollTop() + "px");
            jQuery("#addCarrello").css("left", ( jQuery(window).width() - jQuery("#addCarrello").width() ) / 2+jQuery(window).scrollLeft() + "px");    
            jQuery("#addCarrello").css("margin", 0)
            jQuery("#addCarrello").fadeIn(300, function () {
                  window.setTimeout('jQuery("#addCarrello").animate({"width": "toggle", "height": "toggle", "opacity": "toggle", "left": jQuery("#miniCarrello").offset().left, "top": jQuery("#miniCarrello").offset().top}, 300,"linear",function(){miniCarrello();jQuery("#addCarrello").fadeOut(300);window.location="carrello.aspx";} );',500);
            });
        });
    }

}


function aggiungiACarrello(id){
	            
    var qta = jQuery("#qta" + id).val();
    if (qta.length==0 || isNaN(qta)){
        alert('Quantita\' non corretta');
        return;
    }
    var mag = jQuery("#opt" + id).val();
    var qtaDisp = parseInt(getDisponibilita(mag));
    var qtaACarrello = parseInt(getQtaACarrello(mag));
    if (qta > (qtaDisp - qtaACarrello)) {
        alert("Prodotto non disponibile per queste quantita\'.");
	qta = qtaDisp - qtaACarrello;
	if (qta > 0){
	   alert("La quantita\' disponibile - " + qta + " - verra\' aggiunta a carrello.");
	}
    }
    if (qta > 0){
        jQuery("#addCarrello").load("addCarrello.aspx?id=" + mag + "&qta=" + qta + "&nocache=" + new Date().getTime(),'',function(){
            jQuery("#addCarrello").css("top", ( jQuery(window).height() - jQuery("#addCarrello").height() ) / 2+jQuery(window).scrollTop() + "px");
            jQuery("#addCarrello").css("left", ( jQuery(window).width() - jQuery("#addCarrello").width() ) / 2+jQuery(window).scrollLeft() + "px");    
            jQuery("#addCarrello").css("margin", 0)
            jQuery("#addCarrello").fadeIn(300, function () {
                  window.setTimeout('jQuery("#addCarrello").animate({"width": "toggle", "height": "toggle", "opacity": "toggle", "left": jQuery("#miniCarrello").offset().left, "top": jQuery("#miniCarrello").offset().top}, 300,"linear",function(){miniCarrello();jQuery("#addCarrello").fadeOut(300);} );',500);
                 
            });
        });
    }

}

function miniCarrello(){
    jQuery("#miniCarrello").load("miniCarrello.aspx?nocache=" + new Date().getTime());
}


function chiudiCheckout(url){
    //jQuery("#checkout").dialog('disable');
    window.setTimeout('window.location = \'' + url + '\'',2000);
}

function caricaImmagineArticolo(url){
    jQuery("#fotoArticolo").attr('src',url);
}

//function misure(url,misure){
//    jQuery('#divMisure').slideToggle("slow", function () {
//        if (jQuery('#divMisure').attr('vis') != 'SI'){
//            jQuery("#fotoArticolo").attr('src',misure);
//            jQuery("#divMisure").attr('vis','SI');
//        }else{
//            jQuery("#fotoArticolo").attr('src',url);
//            jQuery("#divMisure").removeAttr('vis');
//        }
//      });
//}
    
function newPsw() {    
    var emailUtente = jQuery("#txtUtenteLogin").val();
    if (emailUtente.length==0){
        alert('Per favore inserisci l\'indirizzo e-mail con il quale ti sei registrato nel campo "utente" \npoi clicca nuovamente su "recupera password"');
        return;
    }
    jQuery("#sendMail").html('Invio mail in corso...');
    jQuery("#sendMail").css("top", ( jQuery(window).height() - jQuery("#sendMail").height() ) / 2+jQuery(window).scrollTop() + "px");
    jQuery("#sendMail").css("left", ( jQuery(window).width() - jQuery("#sendMail").width() ) / 2+jQuery(window).scrollLeft() + "px");    
    jQuery("#sendMail").fadeIn(500, function () {
          var str='emailUtente='+emailUtente+"&nocache=" + new Date().getTime();
          jQuery("#sendMail").load("cambiaPsw.aspx?" + str,'',function(){
            //if (!jQuery("#sendMail").html().indexOf('errore')>=0 && !jQuery("#sendMail").html().indexOf('Errore')>=0){
                //resetForm(id);
            //}
            window.setTimeout('jQuery("#sendMail").fadeOut(500)',2000);
          });
    });			
}

function modPsw(){
    jQuery("#modPsw").load("modPsw.aspx?nocache=" + new Date().getTime(),'',function(){
        jQuery("#modPsw").dialog('open');
    });
}
function chiudiModPsw(){
    window.setTimeout('jQuery("#modPsw").fadeOut(500)',2000);
    jQuery("#modPsw").dialog('close');
}


function apriMailFB(){
window.open('http://www.facebook.com/pages/Sporting-shop/306605141026?v=wall');
return false;
    jQuery("#dettaglio").load("/html/FaceBook.htm?" + new Date().getTime(),'',function(){
        jQuery("#dettaglio").dialog('open');
    });
}
function chiudiMailFB(){
    jQuery("#dettaglio").dialog('close');
}
function inviaEmailFB(){
    var email=jQuery('#emailFB').val();
    var nome=jQuery('#nomeFB').val();
    jQuery('#lblErroreFB').html('Invio E-mail... - Sending E-mail...');
    var q = "?nocache=" + new Date().getTime() + "&id=" + email + "&n=" + nome;
    var ret = jQuery.ajax({  url: "/sendMailFB.aspx" + q, async: false }).responseText;
    if (ret='OK') {
        window.open('http://www.facebook.com/pages/Sporting-shop/306605141026?v=wall');
        chiudiMailFB();
    }else{
        jQuery('#lblErroreFB').html('Errore nell\'invio E-mail... - Error sending E-mail...');
    }
} 


function intOrderClose(){
    jQuery("#dettaglio").dialog('close');
}

function intOrder(id){
    jQuery("#dettaglio").load("html/spedizioniInternazionali.htm?nocache=" + new Date().getTime(),'',function(){
        jQuery("#dettaglio").dialog('open');
    });
}
