$(document).ready(function(){

// ********************************************* //
// The variable below sets the number of columns //
// that will be used in the merchandise table.   //
// Change this to whatever you would prefer.     //
//                                               //
// You can revert to the standard class display  //
// by setting the value of "columns" to "1".     //
// ********************************************* //

var columns = 5;

// ********************************************* //
// This next variable is a message to be shown   //
// in the event that the item has multiple price //
// points (gift card or other nonmerch code).    //
// Type whatever it is that you wish to display  //
// in between the quotation marks.  For best     //
// results this should not be more than a couple //
// of words in length.                           //
// ********************************************* //

var pricePointMsg = "";

// ********************************************* //
// You may also choose to style the MerchList    //
// display by clicking CSS > Custom Group.       //
// Elements are assigned the following classes:  //
//                                               //
// .myMTable                                     //
// Table container for the merchandise items.    //
//                                               //
// .mTableCell                                   //
// Class added to each cell containing an item.  //
//                                               //
// .mTableCellHover                              //
// Class added when hovering over a cell.        //
//                                               //
// a.mDescriptionLink:link   and                 //
// a.mDescriptionLink:visited                    //
// These are for the normal state of an item's   //
// short description links.                      //
//                                               //
// a.mDescriptionLink:active   and               //
// a.mDescriptionLink:hover                      //
// Controls hover state for description links.   //
//                                               //
// .mSKU                                         //
// Class added to all item SKU numbers.          //
//                                               //
// .mPriceFields                                 //
// Class added to all item price fields.         //
// Includes sale price for items on a web sale.  //
// ********************************************* //

// Get information about what inSite page we're on //

var myPathVarOne = /MerchList/;
var myPathVarTwo = /merchlist/;
var loyaltyScreen = /Loyalty/;
var currentPathName = window.location.pathname;
var checkForMatchOne = currentPathName.search(myPathVarOne);
var checkForMatchTwo = currentPathName.search(myPathVarTwo);
var checkForLoyalty = currentPathName.search(loyaltyScreen);

// If we're not on the MerchList page, we're done //

if(checkForMatchOne != -1 || checkForMatchTwo != -1) {
if(checkForLoyalty != -1) return;

// If "columns" is set to 1 or less, we're done //

if(columns > 1){

// Hide the existing merchandise display.         //

$("table[id*=dgMerch]").hide();

// Get page elements we want to use in our custom //
// merch table and place them into arrays.        //

var myMerchCellsArray = $('table[id*=hdrMerchandiseInfo]').parent();
var myArraySize = myMerchCellsArray.length;
var merchImage = $('input[id*=_Image]');
var merchDesc = $('a[id*=MerchDesc]');
var merchSKU = $('span[id*=_LblSKU]');

// Create new merch table, add rows and cells //

var rowCount = 0;

// Create the table //

$('<table id="myMTable" class="myMTable"><tbody></tbody></table>').insertBefore('table[id*=dgMerch]');
for (var i=0; i < myArraySize; i += columns) {

// Create a row //

$('<tr id="myRow' + rowCount + '"></tr>').appendTo('#myMTable');
for (var j=0; (j < columns) && ( (i + j) < myArraySize); ++j) {

// Add cells to the row //
 
$('<td id="mcell' + (i + j) + '"></td>').appendTo("#myRow" + rowCount);
}
++rowCount;
}

// Now append the merch image, short description,  //
// and SKU number to our table cells.              //

jQuery.each(merchImage, function(i, val) {
$(val).appendTo("#mcell" + i);
$("<br />").appendTo("#mcell" + i);
});

jQuery.each(merchDesc, function(i, valD) {
$(valD).appendTo("#mcell" + i);
$("<br />").appendTo("#mcell" + i);
});

jQuery.each(merchSKU, function(i, valS) {
$(valS).appendTo("#mcell" + i);
$("<br /><br />").appendTo("#mcell" + i);

// Work with price fields //

// Get and format the numeric value within the price ID //

var j = i + 3;
var k = j;
if (k < 10) {
k = "0" + k;
}

// Set variable for base price //
var priceID = "dgMerch_ctl" + k + "_LCSP_Block_lblPriceValue";

// Set variables for sale prices //
var sale1ID = "dgMerch_ctl" + k + "_LCSP_Block_lbl1value";
var sale2ID = "dgMerch_ctl" + k + "_LCSP_Block_lbl2value";
var sale3ID = "dgMerch_ctl" + k + "_LCSP_Block_lbl3value";

var saleDIV = "dgMerch_ctl" + k + "_LCSP_Block_divSalePrice";

// Check to see if the base price span        //
// exists.  If so, append it.  If not then    //
// create it and append as empty span.        //

// This will be the only one available unless     //
// the item is on a web sale.  The only ways      //
// this span does not exist is if the item is     //
// on sale or a nonmerch code using price points. //

// Append the base price span if available.  //
if ($('span[id*=' + priceID + ']').length > 0) {
$('span[id*=' + priceID + ']').appendTo("#mcell" + i);
$("<br />").appendTo("#mcell" + i);
} 
// Append sale price div if available.  //
else if ($('div[id*=' + saleDIV + ']').length > 0) {
$('div[id*=' + saleDIV + ']').appendTo("#mcell" + i);
$("<br />").appendTo("#mcell" + i);
} 
// No regular or sale price, so append the price point message. //
else {
$('<span id="' + priceID + '"><strong>' + pricePointMsg + '</strong></span>').appendTo("#mcell" + i);
$("<br />").appendTo("#mcell" + i);
}


// If the item has been added to the cart, //
// display a message and the check mark.   //

var isAdded = "dgMerch_ctl" + k + "_Imgcheck";

$('<br /><span id="itemIsAdded' + i + '"></span><span class="mAddedMsg" id="addedMessage' + i + '">Added to cart.</span>').appendTo("#mcell" + i).hide();

if ($('img[id*=' + isAdded + ']').length > 0) {
$('#itemIsAdded' + i).show();
$('img[id*=' + isAdded + ']').appendTo($('#itemIsAdded' + i));
$('#addedMessage' + i).show();
}


});


// Add classes to content to allow for CSS styling //

$("td[id^=mcell]").addClass("mTableCell");
$("td[id^=mcell]").hover(function() {
$(this).addClass('mTableCellHover');
}, function() {
$(this).removeClass('mTableCellHover');
});
$('a[id*=MerchDesc]').addClass("mDescriptionLink");
$('span[id*=_LblSKU]').addClass("mSKU");
$('span[id*=_salePriceBlock]').addClass("mPriceFields");

// Append navigation, if present //

$('<tr id="mNavHolder"><td colspan=' + columns +'><table class="mNav"><tbody><tr><td id="mNavFirst" class="mNavBtns"></td><td id="mNavPrev" class="mNavBtns"></td><td id="mNavNext" class="mNavBtns"></td><td id="mNavLast" class="mNavBtns"></td></tr></tbody></table></td></tr>').insertBefore("#myRow0");

$('input[alt="<<"]:last').appendTo("#mNavFirst");
$('input[alt="<"]:last').appendTo("#mNavPrev");
$('input[alt=">"]:last').appendTo("#mNavNext");
$('input[alt=">>"]:last').appendTo("#mNavLast");

$('#mNavHolder').clone().insertAfter("#myMTable tr:last");

// Center the navigation //

$(".mNav").css("text-align","center").css("margin-left","auto").css("margin-right","auto");

$("td.mNavBtns").css("padding","2px");

}

}

});

