// Preliminary stuff

$.debug = function(object) {			
	var string;
	$.each(object, function(name, value) {
		string += name+' => '+value+"\r";
	});
	alert(string);
}

$.fn.buttons = function() {
	var c = this.attr('class');

	$('.'+c).append('<span class="button-cap"></span>');
	
	if (! $.browser.version.match(/1\.9/) && $.browser.mozilla) {
		var newheight = parseInt($('.'+c).css('height'))+parseInt($('.'+c).css('padding-top'))+10;
		$('.'+c).css('height', newheight+'px');
	}
	
	/* Fix for FF 2 */
	$('.fi').each(function(i, e) {
		var e = $(e);
		if (e.find('.fi-content').length > 0) {
			e.addClass('fi-has-content-div');
		}
	});
}

$.fn.tabs = function(sectionClass) {
	$.each(this, function(index, element){
		var list = $(element);
		var sections = list.parent().children('.'+sectionClass);
		sections.hide().eq(0).show();
		
		list.find('a').click(function(e){
			list.children().removeClass('current');
			$(e.target).parent().addClass('current');
		
			var target = 0;
			
			list.children().each(function(i, e){
				if ($(e).is('.current')) {
					target = i;
				}
			});
			
			sections.hide().eq(target).show();
		});
	});
}

$.fn.slidingDoors = function(controller, height) {
	var t = this;
	//var height = parseInt(t.css('height'));
	var controller = this.parent().find(controller);
	
	controller.mouseover(function(e) {
		$(this).attr('rel', 'clicked');
		
		var i = 0;
			
		controller.each(function(index, element){
			if ($(element).attr('rel') == 'clicked') i = index;
		});
		
		controller.attr('rel', '');
		
		t.css('background-position', '0 -'+(i*height)+'px');
	});
}

// The meat and potatoes

$(document).ready(function() {
	$('.button').buttons();
	$('.smallTabs, .protabs').tabs('tabSection');
	ql = $('#customers-list a.customer').quicklist({autoTime:7000, items:4, nextPrev:false});
	
	Customers.handle();
	Contact.handle();
	Pf.handle();
	Admin.handle();
	Charlimit.handle();
	
	var email = $('a[href=mailto:#]');
	email.text(email.text().replace(/ \*at\* /, '@').replace(/ \*dot\* /, '.').replace(/\s/, ''));
	email.attr('href','mailto:'+email.text());
	
});

var Customers = {
	handle: function() {
		$('#customers .customers-detail:not(:first)').hide();
		$('#customers-list a.customer:first').addClass('current');
		
		$('#customers-list a.customer').click(Customers.change);
		$('#customers .next, #customers .prev').click(Customers.nextPrev);
	},
	change: function(e) {
		e.preventDefault();
		
		var t = $(this);
		var i;
		$('#customers-list a.customer').removeClass('current');
		$(this).addClass('current');
		
		$('#customers-list a.customer').each(function(index, element){
			if ($(element).is('.current')) {
				i = index;
			}
		});
		
		$('.customers-detail').hide().eq(i).show();
	},
	nextPrev: function(e) {
		e.preventDefault();
		var t = $(this);
		var p = t.parents('.customers-detail');
		var l = $('#customers-list a.customer');
		p.hide();
		
		if (t.is('.next')) {
			var index = ql.lastIndex+1;
			if (p.next('.customers-detail').length > 0) {
				p.next().show();
				l.filter('.current').removeClass('current').next().addClass('current');
			} else {
				p.parent().children('.customers-detail:first').show();
				l.removeClass('current').filter(':first').addClass('current');
				index = 1;
			}
			if (! $('#customers a.customer.current').is(':visible')) {
				ql.change(null, index);
			}
		} else {
			var index = ql.lastIndex-1;
			if (p.prev('.customers-detail').length > 0) {
				p.prev().show();
				l.filter('.current').removeClass('current').prev().addClass('current');
			} else {
				p.parent().children('.customers-detail:last').show();
				l.removeClass('current').filter(':last').addClass('current');
			}
			if (! $('#customers a.customer.current').is(':visible')) {
				ql.change(null, index);
			}
		}
	}
}

