$(document).ready(function() {

    var months = {};
    months["JAN"] = "01";
    months["FEB"] = "02";
    months["MAR"] = "03";
    months["APR"] = "04";
    months["MAY"] = "05";
    months["JUN"] = "06";
    months["JUL"] = "07";
    months["AUG"] = "08";
    months["SEP"] = "09";
    months["OCT"] = "10";
    months["NOV"] = "11";
    months["DEC"] = "12";

    $.tablesorter.addParser({
        id: 'AlphaNum',
        is: function(s) {
            return false;
        },
        format: function(s) {
            s = '' + s;
            s = s.toUpperCase();
			alert(s);
            if (isNaN(s)) {
                return 'ZZ' + s;
            }
            else {
                return 'AA' + PadString(s, '00000000');
            }
        },
        type: 'text'
    });

    $.tablesorter.addParser({
        id: 'DD MMM YY',
        is: function(s) {
            return false;
        },
        format: function(s) {
            s = '' + s; //Make sure it's a string 
            var hit = s.match(/(\d{2}) ([A-Za-z]{3}) (\d{2})/);
            if (hit && hit.length == 4) {
                return hit[3] + months[hit[2].toUpperCase()] + hit[1];
            }
            else {
                return s;
            }
        },
        type: 'text'
    });
	
    $.tablesorter.addParser({
        id: 'Price',
        is: function(s) {
            return false;
        },
        format: function(s) {
			
			//alert(s);
			// Strip out span tags
            s = '' + s; //Make sure it's a string 
			var IsSoldOut=s.indexOf("Sold out");
			//alert(IsSoldOut);
			if (IsSoldOut>= 0) 
            {
				s= "01" + '' + s;
			}
			// Strip out span tags
			result = s.replace(/<span>.*<\/span>/g, "");
			// Strip out paragraph tags
			result = result.replace(/<p>.*<\/p>/g, "");
			// Strip out � sign
			result = result.replace(/\u00A3/g, "");
			// Strip whitespace from either end
			result = result.replace(new RegExp("^[\\s]+", "g"), "").replace(new RegExp("[\\s]+$", "g"), "");
			
           // alert(result);
            
            return parseFloat(result);
            
		},
        type: 'numeric'
    });	
	
    $.tablesorter.addParser({
        id: 'OfferDate',
        is: function(s) {
            return false;
        },
        format: function(s) {
            s = '' + s; //Make sure it's a string
            var offerMonth = s.match(/(January|February|March|April|May|June|July|August|September|October|November|December)/);
            var day = s.match(/\d{2}/);
            if (offerMonth && day) {

                switch (offerMonth[1]) {
                    case "January":
                        offerMonth = "01";
                        break;
                    case "February":
                        offerMonth = "02";
                        break;
                    case "March":
                        offerMonth = "03";
                        break;
                    case "April":
                        offerMonth = "04";
                        break;
                    case "May":
                        offerMonth = "05";
                        break;
                    case "June":
                        offerMonth = "06";
                        break;
                    case "July":
                        offerMonth = "07";
                        break;
                    case "August":
                        offerMonth = "08";
                        break;
                    case "September":
                        offerMonth = "09";
                        break;
                    case "October":
                        offerMonth = "10";
                        break;
                    case "November":
                        offerMonth = "11";
                        break;
                    case "December":
                        offerMonth = "12";
                        break;
                }

                return offerMonth + day;
            }
            else {
                return s;
            }
        },
        type: 'text'

    });

    $.tablesorter.addParser({
        id: 'description',
        is: function(s) {
            return false;
        },
        format: function(s) {
			
			//alert(s);
			// Strip out span tags
            s = '' + s; //Make sure it's a string 

			// Strip out span tags

			result = s.replace(/<p.*<u>/g, "");

			var chkValue=result.indexOf("<u");
			
			if(chkValue>0)
			{
				result = result.replace(/<p.*\"></g, "");
				result = result.replace(/u class.*\">/g, "");
			}
			
			//result = result.replace(/<\/u>.*<\/p>/g, "");
			result = result.replace(/<\/u>/g, "");

			result = result.replace(/<p.*<\/p>/g, "");
			// Strip out paragraph tags
			result = result.replace(/<p.*\">/g, "");
			result = result.replace(/<\/p>/g, "");
			// Strip out � sign
			result = result.replace(/\u00A3/g, "");
			// Strip whitespace from either end
			result = result.replace(new RegExp("^[\\s]+", "g"), "").replace(new RegExp("[\\s]+$", "g"), "");
			
            //alert(result);
            
            return result;
            
		},
        type: 'text'
    });		
	
    $(".gvBreaks").tablesorter(
        {
            headers:
            {
                0: { sorter: "DD MMM YY" },
                1: { sorter: "text" },
                2: { sorter: "integer" },
                3: { sorter: "description" },
                4: { sorter: "Price" },
                5: { sorter: "Price" },
                6: { sorter: false }
            },

            widgets: ['zebra'],
            widgetZebra: { css: ["", "alt"] },

            sortList: [[0, 0], [2, 0]]
        }
    );

    $(".gvOffers").tablesorter(
    {
        headers:
        {
            0: { sorter: "DD MMM YY" },
            1: { sorter: "text" },
            2: { sorter: "integer" },
            3: { sorter: "text" },
            4: { sorter: "Price" },
            5: { sorter: "percent" },
            6: { sorter: false }
        },

        widgets: ['zebra'],
        widgetZebra: { css: ["", ""] },

        sortList: [[0, 0], [2, 0]]
    }
);

  // Sets offer Gridview links to Thickbox
   $(".gvOffers a.special").each(function() {
		$(this).addClass("thickbox");
		var aTag = $(this).attr("href");
		$(this).attr("href",aTag.replace("break/","break/popup/"));
   });
   
   //Sets Price Grid break links
      $(".gvBreaks a.special").each(function() {
		$(this).addClass("thickbox");
		var aTag = $(this).attr("href");
		$(this).attr("href",aTag.replace("break/","break/popup/"));
   });

	//initialize thickbox
	tb_init('.gvBreaks a.thickbox, .gvOffers a.thickbox');

}
);
