var rr_sears_layout = [];

RR.COMPARE_BASE_URL = 'http://www.sears.com/shc/s/Compare?storeId=10153&catalogId=12605&';
RR.QV_ENABLED = true;

function rr_compareGo(cArr) {
	//cArr is the array of id's to compare
	var cArr = RR.explorer.compareItems;
	var linkString = '';
	
	//check to see if we have more than 1 item
	if (cArr.length > 1) {
		//if we have mor than one then build the string and go	
		if (RR.COMPARE_BASE_URL){
			//compile the compare url
			for (i=0;i<cArr.length;i++) {
				linkString += "partNumber_" + (i+1) + "=" + cArr[i] + "&";
			}		
			//add on the product count
			linkString += "prodCount=" + i;
			
			//go to the url
			window.location.href = RR.COMPARE_BASE_URL + linkString;
		}
	}else{
		alert("You must select at least 2 items to compare");		
	}
}

function rrafterMatrixInsert() {
	dotip();

	$('div.tip > a').click(function() {
		$('.qtip-active').qtip('hide');
	});
}
function dotip(){
	
	$('#rr_matrix div.tip').each(function() {
		var content = $($(this).children('div')[0]).html();
		
		$(this).qtip({					
		   	content: content,
		   	show: { when: 'mouseover', delay: 300, effect: { type: 'fade', length: 400 } },
		   	hide: { when: 'mouseout', fixed: true, delay: 300, effect: { type: 'fade', length: 400 } },
	       	style: { background: 'url(//media.richrelevance.com/rrserver/jsmedia/'+RR.explorer.COMMON.getApiKey()+'/explorer/gradient.gif) white repeat-x', tip: { corner: 'topMiddle', size: { x: 24, y: 8 } }, border: { width: 1, radius: 3, color: '#4271ae' }, width: { min: 200, max: 200 } },
	       	position: {
	    	   	corner: {target: 'bottomMiddle', tooltip: 'topMiddle'},
	    	   	adjust: { screen: true }
	    	},
	    	api: {
	    		onFocus: function(event) {
	        		// support map link
	        		$('.qtip-active').find('a.mapLink').click(function() {
	        			mapClick(this);
	        		});
	        	}
	    	}
		});
	});
	
};
function rrqvBtn(expr){
    var screen = '<div class="screen">';
	var screenclose = '</div>';

	// support map link
	$("a.mapLink").click(function(){mapClick(this)});
	
	//hardcoded rr path for image button 
	var QVbtn = '<a href="#quickView" title="View QuickView information for this item" class="QVbtn"><img alt="quick view button" src="//media.richrelevance.com/rrserver/jsmedia/10153/explorer/btn_quickView.gif" style="background:transparent;"></a>';
    
	var photoDiv = $(expr).find('div.qv');
	
	// make sure that quickview is not already on it
	$(photoDiv).unbind();
	$(photoDiv).children('.screen').remove();
	$(photoDiv).children('.QVbtn').remove();

	$(photoDiv).hover(function(){
        var _this = $(this);
        var qvString = _this.find('a').unencode().split("/s/p_")[1].split("?")[0];
		var rrqvString = _this.find('a').attr("href");;
		//alert(rrqvString);
       
        //Check if the product is SHC product - Vendor Product enhancement
        if(qvString != null && (qvString.indexOf("_") > 0)) {
			var qvStrng = qvString.split("_");
        	var qvSID = qvStrng[0];
			var qvCID = qvStrng[1];
	        var qvPID = qvStrng[2].toUpperCase();
			// Omniture variables
        	var vert = omVrt.split("'").join("");
        	var cat = omCat.split("'").join("");
        	var sbcat = omSubCat.split("'").join("");

	        //Check whether its a SHC's store - Vendor Product enhancement
			if(qvSID=="10153"||qvSID=="11201"||qvSID=="10151"||qvSID=="10152"||qvSID=="10701"||qvSID=="11701"||qvSID=="11702"||qvSID=="11703"||qvSID=="10154"){ 
		        _this.addClass("over");
		        _this.append(screen);
		        $(QVbtn).appendTo(_this).css({left:function(){
		            return ($(this).parent().width()/2)-($(this).width()/2)
		        }}).attr('onclick','s.prop26=\''+vert+' > '+cat+' > '+sbcat+' > '+'Quick View\';s.events=\'event27\';s.product=\''+qvPID+'\';s.tl();').hide();
				
				//animate the screen
		        _this.find(".screen").animate({height:50},300,function(){
		            $(this).parent().find(".QVbtn").fadeIn(300);
			        });
					
				//Add thumbs up to the screen	
				_this.find(".screen").click(function(){
		            //alert("Click");
					RR.explorer.thumbsUp(qvPID,rrqvString);
			        });

		   $('a[@href*=#quickView]').click(function(){
				/**
				$.getJSON("js/product.json",function(data){
					SEARS.Item.info(data);
					SEARS.QuickView.launch();
				})**/
				RR.explorer.attribute(rrqvString);
				//alert(qvSID + ' ' + qvCID + ' ' + qvPID)
				showQuickView(qvSID,qvCID,qvPID);
				return false;
			});
			}
	    }
    },function(){
        var _this = $(this);
        _this.removeClass("over");
        _this.find(".QVbtn").remove();
        _this.find(".screen").remove();
    });
}
function addCategory(i, category, checked) {
	$('.rr_categories_filter').append('<div class="rr_category_filter"><input type="checkbox" name="catFilter" value="'+category.extId+'" id="'+category.id+'"'+((checked) ? ' checked="checked"' : '')+'/>&nbsp;<label for="'+category.id+'">'+category.name+'</label></div>');
}
function addBrand(i, brand, checked) {
}
function clearPrice() {
	$('#rr_other_filters input[name="min_price"]').attr('value', 'min');
	$('#rr_other_filters input[name="max_price"]').attr('value', 'max');
	$('#rr_filter > div.rr_filter_inner > span.rr_price > input[name="min_fake"]').attr('value', 'min');
	$('#rr_filter > div.rr_filter_inner > span.rr_price > input[name="max_fake"]').attr('value', 'max');
	delete RR.explorer.constraint.filprmin;
	delete RR.explorer.constraint.filprmax;
}
RR.explorer.afterLayout = function() {
	$('#rr_filter > div.rr_filter_inner > .rr_choose').click(function() {
		RR.explorer.openFilters();
		return false;
	});
	$('#rr_other_filters input[name="min_price"]').change(function() {
		RR.explorer.constraint.filprmin = $(this).attr('value')*100.0;
		$('#rr_filter > div.rr_filter_inner > span.rr_price > input[name="min_fake"]').attr('value', $(this).attr('value'));
	}).focus(function() {
		if ($(this).attr('value') == 'min') {
			$(this).attr('value','');
		}
	}).blur(function() {
		if ($(this).attr('value') == '') {
			$(this).attr('value','min');
		}
	});
	$('#rr_other_filters input[name="max_price"]').change(function() {
		RR.explorer.constraint.filprmax = $(this).attr('value')*100.0;
		$('#rr_filter > div.rr_filter_inner > span.rr_price > input[name="max_fake"]').attr('value', $(this).attr('value'));
	}).focus(function() {
		if ($(this).attr('value') == 'max') {
			$(this).attr('value','');
		}
	}).blur(function() {
		if ($(this).attr('value') == '') {
			$(this).attr('value','max');
		}
	});
	$('#rr_filter > div.rr_filter_inner > span.rr_price > input[name="min_fake"]').focus(function() {
		RR.explorer.openFilters();
		$('#rr_other_filters input[name="min_price"]').focus();
	}).click(function() {
		RR.explorer.openFilters();
		$('#rr_other_filters input[name="min_price"]').focus();
	});
	$('#rr_filter > div.rr_filter_inner > span.rr_price > input[name="max_fake"]').focus(function() {
		RR.explorer.openFilters();
		$('input[name="max_price"]').focus();
	}).click(function() {
		RR.explorer.openFilters();
		$('input[name="max_price"]').focus();
	});
	$('#rr_other_filters .rr_filter_apply').click(function() {
		RR.explorer.applyFilters();
	}).mouseover(function() {
		$(this).addClass('rr_button_active');
	}).mouseout(function() {
		$(this).removeClass('rr_button_active');
	});
	$('#rr_other_filters .rr_filter_close').click(function() {
		$('#rr_other_filters').hide();
		dotip();
	}).mouseover(function() {
		$(this).addClass('rr_button_active');
	}).mouseout(function() {
		$(this).removeClass('rr_button_active');
	});
	$('#rr_no_products .rr_filter_close').click(function() {
		$('#rr_no_products').hide();
	}).mouseover(function() {
		$(this).addClass('rr_button_active');
	}).mouseout(function() {
		$(this).removeClass('rr_button_active');
	});
	$('#rr_cat_select_all').click(function() {
		$('.rr_category_filter > input[name="catFilter"]').attr('checked','checked');
		$('.rr_category_filter > input:checked').each(function() {
			RR.explorer.constraint.filcat.push($(this).attr('value'));
		});
		return false;
	});
	$('#rr_cat_clear_all').click(function() {
		$('.rr_category_filter > input[name="catFilter"]').removeAttr('checked');
		RR.explorer.constraint.filcat = [];
		return false;
	});
}

