$().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
    $("#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").show().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").show().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").html("<span class=\"item-no\">"+count+"</span> items added").show();
        
        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").html("removed").show();
        
        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").html("added").show();
        }
        else
        {
            $(".id-"+response.request.args.EntityID+" a.add-to-list").text("add to list").removeClass("selected");
            $(".indicator").html("removed").show();
        }
        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"));
	});
}