﻿$(document).ready(function() {                   	
	// handle click on an option in the list
	$('..dropdownHolder div ul li').mousedown(function() {       
        // parse the option value from the element id string
        var theID = $(this).attr('id'); 
        var newSortValue = theID.substring(theID.indexOf('input_') + 6);
        
        var theUrl = window.location.toString();
        
        // do nothing if the "sort by" option is unchanged
        var oldSortValue = gup('sort');
        if (newSortValue != oldSortValue) {
            if (newSortValue != null && newSortValue != '') {
            
                // if no existing params, just add the new sort value to the url
                if(theUrl.indexOf("?") == -1){  
                    theUrl = theUrl + '?sort=' + newSortValue;
                }
                else if(oldSortValue == "" || oldSortValue == null) {
                    theUrl = theUrl +  '&sort=' + newSortValue;
                    if(gup("page") != 1 & gup("page") != "" & gup("page") != null) {
                        theUrl = theUrl.replace(gup("page"), "1")
                    }
                }
                else {                    
                    theUrl = theUrl.replace("sort="+oldSortValue, "sort="+newSortValue);
                    if(gup("page") != 1 & gup("page") != "" & gup("page") != null) {
                        theUrl = theUrl.replace(gup("page"), "1")
                    }
                }
                window.location = theUrl;
 
            }           

        } 
	});
});
