

var cururl = document.URL;
var pagenum = 1;
//var psize = 3;
var srttype = 0;
var ototal = 0;

var pos = cururl.indexOf("?");
var prodlink = cururl.substr(0,pos-1);

var n=cururl.indexOf('page=');

if(n>0)
{
	pagenum = cururl.substring(n+5,cururl.length);
	var nn = pagenum.indexOf('&');
	if(nn>0)
	{
		pagenum=pagenum.substring(0,nn);
	}
}
pagenum = Number(pagenum);

/*var n=cururl.indexOf('size=');

if(n>0)
{
	psize = cururl.substring(n+5,cururl.length);
	var nn = psize.indexOf('&');
	if(nn>0)
	{
		psize = psize.substring(0,nn);
	}
}*/

var n=cururl.indexOf('sorttype=');

if(n>0)
{
	srttype = cururl.substring(n+9,cururl.length);
	var nn = srttype.indexOf('&');
	if(nn>0)
	{
		srttype = srttype.substring(0,nn);
	}
}


function dataadd(pid, did, pname, pimage, price, saleprice, orderable, prorating, procode, itemoptions, description, upsells) {
    this.divid = did;
	this.orderable = orderable;
    this.productname = pname;
	this.image = pimage;
    this.productprice = price;	
	this.saleprice = saleprice;
	this.pid = pid;
	this.prorating = prorating;
	this.procode = procode;
	this.itemoptions = itemoptions;
	this.description = description;
	this.upsells = upsells;
}

