/**
 * Class template for miniBasketClass
 * This template has products underneath each other with product name and price
 *
 * Changelog
 * 1.0.0 - 16/06/2008 - Luke Henderson - Initial release
 *
 *
 * @copyright drivebusiness
 * @package miniBasket
 * @class miniBasketTemplate
 * @version 1.0.0
 */
var miniBasketTemplate = Class.create({
	/**
	 * Private elements
	 */
	_name : 'allsaints',

	/**
	 * Dom elements
	 */
	parent : false,
	container : false,
	container_inner : false,
	container_justAdded : false,
	container_yourbag : false,
	container_total : false,
	container_basket_empty : false,

	/**
	 * Bool
	 */
	displayed : false,
	animating : false,

	container_id : '',

	/**
	 * Integer/floats
	 */
	loaded_images : 0,

	/**
	 * Objects
	 */
	options : {
		more_items_text : '<p>' + translateObject.translate('You have {items_diff} other {items_diff_text} in your basket', 'allsaints_more_items_text') + '</p>',
		more_items_id : false,
		display_num_products : 6
	},

	/**
	 * @constructor
	 */
	initialize : function(parent, options)
	{
		this.parent = parent;

		Object.extend(this.options, options);
		}
});


function productCheckoutImage() {
	//If new site selector is active, basket total is in different place
		var num_products = parseInt($j('#basket_total').text());
	
	if(num_products && $j('#product_goto_checkout').length == 0 && num_products > 0 ) {
		$j('.product-page-details-add-to-bag').append('<a class="product_add_ctrl" href="/basket/"><img src="/assets/product/button_checkout_off.gif" id="product_goto_checkout" alt="go to checkout"/></a>');
		
		//Swapping image url states
		$j('img#product_goto_checkout').hover(function() {
				$j(this).attr('src', $j(this).attr('src').replace(/_off/, '_on'));
			}, function(){
				$j(this).attr('src', $j(this).attr('src').replace(/_on/, '_off'));
			});
	}
	else if(num_products == 0) {
		$j('img#product_goto_checkout').parent().remove();
	}
}

function openDeletedProductNotificationEmail(){
	$j('#deleted_product_div').css('height', '64px');
	$j('#deleted_product_stock_notify_email').css('display', 'block');
	$j('#deleted_product_stock_notify_submit').css('display', 'block');
	$j('#deleted_product_stock_notify_email').focus();
}
function clearEmailFieldDeletedProductNotification(){
	$j('#deleted_product_stock_notify_email').val("");
}
function validateDeletedProductEmailNotification(){
	$j('#deleted_product_notification_email_invalid').css('display', 'none');
	$j('#deleted_product_stock_notify_email').css('border','none');
	var email = $j('#deleted_product_stock_notify_email').val();
	if(isValidEmailAddress(email)){
		var prod_desc_ids = $j('#deleted_product_stock_notify_ids').val();
		saveDeletedProductEmailNotification(email, prod_desc_ids);
	}else{
		$j('#deleted_product_notification_email_invalid').css('display', 'inline');
		$j('#deleted_product_stock_notify_email').css('border','1px solid red');
		$j('#deleted_product_stock_notify_email').focus();
	}
}
function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}
function saveDeletedProductEmailNotification(email, prod_desc_ids){
	$j('#deleted_product_notification_loader').css('display', 'inline');
	/** Using ajax to save the line details 
	 */	
	$j.ajax({
		url: "/basket/?act=save-deleted-products-notification&ajax=1&deleted_product_stock_notify_email="+email+"&deleted_product_stock_notify_ids="+prod_desc_ids,
		success: function(data) {
		console.log(data);
			$j('#deleted_product_notification_loader').css('display', 'none');
			if(data.success){
				$j('#delete_product_notification_fields').hide('slow');	
				$j('#delete_product_notification_message').show('slow');
				$j('#deleted_product_notification_success_message').css('display', 'inline');
				$j('#deleted_product_notification_error_message').css('display', 'none');
			}else{
				$j('#deleted_product_notification_success_message').css('display', 'none');
				$j('#deleted_product_notification_error_message').css('display', 'inline');
			}
		}
	});	
}
function closeDeletedProductNotificationDiv(){
	$j('#deleted_product_div').css('display', 'none');
}


//Restore My Account option if there for cache
$( document ).ready( function() {
	if($j('#primary-nav-exist').length > 0) {
		var str = '<div id="primary-nav-my_account" style="float: right;margin-right: 18px;"><a class="pod_facebox_link" href="/account/" id="/account/info/|1100|700" ><span>My account</span></a></div>';
		$j('#primary-nav-sign_in').replaceWith(str);
		//If we're using the new site selector, be sure to add class to new element
		if($j('#primary-nav-exist').hasClass('site_selector')) {
			$j('#primary-nav-my_account').addClass('site_selector');
		}
	}
});
