/*
*	Interface
*/

function Shop() {

}

Shop.prototype.initialze = Shop_Initialize;
Shop.prototype.addToBasket = Shop_AddToBasket;
Shop.prototype.changeSortType = Shop_ChangeSortType;
Shop.prototype.changeQuantity = Shop_ChangeQuantity;
Shop.prototype.removeFromBasket = Shop_RemoveFromBasket;
Shop.prototype.renderBasket = Shop_RenderBasket;
Shop.prototype.refreshBasket = Shop_RefreshBasket;
Shop.prototype.user = null;

Shop.prototype.basketElement = null;

/*
*	Implementation
*/

function Shop_Initialize() 
{
	
	shop.user = new User();
	shop.user.checkStatus();
	
	//	refresh total
	$j.ajax({
		url: base_path + "/shop/index.php/basket/total",
		type: "POST",
		dataType: 'json',
		success: function(data){
			try {
				if (data.success == 1) {
					$j('#basket-icon').html('Moja košarica (' + data.total + ')');
				}
			} catch (ex) {
				console.error(ex);
			}
		}
	});
	
}

/**
* Adds item to basket
* @param {int} item_id Item_id
* @param {elem} elem Element to add/remove loading class
*/
function Shop_AddToBasket(item_id, elem)
{
	if (elem.hasClass('loading')) return false;
	elem.addClass('loading');
	
	$j.ajax({
		url: base_path + "/shop/index.php/basket/add/" + item_id,
		type: "POST",
		dataType: 'json',
		success: function(data){
			try {
				if (data.success == 1) {
					$j('#basket-icon').html('Moja košarica (' + data.total + ')');
				}
			} catch (ex) {
				console.error(ex);
			}
			elem.removeClass('loading');
		}
	});
}

/**
* Change quantity from basket
* @param {int} item_id Item_id
* @param {elem} elem Element to add/remove loading class
*/
function Shop_ChangeQuantity(item_id, elem, func)
{
	if (elem.parent().hasClass('loading')) return false;
	elem.parent().addClass('loading');
	
	$j.ajax({
		url: base_path + "/shop/index.php/basket/change/" + item_id + "/" + elem.val(),
		type: "POST",
		dataType: 'json',
		success: function(data){
			try {
				if (data.success == 1) {
					$j('#basket-icon').html('Moja košarica (' + data.total + ')');
				}
				if (func != undefined) {
					func();
				}
			} catch (ex) {
				console.error(ex);
			}
			elem.parent().removeClass('loading');
		}
	});
}

/**
* Remove item from basket
* @param {int} item_id Item_id
* @param {elem} elem Element to add/remove loading class
*/
function Shop_RemoveFromBasket(item_id, elem, func)
{
	if (elem.hasClass('loading')) return false;
	elem.addClass('loading');
	
	$j.ajax({
		url: base_path + "/shop/index.php/basket/remove/" + item_id,
		type: "POST",
		dataType: 'json',
		success: function(data){
			try {
				if (data.success == 1) {
					$j('#basket-icon').html('Moja košarica (' + data.total + ')');
				}
				if (func != undefined) {
					func();
				}
			} catch (ex) {
				console.error(ex);
			}
			elem.removeClass('loading');
		}
	});
}

/**
* Refresh basket element
*/
function Shop_RefreshBasket()
{	
	$j.ajax({
		url: base_path + "/shop/index.php/basket/refresh/",
		type: "POST",
		dataType: 'json',
		success: function(data){
			try {
				if (data.success == 1) {
					$j('#basket-icon').html('Moja košarica (' + data.total + ')');
				}
			} catch (ex) {
				console.error(ex);
			}
		}
	});
}

/**
* Render basket
* @param {elem} elem Element to render
*/
function Shop_RenderBasket()
{
	var loader = shop.basketElement.parent().parent().find('div.loading');
	loader.css('width', shop.basketElement.width());
	loader.css('height', shop.basketElement.height());
	loader.show();
	
	$j.ajax({
		url: base_path + "/shop/index.php/basket/items",
		type: "POST",
		dataType: 'json',
		success: function(data){
			try {
				if (data.success == 1) {
					shop.basketElement.find('tbody').html('');
					for(var i=0;i<data.items.length;i++) {
						var html = '\
						<tr>\
			                <td>' + data.items[i].code + '</td>\
			                <td>' + data.items[i].category + '</td>\
			                <td><strong>' + data.items[i].name + '</strong></td>\
			                <td nowrap>' + data.items[i].price + '</td>\
			                <td><form onsubmit="return false;"><img src="' + base_path + '/wp-content/themes/refill/images/icons/add-loader.gif" alt="" /><input item_id="' + data.items[i].item_id + '" type="text" value="' + data.items[i].quantity + '" class="input-text quantity" /></from></td>\
			                <td class="last-item"><a href="#" class="add"><img src="' + base_path + '/wp-content/themes/refill/images/icons/add.png" alt="" /></a><a href="#" class="delete" item_id="' + data.items[i].item_id + '"><img src="' + base_path + '/wp-content/themes/refill/images/icons/delete.png" alt="" /></a></td>\
			            </tr>\
						';
						shop.basketElement.find('tbody').append(html);
					}
					shop.basketElement.find('a.add').click(function() {
						$j(this).parent().parent().find('.quantity').val(parseFloat($j(this).parent().parent().find('.quantity').val()) + 1);
						shop.changeQuantity($j(this).parent().parent().find('.quantity').attr('item_id'), $j(this).parent().parent().find('.quantity'), function() { shop.renderBasket(); });
						return false;
					});
					shop.basketElement.find('.delete').click(function() {
						shop.removeFromBasket($j(this).attr('item_id'), $j(this), function() { shop.renderBasket(); });
					});
					shop.basketElement.find('.quantity').change(function() {
						shop.changeQuantity($j(this).attr('item_id'), $j(this), function() { shop.renderBasket(); });
					});
					shop.basketElement.find('.total').html(data.total);
					shop.basketElement.find('.shipping').html(data.shipping);
					shop.basketElement.find('.charge').html(data.charge);
					$j('.payment').attr({'checked':false});
					$j('.payment:eq(' + (data.payment - 1) + ')').attr({'checked':true});
					loader.hide();
				}
			} catch (ex) {
				console.error(ex);
				loader.hide();
			}
		}
	});
}

/**
* Changes sort type
* @param {type} int Sort type
* @param {option} int Sort value
*/
function Shop_ChangeSortType(type, option)
{
	$j.ajax({
		url: base_path + "/shop/index.php/sort/change/" + type + "/" + option,
		type: "POST",
		dataType: 'json',
		success: function(data) {
			if (data.success == 1) {
				window.location.href=window.location.href;
			}
		}
	});
}
