var fpw,fph,tp,cp;

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function mostra_carrello(){
    nascondi_menuutente();
    $('#cart_preview').html('');
	if($('#cart_preview:visible').length) return false;
	//nascondo il menu utente
	$('.usermenu',$('#show_cart').parent()).hide();
	$('#cart_preview').addClass('loading').show();
	
	function mostra_risultati(data){
		$('#cart_preview').html(data).css('height','auto').removeClass('loading');	
	}
	
	$.get('cart_service.php',mostra_risultati);
	$('#cart_preview').mouseleave(function(){
		$(this).fadeOut();
		clearTimeout(nc);
	})
}

function nascondi_carrello(){
	$('#cart_preview').fadeOut();
}

function nascondi_menuutente(){
	$('.usermenu').fadeOut();
}


var nc,nmu,timeout=1000;

$(document).ready(function(){
    
    $('div#bottom p.right a img')
    .mouseenter(function(){
        $(this).animate({'top':'0px'});
    })
    .mouseleave(function(){
        $(this).animate({'top':'4px'});
    })


	$('table.inkarta tr:odd').addClass('odd');

    if($('.tips').length){
            
        $('.tips').each(function(){
            var tips = $(this);
            
            if(tips.hasClass('top')){            
                $(tips).qtip({
                  content: {
                     text: false
                  },
                  position: {
                    corner:{
                        tooltip: 'bottomMiddle',
                        target: 'topMiddle'
                    }
                  },
                  style: {
                    name:'dark',
                    tip:true
                  }
                  
                });
            }else{
                $(tips).qtip({
                  content: {
                     text: false 
                  },
                  position: {
                    corner:{
                        tooltip: 'rightMiddle',
                        target: 'leftMiddle'
                    }
                  },
                  style: {
                    name:'dark',
                    tip:true
                  }
                  
                });
                
            }

        })
    }
    
    if($('div.tabs').length)
        $('div.tabs').tabs();
        
    if($('div.accordion').length)
        $('div.accordion').accordion({autoHeight:false});

	//Tendine
	$('#show_cart').mouseenter(function() {			
		mostra_carrello();
		clearTimeout(nc);
		nc = setTimeout('nascondi_carrello()',timeout);
	}).mouseover(function(){
		clearTimeout(nc);		
	}).mouseleave(function(){
		nc = setTimeout('nascondi_carrello()',timeout);
	});
		
	$('#cart_preview').click(function(){
		location.href="carrello.php";
	}).mouseover(function(){
		clearTimeout(nc);
	}).mouseleave(function(){
		nc = setTimeout('nascondi_carrello()',timeout);
	})
	
	$('#usermenu').mouseenter(function(e) { 
		e.preventDefault();
		$('#cart_preview').hide();
		$('.usermenu',$(this).parent()).show();
		return false;
	}).click(function(e){
		e.preventDefault();
		return false;	
	}).mouseover(function(){
		clearTimeout(nmu);
	}).mouseleave(function(){
		nmu = setTimeout('nascondi_menuutente()',timeout);
	});
	
	$('div.usermenu').mouseleave(function(){
		$(this).fadeOut()
		nmu = setTimeout('nascondi_menuutente()',timeout);
	}).mouseover(function(){
		clearTimeout(nmu);
	})
	
	$('div.menu_categorie select').change(function(){
	   $(this).parent().submit();
       if($(this).attr('id') == 'select_categoria') $('#select_linea').select_replacement_loading();
       if($(this).attr('id') == 'select_linea') $('#select_genere').select_replacement_loading();
	});
	
	//select categoria,linea e genere			
	$('div.menu_categorie select').select_replacement(); 


	$('select[name="language"]')
    .change(function(){
      $('form#fl').submit(); 
    })    
    .select_replacement({img:true}); 


    $('#form_categoria').ajaxForm({
        success:        caricaSelect,  // post-submit callback
        dataType:       'json'
    });


    function caricaSelect(response){
        if(response['status']==0){
            location.href=response['location'];
        }else{
            $('#select_linea').html(response['select_linea']).select_replacement_update();
            $('#select_genere').html(response['select_genere']).select_replacement_update();            
        }
    }
	
	//centro il menu "artistico" e installo l'handler per il mouseover
	var nli = $('ul.hmenu li').length;
    //se ho solo 2 icone faccio finta di averne 4 altrimenti la visualizzazione è bruttina!
    if(nli<3) nli=4;
	var wli = parseInt(parseInt($('ul.hmenu').width())/nli);		
	$('ul.hmenu li').css('width',wli+'px');
    $("ul.hmenu li a").before('<img class="btnshadow" src="img/icons-shadow.jpg" height="20" alt="" />');


    $("ul.hmenu li").hover(function() {
        if($(this).hasClass('selected')) return false;        
    	var e = this;
        $(e).find("a").stop().animate({ top: "-14px" }, 250, function() {
        	$(e).find("a").animate({ top: "-10px" }, 250);
        });
        $(e).find("img.btnshadow").stop().animate({ width: "120px", marginLeft: "-60px", opacity: 0.25 }, 250);
    },function(){
        
        if($(this).hasClass('selected')) return false;        
    	var e = this;
        $(e).find("a").stop().animate({ top: "4px" }, 250, function() {
        	$(e).find("a").animate({ top: "0px" }, 250);
        });
        $(e).find("img.btnshadow").stop().animate({ width: "150px", marginLeft: "-80px", opacity: 1 }, 250);
        
    });


    				
    if($("#btn_login").length){
    	$("#btn_login").fancybox({
    		width:300,
    		onComplete: function(){
    					
                function loginResponse(xml){
                	$.fancybox.hideActivity();
                	
                	var response = $(xml).find("response");
                	var response_code = parseInt(response.text());
                	var uid = $(xml).find("uid");
                	var uid_code = uid.text();
                	if( response_code == 1){
                		window.location.reload();					
                    }else{
                        //bloccato
                        if(response_code == 2){
                            location.href='account.php?account=blocked'
                        }else{
                            //contratto
                            if(response_code == 3){
                                $.fancybox.close();
                                
                                    var html='<iframe src="contratto/contratto.html" frameborder="0" width="100%" height="400px" style="margin-bottom:20px"></iframe>';
                                    html+='<form action="contratto.php" method="post"><input type="hidden" name="uid" value="'+uid_code+'" />';
                                    html+='<input type="hidden" name="redirect" value="'+document.URL+'" />';                                    
                                    html+='<button type="submit" name="accept" value="accept" class="right">Accetta</button>';
                                    html+='<button type="submit" name="decline" value="decline" class="left">Annulla</button></form>';
                                    
                                    $.modal(
                                    html,
                                    {
                                   	    closeHTML:"",
                                        close:false,
                                        containerCss:{
                                            height:450,
                                    		width:850,
                                            padding:10,
                                            backgroundColor:"#ffffff"
                                        },
                                        opacity:40,
	                                    overlayCss: {backgroundColor:"#181315"}                                    
                                    });     
                                                
                                    }else{
                    		  $("#loginmodal-login-error").show();
                    		  $.fancybox.resize();
                            }
                        }
                		
                	}
                }
                	
                function loginSubmit(formData, jqForm, options){
                   for (var i=0; i < formData.length; i++) { 
                        if (!formData[i].value) { 
                			$("#loginmodal-login-error").show();
                			$.fancybox.resize();				             
                            return false; 
                        } 
                    }					
                	$.fancybox.showActivity();
                }
                
                                			  
				var options = { 
				    beforeSubmit:  loginSubmit,
				    success:       loginResponse,
				    dataType:  	   'xml'
				  }; 
                  
				$('#user_login_form').ajaxForm(options); 			

    		}
    	
    	});
    }

    
    $("a#show_sample").fancybox({width:850,height:500,type:'iframe',showCloseButton:false});

    	
    if($(".popup").length)
	   $(".popup").fancybox({width:450,height:400,type:'iframe'});
    if($(".popup_big").length)
    	$(".popup_big").fancybox({width:850,height:500,type:'iframe'});
    if($(".detail_popup").length)	
        $(".detail_popup").fancybox({autoDimensions:false,width:450,height:400,type:'iframe'});
    
     if($(".gallery").length){        
        $(".gallery").fancybox({
            titlePosition:'inside'
        });
    }
    
    if($('input.datepicker_extended').length) 
        $('input.datepicker_extended').datepicker({'dateFormat':'DD d MM yy'});
        
    if($('input.datepicker_simple').length) 
        $('input.datepicker_simple').datepicker({'dateFormat':'dd MM yy'});
        
    if($('input.datepicker').length) 
        $('input.datepicker').datepicker({'dateFormat':'dd-mm-yy'});
    
    $('a.toggle').click(function(e){
        e.preventDefault();
        if($(this).attr('rel').length==0){
            $(this).attr('rel',$(this).html());
            $(this).html(nascondi);                        
        }else{            
            $(this).html($(this).attr('rel'));
            $(this).attr('rel','')
        }
        $('#box_'+$(this).attr('id')).toggle();
        return false;
    })
    
    
})
