/*	Image Fader */
jQuery.fn.extend({'imageFader':function(sleep,fade){var self={};var obj=$(this);if(!sleep)sleep=5000;if(!fade)fade=0.05;if(sleep<1000)sleep=5000;if(fade<0.05)fade=0.05;if(fade>0.95)fade=0.95;self.sleep=sleep;self.fade=sleep*fade;self.execute=function(){var current=obj.find('.slide.current');var next=current.next();if(obj.find('.slide').length<=1)return;if(current.get(0)==obj.find('.slide:last').get(0)){next=obj.find('.slide:first');}
next.fadeIn(self.fade,function(){current.fadeOut(self.fade,function(){current.removeClass('current');next.addClass('current');});});self.trigger();}
self.trigger=function(){setTimeout(self.execute,self.sleep);}
self.trigger();}});


/*!
 * HTML5 Placeholder jQuery Plugin v1.8
 * @link http://github.com/mathiasbynens/Placeholder-jQuery-Plugin
 * @author Mathias Bynens <http://mathiasbynens.be/>
 */
(function(f){var e='placeholder' in document.createElement('input'),a='placeholder' in document.createElement('textarea');if(e&&a){f.fn.placeholder=function(){return this}}else{f.fn.placeholder=function(){return this.filter((e?'textarea':':input')+'[placeholder]').bind('focus.placeholder',b).bind('blur.placeholder',d).trigger('blur.placeholder').end()}}function c(h){var g={},i=/^jQuery\d+$/;f.each(h.attributes,function(k,j){if(j.specified&&!i.test(j.name)){g[j.name]=j.value}});return g}function b(){var g=f(this);if(g.val()===g.attr('placeholder')&&g.hasClass('placeholder')){if(g.data('placeholder-password')){g.hide().next().show().focus()}else{g.val('').removeClass('placeholder')}}}function d(i){var l,k=f(this),h=k,g=k.data('placeholder-init');if(k.val()===''||(!g&&k.val()===k.attr('placeholder'))){if(k.is(':password')){if(!k.data('placeholder-textinput')){try{l=k.clone().attr({type:'text'})}catch(j){l=f('<input>').attr(f.extend(c(k[0]),{type:'text'}))}l.removeAttr('name').data('placeholder-password',true).bind('focus.placeholder',b);k.data('placeholder-textinput',l).before(l)}k=k.hide().prev().show()}k.addClass('placeholder').val(k.attr('placeholder'))}else{k.removeClass('placeholder')}if(!g){h.data('placeholder-init',true)}}f(function(){f('form').bind('submit.placeholder',function(){var g=f('.placeholder',this).each(b);setTimeout(function(){g.each(d)},10)})});f(window).bind('unload.placeholder',function(){f('.placeholder').val('')})}(jQuery));


