/**
 * Validated a for thriught ajax
 * @param {Object} form form object
 * @param {String} url url to the validation service
 * @param {Object} event object object
 */
function ajaxformvalidator(form, url,e) {
        var status = true;
        form.find('div.textbox').removeClass('invalid');
         $.ajax({
              url: url+form.serialize(),
              dataType: 'json',
              async : false,
              success: function (data) {
                  if (data != true) {
                      $.each(data['names'], function(index, value) {
                          $('#'+value).parent().addClass('invalid');
                      });
                      var message = '';
                      $.each(data['messages'], function(index, value) {
                          message += '<p>'+value+'</p>';
                      });
                      $('#errors').html(message).css('display','block');
                      e.preventDefault();
                      status = false;
                  }
              }
        });
        return status;
}
$(document).ready(function(){
/**
 *	content bg opacity
 */ 
$('.TPLbg-content').fadeTo("slow", 0.7);


/**
 *	TOOLTIPS
 */
 $("button[title],.spec a[title]").tooltip({
	layout:'<div><em></em></div>',
	position: 'top center',
	relative:false,
	delay: 0,
	opacity:0.8
 });

/**
 * Reset default value in form
 */
/*
$('input[type="text"],input[type="email"]').focus(function() {
      if (this.value == this.defaultValue){
              this.value = '';
      }
      if(this.value != this.defaultValue){
              this.select();
      }
}).blur( function () {
      if (this.value == '') this.value=this.defaultValue;
});*/

$('label.inlined').addClass('inlined-active');

$('body').delegate('input', 'focus blur', function(e) {
        var $this = $(this);
        $this.siblings('label').toggleClass('focus');
        if($this.val() == '') {
                $this.siblings('label').removeClass('has-text');
                $this.siblings('.required').show();
                $this.siblings('.optional').show();
        }

});
$('body').delegate('input', 'keydown', function(e) {
        var $this = $(this);
        $this.siblings('label').addClass('has-text');
        $this.siblings('.required').hide();
        $this.siblings('.optional').hide();
});
$.each($('input'), function(index,value){
    var el = $(value);
    if (el.val() != '') {
        el.siblings('label').addClass('has-text');
        el.siblings('.required').hide();
        el.siblings('.optional').hide();
    }
});

/**
 * Registration form confirm validation
 */
$("#p_address_country").autocomplete({
        source: CFG_ROOT_URL+'contatti?state=1&xhr=1',
        minLength: 1,
        change: function(event, ui) {
            if(ui.item != null) $('#p_address_country_id').val(ui.item.id);
        }
});
$("#p_address_state_prov").autocomplete({
        source: CFG_ROOT_URL+'contatti?state_prov=1&xhr=1&state_id='+$("#p_address_country_id").val(),
        minLength: 1,
        change: function(event, ui) {
            if(ui.item != null) $('#p_address_state_prov_id').val(ui.item.id);
        }
});
$("#p_address_city").autocomplete({
        source:CFG_ROOT_URL+'contatti?city=1&xhr=1&state_id='+$("#p_address_country_id").val()+'&state_prov='+$('#p_address_state_prov_id').val(),
        minLength: 1,
        change: function(event, ui) {
            if(ui.item != null) $('#p_address_city_id').val(ui.item.id);
        }
});
$("#contacts_form").submit(function (e)
{
     return ajaxformvalidator($(this),CFG_ROOT_URL+'contatti?&xhr=1&',e);
}
);


/**
  * Product scroll 
  */   
 	$('.scrollable').scrollable();
/**
  * Single Product item details generation
 */	
    $('.prod_items').click(function (event) {		
        url = $(this).find('a:first').attr('href');
        $('#product_detail').load(url+'&xhr=1',{},function (){
			$('#product_detail').prepend('<div class="bodyOverlay"><span class="spriteNav close"></span></div>');
			$('.bodyOverlay').fadeTo("fast", 0.9).css('zIndex', 9950);

        });
        event.preventDefault();
    });
	$('.bodyOverlay').live('click', function(event){
		$(this).hide();
		$('#product_detail').empty();
		});

});

