$(document).ready(function(){
	$.getScript("js/timer.js");
	$.getScript("js/jquery.validate.js",
		function(){
		$("#email-list").validate({
			submitHandler:function(form){
				Timer.resetTimeout();
				Timer.initTimeout('list-email-loader');
				var values = $("#email-list").serialize();
				$("#list-email").hide();
				$("#list-email-loader").show();
				$.get("newsletter.php?action=subscribe&"+values,
					function(d){
						Timer.resetTimeout();
						$("#list-email-loader").hide();
						$("#list-email").html(d).show();
					});
			},
			rules: {
				name: {
					required: true
				},
				email: {
					required: true,
					email: true
				}
			},
			messages: {
				name: " ",
				email: " "
			}
		});
	});
	
	$.getScript('js/jquery.sprinkle.js');
	
	$.getScript('js/jquery.fancybox-1.3.4.js',
		function(){
		
				$("a.open-mic").fancybox({
					'opacity'		: true,
					'overlayShow'	: true,
					'transitionIn'	: 'elastic',
					'transitionOut'	: 'elastic',
						'width' : 400,
						'height' : 400,
						'onClosed' : function(){
			location.reload(false);
			
			},
				});
				$("a.video").fancybox({
					'opacity'		: true,
					'overlayShow'	: true,
					'overlayOpacity' : 0.6,
					'overlayColor'   : '#333',
					'transitionIn'	: 'elastic',
					'transitionOut'	: 'elastic',
						'width' : 640,
						'height' : 360,
						'padding' : 0,
						'margin' : 0,
						'autoScale' : true,
						'cyclic'   : true
				});
		});
		
	$.getScript('js/jquery.quickflip.js',
		function(){
			$('#flip-container').quickFlip();
			$('#flip-container2').quickFlip();
			$('#flip-navigation li a').each(function(){
				$(this).click(function(){
					$('#flip-navigation li').each(function(){
						$(this).removeClass('selected');
					});
					$(this).parent().addClass('selected');
					var flipid=$(this).attr('id').substr(4);
					$('#flip-container').quickFlipper({ }, flipid, 1);
					
					return false;
				});
			});	
		});
});
function ticketsCheck(_form){
	var quantity = $(_form+" input[name='quantity']").val();
	var comic = $(_form+" input[name='comic']").val();
	var showtime = $(_form+" select[name='showtime'] option:selected").val();
	var id = _form.replace("#","");
	var msg = '';
	$.getJSON('tixcheck.php?action=count&comic='+comic+'&showtime='+showtime+'&id='+id,
		function(d){
			var msg = d.msg;
			var matched = msg.match(/Left/gi);
			if($("#error-"+id).html()){
				$("#error-"+id).remove();
			}
			if(!$("#tixleft-"+id).html()){
				if(!matched){
					$(_form+" select[name='showtime']").after('<p id="tixleft-'+id+'"><b>'+d.msg+'</b></p>');
				}
			} else {
				if(!matched){
					$("#tixleft-"+id).html('<b>'+d.msg+'</b>');
				}
			}
			if(d.status == 0){
				$(_form+" input[name='submit']").hide();
			} else {
				if(Number(d.msg) < Number(quantity)){
					if(!$("#error-"+id).html()){
						$("#tixleft-"+id).after('<span id="error-'+id+'" style="color:red;font-weight: bold;">To many tickets entered!</span>');
					}
					$("#"+id+" input[name='submit']").hide();
				} else {
					$(_form+" input[name='submit']").show();
				}
			}
		});
}
function validateForm(id,_form,script){
	$("#"+id).append('<div id="loader-'+id+'" class="loader">Sending...</div><div id="response-'+id+'" class="response"></div>');
	var resp = $("#response-"+id);
	var loader = $("#loader-"+id);
	
	ticketsCheck(_form);
	$(_form+" select[name='showtime']").change(function(){ticketsCheck(_form);});
	$(_form+" input[name='quantity']").blur(function(){ticketsCheck(_form);});
	
	$(_form).validate({
		submitHandler:function(form){
			var values = $(_form).serialize();
			loader.fadeIn();
			$(_form).animate({height:'hide'},function(){
				$.getJSON(script+'.php?'+values,
				function(d){
					loader.fadeOut(
						function(){
							resp.html(d.msg);
							loader.remove();
							if(d.status == 2){
								var rsto = setTimeout('$("#gateway_form").submit();',5000);
							}
							if(d.status == 1){
								var rsto = setTimeout('$("#response-'+id+'").remove();',5000);
								var rld = setTimeout('document.location.reload();',10000);
							}
							if(d.status == 0){
								var rsto = setTimeout('$("#response-'+id+'").remove();',15000);
								$(_form).show();
							}
						});
				});
			});
		},
		rules: {
			name: {
				required: true,
				minlength: 3
			},
			email: {
				required: true,
				email: true
			},
			phone: {
				required: true
			},
			quantity: {
				required: true,
				number: true,
				minlength: 1,
				maxlength: 2
			}
		},
		message : {
			name: ' ',
			phone: ' ',
			email: ' ',
			quantity: ' '	
		}
	});
}
