//Script: pagination-new.js

var pagesize		= 12;
var sortby			= 'priceA';
var currentpage		= '0';
var maxcols			= 3;
var contentdiv		= "contentdiv";
var pagingSeparator	= "|";

function setPageSize(size) {
	pagesize=size || 12;
}

function UpdatePagingNav() {
	var pagingConsoles=['pageNavLeft', 'pageNavLeftbottom'];
	var con;
	var strpageSizenav="Show : ";

	strpageSizenav += pagesize==12?"<span class='pg_selected'>12</span>":'<a href="javascript:void(0)" onclick="updateCurrentPage();NumberOfItemsPerPage(\'12\')">12</a>';
	if(Items.length>12)
		strpageSizenav+=pagesize==24?" | <span class='pg_selected'>24</span>":' | <a href="javascript:void(0)" onclick="updateCurrentPage();NumberOfItemsPerPage(\'24\')">24</a>';
	if(Items.length>24)
		strpageSizenav+=pagesize==36?" | <span class='pg_selected'>36</span>":' | <a href="javascript:void(0)" onclick="updateCurrentPage();NumberOfItemsPerPage(\'36\')">36</a>';
	strpageSizenav+=pagesize==Items.length?" | <span class='pg_selected'>All</span>":' | <a href="javascript:void(0)" onclick="updateCurrentPage();NumberOfItemsPerPage(\''+Items.length+'\')">All</a>';
	strpageSizenav+=" Items per page";
	
	for(coni=0; coni<pagingConsoles.length; coni++) {
		document.getElementById(pagingConsoles[coni]).innerHTML = strpageSizenav;
	}
}

function NumberOfItemsPerPage(size) {
	setPageSize(size);
	UpdatePagingNav();
	RefreshContent();
}

