$(document).ready(function(){
	$('#loglink').click(function(){
		$.blockUI({ message: $('#logform'),css: {cursor:'default',border: 0,background:'transparent',top:'30%' },
		overlayCSS:  { 
        backgroundColor: '#000', 
        opacity:         0.6, 
        cursor: 'default'  
    }
		}); 
		return false;
	});
	
	$('#chiudi').click(function(){
	   $.unblockUI();
	  return false;
	});
	
	$('#logform').submit(function(evt){
	     $.ajax({
        type: 'POST',
        url: urlo + 'fuzajax/login',
        data: $('#form-login').serialize(),
          success: function(data){
            if (data.ok == '1') {
              window.location.reload();              
            }
            else {
                Dia(data.message, 200, 100);
            }
        }
    });
    return false;
	});

//placehorder
	var default_values = new Array();
	$('#subject,#name,#email,#message').focusin(function(){
			   if (!default_values[this.id]) {
				      default_values[this.id] = this.value;
				    }
				    if (this.value == default_values[this.id]) {
				      this.value = '';
				        }
				    $(this).blur(function() {
				      if (this.value == '') {
				       this.value = default_values[this.id];
				      }
				    });
			})

	
	 $('#form-contact').bind('submit', function(event){
        event.preventDefault();
       contatta();
    });
	
	
	$('#registration').bind('click',function(event){
	   event.preventDefault();
	   regform();
	});


  $('.products-list a').bind('click',function(evt){
    evt.preventDefault();
//    if($(this).parent('.products-list').hasClass('cselect') ){
//      return;
//    }
    $('.products-list').removeClass('cselect');
    $(this).parent('.products-list').addClass('cselect');
        openProduct($(this));
  });
		
	
});
///////////////////////////////////////////////
function openProduct(obj){
  var tix=obj.attr('rel');
//if(jQuery.trim($('#single-product').html())==''){
//  $('#single-product').css('height',0);
//  
//  //.animate({
//  //  height: '0px'
//  //},600,function(){
//    openProdBack(tix);
//// });
//}
//  else{
//  $('#single-product').animate({
//    opacity: 0
//  },600, function(){
//   openProdBack(tix); 
//  });
//  }
   openProdBack(tix);
}

function openProdBack(tix){
  $.ajax({
        type: 'GET',
        url: urlo+'fuzajax/prod/'+tix,        
        success: function(data){
            if (data.ok == '1') {
           // $('#single-product').html(data.message);
              // var h = $('#prod_container').css('height');
              
			  $.modal(data.message,{
			  	overlayClose: true,
				escClose: true
			  });
			  
//                $('#single-product').animate({
//                  opacity: 1,
//                  height: h
//                },'slow');
            }
            else {
               Dia(data.message, 200, 100,'Error');
            }
        }
    });
  
}





function regform(){
  var form='<form id="regform" action="" method="post">'+
'<span class="likep"><label for="email">Email</label><br />'+
'<input type="text" id="email" name="email" /></span>'+
'<span class="likep"><label for="rpassword">Password</label><br />'+
'<input type="password" id="rpassword" name="rpassword" /></span>'+
'<span class="likep"><label for="repassword">Retype Password</label><br />'+
'<input type="password" id="repassword" name="repassword" /></span>'+
'<span class="likep"><label for="privacy" id="privacylab">Privacy</label> <a href="'+urlo+'privacy-statement" target="_blank">Read</a>'+
'<input type="checkbox" name="privacy" value="1" id="privacy" /></span>'+
'<input type="submit" id="subreg" name="subreg" disabled="disabled"  value="Sign In" />'+
'</form>'+
'<div id="error""></div>';
  
   Dia(form, 156, 286 , 'Registration');
   
    $('#regform').bind('submit', function(event){
         event.preventDefault();
         reguser();
    });
	
	 $('#subreg').css({'backgroundColor':'#ddd','color':'#aaa'});
	
    $('#repassword').keyup( function(event){
      
    if($(this).val()==$('#rpassword').val() && $('#rpassword').val()!=''){
      $('#subreg').removeAttr("disabled").css({'backgroundColor':'#1693A5','color':'#fff'});
    } else {
      $('#subreg').attr( 'disabled', 'disabled' ).css({'backgroundColor':'#ddd','color':'#aaa'});
      }
    });
  
}

function reguser(){
   $.ajax({
        type: 'POST',
        url: urlo+'fuzajax/reg',
        data: $('#regform').serialize(),
        
        success: function(data){
            if (data.ok == '1') {
              
         Dia(data.message, 200, 100,'Registration');
                
            }
            else {
                $('#error').html(data.message);
            }
        }
    });
}




function contatta(){
    $.ajax({
        type: 'POST',
        url: urlo+'fuzajax/contacts',
        data: $('#form-contact').serialize(),
        
        success: function(data){
            if (data.ok == '1') {
                $('input[type=text],textarea').val('');
         Dia(data.message, 200, 100);
                
            }
            else {
                Dia(data.message, 200, 100,'Error');
            }
        }
    });
}






function Dia(testo, w, h,title){
     if (title == null || title=='undefined') {
        title = '';
    }
	else {
		title='<p id="title-dia">'+title+'</p>'
	}
		
    if (testo == null) {
        testo = '';
    }
    if (w == null) {
        w = 300;
    }
    if (h == null) {
        h = 180;
    }
     var t='<div id="dialog" style="width:'+w+'px;min-height:'+h+'px !important; height: auto"><span id="close-dia">X</span>'+testo+'</div>';
	//var t='<div id="dialog" style="width:'+w+'px;height:'+h+'px !important"><span id="close-dia">X</span>'+testo+'</div>';
    //var t='<div id="dialog" style="width:auto;height:auto;"><span id="close-dia">X</span><p>'+testo+'</p></div>';
  
    
    $.blockUI({ 
        message: t, 
      css: { 
        cursor:'default',
        border: 0,
        background:'transparent',
        top:'30%' 
         },
      overlayCSS:  { 
        backgroundColor: '#000', 
        opacity:         0.6, 
        cursor: 'default'  
    }
    }); 
    
    $('#close-dia').click(function(){
       $.unblockUI(); 
       return false;
    });
}
