﻿/// <reference path="jQuery-1.4.2.js"/>

$(document).ready(function() {

    caricaelenco_categorie();
    caricaelenco_marche();
    carica_promo(0);


    visualizza('elenco');    

    $("#ctl00_lnkhome").click(function(){
        caricaelenco_categorie();
        caricaelenco_marche();
    });



    // selezione voce MARCA
    $("#elenco_marca").change(function() {
    
        $("#updateprogress").slideDown("slow");
        
        //alert( $("#elenco_marca option:selected").val().replace("'","|"));
        var count = $('#elenco_tipologia option:selected').val();
        // se non c'è selezionato nulla allora ricarico l'elenco
        if (count==0) {
            $.ajax({
                type: "POST",
                url: "Default.aspx/getelencotipologie",
                data: "{'_marca':'" + $("#elenco_marca option:selected").val().replace("'","|") + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                    ////alert(msg);
                    $("#elenco_tipologia").html(msg);
                    
                    //
                    // caricamento dei dati 
                    //
                    $.ajax({type: "POST",
                    url: "Default.aspx/getdati",
                    data: "{'_marca':'" + $("#elenco_marca option:selected").val().replace("'","|") + "','_tipologia':'" + $('#elenco_tipologia option:selected').val() + "','_pagina':'1'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(msg) {
                            $("#elencoarticoli").html(msg);
                            // elenco delle tipologie legata alla marca
                            visualizza('elenco');
                            $("#updateprogress").slideUp("slow");
                    },
                    error: function(msg) {
                        //alert('err#getdati');
                    }
                    });       
                    
                    
        
                    
                },
                error: function(msg) {
                    //alert('err#getelencotipologie');
                }
                });
        }
        else
        {
                $("#updateprogress").slideDown("slow");
                            //
                    // caricamento dei dati 
                    //
                    $.ajax({type: "POST",
                    url: "Default.aspx/getdati",
                    data: "{'_marca':'" +  $("#elenco_marca option:selected").val().replace("'","|") + "','_tipologia':'" + $('#elenco_tipologia option:selected').val() + "','_pagina':'1'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(msg) {
                            $("#elencoarticoli").html(msg);
                            
                            visualizza('elenco');

                            $("#updateprogress").slideUp("slow");

                            // elenco delle tipologie legata alla marca
                    },
                    error: function(msg) {
                      //  alert('err#selezione#tipologia');
                            $("#updateprogress").slideUp("slow");
                    }
                    });          
        
        }

        
   

    });

    // selezione voce TIPOLOGIA
    $("#elenco_tipologia").change(function() {
        
        var count = $('#elenco_marca option:selected').val();
        // se non c'è selezionato nulla allora ricarico l'elenco
        if (count==0) {
            $("#updateprogress").slideDown("slow");
            $.ajax({
                type: "POST",
                url: "Default.aspx/getelencomarche",
                data: "{'_tipologia':'" +  $("#elenco_tipologia option:selected").val().replace("'","|") + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                    ////alert(msg);
                    $("#elenco_marca").html(msg);
                    //
                    // caricamento dei dati 
                    //
                    $.ajax({type: "POST",
                    url: "Default.aspx/getdati",
                    data: "{'_marca':'" +  $("#elenco_marca option:selected").val().replace("'","|") + "','_tipologia':'" + $('#elenco_tipologia option:selected').val() + "','_pagina':'1'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(msg) {
                            $("#elencoarticoli").html(msg);
                            
                            visualizza('elenco');

                            $("#updateprogress").slideUp("slow");

                            // elenco delle tipologie legata alla marca
                    },
                    error: function(msg) {
                        //alert('err#selezione#tipologia');
                            $("#updateprogress").slideUp("slow");
                    }
                    });  

                },
                error: function(msg) {
                   // alert('err#getelencomarche#01');
                }
                });
        }
        else
        {
                $("#updateprogress").slideDown("slow");
                            //
                    // caricamento dei dati 
                    //
                    $.ajax({type: "POST",
                    url: "Default.aspx/getdati",
                    data: "{'_marca':'" +  $("#elenco_marca option:selected").val().replace("'","|") + "','_tipologia':'" + $('#elenco_tipologia option:selected').val() + "','_pagina':'1'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(msg) {
                            $("#elencoarticoli").html(msg);
                            
                            visualizza('elenco');

                            $("#updateprogress").slideUp("slow");

                            // elenco delle tipologie legata alla marca
                    },
                    error: function(msg) {
//                        alert('err#selezione#tipologia');
                            $("#updateprogress").slideUp("slow");
                    }
                    });  

        }
        

        
        
    });



    $("#updateprogress").slideDown("slow");    
    //carico la vetrina
    $.ajax({type: "POST",
        url: "Default.aspx/getvetrina",
        data: "{'_p':'1'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            
                $("#elencoarticoli").html(msg);
                $("#updateprogress").slideUp("fast");    
                // elenco delle tipologie legata alla marca
        },
        error: function(msg) {
            $("#updateprogress").slideUp("fast");    
        }
    });



	if (browser_version()==false)
	{
         $("#panel_browser").show();
	}

    
});

 function selezione_dati(_categoria)
{   
    $("#updateprogress").slideDown("slow");


    // seleziono la categoria

    $.ajax({type: "POST",
        url: "Default.aspx/setcategoria",
        async: false,
        timeout: 1000, 
        data: "{'_c':'" + _categoria + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            
        },
        error: function(msg) {
            //alert('err#getdati#selezione');
        }
    });       




    
     $.ajax({
            type: "POST",
            url: "Default.aspx/getelencomarche",
            data: "{'_tipologia':'0'}",

            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                ////alert(msg);
                $("#elenco_marca").html(msg);
                
                
               // carico i dati
                $.ajax({type: "POST",
                    url: "Default.aspx/getdati",
                    data: "{'_marca':'','_tipologia':'0','_pagina':'1'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(msg) {
                        
                            $("#elencoarticoli").html(msg);
                            
                            carica_promo(_categoria);
                            
                              $("#updateprogress").slideUp("slow");
                            // *******************
                            // elenco delle tipologie legata alla marca
                    },
                    error: function(msg) {
                        //alert('err#getdati#selezione');
                    }
                });       
                
                caricaelenco_tipologie();
    
                
            },
            error: function(msg) {
                //alert('err#getelencomarche');
                $("#updateprogress").slideUp("slow");
            }

        });
		

 

    visualizza('elenco');
}


