// JavaScript Document
// global vars
var projectsPerPage = 6;
var projectsTotalPages = 1;
var projectsCurrPage = 1;
var f;
var stDomain = window.location.hostname;

// function to remove all vimeo content
function reset_videos()
{
	// search for the video iframes
	// and empty contents then fade in posters
	$('iframe').each(function(index) {
		$(this).parent().parent().find('.media-vimeo-poster').fadeIn(500);	
		$(this).parent().empty();					
	});
}


// user interface blocking function - used between loads/ajax requests
var blockUI = function()
{
	$('#footer').hide();
	$('#ui-blocker').stop(true, true).delay(600).fadeTo('slow', 1);	
}

var unblockUI = function()
{
	$('#ui-blocker').stop(true, true).fadeTo('slow', 0, function(){
																 $(this).hide();	
																 $('#footer').fadeIn(600);
																 });		
}

// function which loads and handles the folder sections
var folder_loader = function(urlTitle)
{	
	blockUI();
	$("#content").load('/index.php/folder_loader/'+urlTitle, function() 
	{
		// add click actions for projects		
		$('.project-media-toggle').hover(
						  function () {
							$(this).addClass('project-media-toggle-ov');
						  }, 
						  function () {
							$(this).removeClass('project-media-toggle-ov');
						  }
						).click(function(){media_click($(this));});
		
		// add click action for similar
		$('.similar-nav-item').click(function(){similar_click($(this));});		
		
		// init pages
		var numProjects = $('#content .project').length;					
		projectsTotalPages = Math.ceil( numProjects / projectsPerPage);		
		projectsCurrPage = 1;	
		//do we need to page
		if(projectsTotalPages > 1 )
		{	
			// pagination links
			paginationLinks = '<ul id="page-links">';
			for(i=1; i < projectsTotalPages+1; i++)
			{				
				if(i > 1)
				{
					paginationLinks = paginationLinks + '<li class="add-border">' + i + '</li>';
				}
				else
				{
					paginationLinks = paginationLinks + '<li class="active">' + i + '</li>';
				}
			}
			paginationLinks = paginationLinks + '</ul>';
			
			$('#content').append(paginationLinks);			
			  // add page actions
			  $('#page-links li').bind({
			  click: function() {
				// do something on click
				$('#page-links li').removeClass('active');
				$(this).addClass('active');	
				pageNumber = $("#page-links li").index(this);
				$('#content').fadeOut(200, function(){
													// stop any video content
													reset_videos();
													// change page
													page_swicther(pageNumber);
													});				
			  },
			  mouseenter: function() {
				$(this).addClass('hover');				
			  },
			  mouseleave: function() {
				$(this).removeClass('hover');
			  },
			});	
			
		}
		
		$('.media-vimeo-play-img').hover(
				  function () {
					$(this).attr("src",'/themes/site_themes/scott_trindle/assets/images/vimeo_play_ov.png');	
				  }, 
				  function () {
					$(this).attr("src",'/themes/site_themes/scott_trindle/assets/images/vimeo_play.png');
				  }
				);
				
				
		$('.media-vimeo-poster').click(function(){
				//stop any current video content
				reset_videos();			
				
				// init new player
				var vimeo_id = $(this).attr('id');
				
				var vimeo_html = '<iframe src="http://player.vimeo.com/video/'+vimeo_id+'?api=1&amp;player_id=player_1&amp;autoplay=1" width="789" height="503" frameborder="0" id="player_1"></iframe>';	
				$(this).parent().find('.media-vimeo-player').append(vimeo_html);
				$(this).fadeOut(500);		
		});	
					
		// start folder/section transition - show first page  
	    page_swicther(0);		
		
		// re-activate interface
		unblockUI();
	});
};

// page transitions for pagination links
var page_swicther = function(pageNumber) 
{
	// stop any video content
	reset_videos();

	// reset page top
	$('html, body').animate({scrollTop: 0}, 0);
	
	// do pages
	pageStart = pageNumber * projectsPerPage;
	pageEnd = pageStart + projectsPerPage;	
	
	// decide which projects we need to show
	$('#content .project').each(function(index)
	{																			 
		 if(index >= pageStart && index < pageEnd )
		 {
			 $(this).show();
		 }
		 else
		 {
			  $(this).hide();
		 }
	});
	
	// fade in page
	$('#content').fadeIn(800);				
};

// function used to load a site page
var page_loader = function(pageID) 
{
	blockUI();
	$("#content").load('/index.php/page_loader/'+pageID, function() 
	{
		unblockUI(); 
		$('#content').fadeIn(800);	 		  
	});
};

// function used to display project image when the more buttonis clicked
var show_project_images = function(clicked_id) 
{	
	// set the media offset so project main images aren't reloaded here										
	var media_offset = $("#project-main-"+clicked_id+ " > li").size(); 		
	$("#project-media-"+clicked_id).addClass('active-media');	
	// hide the media ul before we proceed just incase 
	$("#project-media-"+clicked_id).empty();
	$("#project-media-"+clicked_id).hide();
	$("#project-media-"+clicked_id).load('/index.php/image_loader/'+media_offset+'/'+clicked_id, 
										function(){																					
											unblockUI();
											$("#"+clicked_id+" .project-media-toggle").html('Close').addClass('project-media-toggle-close');
											var slideSpeed = 800;
											$("#project-media-"+clicked_id).slideDown({
																					duration:	slideSpeed,
																					easing:		"easeInExpo",
																					complete:	function(){																		
																					}
																					});
										});
}

// function used to display projects when the similar button is clicked
var similar_click = function(similarLink) 
{	
	//removed ajax naviagtion actions								   
}

// button action for more links
var media_click = function(projectMedia) 
{
	  // pause any video content
	  reset_videos();	  
	  	  
	  var clicked_id = projectMedia.parent().parent().attr('id');  //get the clicked id
	  
	  // check if this is the same media box as what was clicked
	  if($("#project-media-"+clicked_id).hasClass('active-media'))
	  {
		// reset scroll
		var p = $('#'+clicked_id);
		var position = p.position();
		var offset = p.offset();
		var scrollPos = offset.top - 30; 		
		
		var slideSpeed = 800 + (200 * $("#project-media-"+clicked_id+' li').length);		
		if(slideSpeed > 3000) {slideSpeed = 3000;}

		$('html, body').animate({scrollTop: scrollPos},  slideSpeed, "easeOutExpo");
		  
		// if so close this box and re-show more button
		$("#"+clicked_id+" .project-media-toggle").html('More').removeClass('project-media-toggle-close');		
		$("#project-media-"+clicked_id).removeClass('active-media');
		$("#project-media-"+clicked_id).slideUp({
												duration:	slideSpeed,
												easing:		"easeOutExpo",
												complete:	function(){
																		$("#project-media-"+clicked_id).empty();
																	}
												});
	  }
	  else
	  { 
		blockUI();	
		show_project_images(clicked_id);
	  }
	
};


$(document).ready(function(){
	//removed ajax naviagtion actions
	});
