// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }

    return vars;
}

// JavaScript Document
jQuery.historyProductBox = function(data){
	var img = jQuery('<img>');
	img.addClass('prodthumb');
	img.attr('src', data.thumbnailurl);
	img.attr('alt', data.name);
	
	var aImg = jQuery('<a>');
	aImg.attr('href', '/mmh/product.do?id=' + data.id);
	aImg.attr('title', data.name);
	aImg.append(img);
	
	var aText = jQuery('<a>');
	aText.attr('href', '/mmh/product.do?id=' + data.id);
	aText.addClass('f');
	aText.attr('title', data.name);
	aText.append(data.name);
	
	var buyNow = jQuery('<a>');
	buyNow.attr('href', '/mmh/product.do?id=' + data.id);
	buyNow.addClass('mmhMinistoreBuy');
	buyNow.append('Buy Now');
	
	var div = jQuery('<div>');
	div.addClass('productboxHistory');
	div.append(aImg);
	div.append('<br/>');
	div.append(aText);
	div.append('<br/>');
	
	if(data.msrp!=null && data.msrp > data.retailprice){
		div.append('<strike>$'> + data.msrp + '</strike>');	
	}
	
	div.append('<span class="mmhprice">$' + data.retailprice +'</span>');
	div.append('<br/>');
	div.append('<br/>');
	div.append(buyNow);
	
	return div;		
	
}

jQuery.historyProductLine = function(data){
	var img = jQuery('<img>');
	img.addClass('prodthumbLine');
	img.attr('src', data.thumbnailurl);
	img.attr('alt', data.name);
	
	var aImg = jQuery('<a>');
	aImg.attr('href', '/mmh/product.do?id=' + data.id);
	aImg.attr('title', data.name);
	aImg.append(img);
	
	var aText = jQuery('<a>');
	aText.attr('href', '/mmh/product.do?id=' + data.id);
	aText.addClass('f');
	aText.attr('title', data.name);
	aText.append(data.name);
		
	var div = jQuery('<div>');
	div.addClass('productLineHistory');
	div.append(aImg);
	div.append(aText);
	div.append('<div style="clear:both;"></div>');
	
	return div;		
	
}

var recentHistoryViewedScrollable;
var recentHistoryRecommendScrollable;

jQuery(document).ready(function(){
	var hash = getUrlVars();
	
	jQuery("#recentHistory").show();
	jQuery("img.recentHistory").show();
	
	jQuery.getJSON("/notifications/recentlyViewed.php",
				   function(data){
					   
							if(data.viewed.length > 0){													
								
								jQuery.each(data.viewed, function(i,item){
									jQuery("#recentHistoryViewedItems").append(jQuery.historyProductLine(item));
									jQuery("#recentHistoryViewedItems").append('<br/>');
	
								});
	
								recentHistoryViewedScrollable = jQuery("#recentHistoryViewed").scrollable({
									size: 2,
									vertical: true,
									items: ".recentHistoryViewedItems",
									prevPage: ".recentHistoryViewedPrevPage",
									nextPage: ".recentHistoryViewedNextPage",
									api: true
								});
		
								
							}
							else{
								jQuery("#historySide").hide();
								jQuery(".recentHistoryText").hide();
							}
							
							if(data.recommend.length > 0){
								jQuery.each(data.recommend, function(i,item){
									jQuery("#recentHistoryRecommendItems").append(jQuery.historyProductBox(item));
	
								});
							}
							
							recentHistoryRecommendScrollable = jQuery("#recentHistoryRecommend").scrollable({
							  size: 3,
							  items: ".recentHistoryRecommendItems",
							  prevPage: ".recentHistoryRecommendPrevPage",
							  nextPage: ".recentHistoryRecommendNextPage",
							  api: true
							});
							
							jQuery("#recentHistoryLoading").hide();
							jQuery("table.recentHistory").show();
							jQuery('#recentHistory').attr('class', jQuery('#recentHistory').attr('class'));
							
							

				   }
	);

});

