var product_model = (function(){
// Private area
var addItem = $('#add-item'),
removeItem = $('#rm-item'),
/**
* Ajax call to get all availible products from db
*/
get_all_products = function (url, showCart) {
var url = url;
$.ajax({
url : url,
type : "POST",
data : {get_products : "1"},
dataType : "json",
success : function(data) {
var template = $('#productsTpl').html(),
html = Mustache.to_html(template, data);
$('.products-list').html(html);
if (showCart == 1) {
showCartPopup();
}
tableSorter();
chooseNumberOfProducts();
}
});
},
/**
* Ajax call to get customers previous purchases
*/
get_all_purchases = function () {
$.ajax({
url : "../customer/store.php",
type : "POST",
data : {get_customer_summary : "1"},
dataType : "json",
success : function(data) {
template = $('#customerSummaryTpl').html(),
html = Mustache.to_html(template, data);
$('.purchase-history').html(html);
}
});
},
/**
* Ajax call to get customers previous purchases
*/
get_most_popular = function () {
$.ajax({
url : "../customer/store.php",
type : "POST",
data : {get_most_popular : "1"},
dataType : "json",
success : function(data) {
template = $('#mostpopularTpl').html(),
html = Mustache.to_html(template, data);
$('.most-popular-wrapper').html(html);
}
});
},
/**
* Ajax call to get customers previous purchases
*/
get_customer_debt = function () {
$.ajax({
url : "../customer/store.php",
type : "POST",
data : {get_customer_debt : "1"},
dataType : "json",
success : function(data) {
template = $('#customerDebtTpl').html(),
html = Mustache.to_html(template, data);
$('.total-purchase-overview').html(html)
}
});
},
/**
* Functionality for add/remove products
*/
chooseNumberOfProducts = function() {
addItem.live('click', function (){
var inputfield = $(this).siblings('input');
if (!inputfield.val()) {
inputfield.val(1)
} else {
inputfield.val(parseInt(inputfield.val()) + 1);
}
});
removeItem.live('click', function (){
var inputfield = $(this).siblings('input');
if (inputfield.val() > 1) {
inputfield.val(parseInt(inputfield.val()) - 1);
} else {
inputfield.val('');
}
});
},
showCartPopup = function () {
$("#checkout-btn").click( function() {
$("#dialog-receipt").dialog('open');
});
$("#dialog-receipt").dialog({
autoOpen: false,
resizable: true,
width: 500,
modal: true,
title: 'Receipt',
buttons: {
Cancel: function() {
$( this ).dialog( "close" );
},
"Accept": function() {
$( this ).dialog( "close" );
}
}
});
},
tableSorter = function () {
$(".customer .tablesorter").tablesorter({
headers: {
3: {
// disable sorting on the fourth column
sorter: false
}
},
sortList : [[0,0]]
});
}
return {
get_all_products : get_all_products,
get_all_purchases : get_all_purchases,
get_most_popular : get_most_popular,
get_customer_debt : get_customer_debt,
chooseNumberOfProducts : chooseNumberOfProducts
};
}());