if (typeof(TheStudio) == "undefined") TheStudio = {};

// TheStudio.Rotator is a class for rotating the hero images.
// You initialize it with init(), passing and associative array with:
// - the jQuery selector and
// - the duration for each hero in seconds
//
TheStudio.Rotator = function() {
	var that = this;
	
	this.init = function(args) {
		this.sel = args.selector;
		this.duration = args.duration*1000;
		this.time = 0;
	};
	
	this.updateTime = function() {
		// Add a second...	
		this.time += 1000;
		
		// ...and transition if the duration has been reached.
		if (this.time == this.duration) {
			this.transition();
			this.time = 0;
		}
	};
	
	this.start = function() {
		
		// We preload the hero images so there is no flash.
		$(this.sel+' img').each(function() {
			new Image().src = $(this).attr('src');
		});
		
		// We assign ids so we'll have something simple to iterate over.
		$(this.sel).each(function(i) {
			$(this).attr('id', 'hero_'+(i+1));
		});
		
		// We start the timer.
		that.interval = setInterval(function() { that.updateTime(); }, 1000);
	};
	
	// A wrapper for clearing the interval and resetting the time.
	this.stop = function() {	
		clearInterval(that.interval);
		this.time = 0;
	};
	
	this.transition = function() {
		// We grab the current and next elements.
		this.curr = (typeof(this.curr) == 'undefined') ? 1 : this.curr;
		this.next = (this.curr+1 == $(this.sel).length+1) ? 1 : this.curr+1;
		
		// We fade in the next, then out the current so the transition is smooth.
		$('#hero_'+this.next).fadeIn(2500);
		$('#hero_'+this.curr).fadeOut(2500);
		
		// We set the current to the next for the next iteration.
		this.curr = this.next;
	};
};

TheStudio.Form = function() {
	var that = this;
	
	this.init = function(args) {
		this.sel = args.selector;
		this.ajax_url = args.ajax_url;
		this.inputs = $(this.sel+" :input");
	};
	
	this.setup = function() {
		$("#submit").click(function() {
			that.send();
			return false;
		});
	};
	
	this.send = function() {
		var sending = document.createElement("p");
		sending.innerHTML = "Your message is being sent...";
		sending.className = "sending";
		sending.style.display = "none";
		
		$(this.sel).ajaxStart(function() {
			that.inputs.attr("disabled", "disabled");
			
			$(this).append(sending);
			$(sending).fadeIn("slow");
		});

		$(this.sel).ajaxStop(function() {
			that.inputs.attr("disabled", "");

			$(sending).empty();
		});
		
		$.post(this.ajax_url,
			{
				email: $("#email").val(),
				name: $("#name").val(),
				notes: $("#notes").val(),
				phone: $("#phone").val(),
				type: $("#type").val(),
				
				event: $('#event').val(),
				when: $('#when').val(),
				attendees: $('#attendees').val(),
				equipment: $('#equipment').val()
			},
			function(data, textStatus) { 
				that.handleResponse(data, textStatus);
			},
			"json"
		);
	};
	
	this.handleResponse = function(d, t) {
		if (t == "success" && d.sent == "1")
			this.success();
		else
			this.error();
	};
	
	this.success = function() {
		var msg = document.createElement("p");
		msg.innerHTML = "Thank you. The Studio will get back to you as quickly as&nbsp;possible.";
		msg.className = "success";
		
		// Show the success message
		$(this.sel).append(msg);
		
		// Get rid of the success message
		$(msg).click(function() { $(this).fadeOut("fast"); });
		setTimeout(function() {
			if ($(that.sel+" .success"))
				$(that.sel+" .success").fadeOut("fast");
		}, 7000);
		
		// Remove the error message
		if ($(that.sel+" .error"))
			$(that.sel+" .error").empty();

		// Clean the inputs
		this.inputs.each(function() {
			that.cleanInput(this);
		});
	};
	
	this.error = function() {
		var msg = document.createElement("p");
		msg.innerHTML = "We were unable to send your message. Please try again&nbsp;later.";
		msg.className = "error";
		
		$(this.sel).append(msg);
	};
	
	this.cleanInput = function(e) {
	  var input = $(e);
	  
	  if (input.attr('type') != 'submit') {
		  input.val("");
		  input.attr("class", "");
	  }
	};
};

var rotator = new TheStudio.Rotator();
rotator.init({
	selector: '#hero li',
	duration: 10
});

var form = new TheStudio.Form();

$(document).ready(function() {
	// We're adding these here because they're superfluous.
	$('body').prepend('<div id="sky"></div><div id="mountains"></div>');
	
	// We add target="_blank" for external links.
	$('a').each(function() {
		if ($(this).attr('rel') == 'external')
			$(this).attr('target', '_blank');
	});
	
	rotator.start();
	
	// Initialize and set up the contact forms.
	form.init({ 
  	selector: "#contact",
  	ajax_url: "/includes/send_contact.php"
  });
	form.setup();
	
	// Set up FancyZoom if its initializer is present
  if (typeof(setupZoom) != 'undefined')
    setupZoom();
});