function Deal(title, price, strPrice, value, strValue, discount, strDiscount, groupBuySite, referralUrl)
{
    this.title = title;
    this.price = price;
    this.strPrice = strPrice;
    this.value = value;
    this.strValue = strValue;
    this.discount = discount;
    this.strDiscount = strDiscount;
    this.groupBuySite = groupBuySite;
    this.referralUrl = referralUrl;
}

function sortNewDailyDeals(arr, sortAttribute, sortOrder)
{
    //$("#new-daily-deals-table").html(sortDeals(arr, sortAttribute, sortOrder));
    sortDeals("new-daily-deals-table", arr, sortAttribute, sortOrder);
}

function sortExtendedDeals(arr, sortAttribute, sortOrder)
{
    //$("#extended-daily-deals-table").html(sortDeals(arr, sortAttribute, sortOrder));
    sortDeals("extended-daily-deals-table", arr, sortAttribute, sortOrder);
}

function sortDeals(dailyDealSectionId, arr, sortAttribute, sortOrder)
{
    //alert("a" < "b");
    //var sortedDeals = mergeSort(arr, sortAttribute, sortOrder);

    //var deal = arr[0];
    //alert(eval("deal." + sortAttribute));
    //return eval("deal1." + sortAttribute) - eval("deal2." + sortAttribute)

    if (sortAttribute == "title")
    {
        arr.sort(function(deal1, deal2){return deal1.title > deal2.title});
    }
    else if (sortAttribute == "price")
    {
        arr.sort(function(deal1, deal2){return deal1.price - deal2.price});
    }
    else if (sortAttribute == "discount")
    {
        arr.sort(function(deal1, deal2){return deal1.discount - deal2.discount});
    }
    else if (sortAttribute == "groupBuySite")
    {
        arr.sort(function(deal1, deal2){return deal1.groupBuySite > deal2.groupBuySite});
    }
    
    var newDailyDealsTableHtml = "<table id= cellpadding=\"0px\" cellspacing=\"10px\">" + 
        "<tr><td style='width:70%;'>Deal description</td>" +
        "<td style='width:5%;'>Price</td>" +
        "<td style='width:11%;'>Value</td>" +
        "<td style='width:13%;'>Daily deal sites</td></tr>";

    for(var i=0; i<arr.length; i++)
    {
        // construct html
        newDailyDealsTableHtml += "<tr><td>" + 
            "<a href=\"" + arr[i]['referralUrl'] + "\" target='_blank' rel='nofollow'>" +
                arr[i]['title'] + "</a></td>" + 
            "<td valign='top'>" + arr[i]['strPrice'] + "</td>" +
            "<td valign='top'>" + arr[i]['strValue'] + 
            "<br />(<span class='fontDarkBlue largest'>" + arr[i]['strDiscount'] + "</span> <span class='smallest'>off</span>)</td>" + 
            "<td valign='top'><a href=\"" + arr[i]['referralUrl'] + "\" target='_blank' rel='nofollow'>" + arr[i]['groupBuySite'] + "</a></td>" +
            "</tr><tr>" + 
            "<td style='border-bottom: 1px dotted #64AAD0; font-size:0px;' colspan='4'>&nbsp;</td></tr>";
    }

    $("#" + dailyDealSectionId).html(newDailyDealsTableHtml + "</table>");
}

function mergeSort(arr, sortAttribute, sortOrder)
{
 //alert(sortOrder);
    if (arr.length < 2)
        return arr;
 
    var middle = parseInt(arr.length / 2);
    var left   = arr.slice(0, middle);
    var right  = arr.slice(middle, arr.length);
 
    return merge(mergeSort(left, sortAttribute, sortOrder), 
            mergeSort(right, sortAttribute, sortOrder), sortAttribute, sortOrder);
}
 
function merge(left, right, sortAttribute, sortAsc)
{
    var result = [];

    while( left.length > 0 || right.length > 0 )
    {
		if( left.length > 0 && right.length > 0 )
		{
		//alert(left[0][sortAttribute] + " " + right[0][sortAttribute]);
			if(sortAsc)
			{
				if(left[0][sortAttribute] < right[0][sortAttribute] )
				{
					result.push(left.shift());
				} else {
					result.push(right.shift());
				}
			}
			else 
			{
			
				if( left[0][sortAttribute] < right[0][sortAttribute] )
				{
					result.push(right.shift());
				}
				else 
				{
					result.push(left.shift());
				}
			}
		}
		else if( left.length > 0 ) {
			result.push(left.shift());
		} else if( right.length > 0 ) {
			result.push(right.shift());
		}
	}


/*
    while (left.length && right.length) 
    {
            if(sortAsc)
			{	
				if(left[0][sortAttribute] <= right[0][sortAttribute] )
				{
					result.push(left.shift());
				} else {
					result.push(right.shift());
				}
			}
			else 
			{
				if( left[0][sortAttribute] >= right[0][sortAttribute] )
				{
					result.push(left.shift());
				}
				else 
				{
					result.push(right.shift());
				}
			}    
			
			
        if (left[0] <= right[0]) {
            result.push(left.shift());
        } else {
            result.push(right.shift());
        }
			
    }
 
    while (left.length)
        result.push(left.shift());
 
    while (right.length)
        result.push(right.shift());
*/ 

    return result;
}
