	var timing = 500;
	
	function update_cart_qty() {
		var items = $('.cw-element').length;
		$('#items_counter').html('<br />' + items + ' items');
		$('#shopping_cart_dialog_menu').css('display', 'block');
		$('#shopping_cart_dialog_menu').css('margin-top', '-7px');
		$('#shopping_cart_dialog_menu').css('margin-bottom', '-6px');
		
		if (items > 0) {
		    $('#shopping_cart_dialog').removeClass('hidden');
		    var base = 122;
		    if ($.browser.msie) base = 142;
		    var multiplier = 79;
		    if ($.browser.safari) multiplier = 81;
		    var height = base + multiplier * items;
		    $('#widget_right').css("margin-top", height+'px');
		} else {
		    $('#shopping_cart_dialog').addClass('hidden');
		    $('#widget_right').css("margin-top", '13px');
		}
	}
	
	$(function(){

		$('#sub-menu-prodotti').load("/product/index/categoriesmenu");

		update_cart_qty();
		
		var email = $("#email", "#dialog_login_form"),
		password = $("#password", "#dialog_login_form"),
		allFields = $([]).add(email).add(password);

		function checkLength (o,n,min,max) {
			if ( o.val().length > max || o.val().length < min ) {
				o.addClass('ui-state-error');
				updateTips("Length of " + n + " must be between "+min+" and "+max+".");
				return false;
			} else {return true;}
		}
		
		function checkRegexp (o,regexp,n) {
		
			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass('ui-state-error');
				return false;
			} else {return true;}
		}
		
		var login_dialog = {
			bgiframe: true,
			autoOpen: false,
			modal: true,
			resizable: false,
			buttons: {
				'Login': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');

					bValid = bValid && checkLength(email,"email",6,80);
					bValid = bValid && checkLength(password,"password",4, 20);

					bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg. ui@jquery.com");
					bValid = bValid && checkRegexp(password,/^([0-9a-zA-Z])+$/,"Password field only allow : a-z 0-9");
					
					if (bValid) {
						jQuery.ajax({
							type: 'POST',
							url: $('form', "#dialog").attr('action'),
							data: {email: $("#email", "#dialog_login_form").val(), password: $("#password", "#dialog_login_form").val(), context: 'ajax'},
							cache: false,
							dataType: 'json',
							success: function(data, textStatus){
								if(textStatus == 'success') {
									switch(data.status) {
										case "ok": location.reload(); break;
										case "invalid credentials": alert('Invalid User/Pass');break;
										default: $('form:first', '#dialog').submit(); break;
									}// switch
								}// if success
							},// function success
							error: function(XMLHttpRequest, textStatus, errorThrown){alert('Sorry for the inconvenience, an error has occurred, try reloading the page.');}
						});// ajax
						$(this).dialog('close');
					}// if bValid
				},// function login
				Cancel: function() {
					$(this).dialog('close');
				}// function cancell
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		};
		
		var customer_dialog = {
				draggable: true,
				resizable: false,
				position: ['right', 'top'],
				dragStop: function(event, ui){
					$.cookie('ipac_x', parseInt(ui.offset.left, 10));
					$.cookie('ipac_y', parseInt(ui.offset.top, 10));
				},
				open: function(){
					console.log($.cookie('ipac_status'));
					if ($.cookie('ipac_status') === 'close')
						$('#dialog').slideUp();
				}
		}
		if ($.cookie('ipac_x') && $.cookie('ipac_y'))
		{
			customer_dialog.position[0] = parseInt($.cookie('ipac_x'), 10);
			customer_dialog.position[1] = parseInt($.cookie('ipac_y'), 10);
		}

		$('form  #id_state').change(function(){
			if ($(this).val() != 'IT'){
				$('form #id_province').val('--');
			}
		});
		$('form  #id_state').keypress(function(){
			if ($(this).val() != 'IT'){
				$('form #id_province').val('--');
			}
		});

		$("#voce-menu-prodotti").hoverIntent(function(){
			  $('#sub-menu-prodotti').fadeIn('slow');
		},function(){
			 $('#sub-menu-prodotti').fadeOut('slow');
	    });

		$("#voce-menu-support").hoverIntent(function(){
			  $('#sub-menu-support').fadeIn('slow');
			  $('#sub-menu-support').css("display","block");
		},function(){
			 $('#sub-menu-support').fadeOut('slow');
	    });
		
		$('.tab_wid a').click(function(e){
			widget = $(this).closest('.banner_widget');
			tab = $(this).closest('.tab_wiz');

			$('.tab_wid', widget).addClass('link_wid');
			$(this).closest('.tab_wid').removeClass('link_wid');

			$('.w_content', widget).removeClass('hidden');
			$('.w_content', widget).fadeOut('fast');
			$('.' + $(this).attr('rel'), widget).fadeIn('slow');
		});

		if ($("#layout_login_dialog").size() > 0) $("#dialog").dialog(login_dialog);
		
		/**
		 * +-----------------------+
		 * |  CUSTOMER AREA POPUP  |
		 * +-----------------------+
		 */
		$("#dialog").bind('slideToggle', function(){
			if($.cookie('ipac_status') === null) $.cookie('ipac_status', 'open');
			if ($.cookie('ipac_status') === 'open')
			{
				$.cookie('ipac_status', 'close')
				$(this).slideUp();
			}
			else 
			{
				$.cookie('ipac_status', 'open')
				$(this).slideDown();
			}
		});
		$("#dialog").dialog(customer_dialog);
		var minico = $('#minimize_icon').clone();
		$('#minimize_icon').remove();
		$('.ui-dialog-titlebar').append(minico);

		$('#login_dialog').click(function() {
			$('#dialog').removeClass('hidden');
			$('#dialog').dialog('open');
			return false;
		});

//		$('#chart').hoverIntent(function() {
//			$('#shopping_cart_dialog').removeClass('hidden');
//			$('#shopping_cart_dialog').fadeIn('slow');
//		}, function() {
//			$('#shopping_cart_dialog').fadeOut('slow');
//		});
		
		$('.ui-state-default').hover(function(){
			$(this).addClass('ui-state-hover');
		}, function(){
			$(this).removeClass('ui-state-hover');
		});

		/*
		$('.customer_documenti_table').live('mousemove', function(e){
			var c = $(this).data('c');
			var r = $(this).data('r');
			var t = e.target;
			if (c !== $(t).attr('c') || r !== $(t).attr('r')) {
				$(this).data('animate', false);
				c = $(t).attr('c');
				r = $(t).attr('r');
				$(this).data('c', c);
				$(this).data('r', r);
				$('.lens[c!='+c+'], .lens[r!='+r+']', this).css({
					"font-size": $(this).attr('fsl')+'px',
					"font-weight": 'normal'
				});
				$('.lens[c='+c+'], .lens[r='+r+']', this).css({
					"font-size": $(this).attr('fsm')+'px',
					"font-weight": 'bold'
				});
			}
		});
		*/

	});











	$(function(){
	    var a = $('#end-card-text-wrapper').clone()
	    $('#end-card-text-wrapper').remove();
	    $(a).insertAfter('#footer');
/*
	    var spec = $('#end-card-text-wrapper').html();
	    $('#end-card-text-wrapper').remove();
*/
	    $('#end-card-text-wrapper-close').click(function(){
	        $('#end-card-text-wrapper').hide();
	    });
	    
	    $('#end-card-text-wrapper-open').click(function(){
	        $('#end-card-text-wrapper').show();
	    });
	    
	});