var Contact = {
	required: [
		'first_name',
		'last_name',
		'email',
		'company'
	],
	handle: function() {
		
		this.nextStep();
	
		$('form#contact').submit(function(e) {
			var f = $(this);
			
			var errors = [];
			
			f.find('input, textarea, select').each(function(index, element) {
				var input = $(element);
				if (inArray(input.attr('name'), Contact.required) && input.val() == '') {
					errors.push({name: input.attr('name'), message: 'This field is required'});
				} else {
					switch (input.attr('name')){
						case 'email':
							if (! input.val().match(/^(.+)@([^\(\);:,<>]+\.[a-zA-Z]{2,4})/))
								errors.push({name: input.attr('name'), message: 'Please enter a valid email address'});
							break;
					}
				}
			});
			
			$('span.error').remove();
			if (errors.length > 0) {
				for (x in errors) {
					var label = $('#'+errors[x].name);
					label.after('<span class="error">'+errors[x].message+'</span>');
				}
				return false;
			}
			return true;
		});
		
		
		$('form#contact #next_step').click(Contact.nextStep);
	},
	nextStep: function() {
		var t        = $('#next_step');
		var selector = '';
		var all      = $('.notebox:visible');
		
		if (t.find(':selected').attr('rel')) {
			selector = t.find(':selected').attr('rel');
		}
		
		if ($(all).length > 0) {
			$('.notebox:visible').fadeOut('medium',function() {
				$(selector).fadeIn('medium');
			});
		} else {
			$(selector).fadeIn('medium');
		}
	}
}

var Pf = {
	handle: function() {
		$('.pf a').mouseover(function(e){
			$(this).parents('.pf-right').find('.pf li').removeClass('current');
			$(this).parent().addClass('current');
		});
		
		$('#simplePlayer').slidingDoors('ul.pf a', 243);
		$('#fullPlayer').slidingDoors('ul.pf a', 259);
		
		$('#simplePlayer, #fullPlayer').attr('title', 'Click to see the next image');
		
		$('#simplePlayer, #fullPlayer').click(function(e) {
			var currentLink = $(this).parent().find('ul.pf li.current');
			var currentList = currentLink.parent();
			
			if (currentLink.next().length > 0) {
				currentLink.next().children('a').mouseover();
			} else {
				if (currentList.next().length > 0) {
					currentList.next().find('a:first').mouseover();
				} else {
					currentList.parent().find('ul.pf a:first').mouseover();
				}
			}
		});
	}
}

var Admin = {
	handle: function() {
		$('fieldset[id^=customer-] *').hide().filter('legend, a').show();
		$('.toggleCustomer').click(Admin.toggleCustomer);
		
		if ($('fieldset[id^=customer-] ul.errors').length > 0) {
			$('ul.errors').parents('fieldset').find('*').show().parent().find('.toggleCustomer').text('Close');
		}
	},
	toggleCustomer: function(e){
		e.preventDefault();
		if ($(this).text() == 'Edit') {
			$(this).parent().find('*').show();
			$(this).text('Close');
		} else {
			$(this).parent().children().hide().filter('legend, a').show();
			$(this).text('Edit');
		}
	}
}

var Charlimit = {
	handle: function() {
		$('textarea[rel^=limit:]').keyup(Charlimit.keyup);
	},
	keyup: function(e) {
		var limit = parseInt($(this).attr('rel').replace(/limit:/,''));
		if ($(this).val().length > limit) {
			val = $(this).val().substring(0, limit);
			$(this).val(val);
		}
	}
}

function inArray(needle, haystack) {
	for (x in haystack) {
		if (needle == haystack[x]) {
			return true;
		}
	}
	return false;
}