/* Hidden div to contain image for tracking attributables */
rr_sears_layout.push('<div id="rr_hiddenClickImgDiv">');
	rr_sears_layout.push('<img id="rr_hiddenClickImg"/>');
rr_sears_layout.push('</div>');

/* Yout Clicks Area */
rr_sears_layout.push('<div id="rr_outerThumbsUp">');
	rr_sears_layout.push('<div id="rr_yourclicks" style="display:none;">');
		rr_sears_layout.push('<div>Your Clicks</div><div class="rr_reset" onclick="javascript:location.reload(true);"></div>');
	rr_sears_layout.push('</div>');
	rr_sears_layout.push('<div id="rr_thumbsUp">');
		rr_sears_layout.push('<img id="rr_instructions" src="//media.richrelevance.com/rrserver/jsp_myrecsDemo/GiftSee-Instructions-01.gif"/>');
	rr_sears_layout.push('</div>');
rr_sears_layout.push('</div>');

/* Headers over matrix area */
rr_sears_layout.push('<div class="rr_mrTopHeader">');
	rr_sears_layout.push('<div class="rr_header rr_giftsee">');
		rr_sears_layout.push('<span id="rr_feedback_link"><a href="dummy#TB_inline?height=155&width=450&inlineId=rr_feedback_hidden&modal=true" class="thickbox">(feedback)</a></span>');
		rr_sears_layout.push('<div class="rr_mrTopLeftHeader">&nbsp;</div>');
		rr_sears_layout.push('<div class="rr_title">GiftSee<span class="superscript">TM</span></div>');
		rr_sears_layout.push('<div id="rr_filter">');
			rr_sears_layout.push('<div class="rr_filter_inner">');
				rr_sears_layout.push('<span class="rr_choose">Choose Categories<span></span></span>');
				rr_sears_layout.push('<span class="rr_price">Price: $<input name="min_fake" value="min"/> to $<input name="max_fake" value="max"/>&nbsp;<a href="#" onclick="RR.explorer.openFilters(); return false;">More Options</a></span>');
			rr_sears_layout.push('</div>');
			rr_sears_layout.push('<div id="rr_other_filters">');
				rr_sears_layout.push('<div class="rr_filter_inner">');
					rr_sears_layout.push('<span class="rr_choose">Choose Categories</span>');
					rr_sears_layout.push('<span class="rr_price">Price: $<input name="min_price" value="min"/> to $<input name="max_price" value="max"/> <a href="#" onclick="clearPrice(); return false;">Clear</a></span>');
					rr_sears_layout.push('<span class="rr_filter_apply">&nbsp;</span>');
					rr_sears_layout.push('<span class="rr_filter_close">&nbsp;</span>');
				rr_sears_layout.push('</div>');
				rr_sears_layout.push('<div class="rr_text_filters">');
					rr_sears_layout.push('<div class="rr_hierarchy_filter">');
						rr_sears_layout.push('<div class="rr_genres_filter"></div>');
						rr_sears_layout.push('<div class="rr_categories_filter"><div class="rr_cat_filter_control"><a id="rr_cat_select_all" class="rr_cat_filter_control_inner" href="#">Select All</a>&nbsp;|&nbsp;<a id="rr_cat_clear_all" class="rr_cat_filter_control_inner" href="#">Clear All</a></div></div>');
						rr_sears_layout.push('<div class="rr_clear">&nbsp;</div>');
					rr_sears_layout.push('</div>');
				rr_sears_layout.push('</div>');
			rr_sears_layout.push('</div>');
			rr_sears_layout.push('<div id="rr_no_products">');
				rr_sears_layout.push('<div><span>No items were found that matched the set filters. Please change the filters and try again.</span><span class="rr_filter_close">X</span></div>');
			rr_sears_layout.push('</div>');
		rr_sears_layout.push('</div>');
	rr_sears_layout.push('</div>');
