function swatchlistener() {
    $('.swatch').live('mouseover', updateSwatchImage, false);
}

function dropdownListener() {
                                                
    // monitor all dropdowns for change
    // if change, then update the drop down and the remaining ones
    if ($('#product-filter') != undefined) {                            
                                
	    $('#sgfilter_price').bind('change', function(){
	        updateDropDowns('sgfilter_price');
	    });
	                                
	    $('#sgfilter_gender').bind('change', function(){
	        updateDropDowns('sgfilter_gender');
	    });
	    
	    $('#sgfilter_material').bind('change', function(){
	        updateDropDowns('sgfilter_material');
	    });
	
	    $('#orderby').bind('change', function(){
	        outputresults();
	    });
	                                
	    if ($('#sgfilter_brand') != undefined) {
	        $('#sgfilter_brand').bind('change', function(){
	            updateDropDowns('sgfilter_brand');
	        });
	    }
	                                
	    if ($('#sgfilter_style') != undefined) {
	        $('#sgfilter_style').bind('change', function(){
	            updateDropDowns('sgfilter_style');
	        });
	    }
    
    }
    
    swatchlistener();
}
                            
function updateDropDowns(updated_dropdown) {
                                      
    // update the changed dropdown and then change remaining drop downs
    var dropdowns = new Array('sgfilter_gender','sgfilter_price','sgfilter_material','sgfilter_style','sgfilter_brand')
        
    //$('sgfilter_material').hide();
    var valuePair = updated_dropdown + '=' + $('#'+updated_dropdown).val();
    setDropdown(valuePair,updated_dropdown);     
                              
    // iterate through array & update remaining dropdowns
    // exclude the option that has been set
    for ( var i=0, len=dropdowns.length; i<len; ++i ){
        if(dropdowns[i] != updated_dropdown) { // dont update changed option
            if ($(dropdowns[i]) != undefined) { // brand OR style will be ommitted
                valuePair = 'update_' + dropdowns[i] + '=' + 1; // instruct script to update dropdown
                setDropdown(valuePair,dropdowns[i]);
            };
        }
    }
                            
    // update the results DIV
    outputresults();
}
                                        
// do search
function outputresults(){
        
    $('#product-list').html('<div class="ajax-loader"><img src="/img/ajax-loader.gif" /></div>');

    setTimeout(function() {         

	    var orderbyval = $('#orderby').val();
	
	    if(orderbyval == undefined) {
	        orderbyval = '';
	    }
        
	    var url = '/inc/ecommerce/sunglasses-results.inc.php';

	    var myAjaxResponse = jQuery.get(url, {orderby:orderbyval},
	    						function(data){
	    							$('#product-list').html(data);
	    						}); 
    }, 1000);
}
                        
function setDropdown(valuePair,theoption) {

    var url = '/inc/ecommerce/sunglasses-menu.inc.php';
    var pars = valuePair;
    var target = '#holder_' + theoption.substring(9);
    var myAjaxResponse = jQuery.get(url, pars, 
    		function(data){
    			$(target).html(data);
    		});  
            
} 

function updateSwatchImage(event) {
	
    var thisimg = $(event.target);
  
    var swatchid = thisimg.attr('id');
    var swatchurl = thisimg.attr('href');
    var swatchbits = swatchid.split('-');
    var productid = swatchbits[1];
    var variantid = swatchbits[2];

    var productimage = $('#thumbnail-' + productid);
    if(productimage) {
	    var imagepath = '/img/catalogue/products/variants/medium/' + variantid + '-A.jpg';
	    productimage.attr('src', imagepath);  
	    
	    var producturl = $('#producturl-' + productid);
	    producturl.attr('href', swatchurl);
    }
}

dropdownListener();
