﻿$(function() {
		$("#slider_container").slider({
			orientation: "horizontal",
			range: true,
			step: 5,
			values: [0, 120],
			slide: function(event, ui) {
				$("#amount").val('£' + ui.values[0] + ' - £' + ui.values[1])
			},
			stop: function(event, ui) { 
			    var url = document.location.href;
                
                url = UpdateUrl("minprice", ui.values[0], url);               
                url = UpdateUrl("maxprice", ui.values[1], url);
                url = UpdateUrl("currentpage", 1, url);
                
                SendToServer(url);			    			                    
		    }
		});
		$("#amount").val('£' + $("#slider_container").slider("values", 0) + ' - £' + $("#slider_container").slider("values", 1));
	});



function UpdateUrl(name, value, url)
{
    
    if (url.indexOf("#") < 0 && url.indexOf("search=") < 0)
    {        
        url += "#cid=" + url.split(",")[1].split(".aspx")[0];
    }
    else
    {
        if (url.indexOf("#") < 0)
        { 
            url += "#";
        }
    }
   
    
    if (url.indexOf(name + "=") >= 0)
    {   
        var previous = url.split(name + "=")[1].split("&")[0];
        url = url.replace(name + "=" + previous, name + "=" + value);        
    }
    else
    {
        url = url + "&" + name + "=" + value;
    }    
    
   
    return url;
        
}

function CategoryProductsItemsPerPage(value)
{
    var url = document.location.href;
    url = UpdateUrl("itemsperpage", value, url);       
    url = UpdateUrl("currentpage", 1, url);         
    SendToServer(url);
}

function SortCategoryProducts(value)
{    
    var url = document.location.href;
    url = UpdateUrl("sort", value, url); 
    //return;               
    SendToServer(url);
}

function ChangeCurrentPage(value)
{
    var url = document.location.href;
    url = UpdateUrl("currentpage", value, url);
    SendToServer(url);                
}

function SendToServer(url)
{
    $("#CategoryResults").addClass("overlay");
        
    var type = "UpdateCategoryPage";
    if (url.indexOf("search=") > 0)
    {
        type = "UpdateProductSearchPage";    
    }
       
       
    $.post("/Catalog/CatalogAjax.aspx",
      {
           Type: type,                    
           Url: url
                   
      }, function(response){
                                                
           
            $("#CategoryResults").html();
           
           $('html, body').animate({scrollTop:0}, 'slow');
            var html = response.split("<replace>")[1].split("</replace>")[0]; 
           
            $("#CategoryResults").html(html);  
            $("#CategoryResults").removeClass("overlay");
            
            
        });
        
     window.location.href = url;   
    
}

function RefreshUrl()
{
 
    var url = document.location.href;
    if (url.indexOf("#") > 0)
    {
        window.location.href = url.split("#")[0];   
    }
}

function FilterSelect(id)
{
    var url = document.location.href;
    
    var attribute = $("#" + id).attr("filterattributename");
    var value = $("#" + id).attr("FilterValue1") + ";" + $("#" + id).attr("FilterValue2") + ";" + $("#" + id).attr("FilterValue3");
    
  
    url = UpdateFilterUrl(attribute, value, url, document.getElementById(id).checked);                
    
    SendToServer(url);
       
}

function UpdateFilterUrl(name, value, url, selected)
{
    url = UpdateUrl("currentpage", 1, url);
           
    if (url.indexOf("#") < 0 && url.indexOf("search=") < 0)
    {        
        url += "#cid=" + url.split(",")[1].split(".aspx")[0];
    }
       
    if (url.indexOf(name + "=") >= 0)
    {   
        var previous = url.split(name + "=")[1].split("&")[0];
        var currentValue = previous;
        
        value = value.replace(/ /g, "%20");
        
        if (selected)
        {
            currentValue = previous + value;
        }
        else
        {
            
            currentValue = previous.replace(value, "");   
        }
                    
        url = url.replace(name + "=" + previous, name + "=" + currentValue);        
    }
    else
    {
        url = url + "&" + name + "=" + value;
    }    
    
    return url;    
}

function createMessage(text, color, time_to_show)
    {
       
        
        with(document.getElementById('AjaxMessage'))
	    {	        
	      //  alert("messageDialog=" + text);
		    style.visibility = "visible";
		    //innerHTML        = text;
		    style.color      = color;
	    }
	    if ( time_to_show > 0 ) {
		    setTimeout("hideMessage()", time_to_show);
	    }
	    
	   
    }