/*
PrettyGreen Global JS
------------
All global functions
------------ 
*/


//  On doc ready 
$(function(){
	$.prettyGreen.init();
});

//  Global functions

jQuery.prettyGreen = {
	
	init: function()  {
		$.prettyGreen.hitAreas();
		$.prettyGreen.matchHeight();
		$.prettyGreen.prepPagination();
		$.prettyGreen.prepTeam();
		$.prettyGreen.formPlaceholder();
	},
	
	prepPagination: function() {
		$.prettyGreen.num_results = $('ol#screenList li').size();
		if($.prettyGreen.num_results > 1) {
			$.prettyGreen.per_page = 1;
			$.prettyGreen.showHideResults(0,null);
			$("#screenNav").pagination($.prettyGreen.num_results, {
				prev_show_always:false,
				next_show_always:false,
				prev_text:"&lt;",
				next_text:"&gt;",
				items_per_page:$.prettyGreen.per_page,
				num_edge_entries:0,
				num_display_entries:5,
				callback:$.prettyGreen.showHideResults
			});
		}
	},
	
	/* Prepare team page functionality */
	prepTeam: function() {
		$('ul.team li').each(function(){
			$(this).children().hide();
			$(this).children('img.avatar').show();
			$(this).children('h2').show().bind('click',function(){
				$('ul.team li').removeClass('selected').children('h2').css({'color' : 'rgb(77,77,77)', 'border-color' : 'rgb(77,77,77)'});
				$('.navIntro').html($(this).parent().html()).children().show();
				$('.navIntro img.avatar').hide();
				$(this).parent().addClass('selected');
			});
			$(this).bind('click',function(){
				$('ul.team li').removeClass('selected').children('h2').css({'color' : 'rgb(77,77,77)', 'border-color' : 'rgb(77,77,77)'});
				$('.navIntro').html($(this).html()).children().fadeIn();
				$('.navIntro img.avatar').hide();
				$(this).addClass('selected').children('h2').css({'color' : 'rgb(277,87,32)', 'border-color' : 'rgb(277,87,32)'});
			});
			$(this).hover(
				function(){
					$(this).children('h2').css({'color' : 'rgb(277,87,32)', 'border-color' : 'rgb(277,87,32)'})
				},
				function(){
					if($(this).hasClass('selected') == false){
						$(this).children('h2').css({'color' : 'rgb(77,77,77)', 'border-color' : 'rgb(77,77,77)'})
					}
				}
			);
		});
		$('ul.team li:first').click();
	},
	
	/* Match height of elements */
	matchHeight: function() {
		if($('.matchHeight').size() > 0) {
			var maxHeight = '0';
			$('.matchHeight').each(function(){
				maxHeight = ($(this).height() > maxHeight) ? $(this).height() : maxHeight;
			});
			$('.matchHeight').height(maxHeight);
		}
	},

	/* Create large clickable areas */
	hitAreas: function() {
		$('.hitArea').hover(
			function(){
				$(this).css('cursor','pointer');
			},function(){}
		);

		$('.hitArea').bind('click',function(){
			var hitlink = $(this).find('a.hitLink:first').attr('href');
			if(hitlink){
				window.location = hitlink;
			}
			else{
				window.location = $(this).find('a:first').attr('href');
			}
		});
	},
	
	/* Pagination */
	per_page: 5,
	num_results: 0,

	showHideResults: function(page_id,jq) {
		var min = (page_id * $.prettyGreen.per_page)-1;
		if((min + jQuery.per_page) <= $.prettyGreen.num_results) {
			var max = min + $.prettyGreen.per_page;
		} else {
			var max = $.prettyGreen.num_results - 1;
		};
		$('ol#screenList li').hide();
		if (min < 0) {
			$('ol#screenList li:eq(0)').show();
			$('ol#screenList li:gt(0)').show();
		} else {
			$('ol#screenList li:gt('+min+')').show();
		}
		$('ol#screenList li:gt('+max+')').hide();

		if (page_id == 0) {
			$('#text-original:hidden').fadeIn();
			$('#text-replace').hide();
		} else if (page_id > 0) {
			$('#text-original').hide();
			$('#text-replace').html( $('#image-text-'+page_id).html() ).fadeIn();
		}

		return false;
	},
	
	/* Form placeholder */
	formPlaceholder: function(){
		$initialVal = $('#ityujt-ityujt').val();
		if ( $initialVal.length == 0 )
		{
			$('#ityujt-ityujt').val('enter your email address').addClass('empty');
		}
		$('#ityujt-ityujt').focus(function(){
			if ( $(this).hasClass('empty') )
			{
				$(this).removeClass('empty').val('');
			}
		}).blur(function(){
			if ( $(this).val().length == 0 )
			{
				$(this).val('enter your email address').addClass('empty');
			}
		});
		
	}

}