
$.preloadimages = function()
{   for(var i = 0; i<arguments.length; i++)
    {   img = new Image();
        img.src = arguments[i];
    }
}

jQuery.query = function(_name)
{
    var result = new Array();
    qs = location.search.substring(1, location.search.length);
    
    if (qs.length == 0) { 
        return undefined;
    }
    
    qs = qs.replace(/\+/g, ' ');
    var args = qs.split('&');
    
	for (var i = 0; i < args.length; i++) {
		
		var value = '';
	    var pair = args[i].split('=')
		var name = unescape(pair[0])
		
		if (pair.length == 2) {   
            value = unescape(pair[1])
		} else {
            value = name
		}
		
       	if (name == _name || i == _name-1) {       
		    return value;
		}
    }

    return undefined;
};


$(document).ready(function(){
	
	/** 
	 * MENU FILTER
	 */
	var _cat = $.query('category');
	if (_cat == undefined) {
	   _cat = 0;
	}
	/*_cat = 0;
	 */
	 
    var querystring = '';
    
	if ($('#shopBy').length) {
	   
	   var _tmp = $('#shopBy').height();
	   
	   $('#shopBy #criteria').hide();
	   $('#shopBy #themes').hide();
	   $('#shopBy #colorFamilies').hide();
	   $('#shopBy #tileSize').hide();
	   
	   $('#topChoice').hide();
	   $('#shopBy #criteria').show();
	   
	   $('#shopBy').css('height', _tmp+'px');
	   $('#shopBy').css('height', '80px');
	   $('#topChoice').change(function() {
	       if ($(this).val() != '') {
	           $('#shopBy #criteria').show();
	       } else {
	           $('#shopBy #criteria').hide();
		       $('#shopBy #themes').hide();
		       $('#shopBy #colorFamilies').hide();
		       $('#shopBy #tileSize').hide();
	       }
	   });
	   
        $('#shopBy #criteria').change(function() {
	       $('#shopBy #themes').hide();
           $('#shopBy #colorFamilies').hide();
           $('#shopBy #tileSize').hide();
           if ($(this).val() != '') {
               $('#shopBy #'+$(this).val()).show();
           }
        });
	    
	    function onChangeFilter() {
	       if ($(this).val() != '') {
               querystring = 'results.php?secondary='+$(this).val();
               if (_cat != 0) {
                   /** querystring += '&category='+_cat; ** removes the ability to search within a category **/ 
               }
               window.location = querystring;
           }
	    }
	    
	    $('#shopBy #themes').change(onChangeFilter);
	    $('#shopBy #colorFamilies').change(onChangeFilter);
        $('#shopBy #tileSize').change(onChangeFilter);
        
        if ($('#shopBy option[filter=1]').length) {
            var _name = $('#shopBy option[filter=1]').parent().attr('id');
            $('#shopBy #criteria').val(_name).change();
        }
	}
	
	/** END MENU FILTER **/
	
	
	/** START REFERRAL SCRIPT **/
	
	if ($('#cstHowFound').length) {
	   
	   $('#cstHowFound').change(function() {
	       $('select.cstHowFoundChild').hide();
	       if ($('#cstHowFound_'+$(this).val()).length) {
	           $('#cstHowFound_'+$(this).val()).show();
	       }
	   });
	   
	   $('#cstHowFound').change();
	}
	
	/**
	 *	if the international (at this time, ID 12) is set, the system redirects to a special page.
	 */
	if ($('div#placeOrderButton input#orderFormNext').length) {
		$('select#cstShpCountry').change(function() {
			if ($('select#cstShpCountry option:selected').val() == '13') {
				window.location = 'intl_shipping.php';
				return false;
			}
		});
		
		$('div#placeOrderButton input#orderFormNext').click(function() {
			if ($('select#cstShpCountry option:selected').val() == '13') {
				window.location = 'intl_shipping.php';
				return false;
			}
		});
	}
	
	
	/** END REFERRAL SCRIPT **/
	
});