$(document).ready(function() {
	var root = $('#page-header h1 a').attr('href');
/*----------------------------------------------------------------------------
	Fix IE hover:
----------------------------------------------------------------------------*/
	if ($.browser.msie) {
		$('div.col:last-child:not(#content,#aside,.secondary)').css("width", "48%");
		$('.col:last-child, ul.find-us-on li:last-child,ul.inline-list li:last-child,ul.tiles li:last-child,ul.tiles .public:nth-child(4n),ul.tiles .private:nth-child(3n)').css("marginRight","0");
		$('body.events #content > div.gutter').css("width", "671px");
		/* Form Validation */
		$('div.triple:nth-child(6) .information:nth-child(2)').addClass("ie-suburb");
		$('div.triple:nth-child(6) .information:nth-child(4)').addClass("ie-postcode");
		$('div.double:nth-child(3) .information:nth-child(2), div.double:nth-child(4) .information:nth-child(2), #payment div.double .information:nth-child(2)').addClass("ie-double");

		/* Homepage has some specific styles */
		$('#footer .col').css("width","25%");

		/* IE6 Only, 7&8 support these things :) */
		if($.browser.version == "6.0") {
			$('#nav li').hover(
				function() {
					$(this).addClass('hover');
				},
				function() {
					$(this).removeClass('hover');
				}
			).focus(
				function() {
					$(this).addClass('hover');
				},
				function() {
					$(this).removeClass('hover');
				}
			);

			$('ol li.block:first-child','#content').css("borderTop","1px solid #d8e0e2");
			$('#footer dl.inline dt:first-child').css("display","none");
			$('#aside .box:first-child').css("marginTop", "0");
			$('div.single-label div.field:first-child','#content').css("margin","12px 0 0");
			$('div.offset .field:first-child', '#content').css("width","24%");
			$('form.overhead .spacer div.field-check + .information .description', "#content").addClass("ie-fix-field-check-spacer");
			$('form.overhead div.field-check + .information .description',"#content").addClass("ie-fix-field-check").css("left","300px"); /* For some reason IE only works with this */
		}
	}

/*----------------------------------------------------------------------------
	Equal Heights Event Artists
----------------------------------------------------------------------------*/

	$('#content ol.tiles li:not(:nth-child(3n+1))').each(function() {
		var self = $(this),
			mod = $("ol.tiles li").index(this) % 3;

			prev = self.prev(),
			prevv = prev.prev(),
			heights = [
						self.height(),
						prev.height(),
						prevv.height()
					],
			rowHeight = Math.max.apply({}, heights);

			self.height(rowHeight);
			prev.height(rowHeight);

		if(mod % 3 == 2) {
			prevv.height(rowHeight);
		}
	});

	$('body.gallery #content ul.public li:nth-child(4n+4)').each(function() {
		var self = $(this);

			prev = self.prev(),
			prevv = prev.prev(),
			prevvv = prevv.prev(),
			heights = [
						self.height(),
						prev.height(),
						prevv.height(),
						prevvv.height()
					],
		rowHeight = Math.max.apply({}, heights);

		self.height(rowHeight);
		prev.height(rowHeight);
		prevv.height(rowHeight);
		prevvv.height(rowHeight);
	});


/*----------------------------------------------------------------------------
	Equal Heights Event Artists
----------------------------------------------------------------------------*/

	if($("#register-form").length > 0) {
		// Set placeholder and http:// prepend for social media fields
		$('#register-form #online-media input').placeholder().bind('change focus', function() {
			if($(this).val() == "") {
				$(this).val('http://');
			}
		});

		var agent_select = $('#field-1-performer-agent');
		var agent_add = $("#add-agent");
		var agent_details = $("#agent-details");
		var contact_details = $("#contact-details")

		agent_add.hide();
		agent_details.hide();
		agent_select.attr('disabled', 'disabled');

		$(this).find("#field-1-representation").change(function() {
			if($(this).val() == "1") {
				agent_select.removeAttr('disabled').show();
				agent_details.slideDown();
				contact_details.hide();
			} 
			else {
				agent_select.attr('disabled', 'disabled').hide();
				contact_details.slideDown();
				agent_add.hide();
				agent_details.hide();
			}
		});

		$(this).find(".add-agent").click(function() {
			agent_details.hide();
			agent_select.attr('disabled', 'disabled').hide();
			agent_add.slideDown();
			return false;
		});


		$(this).find(".cancel a").click(function() {
			agent_details.show();
			agent_add.slideUp();
			agent_select.removeAttr('disabled').show();

			return false;
		});
	}

/*----------------------------------------------------------------------------
	Lightbox:
----------------------------------------------------------------------------*/
	var lbconfig = {
		imageLoading: root + '/workspace/images/lightbox/lightbox-ico-loading.gif',
		imageBtnClose: root + '/workspace/images/lightbox/lightbox-btn-close.gif',
		imageBtnPrev: root + '/workspace/images/lightbox/lightbox-btn-prev.gif',
		imageBtnNext: root + '/workspace/images/lightbox/lightbox-btn-next.gif'
	};

	$('#content').find('ol.tiles > li a.lightbox').each(function() {
		$(this).lightBox(lbconfig);
	}).end().find('ul.tiles a.lightbox').lightBox(lbconfig);

/*----------------------------------------------------------------------------
	Highlight top
----------------------------------------------------------------------------*/
	if(window.location.hash == "#membership") {
		$('#membership input:eq(0)').focus();
	}

/*----------------------------------------------------------------------------
	Image Fader
----------------------------------------------------------------------------*/
	$('div.rotator').each(function() {
		$(this).imageFader(5000, 0.2);
	});

/*----------------------------------------------------------------------------
	Share Hover (Safari dies with CSS display:block on hover..)
----------------------------------------------------------------------------*/
	$('#share-start').hover(
		function() {
			$(this).find('ul.share-list').stop(true, true).slideDown('fast');
		},
		function() {
			$(this).find('ul.share-list').stop(true, true).slideUp('fast');
		}
	)
/*----------------------------------------------------------------------------
	Google Analytics Validation Event Tracking [Static]
----------------------------------------------------------------------------*/
	$('div.field + div.information > div.error', 'form').each(function() {
		var self = $(this),
			form = self.closest('form').attr('id'),
			field = self.parent('div.information').prev('div.field').find(':input').attr('name');

		_gaq.push(['_trackEvent', form, 'Validation Error: Page Load', self.text()]);
	});

/*----------------------------------------------------------------------------
	Google Analytics Event Tracking
----------------------------------------------------------------------------*/
	$(':input').live('blur', function() {
		var self = $(this),
			form = self.closest('form').attr('id'),
			field = self.attr('name');

		_gaq.push(['_trackEvent', form, 'Field Exit', field]);
	});

});

$(window).ready(function() {
	/*----------------------------------------------------------------------------
		Highlight top
	----------------------------------------------------------------------------*/
		$('a[href$=#login-form]').live("click",function() {
			$("#login-form input").addClass("highlight");
			$("#login-form input:eq(0)").focus();
			return false;
		});

	/*----------------------------------------------------------------------------
		Open external links in a new tab/window
	----------------------------------------------------------------------------*/
		$('a.external, a[rel=external]').live("click", function() {
			window.open($(this).attr('href'));
			return false;
		});
});
