/* Product Listing based on Page Scroll */
/* Class Page */
function Page(){
	
	/* Private variables */
	var __PAGE=1,uniqueID=new Array(),__DEALEND=false,__HOLD=false,viewType='grid',sortBy='spcl_offers';
	this.ajaxResponses=new Array();
	
	/* Methods */
	/* Function to get the page number */
	this.getPageNumber=function(){
		return __PAGE;
	}
	/* Function to increment the page number */
	this.setPageNumber=function(newVal){
		__PAGE = (newVal >='0') ? newVal : (__PAGE+1);
	}
	/* Function to push the unique iD which is generated during the GetJSON function */
	this.setUniqueIDs=function(unique){
		uniqueID.push(unique);
	}
	/* Function to retriev ethe unfique IDs */
	this.getUniqueIDs=function(unique){
		return uniqueID;
	}
	/* Function to check the product list end or not */
	this.getProductsEndStatus=function(){
		return __DEALEND;
	}
	/* Function to set the product end status is End and no product is vailable for Listing */
	this.setProductsEndStatus=function(){
		__DEALEND = true;
	}
	this.resetProductsEndStatus=function(){
		__DEALEND = false;
	}
	this.checkAjaxHold=function(){
		return __HOLD;
	}
	this.ajaxHold=function(bool){
		__HOLD = bool;
	}
	
	/* Function to get the catalog product listing view mode */
	this.getViewType=function(){
		return viewType;
	}
	/* Function to switch view mode */
	this.setViewType=function(newViewType){
		viewType = newViewType;
	}
	/* To get the catalog product listing sort by */
	this.getSortBy=function(){
		return sortBy;
	}
	/* To set sort by */
	this.setSortBy=function(newSortBy){
		sortBy = newSortBy;
	}
}

/* Create an Object of Page Class */
var page=new Page();

$(document).ready(function () {

	
	window.document.onkeydown = function(e) {
		if(!e){ e = event; }
		if(e.keyCode == 27){ lightbox_close(); }
	}
	
	
	$(window).scroll(function () {
	
		if(!page.getProductsEndStatus() && !page.checkAjaxHold()){
		
			/* Condition to check whether the scroll is Bottom */
			var multiProductListDiv = $("#prodListingUl").children(':last');
			
			if(multiProductListDiv.length>0){
		
				if ($(window).scrollTop() == $(document).height() - $(window).height()) {
					
					var key	= 'business_'+page.getViewType()+'_'+page.getSortBy()+'_'+page.getPageNumber();
						
					if(typeof page.ajaxResponses[key] == 'undefined') {
						var uniqueID = +new Date;
						
						$.ajax({
							url: GLOBAL_BASE_PATH + '/stores/ajaxGetBusinessProducts/',
							type: "POST",
							data: {page:page.getPageNumber(), viewType:page.getViewType(), businesskey:businessKey, sortBy:page.getSortBy(), uniqueID:uniqueID},
							beforeSend: function () {
								page.ajaxHold(true);
								$(".pop_loader").removeClass('hide');
							},
							complete:function(){
								page.ajaxHold(false);
								$(".pop_loader").addClass('hide');
							}
						}).done(function(resHtml) {
							if($.trim(resHtml) != ''){
								page.setPageNumber();
								/* Append the new HTML into listing DIV  */
								$("#prodListingUl").append(resHtml);
								page.ajaxResponses[key] = {content:resHtml,uniqueID:uniqueID};
								/* Also push the unique iDs into Array */
							}else{
								page.setProductsEndStatus();
							}	
						});
					}else{
						page.setPageNumber();
						var theResponse = page.ajaxResponses[key];
						$("#prodListingUl").append(theResponse['content']);

					}
				}
			}
		}
	});

	
	
	});
$(document).on('click','.AjaxUnFollow', function () {
        var loadUnfollow = changeFollowBusiness('unfollow');
    });
    $(document).on('click','.AjaxFollow ', function () {
        var loadFollow = changeFollowBusiness('follow');
    });
function changeFollowBusiness(arg) {

    var businessID = $("#businessID").val();
    if (arg == 'unfollow') {
        $(".AjaxUnFollow").text(lblFollow);
        $(".AjaxUnFollow").attr('title', lblFollow);
        $(".AjaxUnFollow").attr('id', 'linkfollow');
        $(".AjaxUnFollow").attr('class', 'icon-like AjaxFollow product-follow pull-right');
    } else {
        $(".AjaxFollow").text(lblUnFollow);
        $(".AjaxFollow").attr('title', lblUnFollow);
        $(".AjaxFollow").attr('id', 'linkUnfollow');
        $(".AjaxFollow").attr('class', 'icon-like AjaxUnFollow  product-follow pull-right');
    }
    $.ajax({
        type: "POST",
        url: GLOBAL_BASE_PATH + "/stores/ajaxFollowMerchant/",
        data: 'businessID=' + businessID,
        success: function (data) { }
    });
}
	$(document).on("click",".qkView ", function(){
		var productID 	= $(this).attr('prodID');
		var busID 	= $(this).attr('busID');
		var ordID 	= $(this).attr('ordID');
		var geturl 	= '/stores/ajaxmerchantreview/id/'+ordID+'/bid/'+busID+'/pid/'+productID;
		$.ajax({
			url: GLOBAL_BASE_PATH + geturl,
			method: "GET",
			cache: true,
			global: false,
			success: function(qvHtml) {
				$("#reviewPopup").empty().html(qvHtml);	//Append the new HTML into listing DIV
				lightbox_open();
			}
		});
	});
	function lightbox_open(){
		//window.scrollTo(0,0);
		$("#reviewPopup, #CategoryFade").fadeIn();
	}
	function lightbox_close(){
		$(".zoomContainer").hide();
		$("#reviewPopup, #CategoryFade").fadeOut();
	}