var	$		= jQuery,
	key		= {'esq' : 27,'enter' : 13},
	pages	= {
				'home'		: ['home'],
				'conteudo'	: ['conteudo','sub1','sub2'],
				'contactos'	: ['contactos']};


$(window).bind('load',function(){	
	
	
	$('.setAmount').tipsy({gravity:'e',html:true,trigger:'focus'});
	
	/*		
	// Selecionar menu
	$('#topmenu .option').toEach(function(){
		var liClass = $('a',this).attr('class');
		
		if($.inArray(getPage,pages[liClass]) >= 0)
			$(this).addClass('current');
	});
	*/	
	
	// Paginação
	$('.pagination .submit').click(function(){
		$(this).children('form').submit();
	});
	$('.selectPagCombo').change(function(){
		$(this).parent('form').submit();
	});
	
	$('.opensub').click(function(){
		
		var opendiv	= $(this).attr('class').split(' ')[0];
		var ul		= $('ul.' + opendiv);
		
		
		if(ul.hasClass('open'))
			ul.removeClass('open').css({'height':0},300);
		else{
			$('ul.submenu').removeClass('open').css({'height':0},300);
			ul.addClass('open').css({'height':'100%'},300);
		}
	});
	
	
	$('#homeslider').cycle({
		fx		: 'scrollLeft'
	});
	
	$('#contentslider').cycle({
		fx		: 'fade'
	});
	
	// STORE
	$('.addToCart').click(function(){
		var vars	= {
						'action'	: 'addCart',
						'prod'		: $(this).attr('name')};
						
		$.post('include/ajax.php',vars,function(rtn){				
			if(IsString(rtn))
				alert(rtn);
			else{
				$('.tbl-basket .total span.item').html(rtn['item']);
				$('.tbl-basket .total span.tax').html(rtn['tax']);
				$('.tbl-basket .total span.voucher').html(rtn['voucher']);
				$('.tbl-basket .total span.total').html(rtn['total']);
				
				$('#totalVoucher').val(rtn['voucher']);
			}
		},'json');
	});
	$('.setAmount').focus(function(){
		var input	= $(this),
			id		= input.parents('tr').attr('name'),
			oldVal	= input.val();
		
		
		$('.setAmount').keydown(function(event) {
			if ( event.keyCode == 46 || event.keyCode == 9 || event.keyCode == 8 || event.keyCode == 37 || event.keyCode == 39 || (event.keyCode >= 96 && event.keyCode <= 105)){}
			else if (event.keyCode < 48 || event.keyCode > 57)
				event.preventDefault();
		});
		
		
		$(document).bind('keyup',function(event){
			if(event.keyCode == key['enter']){
				
				var newVal	= input.val(),
					vars	= {
									'action'	: 'setAmount',
									'prod'		: id,
									'num'		: newVal};
				
				if(newVal != oldVal && newVal > 0){
					$.post('include/ajax.php',vars,function(rtn){
															
						if(IsString(rtn)){
							$('.setAmount.' + id).val(oldVal);
							alert(rtn);
						}else{
							$('.tbl-cart .total span.item').html(rtn['item']);
							$('.tbl-cart .total span.tax').html(rtn['tax']);
							$('.tbl-cart .total span.voucher').html(rtn['voucher']);
							$('.tbl-cart .total span.total').html(rtn['total']);
							
							$('#amount-' + id).val(input.val());
							$('#totalVoucher').val(rtn['voucher']);
							$('#totalCartValue').val(rtn['total']);
							$('.deliveryvalue').attr('name',rtn['delivery']);
							
							if(Number(rtn['total'].replace(',','')) >= minvalue)
								$('.btnBuy').removeClass('hide');
							else if(!$('.btnBuy').hasClass('hide'))
								$('.btnBuy').addClass('hide');
						}
							
					},'json');
				}else
					$('.setAmount.' + id).val(oldVal);
				
				$(document).unbind('keyup');
				input.trigger('blur');
			}
		});
	});
	$('.delCart').click(function(){
		var img			= $(this),
			title		= img.attr('name').split('|')[0],
			question	= img.attr('name').split('|')[1];
		
		jConfirm(question,title, function(option){
			if(option == true){
				var vars	= {
								'action'	: 'delCart',
								'prod'		: img.parents('tr').attr('name')};
								
				$.post('include/ajax.php',vars,function(rtn){
					$('.tbl-cart .total span.item').html(rtn['item']);
					$('.tbl-cart .total span.tax').html(rtn['tax']);
					$('.tbl-cart .total span.voucher').html(rtn['voucher']);
					$('.tbl-cart .total span.total').html(rtn['total']);
					
					$('#totalVoucher').val(rtn['voucher']);
					$('#totalCartValue').val(rtn['total']);
					$('.deliveryvalue').attr('name',rtn['delivery']);
					
					img.parents('tr').remove();
				},'json');
			}
		});
	});
	$('#submitCart').click(function(){
		
		var values	= [];
		
		$('.setAmount').toEach(function(){ values.push($(this).parents('tr').attr('name')) });	
		
		$.post('include/ajax.php',{'action':'checkStock','values':values},function(rtn){
			
			if(rtn == 1)
				$('#shoppingCartForm').submit();
			else
				alert(rtn);
		});
	});
	$('.method').click(function(){
		var method	= $(this).attr('value'),
			form	= $(this).parents('form');
		
		if(method == 'PP')
			form.attr('action',paypal);
		else
			form.attr('action',finishBuy);
		
		$('.method_message').hide().children('input.charges').attr('disabled','disabled');
		$('.method_message.' + method).show().children('input.charges').removeAttr('disabled');
	});
	$('input[name="delevery_method"]').click(function(){
		
		var val = $(this).val();
		$('select[name="handling_cart"]').attr('disabled',true);
		$('#' + val).attr('disabled',false);
	
	});
	
	$('#delVoucher').click(function(){
		var img			= $(this),
			title		= img.attr('name').split('|')[0],
			question	= img.attr('name').split('|')[1];
		
		jConfirm(question,title, function(option){
			if(option == true){
				var vars	= {
								'action'	: 'defineSession',
								'session'	: {'vouchercode':''}};
								
				$.post('include/ajax.php',vars,function(){ location.reload() });
			}
		});
	});
	$('#defineVoucher').click(function(){
		
		var vars	= {
						'action'	: 'defineSession',
						'session'	: {'vouchercode':$('#voucher-input').val()}};
							
		$.post('include/ajax.php',vars,function(){ location.reload() });
	});
	$('option','select[name="handling_cart"]').click(function(){
	
		var val = $(this).attr('class');
		
		$('#shippinglocal').val(val);
	});
	
	$('#show_newAddress').click(function(){
		if($(this).attr('checked') == true)
			$('.newAddress').show();
		else
			$('.newAddress').val('').hide();
	});
});

(function(jq){
	$.fn.toEach = function(fn){
		for (var i = 0, length = this.length; i < length && fn.call(jq.context = jq[0] = this[i], i++, jq) !== false;) {}
		return this;
	};
}($([1])));

function IsNumeric(input){
	return (input - 0) == input && input.length > 0;
}
function IsString(input){
	return typeof(input) == 'string';
}


function disableEnterKey(e){
     var key;

     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

     if(key == 13)
          return false;
     else
          return true;
}
