$().ready(function() {

	// hide the elements we don't need if we have javascript enabled.
	$(".no-script-only").hide();

	// make a '.clickable' element activate the first link it contains (when clicking anywhere on it)
	$(".clickable").biggerlink(); // requires: jquery.biggerlink.pack.js

	// search
	$(".product-code-input").focus(function() {
		if (this.value == "Product code/name")
			this.value = "";
	});
	$(".product-code-input").blur(function() {
		if (this.value == "")
			this.value = "Product code/name";
	});

	// my list stuff
	if(!($.browser.msie && $.browser.version == 8)) // wierd bug which only affects IE8 on production server only.
		$("#my-list").fadeTo(0, 0.7);
	DynamicWebSite.Framework.MyListAjax.GetMyListInfo(UpdateMyListInfo_CallBack)
	$("a.add-to-list").click(function() {
		var id = getIdFromClass($(this).parents(".product-info").attr("class"));
		$(".indicator").text("working...");
		DynamicWebSite.Framework.MyListAjax.ToggleOnMyList(id, ToggleOnMyList_CallBack);
		return false;
	});
	$("a.remove-from-list").click(function() {
		var id = getIdFromClass($(this).parents(".product-info").attr("class"));
		$(".indicator").text("removing...");
		DynamicWebSite.Framework.MyListAjax.RemoveFromMyList(id, RemoveFromMyList_CallBack)
		return false;
	});

	// Initialise Google Tracking for File Downloads
	googleAnalyticsFileTracker();

});

function getIdFromClass(fullclass) {
    var classes = fullclass.split(" ");
    var idclass="";
    for(var i=0; i<classes.length;i++)
    {
        if(classes[i].indexOf("id-") != -1)
        {
            idclass=classes[i];
            break;
        }
    }
    var id = idclass.replace("id-","");
    return id;
}


function UpdateMyListInfo_CallBack(response) {
	if (response.error) {
		alert(response.error.Message);
	}
	else {
		var count = response.value.IDs.length;
		$(".indicator").text( count + " items added");

		for (var i = 0; i < response.value.IDs.length; i++) {
			$(".id-" + response.value.IDs[i] + " a.add-to-list").text("added to list").addClass("selected");
		}
	}
}

function removeFromMyList(id)
{

}

function RemoveFromMyList_CallBack(response)
{
   if(response.error)
   {
        alert(response.error.Message);
   }
   else
   {
        $(".id-"+response.request.args.EntityID).fadeOut();
        $(".indicator").text("removed");
        
        DynamicWebSite.Framework.MyListAjax.GetMyListInfo(UpdateMyListInfo_CallBack);
        
   }   
}


function ToggleOnMyList_CallBack(response)
{
   if(response.error)
   {
        alert(response.error.Message);
   }
   else
   {
        if(response.value == true)
        {
            $(".id-"+response.request.args.EntityID+" a.add-to-list").text("added to list").addClass("selected");
            $(".indicator").text("added");
        }
        else
        {
            $(".id-"+response.request.args.EntityID+" a.add-to-list").text("add to list").removeClass("selected");
            $(".indicator").text("removed");
        }
        DynamicWebSite.Framework.MyListAjax.GetMyListInfo(UpdateMyListInfo_CallBack);
   }
}

function googleAnalyticsFileTracker() {
	$("a[href$=.pdf],a[href$=.doc],a[href$=.docx],a[href$=.xls],a[href$=.xlsx],a[href$=.ppt],a[href$=.pptx]").click(function() {
		pageTracker._trackPageview($(this).attr("href"));
	});
}