$(function(){

  	$.validator.addMethod("phone", function(value, element) {
	        return this.optional(element) || /^[0-9\-\+\s]+$/i.test(value);
	    }, "Phone must contain only numbers, + and -.");

  	$.validator.addMethod("humanname", function(value, element) {
        return this.optional(element) || /^[A-Za-z\-\'\s]+$/i.test(value);
    }, "Name entry must contain only letters, hyphens, or apostrophes.");
  	
  	$.validator.addMethod("username", function(value, element) {
        return this.optional(element) || /^(?=.{6,})[a-z0-9]+$/i.test(value);
    }, "Username should be 6 characters or more, consist only of letters and numbers.");
  	
  	$.validator.addMethod("password", function(value, element) {
        return this.optional(element) || /^.*(?=.{7,})(?=.*\d)(?=.*[a-z]).*$/i.test(value);
    }, "Password should be 7 characters or more, consist only of letters and numbers with at least one of each");
  	
  	addColorbox = function(link)
  	{
  		//console.log('adding popup : '+ link.attr('href'));
  		
  		link.colorbox({
			inline: true,
			href: '#imageeditor',		
			innerWidth:980, 
			innerHeight:800,
			onOpen: function() {
				$('#imageedit').html($('#editwait').clone());
				var url = $(this).attr('href');
				$.get(url, function(data) {
					$('#imageedit').html(data); 
				});		
			},
			onClosed: function() {
				$('#imageedit').empty();
				$('.imgareaselect-selection').parent().remove();
				$('[class^="imgareaselect"]').remove();
	
				if(changed)
				{
					$('#pagewrap').html($('#editwait').clone());
					location.reload();
				}
			}
		});
  	}
  	
  	function updateAjaxForm(e) {
  	  		
  		e.preventDefault();

	    var form = $(this).closest('.ajax_form');
	    var data = form.serialize();

	    $.post(form.attr('action'),data,function(d) {
	    	form.closest('.ajax_container').html(d);
	    	
	    	$(".editorpopup").each(function() {
				addColorbox($(this));
			});
	    	
	    	if($.browser.msie) {
	    		$('.ajax_input').bind('change', updateAjaxForm);
	    	}
	    });
  	}
  	
	$('.ajax_input').live('change', updateAjaxForm);
	
	$('.ajax_container .ajax_form').live('submit', updateAjaxForm);

	if($.browser.msie) {
		$('.ajax_input').bind('change', updateAjaxForm);
	}
	
	$('.ajax_links a, a.ajax_link').live('click', function(e) {
		e.preventDefault();
		
		// If we have a specific container, then load content into there
		var target = $(this).attr('rel');
		// It might be that we don't care about the response (background data saving, for instance)
		if(target == 'noload') {
			$.get($(this).attr('href'));
		} else {
			if((!target || !$(target).length)) {
				target = '.ajax_container';
			}
			
			$(this).closest(target).load($(this).attr('href'), function() {
				
				$(".editorpopup").each(function() {
					addColorbox($(this));
				});
				

				if($.browser.msie) {
					$('.ajax_input').bind('change', updateAjaxForm);
				}
			});
		}
	});
		
	$(".editorpopup").each(function() {
		addColorbox($(this));
	});
		
	$('#showfilters').live('click', function(e) {
		e.preventDefault();
		
		if( $('.filtergroup').is(":visible") ) {
			$.post('/products/finderstatus/', { status: 0 } );
		}
		else
		{
			$.post('/products/finderstatus/', { status: 1 } );
		}
		
		$('.filtergroup').slideToggle();
	});
	
	$('#search_box').focus( function(e) {
		if( $(this).val() == 'Quick search' ) {
			$(this).val( '' );
		}
	});
	
	$('#search_box').blur( function(e) {
		if( $(this).val() == '' ) {
			$(this).val('Quick search');
		}
	})
	
    $('.closelogin').live('click', function() { $.colorbox.close(); return false; });
	$('a.signin').colorbox(
				{ scrolling: false, initialHeight: 305, innerHeight: 305, initialWidth: 305, innerWidth: 305,
					onComplete: function() { /*$('#loginlocation').val(location.href);*/ }  
				});
	
	$('a.signin').show();
	
	$('a.download').live('click', function() {
		(window.open($(this).attr('href'),"","resizable=no,location=no,status=no,toolbar=no,menubar=no,scrollbars=no,width=300,height=300", false));
		return false;
	});
	
	$('a.basketadd').live('click', function() {
		$.get($(this).attr('href'), function(data) {
			if(data)
			{
				count = $(data).children('#totalcount').html();
				$('.baskettotal').html(count);
			}
			$('#basketadd').html(data);
			$('#basketadd').fadeIn(1000).delay(2000).fadeOut(2000);
		});
		return false;
	});
	
	var image = new Image();
	image.src = '/images/icons/download.gif';
	
	$('.partnerlogo').hover(function() {		
				var src = $(this).attr('src');
				var newsrc = $(this).attr('rel');
		
				$(this).attr( { src : newsrc, rel : src } );
	}, function () {
				var src = $(this).attr('src');
				var newsrc = $(this).attr('rel');
		
				$(this).attr( { src : newsrc, rel : src } );
	});
});