function caricaelenco_categorie()
{

    //
    // carico elencocategorie
    //
    $.ajax({
        type: "POST",
        url: "Default.aspx/getelencocategorie",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            
            $("#elencocategorie").html(msg);
    
    
            $("#elencocategorie img").each(function(){
                    $(this).attr('src',$(this).attr('src').replace("h.jpg",".jpg"));
                 
                    $(this).hover(
                        function(event){
                            if ($(this).attr('class') != 'selezionato')
                            {
                                $(this).css("border","1px solid #800080");
                                $(this).attr('src',$(this).attr('src').replace("h.jpg",".jpg"));
                            }

                        },
                        function(event){
                            if ($(this).attr('class') != 'selezionato')
                            {
                                $(this).css("border","");
                                $(this).attr('src',$(this).attr('src').replace(".jpg","h.jpg"));
                            }
                        }
                        );
                     });
                  
                 $("#elencocategorie img").each(function(){
                     if ($(this).attr('class') != 'selezionato')
                     {
                        $(this).attr('src',$(this).attr('src').replace(".jpg","h.jpg"));
                     }
                    });
                
                
                // CLICK : seleziona categoria
                $("#elencocategorie img").click(function(){ 
                 
                    $("#elencocategorie img").each(function(){
                     $(this).removeClass('selezionato');
                     $(this).css("border","1px solid #ffffff");
                    });

                    // modifche grafiche    
                    $("#elencocategorie img").each(function(){
                    
                        $(this).attr('src',$(this).attr('src').replace("h.jpg",".jpg"));
                    });
                 
                    $("#elencocategorie img").each(function(){
                        
                        $(this).attr('src',$(this).attr('src').replace(".jpg","h.jpg"));
                    
                    });


                    $(this).attr('src',$(this).attr('src').replace("h.jpg",".jpg"));
                    
                    
                    $(this).css("border","1px solid #800080");
                    $(this).attr('class','selezionato');
                    $(this).attr('onmouseover','');
                    $(this).attr('onmouseout','');
               
      
          
            });
        },
        error: function(msg) {
            //alert('err#3');
        }

    });

}



function caricaelenco_tipologie()
{

   // ricarico completamente le tipologie  ( solo della catagoria )    
    $.ajax({
        type: "POST",
        url: "Default.aspx/getelencotipologie",
        data: "{'_marca':''}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            ////alert(msg);
            $("#elenco_tipologia").html(msg);

        },
        error: function(msg) {
            //alert('err#getelencotipologie');
        }
        });    

}

function caricaelenco_marche()
{

    //
    // carico elenco marche , al caricamento della pagina
    //
    $.ajax({
        type: "POST",
        url: "Default.aspx/getelencomarche",
        data: "{'_tipologia':'0'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            ////alert(msg);
            $("#elenco_marca").html(msg);
           
           
            $.ajax({
            type: "POST",
            url: "Default.aspx/getelencotipologie",
            data: "{'_marca':''}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                ////alert(msg);
                $("#elenco_tipologia").html(msg);

            },
            error: function(msg) {
                //alert('err#getelencotipologie');
            }

            });

        },
        error: function(msg) {
            //alert('err#getelencomarche');
        }

    });

}


