/* * * * CUSTOM JQUERY * * * * * * * * * * * * * * * * * * * * * * * * *

Author: Jimmy (18/11/2010)
Last Update: 19/07/2011 (Jimmy)

Requirements:

	js/jquery.js
	
	Other requirements based off jQuery modules used in this file.	

Purpose:

	This code allows all the website's jQuery to be stored in a single
	location for easy code changes. All code should use listeners and
	callback functions for easy control in once place.
	
	Standard modules should be launched from here (lightbox, cufon, etc.)
	for easy site-wide management.
	
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/* Add element exists function */
jQuery.fn.exists = function() {
	return jQuery(this).length > 0;
}

function reset_fancybox() {

	// Reset fancy box listener (Don't like this… but it works)
	$('a.lightbox').fancybox({
		'type' : 'image',
		'transitionIn'	: 'fade',
		'transitionOut': 'fade',
		'titleShow': true
	});
	
	$('a.lightbox_url').fancybox({
		'type' : 'iframe',
		'width': '600',
		'height':'500',
		'transitionIn': 'fade',
		'transitionOut': 'fade',
		'titleShow': true
	});
	
	if($.isFunction($.scPlayer.defaults.onDomReady)){
		$.scPlayer.defaults.onDomReady();
	}

}

$(document).ready(function() {
	
/* !Allows validating 
------------------------------------------*/
	$('a.new_window').attr('target', '_blank');
		
/* !Lightbox
------------------------------------------*/	

	reset_fancybox();
	
/* !Menu system 
------------------------------------------*/
	$('ul#main_menu li a').hover(function() {	
		$(this).stop().animate({color: '#559e29'}, { queue: false, duration: 400 });
	}, function() {
		$(this).stop().animate({color: '#ffffff'}, { queue: false, duration: 200 });
	});
	
/* !Widget loading
------------------------------------------*/

	$('div#twitter div.content').hide();
	$('div#rss_widget div.content').hide();

	$('div#twitter div.content').slideDown(500, function() {
		$('div#rss_widget div.content').slideDown(500);
	});
	
/* !Subpage hover resize
------------------------------------------*/

    $('ul.sub_pages a').live({
		mouseenter: function() {
			$(this).stop().animate({'font-size': '70px'}, { queue: false, duration: 300 });
		},
        mouseleave: function() {
			$(this).stop().animate({'font-size': '58px'}, { queue: false, duration: 150 });
		}
	});
	
/* !Subpage menu slide
------------------------------------------*/

	$('ul.sub_pages').hide();
	$('h1').hide();
	$('div#content div.copy').hide();
	
	$('ul.sub_pages').show('slide', {directions: 'left'}, 1000);
	
	if($('h1').exists()) {
		// Home page which has an h1 section, do this first.
		$('h1').show('slide', {directions: 'left'}, 1000, function() {
			$('div#content div.copy').show('slide', { directions: 'left' }, 1000);
		});
	} else if($('div#people').exists()) {
		// Peopl page - slide down description content
		$('div#main_content').hide();
		$('div#content div.copy').show();
		$('div#main_content').slideDown();
	} else if($('div#portfolio').exists()) {
		// People page - slide down description content
		$('div#main_content').hide();
		$('div#content div.copy').show();
		$('div#main_content').slideDown();
	} else if($('div#case_study').exists()) {
		// Case study page - hide all tabs except the current one.
		$('div.tab_content').hide();
		$('div#content div.copy').show();
		$('div.tab_container div.current').slideDown();
		update_tab_arrows($('div.tab_container div.current'));
	} else if($('div.creative_writing').exists()) {
		$('div#content div.copy').slideDown();
	} else {
		// Other pages
		$('div#content div.copy').show('slide', { directions: 'left' }, 1000);
	}
	
/* !Thumbnails on work page 
------------------------------------------*/

	if($('img.portfolio_thumbnail').exists()) {

		/*$('img.portfolio_thumbnail').live({
			mouseenter: function() {
				$(this).stop().animate({'width': '74px' }, { queue: false, duration: 200 });
			},
	        mouseleave: function() {
				$(this).stop().animate({'width': '70px' }, { queue: false, duration: 100 });
			}
		});	*/
		/*
		$('img.portfolio_thumbnail').hover(function() {
			$(this).stop().animate({'width': '74px' }, { queue: false, duration: 200 });
		}, function() {
			$(this).stop().animate({'width': '70px' }, { queue: false, duration: 100 });
		});
		*/
	}
	
	$("div#thumbnail_cycle").jCarouselLite({
		btnNext: "#thumbnail_right_arrow",
		btnPrev: "#thumbnail_left_arrow",
		visible: 8,
		scroll: 2,
	});	

/* !Simple tab system (for Case Study module)
------------------------------------------*/

	$('ul.tabs a').live('click', function() {
	
		$('ul.tabs a.current').removeClass('current');
		$(this).addClass('current');
		
		active_id = 'div' + $(this).attr('href');
		
		$('div.tab_container div.current').slideUp(function() {
			$(this).removeClass('current');
			$(active_id).addClass('current');
			$(active_id).slideDown();
		});
		update_tab_arrows($(active_id));
		return false;
	});
	
	function update_tab_arrows(current_active_div) {
	
		// Hide or show arrows depending on if it exists
		if(current_active_div.prev().exists()) {
			$('a#prev_tab_arrow').show();
		} else {
			$('a#prev_tab_arrow').hide();
		}
		// Hide or show arrows depending on if it exists
		if(current_active_div.next().exists()) {
			$('a#next_tab_arrow').show();
		} else {
			$('a#next_tab_arrow').hide();
		}
		// Set current menu item
		$('ul.tabs a.current').removeClass('current');

		$('ul.tabs a').each(function() {
			if(('#'+current_active_div.attr('id')) == $(this).attr('href')) {
				$(this).addClass('current');
			}
		});
		
	}
	
	$('a#prev_tab_arrow').live('click', function() {

		// Confirm the next div exists
		if($('div.tab_container div.current').prev().exists()) {

			// Load new active div
			new_active_div = $('div#' + $('div.tab_container div.current').prev().attr('id'));
			// Deactivate current div, activate new one
			$('div.tab_container div.current').slideUp(function() {
				$(this).removeClass('current');
				new_active_div.addClass('current');
				new_active_div.slideDown();
			});
			update_tab_arrows(new_active_div);
			
		} // end if previous tab_content exists
	
	});
	
	$('a#next_tab_arrow').live('click', function() {

		// Confirm the next div exists
		if($('div.tab_container div.current').next().exists()) {	

			// Load new active div
			new_active_div = $('div#' + $('div.tab_container div.current').next().attr('id'));
			// Deactivate current div, activate new one
			$('div.tab_container div.current').slideUp(function() {
				$(this).removeClass('current');
				new_active_div.addClass('current');
				new_active_div.slideDown();
			});
			
			update_tab_arrows(new_active_div);
			
		} // end if next tab_content exists
	});
	


/* !Ajax Loads
------------------------------------------*/

	function update_work_block(new_portfolio_url, element_id) {
	
		// Load current div
		current_div = $(element_id);
		
		// Hide current portfolio
		// WAIT until it's hidden so that it doesn't load
		// halfway through - slows page load but only marginally
		// for the correct visual effect.
		current_div.fadeOut(100, function() {

			// Show loading gif animation (hello angelfire!)
			$('div#loading').show();
			
			// Turn off the current playing sound, if it exists
			$('.sc-player.playing .sc-pause').click();

			// Google Analytics AJAX tracking			
			_gaq.push(['_trackPageview', new_portfolio_url + ' ' + element_id],['_trackPageview', new_portfolio_url + ' ' + element_id]);

			// Load the data from the new page and update all data
			// WAIT until content is fully loaded to hide the rest.
			current_div.load(new_portfolio_url + ' ' + element_id + ' > *', function(response) {				
				
				if($('div#loading').exists()) {
					// Hide the loader and show the new content
					$('div#loading').delay(1000).fadeOut(100, function() {
						current_div.fadeIn(200);
					});
				} else {
					current_div.fadeIn(200);
				}
				
				reset_fancybox();
				
				// Reset jCarousel listener if we're loading a brand new thumbnail cycle
				if(element_id == "div#main_content" && $(response).find('div#thumbnail_cycle')) {
					$("div#thumbnail_cycle").jCarouselLite({
						btnNext: "#thumbnail_right_arrow",
						btnPrev: "#thumbnail_left_arrow",
						visible: 8,
						scroll: 2,
					});
				}

				
			});
		});
		
		return false;		
	}

	// Send it a new URL and the element ID so you can rip the 
	// new content and fill in the div with animation included.
	
	function update_data_block(new_portfolio_url, element_id) {

		// Load current div
		current_div = $(element_id);
		
		// Hide current portfolio
		// WAIT until it's hidden so that it doesn't load
		// halfway through - slows page load but only marginally
		// for the correct visual effect.
		current_div.slideUp(300, function() {

			// Show loading gif animation (hello angelfire!)
			$('div#loading').show();
			
			// Turn off the current playing sound, if it exists
			$('.sc-player.playing .sc-pause').click();

			// Google Analytics AJAX tracking
			_gaq.push(['_trackPageview', new_portfolio_url + ' ' + element_id],['_trackPageview', new_portfolio_url + ' ' + element_id]);

			// Load the data from the new page and update all data
			// WAIT until content is fully loaded to hide the rest.
			current_div.load(new_portfolio_url + ' ' + element_id + ' > *', function(response) {
				// Hide the loader and show the new content
				$('div#loading').hide();
				current_div.slideDown(1000);
				
				reset_fancybox();
								
				// Reset jCarousel listener if we're loading a brand new thumbnail cycle
				if(element_id == "div#main_content" && $(response).find('div#thumbnail_cycle')) {
					$("div#thumbnail_cycle").jCarouselLite({
						btnNext: "#thumbnail_right_arrow",
						btnPrev: "#thumbnail_left_arrow",
						visible: 8,
						scroll: 2,
					});
				}

				
			});
		});
		
		return false;		
	}
	
	// Portfolio thumbnail
	$('a.portfolio_thumbnail').live('click', function() {
		// Check if this is the click was the "current" portfolio
		if($(this).hasClass('current')) {
			return false;
		} else {
			// Not current so reset current and set new current
			$('a.portfolio_thumbnail.current').removeClass('current');	
			$(this).addClass('current');
			// Load and update with new portfolio
			return update_work_block($(this).attr('href'), 'div#portfolio');
		}
	});

	// Next and previous arrow buttons
	$('div#portfolio a.right_arrow').live('click', function() {
		return update_work_block($(this).attr('href'), 'div#portfolio');
	});
	$('div#portfolio a.left_arrow').live('click', function() {
		return update_work_block($(this).attr('href'), 'div#portfolio');
	});		

	// Home/subpage 
	
	// Portfolio category
	$('ul.sub_pages a').live('click', function() {
		// Check if this is the click was the "current" portfolio
		if($(this).hasClass('current')) {
			return false;
		} else {
			// Not current so reset current and set new current
			$('ul.sub_pages a.current').removeClass('current');	
			$(this).addClass('current');

			// Load and update with new portfolio
			return update_data_block($(this).attr('href'), 'div#main_content');
		}
	});   


});
