$(document).ready(function () {
    $('.attributeSelector').change(function(){
    	attributeSelectorChange(this);
    });
	
    $('#filterResults').closest('form').submit(function(event){
    	$('[name="addToSearchTrail"]').val('1$'+$('#keyword').val());
    });
    
    $('.tabs li').click(function(){
    	$('.tabs li').removeClass('active');
    	$(this).addClass('active');
    	$('.tab').css('display', 'none');
    	var tab = parseInt($(this).attr('id').toString().replace('tabcontrol', ''));
    	$('div#tab'+tab).fadeIn();
    });
    
    $('#viewAllManufacturers').click(function(){
		if ($(this).text() != 'Collapse') {
			$('#viewAllManufacturers').text('Collapse');
			if($('#allManufacturers').html() == '') {
				$('#allManufacturers').hide();
		    	$.get(
					"ajaxRequests.php?"+parseUrl(), 
					{'type': 'manufacturers'},
					function(data){
						if(data == '') {
							$('#viewAllFeatures').text('Already shown all');
							$('#viewAllFeatures').fadeOut(2500);
							return;
						}
						$('#allManufacturers').html(data);
						$('#allManufacturers').slideDown();
					}
				);
			} else {
				$('#allManufacturers').slideDown();
			}
			
		} else {
			$('#allManufacturers').slideUp();
			$('#viewAllManufacturers').text('View all');
		}
    });
    
    $('#viewAllFeatures').click(function(){
    	if ($(this).text() != 'Collapse') {
    		$('#viewAllFeatures').text('Collapse');
    		if($('#allFeatures').html() == '') {
    			$('#allFeatures').hide();
    			$.get(
    					"ajaxRequests.php?"+parseUrl(), 
    					{'type': 'features'},
    					function(data){
    						if(data == '') {
    							$('#viewAllFeatures').text('Already shown all');
    							$('#viewAllFeatures').fadeOut(2500);
    							return;
    						}
    						$('#allFeatures').html(data);
    						$('#allFeatures').slideDown();
    						$('.attributeSelector').change(function(){
    							attributeSelectorChange(this);
    						});
    					}
    			);
    		} else {
	    		$('#allFeatures').slideDown();
    		}
    	} else {
    		$('#allFeatures').slideUp();
    		$('#viewAllFeatures').text('View all');
    	}
    });
    
    $('#viewAllCategories').click(function(){
    	if ($(this).text() != 'Collapse') {
    		$('#viewAllCategories').text('Collapse');
    		if($('#allCategories').html() == '') {
    			$('#allCategories').hide();
    			$.get(
					"ajaxRequests.php?"+parseUrl(), 
					{'type': 'categories'},
					function(data){
						if(data == '') {
							$('#viewAllCategories').text('Already shown all');
							$('#viewAllCategories').fadeOut(2500);
							return;
						}
						$('#allCategories').html(data);
						$('#allCategories').slideDown();
					}
    			);
    		} else {
    			$('#allCategories').slideDown();
    		}
    	} else {
    		$('#allCategories').slideUp();
    		$('#viewAllCategories').text('View all');
    	}
    });
    
});


function attributeSelectorChange(item)
{
	var location = window.location.toString();
	var parts = location.split('&');
	location = '';
	var newParts = new Array();
	var k = 0
	for (i in parts) {
		if(parts[i].toString().substring(0,16) != 'addToSearchTrail') {
			newParts[k] = parts[i]; 
			k++;
		}
	}
	
	location = newParts.join('&');
	
	var caption = '';
	var attributeCaption = $('#attributeCaption'+$(item).attr('name')).text();
	
	caption += attributeCaption + ': ' + $('[name="' + $(item).attr('name') + '"] option[value="' + $(item).val() + '"]').text();
	location += '&addToSearchTrail=' + '7$' + $(item).attr('name') + '$' + $(item).val() + '$' + escape(caption);
	
	var operator = $('[name="operator' + $(item).attr('name') + '"]').val();
	if (operator != undefined) {
		location += '&operator' + $(item).attr('name') + '=' + operator;
	}
	var trail = $('[name="trail"]').val();
	window.location = location+'&trail='+trail;
}


function in_array( what, where ){
	var a=false;
	for(var i=0;i<where.length;i++){
	  if(what == where[i]){
	    a=true;
        break;
	  }
	}
	return a;
}

function parseUrl()
{
	var neededParameters = new Array('trail', 'addToSearchTrail', 'removeFromTrail');
	var url = window.location.toString();
	var parts = url.split('?');
	var qString = parts[1].toString();
	//return qString;
	parts = qString.split('&');
	var ret = new Array(); 
	for (i in parts) {
		var part = parts[i].toString().split('=');
		if (in_array(part[0], neededParameters))
			ret[ret.length] = parts[i];
	}
	return ret.join('&');
}
