jQuery(document).ready(function(){
	jQuery('a').click(link_click_handler);
	jQuery('form').submit(form_submit_handler);
	jQuery('#content input:submit').click(form_submit_click_handler);
	jQuery('#socialmenu div.social_button').hover(function(){
		var jq_this = jQuery(this);
		jq_this.stop(true, true);
		jq_this.animate({marginTop: '-10px'}, 500);
	}, function(){
		var jq_this = jQuery(this);
		jq_this.stop(true, true);
		jq_this.animate({marginTop: '0px'}, 500);
	});
});

/**
 *
 * @access public
 * @return void
 **/
function after_refresh()
{
	jQuery('#content a').click(link_click_handler);
	jQuery('#content form').submit(form_submit_handler);
	jQuery('#content input:submit').click(form_submit_click_handler);

	jQuery("#content a.group").fancybox({
		'speedIn':			300,
		'speedOut':			300,
		'overlayColor':		'#000',
		'overlayOpacity':	0.7
	});
}

/**
 *
 * @access public
 * @return void
 **/
function link_click_handler()
{
	var jq_this = jQuery(this);
	if(!jq_this.hasClass('group'))
	{
		var href = jq_this.attr('href');
		var intenal = is_internal_link(href);
		if(href != '' && intenal && !jq_this.hasClass('no_ajax'))
		{
			href = fix_link(href);
			if(href.indexOf('?') > -1)
			{
				href += '&ajax=1';
			}
			else
			{
				href += '?ajax=1';
			}
			jQuery('#content').load(href, function(response, status, xhr)
			{
				if (status != "error")
				{
					after_refresh();
				}
			});
		}
		if( (intenal && !jq_this.hasClass('no_ajax') ) || href == '')
		{
			return false;
		}
		else if(!intenal)
		{
			//external link
			var target = jq_this.attr('target');
			if(target == null || target == '')
			{
				jq_this.attr('target', '_blank');
			}
		}
	}
}

function form_submit_click_handler()
{
	var jq_submit = jQuery(this);
	var jq_form = jq_submit.parents('form:first');
	submit_form(jq_submit, jq_form);

	return false;
}

/**
 *
 * @access public
 * @return void
 **/
function form_submit_handler()
{
	var jq_form = jQuery(this);
	var jq_submit = jq_submit.children('input:submit:first');
	submit_form(jq_submit, jq_form);

	return false;
}

/**
 *
 * @access public
 * @return void
 **/
function submit_form(jq_submit, jq_form)
{
	var data_string = jq_form.serialize();
	if(jq_submit != null && jq_submit.attr('name') != null)
	{
		data_string += '&' + jq_submit.attr('name') + '=' + jq_submit.attr('value');
	}
	var href = jq_form.attr('action');
	if(is_internal_link(href))
	{
		if(href.indexOf('?') > -1)
		{
			href += '&ajax=1&' + data_string;
		}
		else
		{
			href += '?ajax=1&' + data_string;
		}
		href = fix_link(href);
	}
	jQuery.ajax({
		type: 'POST',
		url: href,
		data: data_string,
		dataType: "html",
		success: function(html)
		{
			jQuery('#content').html(html);
			after_refresh();
		},
		error:function (xhr, ajaxOptions, thrownError){
			alert('Er is een fout opgetreden met het versturen van de data.');
		}
	});
}

/**
 *
 * @access public
 * @return void
 **/
function is_internal_link(link)
{
	var i = link.indexOf(window.location.hostname);
	var j = link.indexOf('http://');
	if((i >= 0 && i <= 13) || j == -1)
	{
		return true;
	}
	else
	{
		return false;
	}
}

/**
 *
 * @access public
 * @return void
 **/
function fix_link(link)
{
	var site_host = window.location.hostname;
	var link_host = link.replace('http://', '');
	if(link_host.indexOf('www.') != site_host.indexOf('www.'))
	{
		link = link.replace('www.', '');
	}
	return link;
}

function show_div(selector)
{
	var jq_div = jQuery(selector);
	if(jq_div.is(':hidden'))
		jq_div.show(400);
	else
		jq_div.hide(400);
}

/*
												jPlayer
*/


jQuery(window).load(function(){
	var url_prefix = window.location.protocol + '//' +
		window.location.hostname +
		window.location.pathname +
		'mp3/';
	var media_url = url_prefix + 'list_audio.php';

	jQuery.ajax({
		type: 'POST',
		url: media_url,
		data: 'url_prefix=' + url_prefix,
		success: function(result)
		{
			var script = 'var media = ' + result + ';';
			eval(script);
			var audioPlaylist = new Playlist(media);
		}
	});
});

var Playlist = function(playlist) {
	var self = this;

	this.playlist = playlist; // Array of Objects: The playlist

	this.current = 0;

	$("#jquery_jplayer_1").jPlayer({
		ready: function() {
			self.playlistInit(true); // Parameter is a boolean for autoplay.
		},
		ended: function() {
			self.playlistNext();
		},
		play: function() {
			$(this).jPlayer("pauseOthers");
		},
		swfPath: "/cmsms/uploads/maurits/jquery/plugins/jQuery.jPlayer.2.0.0",
		supplied: "mp3"
	});

	$("#jp_interface_1 .jp-next").click(function() {
		self.playlistNext();
		$(this).blur();
		return false;
	});
};

Playlist.prototype = {
	playlistInit: function(autoplay) {
		if(autoplay) {
			this.playlistChange(this.current);
		} else {
			this.playlistConfig(this.current);
		}
	},
	playlistConfig: function(index) {
		this.current = index;
		$("#jquery_jplayer_1").jPlayer("setMedia", this.playlist[this.current]);
	},
	playlistChange: function(index) {
		this.playlistConfig(index);
		$("#jquery_jplayer_1").jPlayer("play");
	},
	playlistNext: function() {
		var index = (this.current + 1 < this.playlist.length) ? this.current + 1 : 0;
		this.playlistChange(index);
	},
	playlistPrev: function() {
		var index = (this.current - 1 >= 0) ? this.current - 1 : this.playlist.length - 1;
		this.playlistChange(index);
	}
};