rr_sears_layout.push('</div>');


/* Message above matrix */
rr_sears_layout.push('<div id="rr_matrixMsg"></div>');

/* Matrix */
rr_sears_layout.push('<div class="rr_outerMatrix">');
	rr_sears_layout.push('<div id="rr_matrix">');
	rr_sears_layout.push('</div>');
rr_sears_layout.push('</div>');

/* Feedback modal dialog */
rr_sears_layout.push('<div id="rr_feedback_hidden" style="display:none;">');
	rr_sears_layout.push('<div id="rr_feedback_message">');
		rr_sears_layout.push('GiftSee is a new feature and we would love to hear what you think of this shopping experience.  Please send us your feedback as we work to improve the experience.');
	rr_sears_layout.push('</div>');
	rr_sears_layout.push('<div id="rr_feedback_box" title="GiftSee Feedback">');
		rr_sears_layout.push('<textarea rows="4" cols="50" name="rr_feedbackMessage" id="rr_feedbackMessage"></textarea><br /><br/>');
		rr_sears_layout.push('<img src="//media.richrelevance.com/rrserver/jsp_myrecsDemo/send_feedback.png" alt="Send Feedback" onclick="RR.explorer.handleFeedback();" />');
		rr_sears_layout.push('<img src="//media.richrelevance.com/rrserver/jsp_myrecsDemo/cancel_feedback.png" alt="Cancel" onclick="tb_remove()" />');
	rr_sears_layout.push('</div>');
