jQuery.noConflict()(function($){

	$(function()
	{
		// Image actions
		$('.pricing-table-white, .pricing-table-grey').hover(
			function() { $(this).find('.image-actions').css('display', 'none').fadeIn('fast').css('display', 'block'); }, // Show actions menu
			function() { $(this).find('.image-actions').fadeOut(100); } // Hide actions menu
		);

		$('form').submit(function()
		{
			if(!validate(this.id))
				return false;
		});
		
		function validate(form)
		{
			$('form#'+form+' .error').remove();
			var hasError = false;
			var error_msg = '';
			$('form#'+form+' .required').each(function()
			{
				var labelText = $(this).prev('label').text();
				if(jQuery.trim($(this).val()) == '')
				{
					error_msg = error_msg + 'You forgot to enter '+labelText+'<br/>';
					hasError = true;
				}
				else if($(this).hasClass('email'))
				{
					var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
					if(!emailReg.test(jQuery.trim($(this).val())))
					{
						error_msg = error_msg + 'You entered an invalid '+labelText+'<br>';
						hasError = true;
					}
				}
				else if($(this).hasClass('number'))
				{
					var no = /^[0-9]+(\.[0-9][0-9]?)?$/;
					if(!no.test(jQuery.trim($(this).val())))
					{
						error_msg = error_msg + 'You entered an invalid '+labelText+'<br>';
						hasError = true;
					}
				}
				else if($(this).hasClass('match'))
				{
					var ph = $(this).attr('rel');
					if($(this).val() != $('input#' + ph + '').val())
					{
						error_msg = error_msg + 'Passwords do not match<br>';
						hasError = true;
					}
				}
			});
			if(!hasError)
			{
				return true;
			}
			jQuery.facebox("<strong>Errors</strong><br><div class='simple-error'>"+error_msg+"</div>");
			return false;
		}

		$('form#biodata select#artiste').change(function()
		{
			$.ajax(
			{
				url : 'ajax.php?act=bio&artiste='+$(":selected", this).val(),
				type :'GET',
				success : function( resp )
				{
					var string = new Array();
					string = resp.split('|break|');

					$("#biotxt").val(string[0]);
					$("#url").val(string[1]);
					$("#fb").val(string[2]);
					$("#id").val(string[3]);

					if (string[4] != 0)
					{
						$("#action").val('update');
					}
					else
					{
						$("#action").val('insert');
					}
				}
			});
		});
		
		$(".alert-message").alert('close');

		$('form#tracks select#artiste').change(function()
		{
			$.ajax(
			{
				url : 'ajax.php?act=tracks&artiste='+$(":selected", this).val(),
				type :'GET',
				success : function( resp )
				{
					var string = new Array();
					string = resp.split('|break|');
					$('form#tracks div#songs').html(string[0]);
					$('span#total').html(string[1]);
				}
			});
		});

		// VIDEO OF THE WEEK
		$('form#video_week select#artiste').change(function()
		{
			$.ajax(
			{
				url : 'ajax.php?act=video&artiste='+$(":selected", this).val(),
				type :'GET',
				success : function( resp )
				{
					var child = 'form#video_week select#video';
					var sel = 'form#video_week select#video option:selected';
					$(child).html(resp);
					if (!$(sel).length)
					{
						$("option", child).first().attr("selected", "selected");
					}
					$(child).trigger('change');
				}
			});
		});

		$('form#video_week select#video').change(function()
		{
			$('form#video_week input#linkutube').val($(":selected", this).attr('id'));
		});		
		
		//TOP TEN
		$('form#top_ten select[id*=artiste]').change(function()
		{
			var id = this.id;
			$.ajax(
			{
				url : 'ajax.php?act=songs&artiste='+$(":selected", this).val(),
				type :'GET',
				success : function( resp )
				{
					var child = 'form#top_ten select#song_'+id;
					$(child).html(resp);
				}
			});
		});
	});
});