function visualizza(obj, id)
{
    switch(obj)
    {
        case 'dettaglio' :
            
            $("#updateprogress").slideDown("slow");    
            $("#btnacquista").show();
            $("#elencoarticoli").hide('normal');


           $.ajax({type: "POST",
                url: "Default.aspx/getarticolo",
                data: "{'_modello':'" + id + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) {
                        $("#dettaglio").html(msg);
                        $("#dettaglio").show('normal');
                        $("#updateprogress").slideUp("fast");    
                },
                error: function(msg) {
                    //alert('err#getmodel');
                    $("#updateprogress").slideUp("fast");    
                }
                
            });       
    

            break;
        
        default:
            $("#elencoarticoli").show('normal');
            $("#dettaglio").hide('normal');
        
        break;
    
    }

}


function paginatore(value)
{

    $("#updateprogress").slideDown("slow");    
    
    $.ajax({type: "POST",
        url: "Default.aspx/getdati",
        data: "{'_marca':'" +  $("#elenco_marca option:selected").val().replace("'","|") + "','_tipologia':'" + $('#elenco_tipologia option:selected').val() + "','_pagina':'" + value + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
                $("#elencoarticoli").html(msg);
                            $("#updateprogress").slideUp("fast");    
                // elenco delle tipologie legata alla marca
        },
        error: function(msg) {
            //alert('err#paginatore');
                        $("#updateprogress").slideUp("fast");    
        }
    });       
      
    
}

function acquista()
{

    $("#updateprogress").slideDown("slow");    
    
    var _taglia = $('#taglia option:selected').val();
    
    if (_taglia > 0 ) {

        var cartnumber=0;
        if ( readCookie("mydernacart")!= null ) 
        {
            cartnumber = readCookie("mydernacart");
            ////alert(cartnumber);
        }
                           
    $.ajax({type: "POST",
        url: "Default.aspx/acquista",
        data: "{'_carrello':'" + cartnumber + "','_articolo':'" + _taglia + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
                
                if (msg > 0 ) {
                    
                    createCookie("mydernacart",msg,5);
                    
                    $("#btnacquista").fadeOut("slow");
                    
                    
                }
                else
                {
                    //alert('Attention, purchase problem, try later ! Thank you');
                }
                $("#updateprogress").slideUp("slow");    
                //location.reload(true);
        },
        error: function(msg) {
            //alert('err#acquisto');
            $("#updateprogress").slideUp("slow");    
        }
    });       
       
    
    }
}


function carica_promo(_categoria)
{
        //getpromozione
        $.ajax({type: "POST",
            url: "Default.aspx/getpromozione",
            data: "{'_categoria':'" + _categoria + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
    
              $("#elencopromo").html(msg);
            
              $('.slideshow').cycle({
                   fx: 'scrollUp',
                   timeout: 6000, 
		           speed:2000
                    });

                  $('.slideshow').hover(
                 function(){
                  $('.slideshow').cycle('pause');
                      },
                      function(){
                   $('.slideshow').cycle('resume');
              }

            );
            
            

            },
            error: function(msg) {
            //alert('err#promo');
            }
        });   

}





function preview(_src)
{

    $('#container').empty();
    var _titolo = $('#titolo').html(); 
   
    _titolo = '<div id="titolo" style="float:left;text-align:left;width:100%;">'  +  _titolo            //border-bottom : solid 1px #C6C6C6;
    _titolo = _titolo + "</div>"
    _titolo = _titolo.replace("onclick","fake");
    
    //alert(_src);
       
        _src = _src.replace("thumb_2","thumb_D");
		var img = new Image();
		$(img).load(function() {
			$(this).hide();
			
			$('#container').removeClass('loading').html(this);
//			$('.fullSizeImage').wrap('<a class="fullImageLink"></a>');
			$('#container a').attr('href', _src);
			$(this).css({"height": 'auto', "width": 'auto'});
			iHeight = $(this).height();
			lHeight = $('#container').height();
			
			////alert("1) " + iHeight );
			////alert("2) " + lHeight );
			
			if(lHeight == iHeight) {
				$(this).fadeIn();
			} else {
				    tempImg = $(this);
                    $('#container').animate({height: iHeight+50}, 600).animate({width: 800}, 600+50, function() {
				    //$('#container').animate( {height: iHeight}, 400).animate({width: 400}, 400, function() {
				    $(tempImg).fadeIn();
				     
				    });
				
			
			}
		    $(_titolo).prependTo("#container");

			$('#container').css("margin","auto");
		    $("#container").css("border","solid 3px #800080");
		    $("#container").css("background-color","#ffffff");
	    
	    
		    ////alert($('#container').html());
		//}).attr('src', _src).addClass('fullSizeImage');
		}).attr('src', _src);
        
       
        $("#preview").show("fast");
        

}

//--------------------------------------------------------------------------------------------------------
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


/*

     $.ajax({
            type: "POST",
            url: "Default.aspx/getelencomarche",
            data: "{'_categoria:'" + _categoria + "','_tipologia':'0'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                ////alert(msg);
                $("#elenco_marca").html(msg);
            },
            error: function(msg) {
                //alert('err#selezione_dati#getelencomarche ' + msg.statusText );
            }

        });


*/