rr_sears_layout.push('</div>');

RR.explorer.LAYOUT = rr_sears_layout.join('');

var rr_sears_item = [];

//Grid and promoted items
//Promoted items are surrounded by div with class rr_thumbsUp

rr_sears_item.push('<div class="rr_matrixItem">');
	rr_sears_item.push('<div class="rr_lessInfo">');
		rr_sears_item.push('<div onclick="RR.explorer.thumbsUp(\'$ID$\',\'$URL$\');" class="rr_promote">Promote&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>');
		rr_sears_item.push('<div onclick="RR.explorer.thumbsDown(\'$ID$\');" class="rr_remove">Remove&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>');
		rr_sears_item.push('<div class="qv tip">'); 
			rr_sears_item.push('<a href="$URL$" onclick="return false">'); 
				rr_sears_item.push('<img src="$IMAGE$" onclick="RR.explorer.thumbsUp(\'$ID$\',\'$URL$\'); return false;"/>');
			rr_sears_item.push('</a>');
			rr_sears_item.push('<div style="display: none">');
				rr_sears_item.push('<div class="rr_matrixTooltipText">$NAME$</div><div class="saveStory">$CUSTOMattr_savestory$</div>');
			rr_sears_item.push('</div>');
		rr_sears_item.push('</div>');
		//Leave this here for debugging: rr_sears_item.push('<div id="rank$ID$" style="display:none;">$RANK$</div>');
	rr_sears_item.push('</div>');

	//First Item
	rr_sears_item.push('<div class="rr_moreInfo">');
		rr_sears_item.push('<div onclick="RR.explorer.thumbsDown(\'$ID$\');" class="rr_remove">Remove&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>');
		rr_sears_item.push('<div style="height: 20px;"></div>');
		rr_sears_item.push('<div class="qv">'); 
			rr_sears_item.push('<a href="$URL$">');
				rr_sears_item.push('<img src="$LARGEIMAGE$" style="border:0 none;" />');
			rr_sears_item.push('</a>');
		rr_sears_item.push('</div>');
		rr_sears_item.push('<br/><a class="rr_itemname" title="$NAME$" href="$URL$">$NAME$</a>');
		rr_sears_item.push('<br/><div class="saveStory">$CUSTOMattr_savestory$</div>');
	rr_sears_item.push('</div>');

	//BothFirst and Last
	rr_sears_item.push('<div onclick="window.location.href=\'$URL$\'" class="rr_detail"></div>');
	rr_sears_item.push('<p class="rr_compare">');
			rr_sears_item.push('<input id="compareFirst-$ID$" type="checkbox" style="float: left" onclick="RR.explorer.compareClick(\'$ID$\');" name="$ID$" />');
			rr_sears_item.push('<label class="compareLabel" for="compareFirst-$ID$">Compare</label>');
			rr_sears_item.push('<a class="compareLinkHidden" onclick="RR.explorer.compareGo()" title="Compare this item"></a>');
		rr_sears_item.push('</p>');
rr_sears_item.push('</div>');
RR.explorer.ITEM = rr_sears_item.join('');
