var mouseX=0;
var mouseY=0;

$(document).mousemove(function(e){
	mouseX = e.pageX;
	mouseY = e.pageY;
	});

$(function() {

// TOOLTIPS

$('.tps_product').each(function() {
	var qty0 = $(this).find('.qty_amount0').text();
	var qty1 = $(this).find('.qty_amount1').text();
	var price0 = $(this).find('.amount0').text();
	var price1 = $(this).find('.amount1').text();
	var offer0 = $(this).find('.price_offer_number').text();
	
	var tooltipText = '';
		
	if(qty0) tooltipText += shop_txt[lang].minimum_amount_tip(qty0);
	if(offer0) {
		tooltipText += shop_txt[lang].offer_tip(offer0,price0);
	}
	
	else {
		tooltipText += shop_txt[lang].normal_tip(price0);
		if(price1) tooltipText += shop_txt[lang].quantity_tip(price1,qty1);
	}
	
	$(this).find(".pricing").qtip({
		content: tooltipText,
		show: 'mouseover',
		hide: 'mouseout',
		style: { width: 320, padding: 5, background: '#eeeeee', color: 'black', textAlign: 'center', border: { width: 1, color: '#000000' } },
		position: { adjust: { x: -240, y: 3 } }
	});
});

tooltipText = shop_txt[lang]['more_info_tip'];
	
$(".tps_product .info").qtip({
	content: tooltipText,
	show: 'mouseover',
	hide: 'mouseout',
	style: { width: 100, padding: 2, background: '#eeeeee', color: 'black', textAlign: 'center', border: { width: 1, color: '#000000' } },
	position: { adjust: { x: -50, y: 5 } }
});	

tooltipText = shop_txt[lang]['zoom_image_tip'];
	
$(".tps_product .zoom").qtip({
	content: tooltipText,
	show: 'mouseover',
	hide: 'mouseout',
	style: { width: 100, padding: 2, background: '#eeeeee', color: 'black', textAlign: 'center', border: { width: 1, color: '#000000' } },
	position: { adjust: { x: -50, y: 5 } }
});	

tooltipText = shop_txt[lang]['remove_product_tip'];

$(".tps_product .remove_button").qtip({
	content: tooltipText,
	show: 'mouseover',
	hide: 'mouseout',
	style: { width: 200, padding: 2, background: '#eeeeee', color: 'black', textAlign: 'center', border: { width: 1, color: '#000000' } },
	position: { adjust: { x: -100, y: 5 } },
	api : { 
		beforeShow : function(e) { if($(e.target).hasClass('disabled')) return false; }
	}	
});	


	
// END TOOLTIPS	
	

$('.tps_product_info_popup').draggable();
$('.tps_product_image_popup').draggable();

$('.tps_product_info_popup .close').
	bind('click', function() {
		$('.tps_product_info_popup').hide();
	});

$('.tps_product_image_popup .close').
	bind('click', function() {
		$('.tps_product_image_popup').hide();
	});	
		
		
// cart
		
$('#tps_cart').bind('mouseover', function() {
	if(!$('#tps_cart_summary').hasClass('disabled')) $('#tps_cart_summary').show();

});		
		
$('#tps_cart').bind('mouseout', function() {
	$('#tps_cart_summary').hide();

});		

$('#tps_cart #cart_edit_button').bind('click', function() {
	if(!$(this).hasClass('disabled')) location = "eurobook.php?action=checkout";
});

$('#tps_cart #cart_checkout_button').bind('click', function() {
	if(!$(this).hasClass('disabled')) {
		cart_total = parseFloat($('#cart_price_total').html());
		
		if(cart_total<shop.minimum_order) {
			alert(shop_txt[lang].minimum_order(shop.minimum_order));
		}
		else {
			location = "eurobook.php?action=checkout";
		}
	}
		
});

$('#send_order_button').bind('click', function() {
	order_total = parseFloat($('#order_price_total').html());		
	
	if(order_total<shop.minimum_order) {
		alert(shop_txt[lang].minimum_order(shop.minimum_order));
	}
	else {
		location = "eurobook.php?action=details";
	}
});


$('#tps_cart #cart_empty_button').bind('click', function() {
	if(!$(this).hasClass('disabled')) shop.emptyCart();
});


// products

$('.tps_product .info').bind('click', function() {
	$('.tps_product_info_popup').css({
            top: ($(window).scrollTop()+50) + "px",
            left: (parseInt($(window).width() / 2)-250) + "px"
        });

	code = $(this).parents(".tps_product").attr('id');
	
	$('.tps_product_info_popup .info').empty().hide();
	$('.tps_product_info_popup .info').load('eurobook_actions.php?action=load_info&product_code='+code+'&info_lang='+SHOP_LANG, function() { $(this).show(); });
	$('.tps_product_info_popup, .tps_product_info_popup .info').show();
});

        $('#zoom_image').bind('load', function() {

	$('.tps_product_image_popup').width(this.width+140);
        });


$('.tps_product .zoom, .tps_product .thumbnail').bind('click', function() {
	$('.tps_product_image_popup').css({
           top: ($(window).scrollTop()+50) + "px",
            left: (parseInt($(window).width() / 2)-250) + "px"
        });	
		
	code = $(this).parents(".tps_product").attr('id');


	$('#zoom_image').attr('src','tpdata/tps/product_images/shop_2/'+code+'.jpg');
	
	$('.tps_product_image_popup').show();
		
});

$('.tps_product .add_button').bind('click', function() {
	code = $(this).parents('.tps_product').attr('id');
	quantity = $(this).siblings('.qty_input').val();	
	shop.addToCart(code,quantity);
});

$('.tps_product .remove_button').bind('click', function() {
	code = $(this).parents('.tps_product').attr('id');
	shop.removeFromCart(code);
});

$('.qty_input').bind('keypress', function(e) { 
		var keynum;
		var keychar;
		var numcheck;
		
		if(window.event) // IE
		  {
		  keynum = e.keyCode;
		  }
		else if(e.which) // Netscape/Firefox/Opera
		  {
		  keynum = e.which;
		  }
		keychar = String.fromCharCode(keynum);
		numcheck = /\d/;
		
		if(!keynum || keynum < 32 || numcheck.test(keychar)) return true;
		else return false;
}).bind('click', function() {
	$(this).val('');
}).bind('focus', function() { shop.oldval = $(this).val() })
.bind('blur', function() {
	if(!/[\d]+/.test($(this).val())) $(this).val(shop.oldval);
});

// login notice

$('.tps_product .login').click(function() {
	$('#pro_login_form input#username').focus();
}); 

// contact form

$('#contact_form').bind('submit', function(e) { return shop.checkDetails(); });

$('#contact_form a').bind('click', function(e) { window.open('eurobook_legal.php'); return false; });

// disable accept terms checkbox from activating when clicking label

$('#contact_form label[for=accept_terms]').bind('click', function(e) { e.preventDefault(); });

// search 
$('#tps_product_search_form').bind('submit', function() { return shop.validateSearch(); });

// center pagination
var paginationWidth = $('.tps_pagination ul').outerWidth(); 
$('.tps_pagination ul').css({'margin-left': parseInt((740 - paginationWidth) / 2)-10});
$('.tps_pagination').css({'visibility':'visible'});


// end ready
});