function drawArray(source) {
	this.DataSource = (source=='undefined')?new Array():source;
	this.Console	= null;
	this.TotalItems	= this.DataSource.length;
	this.MaxCols	= 3; ///
	
	this.SortBy = function() {
		switch (this.SortByColumn) {
			case 'priceA' :
				this.DataSource.sort(this.sortByPriceA);
				break;
			case 'priceD' :
				this.DataSource.sort(this.sortByPriceD);
			break;
		}
	}

	this.sortByPriceA = function(a, b) {
		var x = parseFloat(a.Saleprice) || parseFloat(a.Price) || 0;
		var y = parseFloat(b.Saleprice) || parseFloat(b.Price) || 0;
		return ((x < y) ? -1 : ((x > y) ? 1 :0));
	}

	this.sortByPriceD=function(a, b) {
		var x = parseFloat(a.Saleprice) || parseFloat(a.Price) || 0;
		var y = parseFloat(b.Saleprice) || parseFloat(b.Price) || 0;
		return ((x > y) ? -1 : ((x < y) ? 1 :0));
	}

	this.init = function(source) {
		if(source!='undefined') this.DataSource = source;
		this.TotalItems = this.DataSource.length;
	}

	this.SetDataSource = function(source) {
		if(source!=undefined) this.DataSource = source;
		this.TotalItems=this.DataSource.length;
	}

	this.DrawContent = function(sortby,pagenumber,pagesize, maxcols,console) {
		if(sortby!=undefined)		this.SortByColumn = sortby;
		if(pagenumber!=undefined)	this.PageNumber	  = pagenumber;
		if(pagesize!=undefined) 	this.PageSize	  = pagesize;
		if(maxcols!=undefined) 		this.MaxCols	  = maxcols;
		if(console!=undefined) 		this.Console	  = console;
		this.SortBy();
		try {
			if(conceirgepage == 1)
				this.ShowConceirgepageResult();
			else
				this.ShowResult();
		} catch(ex){ this.ShowResult(); }
	}
	
	this.ShowResult = function() {
		var startOffset	= this.PageNumber*this.PageSize;
		var endOffset	= parseInt(startOffset)+parseInt(this.PageSize);
		var endOffset	= (endOffset>=this.TotalItems)?(this.TotalItems):endOffset;
		var rows		= this.TotalItems%this.MaxCols==0?(this.TotalItems/this.MaxCols):parseInt(this.TotalItems/this.MaxCols)+parseInt(1);					
		var itemcount	= startOffset;

		var v = "<table id='contents-table' width='100%' cellspacing='0'>";
		for(var i=0; i<rows; i++) {
			v += "<tr>";
			for( var j=0; j<this.MaxCols; j++) {
				v += (j<this.MaxCols-1)?"<td valign='top' style='border-right: 1px solid rgb(199, 199, 199);' >":"<td valign='top'>";

				var saveprice	= 0;
				var savepercent	= 0;
				if(!isNaN(parseFloat(this.DataSource[itemcount].Promoprice))) { //promoprice, saleprice ->swap
					saveprice	= ((parseFloat(this.DataSource[itemcount].Price)-parseFloat(this.DataSource[itemcount].Saleprice)).toFixed(2));
					savepercent	= Math.round((saveprice*100)/parseFloat(this.DataSource[itemcount].Price));
				} else {
					if(!isNaN(parseFloat(this.DataSource[itemcount].Saleprice))) {
						saveprice	= ((parseFloat(this.DataSource[itemcount].Price)-parseFloat(this.DataSource[itemcount].Saleprice)).toFixed(2));
						savepercent	= Math.round((saveprice*100)/parseFloat(this.DataSource[itemcount].Price));
					}
				}

				if((parseFloat(this.DataSource[itemcount].Price) > 0) || (parseFloat(this.DataSource[itemcount].Saleprice) > 0)) {
					v += '<table class="productInfo" align="center" border="0" cellpadding="0" cellspacing="0" width="165">';
					v += '<tr><td align="center" valign="top"><div class="imgdiv">';
					if(saveprice>0) {
						v += '<div class="save"><p>Save<br />'+savepercent+'%</p></div>';
					}
					
					if((parseFloat(this.DataSource[itemcount].Price) > 0) && (parseFloat(this.DataSource[itemcount].Saleprice) > 0)){
						price_txt  = "Price: $"+this.DataSource[itemcount].Price;
						sale_price = "Sale Price: $"+ this.DataSource[itemcount].Saleprice;
						price_disptxt = '<span class="txtBlack11px">'+price_txt+'</span><br>'+'<span class="txtRed11pxb">'+sale_price+'</span>';
						
					} else if( (parseFloat(this.DataSource[itemcount].Price) <= 0) && (this.DataSource[itemcount].Saleprice != "")) {
						sale_price = "Sale Price: $"+ this.DataSource[itemcount].Saleprice;
						price_disptxt = '<span class="txtRed11pxb">'+sale_price+'</span>';
					} else {
						
						sale_price = "Price: $"+ this.DataSource[itemcount].Price;
						price_disptxt = '<span class="txtRed11pxb">'+sale_price+'</span>';
					}
					
					v += '<a href="'+this.DataSource[itemcount].ID+'.html">'+this.DataSource[itemcount].ProdImage+'</a></div></td></tr><tr><td><p align="center"><span class="txtBlack12pxb"><a class="txtBlack12pxb" style="text-decoration: none;" href="'+this.DataSource[itemcount].ID+'.html">'+this.DataSource[itemcount].Name+'</a></p></td></tr><tr><td style="padding: 4px 0pt;"><p class="txtRed11pxb">'+price_disptxt+'<br>&nbsp;';

					if(saveprice>0) {
						v += '<span class="txtBlack11px">You save: $'+saveprice+'</span>';
					}
					
					v += '</p></td></tr><tr><td><table border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td><iframe style="border: medium none ; width: 100px; height: 18px;" src="http://secure-ecommerce-services.com/storeservices/bms/yahoo-stores/star-rating-frame.php?type=3&amp;store_id=luggageandgifts&amp;prod_id='+this.DataSource[itemcount].ID+'" frameborder="0" scrolling="no"></iframe></td></tr><tr>  <td><input id="chkCompare" name="chkCompare[]" value="'+this.DataSource[itemcount].ID+'" onClick="javascript: AddToCompare(\''+itemcount+'\', this);" type="checkbox" /><span class="txtBlack11px">&nbsp;<a href="javascript:ShowComparisionChart();" target="_self">Compare</a></span></td></tr><tr><td rowspan="2" style="padding: 5px;"><a href="'+this.DataSource[itemcount].ID+'.html"><img src="/lib/luggageandgifts/btnDetails.gif" border="0" /></a></td></tr></tbody></table></td></tr></tbody></table>';
				} else {
					v += '<table class="productInfo" align="center" border="0" cellpadding="0" cellspacing="0" width="165">';
					v += '<tr><td align="center" valign="top"><div class="imgdiv"><a href="'+this.DataSource[itemcount].ID+'.html">'+this.DataSource[itemcount].ProdImage+'</a></div></td></tr>';
					v += '<tr><td><p align="center"><span class="txtBlack12pxb"><a class="txtBlack12pxb" style="text-decoration: none;" href="'+this.DataSource[itemcount].ID+'.html">'+this.DataSource[itemcount].Name+'</a></p></td></tr></table>';
				}
				
				v += "</td>";
				itemcount++;
				if(itemcount>=endOffset) break;
			}
			
			if(itemcount>=endOffset) break;
			v += "</tr>";
			if(i<(rows-1)) {
				v += "<tr>";
				for(var k=0; k<this.MaxCols; k++) {
					v += k<this.MaxCols-1?'<td align="center" style="border-right: 1px solid rgb(199, 199, 199);"><img width="170" height="8" src="/lib/luggageandgifts/borHor.gif"/></td>':'<td align="center" ><img width="170" height="8" src="/lib/luggageandgifts/borHor.gif"/></td>';
				}
				v += "</tr>";
			}
		}
		v += "</table>";
		if(document.getElementById(this.Console)) {
			document.getElementById(this.Console).innerHTML = v;
		}
	}
}	