function sortByNameAscending(a,b)
{
	var x = a.productname.toLowerCase();
	var y = b.productname.toLowerCase();
	return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

function sortByNameDescending(a,b)
{
	var x = b.productname.toLowerCase();
	var y = a.productname.toLowerCase();
		return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

function sortByPriceAscending(a,b)
{
	var x = a.saleprice;
	var y = b.saleprice;
	return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

function sortByPriceDescending(a,b)
{
	var x = b.saleprice;
	var y = a.saleprice;
	return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

function sortArray(val)
{
	sorttype = val;

	if(val==1)
	{
		data.sort(sortByNameAscending);
	}
	else if(val==2)
	{
		data.sort(sortByNameDescending);
	}
	else if(val==3)
	{
		data.sort(sortByPriceAscending);
	}
	else if(val==4)
	{
		data.sort(sortByPriceDescending);
	}
	document.getElementById("topsorting").selectedIndex = sorttype;
	viewpaging(len,pagenum);
}

/*function pagingsizechange(val)
{
	if(val == 'top')
	{
		index = document.getElementById("itemt-sorting").selectedIndex;
	}
	else
	{
		index = document.getElementById("itemb-sorting").selectedIndex;
	}

	if(index != 4)
		url = pagelink + "?sorttype=" + srttype + "&size=" + index + "&page=1";
	else
		url = pagelink + "?sorttype=" + srttype + "&size=" + index + "&page=404";

	document.location.href = url;
	document.getElementById("itemb-sorting").selectedIndex = index;
//	viewpaging(len, 1);
	
}*/

function sortingchange(val)
{
	/*if(val == "top")
	{
		index = document.getElementById("topsorting").selectedIndex;		
	}
	else
	{
		index = document.getElementById("bottom-sorting").selectedIndex;
	}*/
	index = document.getElementById("topsorting").selectedIndex;

	url = pagelink + "?sorttype=" + index + "&size=" + psize + "&page=" + pagenum;

	document.location.href = url;
}

function pagechange(len, curpage)
{
	//index = document.getElementById("itemt-sorting").selectedIndex;	
	url = pagelink + "?sorttype=" + srttype + "&size=" + psize + "&page=" + curpage;
	document.location.href = url;
}
function viewpaging(len,curpage)
{	
	
	pagenum=curpage;
	pno = psize;
	totalpage=Math.ceil(len / pno);		

	if(curpage > totalpage && curpage != 404)
	{
		curpage = totalpage;
	}
	
	startpos = (curpage - 1) * pno;
	endpos = (curpage * pno) - 1;	

	var count = 0;
	var itemno=0;
	
	if(curpage == 404)
	{
		ototal = len;
	}
	else
	{
		ototal = endpos - startpos;
	}
	
	var dhtml = "";
	itemno = 1;
	
	for(i=0;i<len;i++)
	{

		if((i>=startpos && i<=endpos) || (curpage==404))
		{
			dhtml = dhtml + "<div class='cat_fp_box'>";
			
			dhtml += "<div class='cat_img_box'><img src='http://site.verseo.com/ystore/images/cat_top_round.gif' class='img_left' alt='Cat Top'><div class='img_center'><a href='"  + data[i].pid + ".html'><img src='" + data[i].image + "' alt='" + data[i].productname + "' title='" + data[i].productname + "' border='0'><img id='quickview"+i+"' name='quickview"+i+"' class='preview' src='http://site.verseo.com/ystore/images/quick_view.gif' alt='Quick View' title='Quick View' onClick='javascript:snapshop("+i+"); return false;'/></a></div><img src='http://site.verseo.com/ystore/images/cat_bottom_round.gif' class='img_left' alt='Cat Bottom'></div>";
			
			if(data[i].productname.length > 40)
				productname = data[i].productname.substr(0, 38) + "...";
			else
				productname = data[i].productname;
			dhtml += "<p style='height:35px;'><a href='"  + data[i].pid + ".html'>" + productname + "</a></p>";

			if(data[i].productprice > 0 && data[i].saleprice > 0)
			{
				dhtml += "<p><span class='product_line'>$" + data[i].productprice.toFixed(2) + "</span><span class='price'>$" + data[i].saleprice.toFixed(2) + "</span></p>";
			}
			else
			{
				dhtml += "<p><span class='price'>$";
				if(data[i].saleprice > 0)
					dhtml += data[i].saleprice.toFixed(2);
				else
					dhtml += data[i].productprice.toFixed(2);
				dhtml += "</span></p>";
			}
			if(data[i].prorating > 0){
				dhtml += "<p style='height:17px;'><img src='http://site.verseo.com/ystore/images/rating"+data[i].prorating+".jpg' alt='Rating' title='Rating'></p>";
			}
			/*else
				dhtml += "<p>Not rated yet</p>";*/
			dhtml += "<div style='height:115px;'>"+data[i].description+"</div>";
			
			dhtml += "</div>";
		}
	}
	//$("#pagedisplay").wait();
	document.getElementById("pagedisplay").innerHTML = dhtml;
	
	writepages(totalpage, startpos, endpos);
}


function writepages(totalpages, stpageno, endpageno)
{
	var distxt= " ";
	
	if(pagenum > 1 && pagenum!=404)
	{
		distxt += "<a href='javascript:pagechange(" + totalpages + ", " + (pagenum - 1) + ");'>< Prev</a> ";
	}
	
	distxt += " [ ";	
	   
	if(totalpages > 30)
	{
		numarray = new Array();
		if(pagenum > 4)
		{
			startno = pagenum - 2;
			endno = pagenum + 2;
			
			if(pagenum == 1)
			{
				distxt += "<span>1</span> <span>......</span> ";
			}
			else
			{
				distxt += "<a href=javascript:pagechange(" + totalpages + ", 1);>1</a> | <span>......</span> ";
			}
			if(endno >= totalpages)
			{
				endno = totalpages - 1;
			}
			for(i=startno; i <= endno; i++)
			{
				if(i == pagenum)
				{
					distxt += "<span>" + i + "</span>";
					if(i==totalpages)
					{						
					}
					else
					{
						distxt += " | ";
					}
				}
				else
				{
					distxt += "<a href=javascript:pagechange(" + totalpages + "," + i + ");>" + i + "</a>";
					if(i==totalpages)
					{						
					}
					else
					{
						distxt += " | ";
					}
				}
			}
			if(endno == totalpage - 1)
			{
				if(pagenum == totalpages)
				{
					distxt += "<span>" + totalpages + "</span>";
				}
				else
				{
					distxt += " <a href=javascript:pagechange(" + totalpages + "," + totalpages + ");>" + totalpages + "</a>";
				}
			}
			else
			{
				if(pagenum == totalpages)
				{
					distxt += "<span>......</span> <a href='#'>" + totalpages + "</a>";
				}
				else
				{
					distxt += "<span>......</span> <a href=javascript:pagechange(" + totalpages + "," + totalpages + ");>" + totalpages + "</a>";
				}
			}
		}
		else
		{
			startno = 2;
			endno = 5;
			distxt += "<a href=javascript:pagechange(" + totalpages + ",1);>1</a> | ";
			for(i=startno; i <= endno; i++)
			{
				if(i == pagenum)
				{
					distxt += "<span>" + i + "</span>";
					if(i==totalpages)
					{						
					}
					else
					{
						distxt += " | ";
					}
				}
				else
				{
					distxt += "<a href=javascript:pagechange(" + totalpages + "," + i + ");>" + i + "</a>";
					if(i==totalpages)
					{						
					}
					else
					{
						distxt += " | ";
					}
				}
			}
			distxt += "<span>......</span> <a href=javascript:pagechange(" + totalpages + "," + totalpages + ");>" + totalpages + "</a>";
		}
	}
	else
	{
		if(pagenum==404)
       {
               distxt += "<span>View All</span> | ";
       }
       else
       {
               distxt += "<a href=javascript:pagechange(" + totalpages + ",404); class='next_pre'>View all</a> | ";
       }

		for(i=1; i <= totalpages; i++)
		{
				if(i == pagenum)
				{
					distxt += "<span>" + i + "</span>";
					if(i==totalpages)
					{						
					}
					else
					{
						distxt += " | ";
					}
				}
				else
				{
					distxt += "<a href=javascript:pagechange(" + totalpages + "," + i + ");>" + i + "</a>";
					if(i==totalpages)
					{						
					}
					else
					{
						distxt += " | ";
					}
				}
		}
	}
	distxt += " ] ";
	if(pagenum < totalpages)
	{
		distxt += " <a href='javascript:pagechange(" + totalpages + ", " + (pagenum + 1) + ");'>Next ></a>";
	}
	if(endpageno > len)
	{
		endpageno = len;
	}
	else
	{
		endpageno++;
	}
	
	document.getElementById('pagging_disp').innerHTML=distxt;
}

// Wait function jquery Start
$.fn.wait = function(time, type) {
	time = time || 2000;
	type = type || "fx";
	return this.queue(type, function() {
		var self = this;
		setTimeout(function() {
			$(self).dequeue();
		}, time);
	});
};
// Wait function jquery End

function snapshopclose(poss)
{

	$("#snapshop").fadeOut("slow");
	document.getElementById("quickview"+poss).src = 'http://site.verseo.com/ystore/images/quick_view.gif';
}

function snapshop(pos)
{

	document.getElementById("quickview"+pos).src = 'http://site.verseo.com/ystore/images/quick_view_hover.gif';
	$("#snapshop").wait();

	ihtml = "<div class='popup_border'><div class='popup_width'> <div id='hideval' style='display:none;'></div><div id='hidecolor' style='display:none;'></div> <abbr id='titleBar' style='cursor:move;'><img src='http://site.verseo.com/ystore/images/move_img.jpg' alt='Drag' title='Drag'></abbr><img src='http://site.verseo.com/ystore/images/close_img.jpg' alt='Close' class='img_right' style='padding:0px 0px 5px 0px;' onClick='javascript:snapshopclose("+pos+");' /><h1>"+data[pos].productname+"</h1> <div style='padding:5px; float:right; cursor:pointer;'> </div><div class='tabing' id='item_tabbing'><a href='' class='description over'><span>Order</span></a> </div> <div class='tabcontentstyle_new'> <div class='tab-content-top-corners_new'> <img src='http://site.verseo.com/ystore/images/right_corner1.jpg' alt='Right Corner' class='img_right'  title='Right Corner'> </div> <div class='tabbing-content-main_new'> <div id='tabdiv1'  class='tabcontent'> <form method='POST' action='https://order.store.yahoo.net/cgi-bin/wg-order?verseo+"+data[pos].pid+"' onsubmit='return checkorder("+pos+");' target='orderframe'><table width='100%' border='0' cellspacing='0' cellpadding='0'> <tr> <td><div class='popup_product'> <div class='popup_left'> <div class='img_center'><span></span><img src='" + data[pos].image + "' alt='" + data[pos].productname + "' title='" + data[pos].productname + "' border='0'></div><div class='view_product'><a href='"+data[pos].pid+".html'>View Product</a> </div> </div><div class='popup_description'> <div class='description_text'> <p>Item #:</p> <span>" + data[pos].procode + "</span> </div>";
	
	if(parseInt(data[pos].productprice) > 0 && parseInt(data[pos].saleprice) > 0)
	{
		ihtml += "<div class='description_text' ><p>Price:</p><span><abbr style='text-decoration: line-through;'>$"+data[pos].productprice.toFixed(2)+"</abbr></span></div><div class='description_text'> <p>Sale price:</p> <span>$" + data[pos].saleprice.toFixed(2) + "</span></div>";
	}
	else if(parseInt(data[pos].productprice) > 0)			
	{
		ihtml += "<div class='description_text' ><p>Price:</p><span><abbr style='text-decoration: line-through;'>$"+data[pos].productprice.toFixed(2)+"</abbr></span></div>";
	}
	else if(parseInt(data[pos].saleprice) > 0)
	{
		ihtml += "<div class='description_text'> <p>Sale price:</p> <span>$" + data[pos].saleprice.toFixed(2) + "</span></div>";
	}
	if(data[pos].itemoptions != '')
	{
		ihtml += "<div class='description_text'>" + data[pos].itemoptions + "</div>";
	}
	
	ihtml += "<div class='description_text'><p>Quantity:</p> <span> <input type='text' class='qty_fild' name='vwquantity' value='1'></span> </div><input name='vwitem' type='hidden' value='"+data[pos].pid+"' /><input name='vwcatalog' type='hidden' value='verseo' />";
	if(data[pos].upsells == 'No')
	{
		ihtml += "<div class='description_text'><input type='image' src='http://site.verseo.com/ystore/images/add_to_cart.jpg' class='img_left' alt='Add to Cart' title /></div>";
	}
	else
	{
		ihtml += "<div class='description_text'><a href='"+data[pos].pid+".html'><img src='http://site.verseo.com/ystore/images/add_to_cart.jpg' class='img_left' alt='Add to Cart' title /></a></div>";
	}
	ihtml += "</div></div></td>  </tr></table></form></div></div></div></div></div>";
	
	//ihtml += "<div class='tabcontent' id='tabdiv2' style='display:none;'><table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td><div class='popup_product'><div class='popup_left'><div class='img_center'><span></span><img src='" + data[pos].image + "' border='0'></div><div class='view_product'><a href='"+data[pos].pid+".html'>View Product</a> </div></div><p> <div class='scroll_box' id='prodetails' style='height:200px; width:310px; overflow:auto; padding-left:5px;'</div></p></div></td></tr></table></div>";
	
	// snapshop height width
	if((pos+4) % 3 == 0)
	{
		var swidth = 670;
	}
	else
	{
		var swidth = 470;
	}
	
	var sheigh = 345;
	// find mouse pos
	var wpos = 0;
	var hpos = 0;
	var counter = 0;

	function showit()
	{
		if(counter == 0)
		{
			document.getElementById("snapshop").style.left = (event.clientX + document.documentElement.scrollLeft) - (swidth / 2) + "px";
			var HeightCalcBot = document.documentElement.clientHeight - event.clientY;
			if(event.clientY < (sheigh / 2))
			{
				document.getElementById("snapshop").style.top = document.documentElement.scrollTop +"px";
			}
			else if(HeightCalcBot < (sheigh / 2))
			{
				document.getElementById("snapshop").style.top = (event.clientY + document.documentElement.scrollTop + HeightCalcBot) - (sheigh + 20) +"px";
			}
			else
			{
				document.getElementById("snapshop").style.top = (event.clientY + document.documentElement.scrollTop) - (sheigh / 2) +"px";
			}
			document.getElementById("snapshop").innerHTML = ihtml;
			/*document.getElementById("snapshop").style.display = "";*/
			$("#snapshop").fadeIn("slow");
			//display product details
			//document.getElementById("prodetails").innerHTML = data[pos].productdetails;
			counter++;
		}
	}
	function showitMOZ(e)
	{
		if(counter == 0)
		{
			document.getElementById("snapshop").style.left = e.pageX - (swidth / 2) + "px";
			var HeightCalcTop = e.pageY - window.pageYOffset;
			var HeightCalcBot = (window.pageYOffset + window.innerHeight) - e.pageY;
			if(HeightCalcTop < (sheigh / 2))
			{
				document.getElementById("snapshop").style.top = (e.pageY - HeightCalcTop) + 5 +"px";
			}
			else if(HeightCalcBot < (sheigh / 2))
			{
				document.getElementById("snapshop").style.top = (e.pageY - sheigh - 20) + (HeightCalcBot) +"px";
			}
			else
			{
				document.getElementById("snapshop").style.top = e.pageY - (sheigh / 2) +"px";
			}
			document.getElementById("snapshop").innerHTML = ihtml;
			/*document.getElementById("snapshop").style.display = "";*/
			$("#snapshop").fadeIn("slow");
			//display product details
			//document.getElementById("prodetails").innerHTML = data[pos].productdetails;
			counter++;
		}
	}
	if (!document.all){
		window.captureEvents(Event.CLICK);
		window.onclick=showitMOZ;
	}
	else
	{		
		document.onclick=showit;
	}
	// end mouse pos
	
	// Start Dragging Coading
	$('#snapshop').draggable({ handle: 'abbr' });
    var handle = $('#snapshop').draggable('option', 'handle');
    $('#snapshop').draggable('option', 'handle', 'abbr');
	
	$('#snapshop').draggable({ opacity: 0.35 });
	var opacity = $('#snapshop').draggable('option', 'opacity');
	$('#snapshop').draggable('option', 'opacity', 0.35);
	// End Dragging Coading
	
	// Call function inside script
	setTimeout("displaysize()",2000);
	$("#psizepop").wait();
}