var readNews = 0;

(function ($) {
  $.fn.fadeTransition = function(options) {
    var options = $.extend({pauseTime: 30000, transitionTime: 2000}, options);

    Trans = function(obj) {
      var timer = null;
      var current = 0;
      var els = $("> *", obj).css("display", "none").css("left", "0").css("top", "0").css("position", "absolute");
      $(obj).css("position", "relative");
      $(els[current]).css("display", "block");

      function transition() {
        var next = (current + 1) % els.length | 0;
        $(els[current]).fadeOut(options.transitionTime);
        $(els[next]).fadeIn(options.transitionTime);
        current = next;
        cue();
      };

      function cue() {
        if ($("> *", obj).length < 2) return false;
        if (timer) clearTimeout(timer);
        timer = setTimeout(transition, options.pauseTime);
      };

      cue();
    };

    return this.each(function() {
      var t = new Trans(this);
    });
  }
})(jQuery);

$(document).ready(function(){
	$(".toggle_venue").click(function() {
		$(".roundedPanel:not(.#venue_list)").hide();
		$("#venue_list").slideToggle("slow");
		$("#venue_list").animate({opacity: "1"}, 1200);
		$(this).toggleClass("active");
		$("input:not(:submit)").val("");
		return false;
	});
	
	$(".toggle_order").click(function() {
		$(".roundedPanel:not(.#order_event)").hide();
		$("#order_event").slideToggle("slow");
		$("#order_event").animate({opacity: "1"}, 1200);
		$(this).toggleClass("active");
		$("input:not(:submit)").val("");
		return false;
	});	
	
	$(".toggle_booking").click(function() {
		$(".roundedPanel:not(.#book_event)").hide();
		$("#book_event").slideToggle("slow");
		$("#book_event").animate({opacity: "1"}, 1200);
		$(this).toggleClass("active");
		$("input:not(:submit)").val("");
		return false;
	});	
	
	$(".toggle_credit").click(function() {
		$(".roundedPanel:not(.#credit_payment)").hide();
		$("#credit_payment").slideToggle("slow");
		$("#credit_payment").animate({opacity: "1"}, 1200);
		$(this).toggleClass("activate");
		$("input:not(:submit)").val("");
		return false;
	});
	
	$(".toggle_profile").click(function() {
		$(".roundedPanel:not(.#profile)").hide();
		$("#profile").slideToggle("slow");
		$("#profile").animate({opacity: "1"}, 1200);
		$(this).toggleClass("activate");
		return false;
	});

	$("#venue_list .accordion h3:first").addClass("active");
	$("#venue_list .accordion p").hide();
	$("#venue_list .accordion h3").click(function(){
		$(this).next("p").slideToggle("slow")
		.siblings("p:visible").slideUp("slow");
		$(this).toggleClass("active");
		$(this).siblings("h3").removeClass("active");
	});
	
	$("#profile .accordion div:first").addClass("active");
	$("#profile .accordion p").hide();
	$("#profile .accordion div").click(function(){
		$(this).next("p").slideToggle("slow")
		.siblings("p:visible").slideUp("slow");
		$(this).toggleClass("active");
		$(this).siblings("div").removeClass("active");
	});
	
	$(".toggle_featured").click(function() {
		$(".roundedPanel:not(.#featured)").hide();
		$("#featured").slideToggle("slow");
		$("#featured").animate({opacity: "1"}, 1200);
		$(this).toggleClass("activate");
		// Fetch Content
		if($(".ajax-loading:visible").length > 0) {
			$.ajax({
				type: "POST",
				url: "./case_runs/aj_featured.php",
				data: "",
				success: function(msg){
					$(".accordion:visible").html(msg);
				}
			});
		}
		return false;
	});
	
	$(".toggle_special").click(function() {
		$(".roundedPanel:not(.#special)").hide();
		$("#special").slideToggle("slow");
		$("#special").animate({opacity: "1"}, 1200);
		$(this).toggleClass("activate");
		// Fetch Content
		if($(".ajax-loading:visible").length > 0) {
			$.ajax({
				type: "POST",
				url: "./case_runs/aj_special.php",
				data: "",
				success: function(msg){
					$(".accordion:visible").html(msg);
				}
			});
		}
		return false;
	});
	
	$(".toggle_about").click(function() {
		$(".roundedPanel:not(.#about)").hide();
		$("#about").slideToggle("slow");
		$("#about").animate({opacity: "1"}, 1200);
		$(this).toggleClass("activate");
		// Fetch Content
		if($(".ajax-loading:visible").length > 0) {
			$.ajax({
				type: "POST",
				url: "./case_runs/aj_about.php",
				data: "",
				success: function(msg){
					$(".accordion:visible").html(msg);
				}
			});
		}
		return false;
	});
	
	$("#newsBox").fadeTransition();
	
	$(".create_booking").click(function() {
		var function_id = $(this).attr("name");
		$("#venue_list").hide();
		$("#order_event").hide();
		$("#book_event").slideToggle("slow");
		$.ajax({
			type: "POST",
			url: "./scripts/getbookinginfo.php",
			data: "function_id=" + function_id,
			success: function(msg){
				$(".booking_info .details").html(msg);
				$.ajax({
					type: "POST",
					url: "./scripts/placesselect.php",
					data: "function_id=" + function_id,
					success: function(msg){
						$(".page_loader").hide();
						$(".booking_info").show();
						$("[name=no_guests]").html(msg);
					}
				});
			}
		});
		return false;
	});
	
	$(".show_home").click(function() { $("#venue_list, #order_event, #book_event").hide(); return false; });
	$(".show_venue").click(function() { 
		$("#venue_list, #order_event, #book_event").hide(); 
		$("#venue_list").slideToggle("slow");
		$("#venue_list").animate({opacity: "0.9"}, 1200);
		$(this).toggleClass("active");
		return false;
	});
	
	// Booking Form Page 1
	$("#booking_info input[type=submit]").click(function() {
		var empty = 0;
		$(".red_star").hide();
		
		$("#booking_info input[type=text]:not([name=address2]").each(function() {
			if($(this).val().length == 0) {
				empty = 1;
				$(this).parent().parent().find(".red_star").show();
			}
		});
		
		if(empty == 0) {
			// Check for valid email address
			if($("[value*=@][name=email]").length + $("[value*=.][name=email]").length == 2) {
				$(".page_loader").show();
				$(".booking_info").hide();
				$.ajax({
					type: "POST",
					url: "./scripts/create_booking.php",
					data: "function_id=" + $("[name=function_id]").val() + "&first_name=" + $("[name=first_name]").val() + "&last_name=" + $("[name=last_name]").val() + "&phone=" + $("[name=phone]").val() + "&email=" + $("[name=email]").val() + "&address=" + $("[name=address1]").val() + "," + $("[name=address2]").val() + "," + $("[name=address3]").val() + "&postcode=" + $("[name=postcode]").val() + "&state=" + $("[name=state]").val() + "&booked_places=" + $("[name=no_guests]").val(),
					success: function(msg){
						$(".page_loader").hide();
						$("#book_event").hide();
						//$(".credit_info").html("Transaction Complete");
						$("#credit_payment").show();
						$("[name=booking_id]").val(msg);
					}
				});
				return false;
			}
			else {
				$("[name=email]").parent().parent().find(".red_star").show();
				alert("You have an invalid email address");
				return false;
			}
			
		}
		else {
			return false;
		}
	});
	
	/*$("[name=credit_payment]").click(function() {
		var count = 0;
		$("#credit_booking_info input[type=text][value=]").each(function() {
			$(this).parent().parent().find(".red_star").show();
			count = 1;
		});
		
		if(count == 0) {
			$(".page_loader").show();
			$(".booking_info").hide();
			$.ajax({
				type: "POST",
				url: "./scripts/transact.php",
				data: "booking_id" + $("[name=booking_id]").val(),
				success: function(){
					$(".page_loader").hide();
					$(".credit_info").html("Transaction Complete");
				}
			});
			
			return false;
		}
		else {
			alert("All fields are required!");
			return false;
		}
	});*/
	
	// Venue Sorting
	
	// By FUNCTION DATE
	$(".sort_by_function").click(function() {
		$.ajax({
			type: "POST",
			url: "./case_runs/venue_by_date.php",
			data: "",
			success: function(msg){
				$("#venue_list .accordion").html(msg);
				$("#venue_list .accordion h3").click(function(){
					$(this).next("p").slideToggle("slow")
					.siblings("p:visible").slideUp("slow");
					$(this).toggleClass("active");
					$(this).siblings("h3").removeClass("active");
				});
			}
		});
	});
	
	// By NAME
	$(".sort_by_name").click(function() {
		$.ajax({
			type: "POST",
			url: "./case_runs/venue_by_name.php",
			data: "",
			success: function(msg){
				$("#venue_list .accordion").html(msg);
				$("#venue_list .accordion h3").click(function(){
					$(this).next("p").slideToggle("slow")
					.siblings("p:visible").slideUp("slow");
					$(this).toggleClass("active");
					$(this).siblings("h3").removeClass("active");
				});
			}
		});
	});
	

	$(".image_slider").each(function() {
		$(this).find(".thumbs").lightBox({fixedNavigation:true});	
	});
	
	$("#newsBox").hover(
		function() {
		},
		function() {
		}
	);
	
});