function PagingClass(Source, PageSize) {
	this.DataSource	= (Source==undefined)?new Array():Source;
	this.ItemCount	= this.DataSource.length;
	this.PageNumber	= 1;
	this.PageSize	= (PageSize==undefined)?this.ItemCount:PageSize;
	this.TotalPages	= 0;

	if(this.PageSize>0 && this.ItemCount>0) {
		this.TotalPages = (this.ItemCount%this.PageSize==0)?this.ItemCount/this.PageSize:parseInt(this.ItemCount/this.PageSize)+1;
	}
	
	this.ShowPagination = function(console, separator, callbackfunction, currentpage) {
		var paginationConsole	= ['pagination','pageNavbottom'];
		var pagination			= "";
		var sep					= (separator==undefined)?"|":separator;

		for(i=0; i<this.TotalPages; i++) {
			if(i==currentpage) {
				pagination += "<strong>"+(i+1)+"</strong>";
			} else {
				pagination += "<a href='javascript:SetCurrentPage(\""+i+"\")' onclick='javascript:"+callbackfunction+"'>"+(i+1)+"</a>";
			}
			pagination += (i<(this.TotalPages-1))?" "+sep+" ": "";
		}
		for(var i=0; i<paginationConsole.length; i++) {
			document.getElementById(paginationConsole[i]).innerHTML="Show Page: "+pagination;
		}
	}
	
	this.setPageSize = function (pageSize) {
		this.PageSize=pageSize;
		if(this.PageSize>0 && this.ItemCount>0) {
			this.TotalPages=(this.ItemCount%this.PageSize==0)?this.ItemCount/this.PageSize:parseInt(this.ItemCount/this.PageSize)+1;
		}
	}
	
	this.ShowCurrentPage = function(currentpage, console) {
		document.getElementById(console).innerHTML="Page "+ (parseInt(currentpage)+parseInt(1)) +" of "+ this.TotalPages;
	}

	this.ShowingProducts = function(currentpage, console) {
		var fromProducts	= parseInt(currentpage)*this.PageSize;
		var toProducts		= parseInt(fromProducts)+parseInt(this.PageSize);
		toProducts			= toProducts>this.ItemCount?this.ItemCount:toProducts;
		fromProducts++;
		document.getElementById(console).innerHTML="Showing products "+ fromProducts +" - "+ toProducts+ " of "+ this.ItemCount;
	}
}

//CreateDataTable(Items[0]);
var p = new PagingClass(Items, pagesize);
var x = new drawArray(Items);
	
function SortByColumn(_sortby) {
	if(typeof CompareItems != "undefined") {
		CompareItems.length = 0;
	}
	sortby =_sortby;
	RefreshContent();
}

function SetCurrentPage(_page) {
	currentpage = _page;
	x.SetDataSource(Items);
	RefreshContent();
}

function updateCurrentPage() {
	currentpage = '0';
}

function RefreshContent() {
	p.setPageSize(pagesize);
	p.ShowPagination('pagination',pagingSeparator, 'x.DrawContent("'+sortby+'", "'+currentpage+'", "'+pagesize+ '", "'+ maxcols +'", "' + contentdiv+'")', currentpage);
	x.DrawContent(sortby, currentpage, pagesize, maxcols, contentdiv);
}
