$(function(){
	
	//thumbs cycle
	$('#thumbs-images').cycle({ 
		timeout: 6000,
		pause: true,
		after: function(prevEl, curEl, options, forward){
			var curIndex = $('#thumbs-images a').index(curEl);
			var count = $('#thumbs-images a').length;
			$('#thumbs-pager').text((forward ? ++curIndex : --curIndex) + '/' + count);
		}
	});
	
	//thumbs colorbox
	$('#thumbs-images a').colorbox({
		opacity: .7,
		onOpen: function(){
			$('#cboxCurrent, #cboxClose').remove();
		}
	});	
});

function formCode(form){
	if (!("autofocus" in document.createElement("input"))) {
		$('#form_event_name').focus();
	}
	
	$('#'+form+'_submit').click(function(){
		validate_form(form);
		return false;
	});
}


var validate = true;
function validate_form(form) {
	
	$("#" + form + " input").removeClass('error');
	
	/*if ($('#form_contact_email').val() != $('#form_contact_email2').val()) {
		 $('#form_contact_email2').addClass('control_error');
		return false;
	}*/
	
	
	$("#" + form + "_submit").hide();
	var validate = true;

	if (validate) {
		var params = $("#" + form).serialize();
		params += "&form_id=" + form;
		params += "&id=" + $('#'+form+'_id').val();	
		
		$.ajax( {
			type : "POST",
			url : "/ajax/validate-form/",
			data : params,
			success : function(msg) {
				$("#" + form + "_submit").show();
				
				validate = true;
			
				msg = eval(msg);
	
				$("input, textarea, select").each( function() {
					if (this.id.indexOf(form) != -1) {
			
						$(this).removeClass("error");
						$(this).removeClass("error_text");
						$(this).addClass($(this).attr("type"));
					}
				});
	
				if (msg) {
					var goNext = true;	
					
					
					if (msg[0]) {
					
						for ( var i = 0; i < msg[1].length; i++) {
							var obj = $("#" + msg[1][i]);
							
							if (!obj.attr("id")) {
								$('input').each( function() {
									if (this.id.indexOf(msg[1][i])!=-1) {										
										obj = $("#" + this.id);
									}
								});								
							}
								
							if (i == 0) {
								obj.focus();
								obj.select();
							}
							
							var error = "error";
							var type = obj.attr("type");
							
							if (type == "text" || type == "password") {
								error += "_" + type;
							}
	
							if (obj.attr("type") == "checkbox") {
								obj.parent().attr("class", error);			
							}
							obj.addClass("error");
						}
					} else {
						$("form").hide().parent().find("#form_success_message").show();
					}		
				}
			}
	});
	}
	
	return false;
}

