/**
 * Provide basic hover on for images
 * @author Nicholas Cooper
 */
jQuery.fn.extend({
	hoverOn: function(options) {
		if (typeof(options)!='object') {
			options = {menubar:false};
		}
		$(this).live('mouseover', function() {
			if ($('img',$(this)).length==1) {
				if (!$('img',$(this)).attr('src').match(/_oh.png/)) {
					$('img',$(this)).attr('src',$('img',$(this)).attr('src').replace(/.png/,'_oh.png'));
				}
			}
			if (options.menubar){
				options.menubar.each(function(){
					$(this).addClass('menubar_oh');
				});
			}
		});
		$(this).live('mouseout', function() {
			if ($('img',$(this)).length==1) {
				if ($('img',$(this)).attr('src').match(/_oh.png/)) {
					$('img',$(this)).attr('src',$('img',$(this)).attr('src').replace(/_oh.png/,'.png'));
				}
			}
			if (options.menubar){
				options.menubar.each(function(){
					$(this).removeClass('menubar_oh');
				});
			}
		});
		return this;
	}
});

$(document).ready(function() {
	$("#login form").validate({
		errorPlacement: function(error, element) {
			error.appendTo($('div.error',element.parent()));
		},
		rules: {
			username: {
				required: true
			},
			password: {
				required: true
			}
		},
		messages: {
			username: {
				required: "*required"
			},
			password: {
				required: "*required"
			}
		},
		submitHandler: function(form) {
			$('div.error','#login div.info').html('*invalid username or password');
		}
	});
	
	$("#contact form").validate({
		errorPlacement: function(error, element) {
			error.appendTo($('div.error',element.parent()));
		},
		rules: {
			name: {required: true},
			email: {required: true,email: true},
			telephone: {required: true},
			message: {required: true}
		},
		messages: {
			name: {required: "*required"},
			email: {required: "*required",email:"*valid email address"},
			telephone: {required: "*required"},
			message: {required: "*required"}
		},
		submitHandler: function(form) {
			var req = {};
			req.name = $('input[name=name]',"#contact form").val();
			req.email = $('input[name=email]',"#contact form").val();
			req.company = $('input[name=company]',"#contact form").val();
			req.telephone = $('input[name=telephone]',"#contact form").val();
			req.message = $('textarea[name=message]',"#contact form").val();
			$.ajax({
				type: "POST",
				url: "/includes/ajax/contact.php",
				cache: false,
				data: (req),
				beforeSend: function() {
					$('div.submit input',"#contact form").attr("disabled","disabled");
				},
				success: function(reply){
					var json;
					try {
						json = JSON.parse(reply);
					} catch(err) {
						$('div.info .error',"#contact form").show().html('Sorry message sending has failed.');
						$('div.submit input',"#contact form").removeAttr("disabled");
					}
					if (json['error']==1) {
						$('div.info .error',"#contact form").show().html('Sorry message sending has failed.');
					} else {
						var height = $('div.submit',"#contact form").height();
						$('div.info .error, div.entry, div.submit input',"#contact form").hide();
						$('div.submit',"#contact form").css('height',height);
						$('div.success',"#contact form").show();
					}
				},
				complete: function() {
					$('div.submit input',"#contact form").removeAttr("disabled");
				}
			});

		}
	});
	$('input, textarea','#contact form, #login form').focus(function(){
		$('input, textarea','#contact form, #login form').parents('div').removeClass('active');
		$(this).parents('div').addClass('active');
	});
	$('a[name=modal]').click(function(e) {
		$('input, textarea','#contact form, #login form').parents('div').removeClass('active');
	});
});

/**
 * Used with showcase, copied from old site.
 */
function changeImg(item,imgId){
	$("#small_images div.holder").removeClass('active');
	$(item).addClass('active');
	$("#large_image .bigimage").css('display', 'none');
	$('#large_image #bigimage' + imgId).show();
}


$(window).bind("load", function() {
    var preload = new Array();
    $(".hover_on").each(function() {
		if ($('img',$(this)).length==1) {
			var s = $('img',$(this)).attr("src").replace(/\.(.+)$/i, "_oh.$1");
			preload.push(s)
		}
    });
    var img = document.createElement('img');
    $(img).bind('load', function() {
        if(preload[0]) {
            this.src = preload.shift();
        }
    }).trigger('load');
});
