// JavaScript Document

function htmlEncode(value){ 
  return jQuery('<div/>').text(value).html(); 
} 

function htmlDecode(value){ 
  return jQuery('<div/>').html(value).text(); 
}

var recentHistoryRecommendScrollable;
var recentHistoryViewedScrollable;

// 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.recommendProductBox = 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;		
	
}

function loadMMHRecommendations(divId, type, payload){
	jQuery.getJSON("/notifications/productRecommendations.php",
				   {type: type, payload: payload},
				   function(data){
						if(data){
							var hash = getUrlVars();

							jQuery("img.recentHistory").show();	
							
							if(data.viewed && 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("#recentHistoryRightColumn").remove();
							}
							
							jQuery.each(data.recommend, function(i,item){
								jQuery("#recentHistoryRecommendItems").append(jQuery.recommendProductBox(item));
							});
							
	
							recentHistoryRecommendScrollable = jQuery("#recentHistoryRecommend").scrollable({
							  size: 3,
							  items: ".recentHistoryRecommendItems",
							  prevPage: ".recentHistoryRecommendPrevPage",
							  nextPage: ".recentHistoryRecommendNextPage",
							  api: true
							});
							
							jQuery("#recentHistoryLoading").hide();
							jQuery("table.recentHistory").show();
							jQuery(divId).attr('class', jQuery(divId).attr('class'));
						}
						else{
							alert("no data");   
					   }
				   }
				   
	);
				   
}

function loadMMHVerticalRecommendations(divId, type, payload){
	jQuery.getJSON("/notifications/productRecommendations.php",
				   {type: type, payload: payload},
				   function(data){
						if(data){
							var hash = getUrlVars();
	
							var startingHtml = '<h1 class="title">Customers Who Bought These Items Also Bought</h1><a class="bestsellers_vert_prev bestsellers_vert_browse bestsellers_vert_top"></a><div id="bestsellers_vert_scrollable"><div class="bestsellers_vert_items" id="bestsellers_vert_items"></div></div><a class="bestsellers_vert_next bestsellers_vert_browse bestsellers_vert_bottom"></a>';	
							jQuery(divId).append(startingHtml);
							
							jQuery(divId).show();

							jQuery.each(data.recommend, function(i,item){
								var product = jQuery("<div>");
								product.addClass('bestsellers_vert_item');
								product.append('<a href="/mmh/product.do?id=' + item.id + '" title="' + htmlEncode(item.name) +'"><img src="'+ item.thumbnailurl +'" alt="' + item.name +'" border="0" class="bestseller"></a>');
								product.append('<br/>');
								product.append('<a class="bestseller" href="/mmh/product.do?id=' + item.id + '" title="' + htmlEncode(item.name) +'">' + htmlEncode(item.name) +'</a>');
								jQuery("#bestsellers_vert_items").append(product);
								//alert(product.html());
							});
							
							
							jQuery(divId).attr('class', jQuery(divId).attr('class'));
							
							jQuery("#bestsellers_vert_scrollable").scrollable({
								vertical: true,
								size: 3,
								items: ".bestsellers_vert_items",
								prevPage: ".bestsellers_vert_prev",
								nextPage: ".bestsellers_vert_next",
								disabledClass: "bestsellers_vert_disabled"
							});
							return true;
						}
						else{
							alert("no data");  
							return false;
					   }
				   }
				   
	);	
}



