var downloadLinkPrepared = false;

function prepareToggler() {
		$j('.product_header').click(
			function(){
				var $this = $j(this);
				var divID = $this.attr("id").replace("product_toggler_", "product_toggle_");
				var $div = $j('#'+divID);
				var $img = $this.find('img.prod_toggleicon');
				if ($div.is(':visible')) {
					$div.slideUp("normal");
					$this.find('.attributes_mini').css('visibility','visible');
					$img.attr('src','mod_products/images/expand.gif');
				} else {
					$div.slideDown("normal");
					$this.find('.attributes_mini').css('visibility','hidden');
					$img.attr('src','mod_products/images/collapse.gif');
				}
				return false;
			}
		);
		
		$j('.product_path').click(
			function(){
				var $this = $j(this);
				var divID = $this.attr("id").replace("categorie_toggler_", "product_path_product_");
				var $div = $j('.'+divID);
				var $img = $this.find('img.cat_toggleicon');
				if ($div.is(':visible')) {
					$div.slideUp("normal");
					$img.attr('src','mod_products/images/expand.gif');
				} else {
					$div.slideDown("normal");
					$img.attr('src','mod_products/images/collapse.gif');
				}
				return false;
			}
		);
		
		var hasNotChecked = true;
		$j("#catalogue_form input.attribute[name='attributes_applicable[]']").each(
			function() {
				if($j(this).attr('checked')) {
					hasNotChecked = false;
				}
			}
		);
		
		var hasNotPrinterSelected = true;
		$j("#printer_select").each(
			function() {
				if(!($j(this).val() == 0)) {
					hasNotPrinterSelected = false;
				}
			}
		);
		
		var hasNotApplicationSelected = true;
		$j("#application_select").each(
			function() {
				if(!($j(this).val() == 0)) {
					hasNotApplicationSelected = false;
				}
			}
		);
		
		var hasNotInputSearch = true;
		$j("#input_search").each(
			function() {
				if(!($j(this).attr('value') == '')) {
					hasNotInputSearch = false;
				}
			}
		);

		if (hasNotInputSearch && hasNotApplicationSelected && hasNotPrinterSelected && hasNotChecked) {
			$j('.product_content').hide();		
			$j('.product_path_product').hide();
			$j('.product_path:first').click();
			$j('.product_path_product:first .product_header:first').click();
		}
		else {
			$j('.attributes_mini').css('visibility','hidden');
		}
		
		$j('.product_closeall').click(
			function(){
				$j('.product_content').hide();
				$j('.product_path_product').hide();
				return false;
			}
		);
		
		$j('.product_openall').click(
			function(){
				$j('.product_content').slideDown("normal");
				$j('.product_path_product').slideDown("normal");
				return false;
			}
		);
}

function prepareAjaxLinks() {
	
	tb_init('a.thickbox, area.thickbox, input.thickbox');
	
	$j('div.pagination a').click(
		function() {
			if(this.href.indexOf('ajax=1') == -1) {
				var href = this.href+"&ajax=1";
			}
			else {
				var href = this.href;
			}
			
			$j('#ac').load(href);
			
			return false;
		}
	);
	
	$j('#download_catalogue_from_search').unbind('click');
	
	$j('#download_catalogue_from_search').click(
		function() {
			
			$j('#export_form input').attr('checked', false);
			
			$j("#catalogue_form input.attribute[name='attributes_applicable[]']").each(
				function() {
					if($j(this).attr('checked')) {
						var tmp = $j(this).attr('id').split('_');
						var id = tmp[2];
						if($j("#export_form input[id='match_"+id+"']")) {
							$j("#export_form input[id='match_"+id+"']").attr('checked', true);
						}
					}
				}
			);
			
			$j('#export_printer').attr('value', $j('#printer_select').val());
			$j('#export_application').attr('value', $j('#application_select').val());
			$j('#export_search').attr('value', $j('#input_search').val());
			
			$j('#export_form').submit();
			
			return false;
		}
	);
	
	/*if($j('#products_count').length > 0) {
		if(parseInt($j('#products_count').html()) > 0) {
			$j('#download_search_results').fadeIn();
		}
		else {
			$j('#download_search_results').fadeOut();
		}
	}*/

	if($j('dl.product').length > 0) {
		$j('#download_search_results').fadeIn();
	}
	else {
		$j('#download_search_results').fadeOut();
	}
}

