/**
 * Description
 * 
 * @author	Max Silva
 * @email		max.silva@live.uwe.ac.uk
 * 
 * @file				site.js
 * @version		1.0
 * @date			07/10/2011
 * 
 * Copyright © 2012
 */
 
jQuery(document).ready(function($) {

// --------------------------------------------------------------------------
//	Fader
// --------------------------------------------------------------------------

$('.fader').fadeTo('slow', 0.6)

$('.fader').hover(function()
{
	$(this).fadeTo('slow', 1.0);
},
function()
{
	$(this).fadeTo('slow', 0.6);
});

// --------------------------------------------------------------------------
//	Gallery Hover
// --------------------------------------------------------------------------
$('.gallery_item_hover').fadeTo('slow', 0.6).hover(function()
{
	$(this).fadeTo('slow', 1.0);
	$(this).find('div').slideDown('slow');
},
function()
{
	$(this).find('div').slideUp('slow');
	$(this).fadeTo("slow", 0.6);
});

// --------------------------------------------------------------------------
//	FancyBox
// --------------------------------------------------------------------------

$('a.gallery-photo').fancybox({
	'transitionIn'				: 'elastic',
	'transitionOut'			: 'elastic',
	'easingIn'      			: 'easeOutBack',
	'easingOut'     			: 'easeInBack',
	'speedIn'      			: 500,
	'speedOut'     			: 500,
	'centerOnScroll'	: true,
	'titlePosition' 	  	: 'over',
	'titleFormat'		  	: function(title) {
		return '<span id="fancybox-title-over">' + title + '</span>';
	 }
 });
				
$('a.gallery-video').fancybox({
	'transitionIn'				: 'elastic',
	'transitionOut'			: 'elastic',
	'easingIn'      			: 'easeOutBack',
	'easingOut'     			: 'easeInBack',
	'speedIn'      			: 500,
	'speedOut'     			: 500,
	'centerOnScroll'	: true,
     'autoscale'       	: true,
     'padding'           	: 0,
     'type'    				: 'swf',
     'swf'   					: { 'wmode':'transparent', 'allowfullscreen':'true' }
   });
			       

$('a.media-item').fancybox({
	'transitionIn'				: 'elastic',
	'transitionOut'			: 'elastic',
	'easingIn'      			: 'easeOutBack',
	'easingOut'     			: 'easeInBack',
	'speedIn'      			: 500,
	'speedOut'     			: 500,
	'centerOnScroll'	: true,
     'autoscale' 			: true,
     'padding' 			: 0,
     'type'    				: 'swf',
     'height' 				: 30,
     'swf'   					: { 'wmode':'transparent', 'allowfullscreen':'false' }
   });
			   
$('a.contact-item, a.comment-item, a.user-profile').fancybox({
	'transitionIn'				: 'elastic',
	'transitionOut'			: 'elastic',
	'easingIn'      			: 'easeOutBack',
	'easingOut'     			: 'easeInBack',
	'speedIn'      			: 500,
	'speedOut'     			: 500,
	'centerOnScroll'		: true,
	'padding'          		: 0,
	'autoScale'     			: true,
	'height'						: 250,
	'type'						: 'iframe',
	'showCloseButton'	: false
	});

$('a#create-comment').fancybox({
	'transitionIn'				: 'elastic',
	'transitionOut'			: 'elastic',
	'easingIn'      			: 'easeOutBack',
	'easingOut'     			: 'easeInBack',
	'speedIn'      			: 500,
	'speedOut'     			: 500,
	'centerOnScroll' 		: true,
	'padding'         	  		: 0,
	'autoDimensions'		: true,
	'scrolling'					: 'no',
	'titleShow'					: false,
	'showCloseButton'	: false,
	'onClosed'				: function()
	{
		// Resets response div
		$('div#comment-modal-response').html('');
		
		// Clear errors and input values then show form.
	    $('p.error').hide();
	    $('textarea#comment, input#captcha').val('');
	    $('form#comment-modal-form').show();
	}
});
	
// onSubmit check user is logged in, validate form and add comment.
$('form#comment-modal-form').bind('submit', function() {

	// Add comment to the database.
	$.ajax({
		type		: 'POST',
		cache	: false,
		url		: $('input#authedCheck-url').val(),
		success: function(data)
		{
			// User is logged in.
			if (data.length == 0)
			{
				// Check comment is longer than 3 characters
				if ($('textarea#comment').val().length < 3)
				{
					$('p.error').hide();
				    $('form#comment-modal-form fieldset').before('<p class="error">Comment field must be more than 3 characters.</p>');
				    $.fancybox.resize();
				    return false;
				}
				
				// Check comment is longer than 250 characters
				if ($('form#comment-modal-form textarea#comment').val().length > 250)
				{
					$('p.error').hide();
				    $('form#comment-modal-form fieldset').before('<p class="error">Comment field must be no more than 250 characters.</p>');
				    $.fancybox.resize();
				    return false;
				}
				
				// Check captcha matches given code
				if ($('input#captcha').val() != $('p#captcha-code').text())
				{
					$('p.error').hide();
				    $('form#comment-modal-form fieldset').before('<p class="error">Captcha field must match the given code.</p>');
				    $.fancybox.resize();
				    return false;
				}
			
				// Show loading icon.
				$.fancybox.showActivity();
			
				// Add comment to the database.
				$.ajax({
					type		: 'POST',
					cache	: false,
					url		: $('input#ajax-url').val(),
					data		: $('form#comment-modal-form').serializeArray(),
					success: function(data) 
					{
						// Hide the form and show the response.
						$('form#comment-modal-form').hide();
						$('div#comment-modal-response').html(data);
						$.fancybox.hideActivity();
					}
				});
				
				return false;
			}
			else
			{
				// Hide the form and show the response.
				$('form#comment-modal-form').hide();
				$('div#comment-modal-response').html(data);
				$.fancybox.hideActivity();
			}
			
		} // End Success
		
	}); // End Ajax
	
	return false;
	
}); // End bind

}); // End jQuery

