		jQuery.query = function(s) {
		     var r = {};
		     var q = s.substring(s.indexOf('?') + 1); // remove everything up to the ?
		     q = q.replace(/\&$/, ''); // remove the trailing &
		     jQuery.each(q.split('&'), function() {
		         var splitted = this.split('=');
		         var key = splitted[0];
		         var val = splitted[1];
		         // convert floats
		         if (/^[0-9.]+$/.test(val)) val = parseFloat(val);
		         // ignore empty values
		         try {
		        	 if(val){	 
		        		 if (typeof val == 'number' || val.length > 0) r[key] = val;
		        	 } 
		         } catch (e) {}
		     });
		     return r;
		};


		$(document).ready( function() {
			$("input[type='radio'],input[type='checkbox']").prettyCheckboxes({
									checkboxWidth: 10, // The width of your custom checkbox
									checkboxHeight: 10 // The height of your custom checkbox
								});
			$("#filter input[type='checkbox'], #filter input[type='radio']").click(function ()	{
				var query_string = '';	
				$('#filter input:checked').each(function(){
					//var hidden = $('#'+$(this).parent().parent().attr('id')+'_ope');
					query_string += '&'+escape(this.name)+'=' + escape(this.value);
				});
				query = $('#search').val();
				if (query && query != 'Product name or no.') query_string += '&query=' + query;
				$("#loading").show();
				$("#products").load( "_search.php?"+query_string.substring(1), function(data, status, req) {
					if (parseInt(req.status) != 200) {
						document.location.href="login.php";
						return;
					}
					$("#loading").hide();
					});
			});
			
			var searchvalue = $('#search').val();
			$('#search').focus(function(){
	            if ($('#search').val() == searchvalue) {
	                $('#search').val('');
	            }
	        }).blur(function(){
	            if ($.trim($('#search').val()) == '') {
	                $('#search').val(searchvalue);
	            }
	        });
	        
	        $("#fancy_close").click( function() { $("#fancy_outer").hide(); return false; } );
	        
	        $("#compare").click( function() {
	        	$("#fancy_content").load('_managedownload.php', function(data, status, req) {
					if (parseInt(req.status) != 200) {
						document.location.href="login.php";
						return;
					}
		        		
	        			$("#fancy_outer").show();
	        			centerViewer(); 
	        			
	        			$("#button-save-selection").click(function() {
	        				var title = prompt('Selection title?');
	        				if (title)
	        					$.get("_saveselection.php?title="+title);
	        			});
	        			$("#button-load-selection").click(function() {
	        				var offset = $(this).offset();
	        				this.blur();
	        				var fc_offset = $("#fancy_content").offset();
	        				$("#my-selections").load("_getselections.php").toggle().css({ top: offset.top - fc_offset.top + $(this).height(),
	        																												left: offset.left - fc_offset.left,
	        																												width: $(this).width() });
	        			});
        			});
        			return false;
	        } );
	        

	        $(".fieldsetWrapper fieldset legend span").click( function() {
	        	ul = $(this).parent().parent().find('ul');
	        	if (!ul.is(":visible")) {
	        		ul.show();
	        		$(this).parent().parent().removeClass('closed');
	        	} else {
	        		ul.hide();
	        		$(this).parent().parent().addClass('closed');
	        		}
	        });
	        
        	$("a[rel='basket']").live('click', function() {
				var query = jQuery.query($(this).attr('href'));
				var id = query.id;
				
				$("#basket").load( $(this).attr('href').replace('goback',''), function(data, status, req) {
					if (parseInt(req.status) != 200) {
						document.location.href="login.php";
						return;
					}
					
					var ids = String(id).split(',');
					if (ids.length == 1) {
						updateProductlistAction(id);
					} else { // id=0
						jQuery.each(ids, function() {
							updateProductlistAction(this);
						});
					}
				} );
				return false;
			});
			
			$("#productsList li a[rel='viewlarge']").live('click', function() {
	        	var id = $(this).attr('id').replace('viewlarge-','');
	        	$("#loading").show();
	        	$("#fancy_content").load('_item.php?id='+id, function(data, status, req) {
	        			
						if (parseInt(req.status) != 200) {
							document.location.href="login.php";
							return;
						}
						
	        			$("#loading").hide();
	        			$("#fancy_outer").show();
	        			centerViewer();
	        		});
	        	return false;
	        });
			$(".pageNo").live('click', function() {
				$("#loading").show();
	        	$("#products").load( $(this).attr('href').replace('index.php','_search.php'), function() { $("#loading").hide(); } );
	        	return false;
	        });
			$("a[rel='sendemail']").live('click', function() {
				$("#loading").show();
	        	$("#fancy_content").load($(this).attr('href'), function(data, status, req) {
						if (parseInt(req.status) != 200) {
							document.location.href="login.php";
							return;
						}
						
				       $("#sendemail").ajaxForm( {target: '#sendemail-content'
       												//beforeSubmit: function() { $("#loading").show(); },
       												//successs: function() { $("#loading").hide(); }
       												} );
						
	        			$("#loading").hide();
	        			$("#fancy_outer").show();
	        			centerViewer();
	        		});
	        	return false;
	        });
	        $("#loading").hide();
	       
	       centerLoader();
	       centerViewer();
	       

		});
		
		function centerLoader() {
			var win = [$(window).width(), $(window).height(), $(document).scrollLeft(), $(document).scrollTop() ];
			$("#loading").css({"top": (win[1] - win[3])/2 - 16, "left": (win[0] - win[2])/2 - 16});
		}
		
		function centerViewer() {
			var pos = [ $(window).width(), $(window).height(), $(document).scrollLeft(), $(document).scrollTop() ];
			var e = $("#fancy_outer");
			var height = e.height(); if (height < 500) height = 540;
			var width = e.width();
			
			e.css('left', ((width	+ 36) > pos[0] ? pos[2] : pos[2] + Math.round((pos[0] - height	- 36)	/ 2)));
			e.css('top',  ((height+ 50) > pos[1] ? pos[3] : pos[3] + Math.round((pos[1] - height	- 50)	/ 2)));
		}
		
		function updateProductlistAction(id) {
			if ($('#compareItem-'+id).length > 0) {
					$("#actions-header-"+id).text('Remove');
					$("#actions-"+id).html('<a  rel="basket" href="_basket.php?id='+id+'" class="action">Remove</a>');
				} else {
					$("#actions-header-"+id).text('Add to selection');
					$("#actions-"+id).html( ' <a href="_basket.php?res=high&id='+id+'" class="action" rel="basket">High</a> | <a rel="basket" href="_basket.php?res=low&id='+id+'" class="action">Low</a>' );
					$("#managedownload-item-"+id).remove();
				}
			}
