$().ready(function(){
	$('#gallery li:nth-child(4n)').addClass('right');
	$("a[rel='colorbox']").colorbox();
	var options = { 
		target: '#form-message',   	// target element(s) to be updated with server response 
		beforeSubmit: validate,  	// pre-submit callback 
		success: hideForm,  		// post-submit callback 
		//type: type        		// 'get' or 'post', override for form's 'method' attribute 
		//dataType: null       		// 'xml', 'script', or 'json' (expected server response type) 
		clearForm: true,       		// clear all form fields after successful submit 
		resetForm: true        		// reset the form after successful submit 
	};
	$('#ujemi-form').submit(function() { 
		$(this).ajaxSubmit(options); 
		return false; 
	}); 	
});

/* force IE6 to properly cache background images */
if (($.browser.msie) && ($.browser.version == "6.0")) {
	document.execCommand("BackgroundImageCache",false,true);
}

function validate(formData, jqForm, options) {
	var errors = new Array();
	var nameValue = $('#name').val(); 
	var addressValue = $('#address').val(); 
	var zipValue = $('#zip').val(); 
	var emailValue = $('#email').val(); 
	var fileValue = $('#name').val(); 
	if (!nameValue[0])errors.push('Vpišite vaše ime.'); 
	if (!addressValue[0])errors.push('Vpišite vaš naslov.'); 
	if (!zipValue[0])errors.push('Vpišite vašo poštno številko.'); 
	if (!emailValue[0])errors.push('Vpišite vaš e-mail naslov.'); 
	if (!fileValue[0])errors.push('Izberite sliko.'); 
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
	if(!emailPattern.test(emailValue))errors.push('E-mail naslov ni pravilno vpisan.');
	if(errors.length > 0){
		var str = '<ul>';
		for (err in errors){
			str = str + '<li>' + errors[err] + '</li>';
		}
		str = str + '</ul>';
		$('#form-message').html(str);
		return false;
	} else {
		$('.submit').html('Prosimo počakajte ...');
		return true;
	}
}

function hideForm(){
	$('#ujemi-form').fadeOut();
}