// --------------------------------------------------------------------------
//	Music Player
// --------------------------------------------------------------------------

function player(url)
{
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open('"+url+"', '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=1,menubar=0,resizable=0,width=500,height=150,left = 520,top = 350');");
}
			
// --------------------------------------------------------------------------
//	Cufon
// --------------------------------------------------------------------------

Cufon.replace('ul#nav li a, h2, div.admin-item h3');
			
// --------------------------------------------------------------------------
//	videoThumb
// --------------------------------------------------------------------------

function videoThumb()
{
	if ($('select#type').val() == 'videos')
	{
		$('li#thumb').append(
            '<label for="thumbnail">Gallery Item Thumbnail <em>*</em></label>'+
            '<input type="file" name="video_thumb" id="thumbnail" size="20"/>'
        );
	}
	
	if ($('select#type').val() == 'photos')
	{
		$('#thumb').empty();
	}
}

// --------------------------------------------------------------------------
//	displayTweets
// --------------------------------------------------------------------------

function displayTweets(tweets)
{
	// Parse JSON into object form
	var objTweets = jQuery.parseJSON(tweets);
	
	// Checks if any tweets exist.
	if (objTweets.error)
	{
		// Show error message.
		$('p#tweets-carousel').html(objTweets.error);
	}
	else
	{	
		// Create first tweet
	    $('p#tweets-carousel').hide()
	    .html('<a href="'+objTweets[0].href+'" target="_blank">'+objTweets[0].tweet+'</a> <span class="tweet-date">'+objTweets[0].date+'</span>')
	    .fadeIn(200);
	    
	    // Check there is more than one tweet.
	    if (objTweets.length > 1)
	    {
		    // Set indicator 
		    var i = 1;

			// Loop
		    setInterval(
		        function() {
		            $('p#tweets-carousel').fadeOut(200).hide()
		            .html('<a href="'+objTweets[i].href+'" target="_blank">'+objTweets[i].tweet+'</a> <span class="tweet-date">'+objTweets[i].date+'</span>')
		            .fadeIn(200);
		            i++;
		            
		            // If interval is equal to length of object.
		            if (i == objTweets.length)
		            { 
			            // Reset interval indicator.
			            i = 0; 
		            }
		        }, 
	        10000); //10 seconds interval rotation.
        }
	}
}	
	
// --------------------------------------------------------------------------
