function cleardefault(element, defaultv) {
    if (element.value == defaultv) {
        element.value = "";
    }
}
function sendtoafriend(page) {
    window.open('/sendtoafriend/'+escape(page)+'/', '', 'scrollbars=yes,menubar=no,height=335,width=490,resizable=yes,toolbar=no,location=no,status=no');
    return false;
}

jQuery.fn.extend({
  drag: function(params){
    var jQ = jQuery;
    return this.each(function(){
			var clicked = false;
			var start_x;
			var start_y;
			var drag_div = this;

			//hide the image & add as bg image
			bgimg = $('#'+this.id + ' img');
			imgx = bgimg.width();
			imgy = bgimg.height();
			$(this).css('background', "url('"+bgimg.attr('src')+"') no-repeat center center");
			bgimg.css('display', 'none');
			//mouse down
			$(this).mousedown(function(e){
				$(this).css('cursor', 'move');
				clicked = true;
				start_x = Math.round(e.pageX - $(this).eq(0).offset().left);
				start_y = Math.round(e.pageY - $(this).eq(0).offset().top);
			});
			//mouse up
			$(this).mouseup(function(e){
				$(this).css('cursor', 'default');
				clicked = false;
			});
			//mouse move
			$(this).mousemove(function(e){
				if(clicked){ //as we only want this to work while they have clicked
					bg = $(this).css('background-position');
					if(bg.indexOf('%')>1){
						leftpos = ($(this).width()/2) - (imgx/2);
						toppos = ($(this).height()/2) - (imgy/2);
					}else{
						bg = bg.replace("px", "").replace("px", "").split(" ");
						leftpos = parseInt(bg[0]);
						toppos = parseInt(bg[1]);
					}
					var mouse_x = Math.round(e.pageX - $(this).eq(0).offset().left) - start_x;
					var mouse_y = Math.round(e.pageY - $(this).eq(0).offset().top) - start_y;
					var x = leftpos + (mouse_x);
					var y = toppos + (mouse_y);
					start_x = Math.round(e.pageX - $(this).eq(0).offset().left);
					start_y = Math.round(e.pageY - $(this).eq(0).offset().top);

					$(drag_div).css('background-position', x+ "px " + y + "px");
				}
			});
    });
  }
});

jQuery.fn.extend({
  zoom: function(params){
    var jQ = jQuery;
		var zoom_str = "";
		if(!params || !params['zoom_images']){return false;}
    return this.each(function(){
			var container = this;
			var zoom_images = params['zoom_images'];
			/*if the zoom ul doesnt exist, then add it*/
			if(!jQ('#'+this.id+' .zoom_control').length){
				zoom_str = '<ul class="zoom_control"><li class="zoom_plus"><a href="#" class="plus"><span>&nbsp;</span></a></li>';
				for (pos=zoom_images.length; pos>0; pos=pos-1){
					zoom_str += "<li class='zoom_"+pos;
					if(pos == 1){zoom_str += " current_zoom";}
					zoom_str  += "'><a href='#' class='zoom_level'  rel='zoom_"+pos+"'><span>&nbsp;</span></a></li>";
				}
				zoom_str +='<li class="zoom_minus"><a href="#" class="minus"><span>&nbsp;</span></a></li></ul>';
				jQ(this).html(zoom_str);
			}
      jQ(this).find(' li a').click(function(){
				current = parseInt($('#'+container.id + ' li.current_zoom a').attr('rel').replace('zoom_', ""));
				$('#'+container.id +' li').removeClass('current_zoom');
				if($(this).attr('class') == "minus" && current>1){
					current--;
				}else if($(this).attr('class') == "plus" && current < zoom_images.length){
					current++;
				}else if($(this).attr('class') == "zoom_level"){
					current = parseInt($(this).parent().attr('class').replace('zoom_', ""));
				}
				$('#'+container.id +' li.zoom_'+current).addClass('current_zoom');
				if(params && params['target_div']){
					$('#'+params['target_div']).css('background-image', "url('"+zoom_images[current-1]+"')");
				}else{
					$('#'+container.id).parent().css('background-image', "url('"+zoom_images[current-1]+"')");
				}
				return false;
      });
    });
  }
});

$(document).ready(function() {
    $("form").validate({
        invalidHandler: function(form, validator) {
          var errors = validator.numberOfInvalids();
          if (errors) {
            var message = errors == 1
              ? 'You missed 1 field. It has been highlighted'
              : 'You missed ' + errors + ' fields. They have been highlighted';
            alert(message);
          }
        }
     })
})

var RecaptchaOptions = {
   theme : 'custom',
   lang: 'en',
   custom_theme_widget: 'recaptcha_widget'
};