String.prototype.format = function string_format( a ){
    var args, ret, search;
    if( arguments.length > 1 ){
        args = arguments;
    }
    
    ret = this
    for( var i=0; i < args.length; i++ ){
        ret = ret.replace( /%s/, args[i] );
    }
    return ret;
}

/** returns a string of ids separated by + */
function id_addition( items ){
    var o;
    o = "";
    for( var i=0; i < items.length; i++){
        o += items[i].id + "+";
    }
    return o.slice( 0, -1 );
}

/** Generates the URL to get the listing from */
function filter_url(){
    var styles, colours, widths, url;
    styles = id_addition( $( ".checked.style" ) );
    colours = id_addition( $( ".colour.checked" ) );
    widths = id_addition( $( ".width.checked" ) );
	powers = id_addition( $( ".power.checked" ) );
	if(typeof redirect == 'undefined') {
    	url = "/find/style/%s/colour/%s/width/%s/power/%s"
	}	else {
		url = "/find/?style=%s&colour=%s&width=%s&power=%s"
	}
    
    this.url = url.format( styles,colours,widths, powers);    
}

/** Generates Product listing for center col */
function acts_as_listing_generator(){
    target = new filter_url;
	if(typeof redirect == 'undefined') {
	    jQuery.get( target.url, function( data ){
	        $( "#filter_products" ).html( data );
	    });
	}	else {
		window.location = target.url;
	}
}

function toggle_click(element,disable) {

	var id = element.attr('id');
	 if(id == 11  
		 	|| id == 7 
			|| id == 4
			|| id == 10) {
		if(disable == true) {
			element.removeClass('click');
			element.addClass('disabled');
		}	else {
			element.removeClass('disabled');
			element.addClass('click');
		}
	}				

}

$(document).ready(function(){
    if( checked === true ){
        $( ".filter li" ).addClass( "checked" );
    }
    else{
        //for( var option in checked ){
		var disable = false;
		if(checked.power) {
			for( var i=0; i<checked.power.length; i++ ) {
            	 $("li.power[id=" + checked.power[i] + "]").addClass( "checked" );
				 if(checked.power[i] == 1) disable = true;
			}
		}
		//alert(disable);
		if(checked.style) {
			for( var i=0; i<checked.style.length; i++ ) {
            	 $("li.style[id=" + checked.style[i] + "]").addClass( "checked" );
			}
		}
		if(checked.width) {
			for( var i=0; i<checked.width.length; i++ ) {
            	 $("li.width[id=" + checked.width[i] + "]").addClass( "checked" );
			}
		}
		if(checked.colour) {
			for( var i=0; i<checked.colour.length; i++ ) {
            	 $("li.colour[id=" + checked.colour[i] + "]").addClass( "checked" );
			}
		}
		$('li.style').each(function() { toggle_click($(this), disable); });
        //}
    }
	
	$('#left_column li.power').click( function() {
		$(this).siblings('.power').removeClass('checked');
		if($(this).hasClass('checked')) {
			disable = false;
		}	else {
			disable = true;
		}
		if($(this).attr('id') == 1) {
			$('li.style').each(function() { toggle_click($(this),disable); });
		}	else {
			$('li.style').each(function() { toggle_click($(this),false); });
		}
	});
	

    $( "#left_column li.click" ).live('click', function(){
        $( this ).toggleClass( "checked" );
        acts_as_listing_generator();
    });
	
	$('#clear_styles').click(function() {
        $("li.style").each(function() {$(this).removeClass( "checked" )});
		$("li").each(function() {$(this).removeClass( "disabled" ).addClass( "click" )});
		acts_as_listing_generator();
	});
	
	$('#clear_size').click(function() {
        $("li.width").each(function() {$(this).removeClass( "checked" )});
		$("li").each(function() {$(this).removeClass( "disabled" ).addClass( "click" )});
		acts_as_listing_generator();
	});
	
	$('#clear_colours').click(function() {
        $("li.colour").each(function() {$(this).removeClass( "checked" )});
		$("li").each(function() {$(this).removeClass( "disabled" ).addClass( "click" )});
		acts_as_listing_generator();
	});
	
	$('#clear_power').click(function() {
        $("li.power").each(function() {$(this).removeClass( "checked" )});
		$("li").each(function() {$(this).removeClass( "disabled" ).addClass( "click" )});
		acts_as_listing_generator();
	});
	
	$('#clear_all').click(function() {
        $("li").each(function() {$(this).removeClass( "checked" )});
		$("li").each(function() {$(this).removeClass( "disabled" ).addClass( "click" )});
		acts_as_listing_generator();
	});
	

});
