/* Code by Giancarlo "GM" Moschitta (info@myphp.it) and Negatyve (http://www.negatyve.com) Powered by jQuery (http://jquery.com)) */

/**
 * Filtra le categoria sulla base della classe selezionata
**/
function filter_contents()
{
	var filter_type = jQuery( "#filter-type" ).val();
	if( filter_type == '' ) {
		return false;
	}

	if( jQuery( "#filter-type" ).length == 0 ) {
		filter_type = window.location.pathname.split( '/' )[1];
	}

	var page_location = window.location.protocol + '//' + window.location.host + '/' + filter_type + '/';
	var class_selected = jQuery( "#filter-class" ).val().match( /^([0-9]+)\|([a-z0-9_-]+)$/ );
	if( class_selected != null ) {
		var cat_selected = jQuery( "#filter-category" ).val().match( /^([0-9]+)\|([a-z0-9_-]+)$/ );
		page_location += class_selected[2] + '/';
		if( filter_type == 'prodotti' ) {
			if( cat_selected == null ) {
				page_location += class_selected[1] + '/';
			}
		} else {
			page_location += class_selected[1] + '/';
		}

		if( cat_selected != null ) {
			page_location += cat_selected[2] + '/' + cat_selected[1] + '/';
		}
	}

	var qs = 'ob=' + ORDER_BY + '|' + 'd=' + DIRECTION;
	var man_selected = jQuery( "#filter-manufacturer" ).val().match( /^mi\|([0-9]+)$/ );
	if( man_selected != null ) {
		qs += '|mi=' + man_selected[1];
	}

	page_location += '?qs=' + Base64.encode( qs );
	window.location.href = page_location;
}

/**
 * Cambia la modalità di visualizzazione della lista
 *
 * @param link, object, required, Il link scelto
**/
function setListViewStyle()
{
	var href = jQuery( this ).attr( 'href' );
	var style = href.substr( 1, 4 );

	jQuery( '#results-list' ).removeClass( 'grid' ).removeClass( 'list' ).addClass( style );
	jQuery( '#view-choices li' ).removeClass( 'selected' );
	jQuery( this ).parent().addClass( 'selected' );

	jQuery.cookie( 'products_list_view', style, { path: '/' } );
	return false;
}

/**
 * Avvia tutti gli script necessari alle liste
**/
function initLists()
{
	jQuery( '#filter-class' ).change( load_class_categories );
	jQuery( 'li.button-item button[class=form-styled-button]' ).click( filter_contents );
	jQuery( '#view-choices a' ).click( setListViewStyle );
}

/**
 * Associa la funzione initLists all'evento onload della pagina
**/
jQuery( document ).ready( initLists );