function prepareFormElements() {
	
	var options = {
		url: $j('#catalogue_form').attr('action')+'&ajax=1',
		target: '#ac'
	};
	
	$j('#catalogue_form').bind(
		'Search',
		function() {
			
			var setDefaultSearchValue = false;
			
			if($j('#input_search').attr('value') == searchValue) {
				$j('#input_search').attr('value', '');
				setDefaultSearchValue = true;
			}
			else {
				if($j('#input_search').attr('value').length < 3) {
					return false;
				}
			}
			
			$j(this).ajaxSubmit(options);
			
			if(setDefaultSearchValue) {
				$j('#input_search').attr('value', searchValue);
			}
			else {
				$j('#catalogue_form a.reset').fadeIn();
			}
			
			return false;
		}
	);
	
	$j('#catalogue_form input.attribute').hide();
	
	/*$j('#printer_select').change(
		function() {
			$j('#catalogue_form').trigger('Search');
			return false;			
		}
	);
	
	$j("#catalogue_form input[name='submit']").click(
		function() {
			$j('#catalogue_form').trigger('Search');
			return false;
		}
	);
	
	$j("#catalogue_form input[name='reset']").click(
		function() {
			document.location.href = document.location.href;
			return false;
		}
	);*/
	
	$j('#input_search').attr('autocomplete', 'off');
	
	$j('#input_search').focus(
		function() {
			if(this.value == searchValue) {
				this.value = '';
			}
		}
	);
	
	$j('#input_search').blur(
		function() {
			if(this.value == '') {
				this.value = searchValue;
			}
		}
	);
	
	$j('#catalogue_form div.attribute').hover(
		function() {
			if($j(this).attr('class') != 'attribute preselected') {
				$j(this).attr('class', 'attribute selected');
			}
			overlib(
				$j('#note_'+$j(this).attr('id')).html(),
				FGCOLOR, '#eaeaea'
			);
		},
		function() {
			if($j(this).attr('class') != 'attribute preselected') {
				$j(this).attr('class', 'attribute');
			}
			nd();
		}
	);
	
	$j('#catalogue_form div.attribute').click(
		function() {
			var id = $j(this).attr('id');
			var input_id = 'a_'+id;
			var div = $j(this);
			
			$j('#catalogue_form input.attribute').each(
				function() {
					
					if($j(this).attr('id') == input_id) {
						if($j('#'+input_id).attr('checked') == 'checked') {
							$j('#'+input_id).removeAttr('checked');
							$j(div).attr('class', 'attribute');
						} else {
							$j('#'+input_id).attr('checked', 'checked');
							$j(div).attr('class', 'attribute preselected');
						}
					
					}
					else {
						//$j(this).attr('checked', false);
						//$j('#'+$j(this).attr('id').substr(2)).attr('class', 'attribute');
					}
				
				}
			);

			//$j('#catalogue_form').trigger('Search');
		}
	);
	
}

$j(document).ready(
	function() {
		prepareFormElements();

		$j('#ac').ajaxStart(
			function() {
				$j(this).hide();
				$j('#ac_loading').show();
			}
		);
		
		$j('#ac').ajaxStop(
			function() {
				prepareAjaxLinks();
				prepareToggler();
				//$j('#ac_loading').hide();
				//$j(this).show();
			}
		);
		
		prepareAjaxLinks();
		prepareToggler();
		$j('#content_main_fullwidth_catalogue').show();
		$j('#ac_loading').hide();
	}
);
