/**
 * @section   : Global JavaScript functions
 * @project   : Breda Hippique
 * @author	: Joris van Summeren <joris@e-sites.nl>
 * @version   : 1.0
 */

/* Sets external links onclick */
function setExtLinks() {
	window.open(this.href);

	return false;
}

/**
 * Init Cufon elements, IE needs a specific Cufon.now call
 *
 * @return void
 * @author Joris van Summeren <joris@e-sites.nl>
 * @since 25 mei 2010
 */
function initCufon() {
	Cufon.replace('h1, .h1, #main h2');
	Cufon.replace('#largeImage .title');

	if (document.all) {
		Cufon.now();
	}
}

/**
 * Small jQuery extension to empty an input field on focus and put the standard value back onblur
 * @author Boye Oomens <boye@e-sites.nl>
 * @version 0.5
 * @usage $('#quickSearch').emptyFieldOnFocus();
 * @return {Object} this
 */
jQuery.fn.extend({
	emptyFieldOnFocus : function () {

		var invalidTypes = ['hidden', 'submit', 'radio', 'checkbox'],
			frmElements = $(this).find('input[type=text], textarea');

		return frmElements.each(function() {

			var self = $(this),
				def = self.val();

			/* Check whether if we are dealing with the correct input type */
			if ($.inArray(self.attr('type'), invalidTypes) === -1) {

				/* Set event handlers */
				self
					.bind('focus', function () {
						if (this.value === this.title) {
							this.value = '';
						} else {
							this.select();
						}
					})
					.bind('blur', function () {
						if (this.value === '') {
							this.value = this.title;
						}
					});
			}
		});
	}
});

/**
 * Init unobtrusive jQuery tab functionality, fades in corresponing clicked tab and equalizes all tabs
 *
 * @return this - jQuery Object
 * @author Joris van Summeren <joris@e-sites.nl>
 * @since 25 mei 2010
 */
jQuery.fn.tabs = function () {
	return this.each(function () {

		var self = $(this),
			tabs = self.find('.tab'),
			tabIdx = jQuery('#tabIdx ol');

		tabs.not('.top').children('.inner').hide();

		jQuery('#tabIdx a').bind('click', function (e) {
			var link = $(this),
				index = $('#tabIdx a').index(link);

			jQuery('#tabIdx .active').removeClass('active');
			jQuery('#tabIdx li').eq(index + 1).addClass('active');

			tabs.children('.inner').fadeOut('fast').end().removeClass('top');
			tabs.eq(index).addClass('top').children('.inner').fadeIn();

			link.blur();
			e.preventDefault();
		});

	});
};

/**
 * Initializes jQuery Scrollable plugin for news & media sliders
 *
 * @param none
 * @return void
 * @author Joris van Summeren <joris@e-sites.nl>
 * @since 26 mei 2010
 */
function initScrollable() {
	var apiNews;

	if ($('#largeImageWrapper').length) {
		apiNews = $('#largeImage').scrollable({ size: 1, speed: 500, circular: true }).autoscroll({ interval: 6000 }).data('scrollable');
	} else {
		apiNews = $('#largeImage').scrollable({ size: 1 }).data('scrollable');
	}

	if (document.all) {
		apiNews.onSeek = function () {
			$('.cloned .title').children().hide();
		};
	}

	Cufon.replace('.cloned .title');

	$('.sliderNav').each(function () {
		var self = $(this),
			amount = apiNews.getSize();

		while (amount--) {
			self.children('li').eq(amount).children('a').bind('click', function () {
				apiNews.seekTo( $(this).parent().index() );
			});
		}
	});

	$('#mediaSlider').scrollable({ size: 3, speed: 500 }).navigator({ navi: '.navi' });
}

/**
 * Expose currently hovered event and make all other events less opaque. Expands an event if there is more
 * information to show. Inserts a dotted line at the original height so a visitor can still determine the
 * event's end time.
 *
 * @param none
 * @return void
 * @author Joris van Summeren <joris@e-sites.nl>
 * @since 1 jun 2010
 */
function eventEnter() {
	var self = $(this),
		currHeight = self.height(),
		minHeight = self.css('height', 'auto').height(),
		others = $('.event', '#timetable').not(self);

	self.css('height', currHeight).data('origHeight', currHeight);
	others.css('opacity', '0.6');

	if (currHeight < minHeight) {
		self.css({ height: 'auto', zIndex: 6 });

		if ($('.dotted', self).length < 1) {
			self.append('<div class="dotted" style="top:' + (currHeight + 10) + 'px; left:0; position:absolute; width:100%; height:1px; border-top:1px dotted #fff; opacity:0.5; filter:alpha(opacity=50)"');
		}
	}
}

/**
 * Does the opposite of eventEnter(), all events are fully visible now
 *
 * @param none
 * @return void
 * @author Joris van Summeren <joris@e-sites.nl>
 * @since 1 jun 2010
 */
function eventLeave() {
	var self = $(this),
		others = $('.event', '#timetable').not(self);

	others.css('opacity', '1');

	$('.dotted', self).remove();

	self.height(self.data('origHeight')).css('z-index', 5);
}

/**
 * Shows the medium sized variant of an image
 *
 * @param {Object} - jQuery Object
 * @return void
 * @author Joris van Summeren <joris@e-sites.nl>
 * @since 2 jun 2010
 */
function showLargeImg(e) {
	var self = $(this),
		mediumURL = self.attr('href'),
		largeURL = self.attr('rel'),
		placeholder = $('#mediumPhoto');

	if (mediumURL) {
		placeholder.css('background-image', 'url(' + mediumURL + ')');
	}

	if (largeURL) {
		$('#enlarge').attr('href', largeURL);
	}

	e.preventDefault();
}

/**
 * Submits newsletter form signup with ajax
 *
 * @param {Object} - jQuery Object
 * @return void
 * @author Joris van Summeren <joris@e-sites.nl>
 * @since 4 jun 2010
 */
function submitEsender(e) {
	var frm = $('#quickSignUp'),
		values = frm.serializeArray(),
		submit = frm.find('.submit');

	submit.next('span').remove();
	submit.after('<img src="/images/misc/loaderSmall.gif" class="message" alt="">');

	$.ajax({
		type: 'POST',
		url: '/aanmelden-nieuwsbrief.html?quick=true',
		data: values,
		success: function (data) {
			if (data !== '') {
				var doc = document,
					name = doc.getElementById('signUpName'),
					email = document.getElementById('signUpEmail');

				name.value = name.title;
				email.value = email.title;
				submit.next('img').remove();
				submit.after('<span class="message">' + data + '</span>');
			}
		}
	});
}

/**
 * Animates / fades a list with sponsors
 *
 * @param {Object} - jQuery Object
 * @return void
 * @author Joris van Summeren <joris@e-sites.nl>
 * @since 11 jun 2010
 */
function slideSwitch(wrapper) {
	var active = $('.active', wrapper),
		next =  active.next().length ? active.next() : $('li:first', wrapper),
		sibs  = active.siblings(),
		rndNum = Math.floor(Math.random() * sibs.length),
		next  = $(sibs[rndNum]);

	if (active.length === 0) {
		active = $('li:last', wrapper);
	}

	active.addClass('last-active');
	next
		.css({ opacity: 0.0 })
		.addClass('active')
		.animate({ opacity: 1.0 }, 1000, function () {
			active.removeClass('active last-active');
	});
}