shop = {
	num_decimals : (CUSTOMER_GROUP == 1 ? 2 : 0),
	currency : (CUSTOMER_GROUP == 1 ? '&euro;' : 'kr'),
	minimum_order : (CUSTOMER_GROUP == 1 ? 100 : 1000), // 100 EUR, 1000 kr
	
	addToCart : function(code,quantity) {		
		numcheck = /[\d]+/;
		textfield = $('#'+code+' .qty_input');
		qty = $(textfield).val();
		minQty = min_quantities[code];

		if(!numcheck.test(qty)) {
			$(textfield).val(minQty);
			quantity = minQty;
		}

		currentQuantity = parseInt($('#'+code+' .qty_number').html());
		newQuantity = currentQuantity + parseInt(quantity);
		
		if(newQuantity < minQty) {
			alert(shop_txt[lang].minimum_amount(minQty));
			return;
		}
		
		
		$.getJSON('eurobook_actions.php?ajax=1&action=add&package_code='+code+'&qty='+quantity, function(cartData) {
			shop.updateCart(cartData);
			
			$('#'+code+' .qty_number').hide();
			
			$('#'+code+' .qty_number').html(newQuantity+ ' '+ shop_txt[lang]['pcs']);
			
			$('#'+code+' .qty_number').fadeIn(500,function() {
				$('#'+code+' .remove_button').removeClass('disabled');	
				$('#tps_cart .cart_button').removeClass('disabled');
				});
			});	
		},
		
	removeFromCart : function(code) {
		$.getJSON('eurobook_actions.php?ajax=1&action=remove&package_code='+code, function(cartData) {
			shop.updateCart(cartData);
			
			$('#'+code+' .qty_number').hide();
			$('#'+code+' .qty_number').html('0 '+ shop_txt[lang]['pcs']);
			$('#'+code+' .qty_number').fadeIn(function() {
				$('#'+code+' .remove_button').addClass('disabled');	
				});
			});


	},		
		
	updateCart : function(cartData) {
		c=cartData;		
		$('#cart_qty_total').html(c.qty_total + (c.qty_total == 1 ? ' ' + shop_txt[lang]['product'] : ' ' + shop_txt[lang]['products']));
		$('#cart_price_total').html(c.price_total.toFixed(this.num_decimals));
			
	    new_summary = '<div class="transp_dialog"><div class="content" style="position:relative;padding-top:20px;padding-bottom:5px"><div class="t"></div>';
		new_summary += '<table><tr><th>'+ shop_txt[lang]['amount'] +'</th><th>'+ shop_txt[lang]['product_name'] +'</th><th>'+ shop_txt[lang]['price_per_piece'] +'</th><th>'+ shop_txt[lang]['price_total'] +'</th></tr>';

		for(i=0;i<c.packages.length;i++) {
			cur = c.packages[i];
			price = parseFloat(cur.price);
			total = (price * cur.qty).toFixed(this.num_decimals);
			new_summary += '<tr><td>'+cur.qty+'</td><td>'+cur.product_name+'</td><td>'+price.toFixed(this.num_decimals)+' '+ this.currency +'</td><td>'+total+' '+ this.currency +'</tr>';
		}

		new_summary += '</table>';
		new_summary += '</div><div class="b"><div></div></div></div>';
		
		$('#tps_cart_summary').html(new_summary);
		if(c.qty_total==0) {
			$('#tps_cart_summary').addClass('disabled');
			$('#tps_cart .cart_button').addClass('disabled');
		}
			
		else $('#tps_cart_summary').removeClass('disabled');
			
			
			
	},
	
	showSummary : function() {
		$('tps_cart_summary').show();
		},
		
	hideSummary : function() {
		$('tps_cart_summary').hide();
		},
		
	emptyCart : function()	 {
		location = 'eurobook_actions.php?action=empty';	
		},		
		
	checkDetails : function() {
		if(!$('#contact_form #accept_terms').attr('checked')) {
			alert(shop_txt[lang]['must_accept_terms']);
			return false;
		}
		return true;
	},
	
	validateSearch : function() {
		var termLength = $('#tps_searchbox #search_term').attr('value').length;

		if(!termLength) {
			return false;
		}
		
		else if(termLength < 3) {
			alert(shop_txt[lang]['search_term_too_short']);
			return false;
		}
		
		
	}
	
		
}
