/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 	*
 * Newby Website Javascript        	 						*
 * @Copyright	-	Artsgraphica     	 						*
 * @Author		-	Dan, Mark, Yaqub 	 						*
 * @Version		-	1.04				 						*
 * 
 * @Changes		-	{
 * 
 * 1.04 - Comments animation code (just for dan)
 * 1.03	- Added Animation code
 * 1.02	- Adding code to pickup combined dropdown menu
 * 1.01 - Tidied up jQuery
 * 1.00 - First version drafting
 * }
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */

/**
 * Enables PNG Transparency
 * 
 * @return
 */
$(function(){
	// Setup supersleight on the body
	$('body').supersleight({shim: '/images/page/blank.gif'});
});


/* HOME PAGE ANIMATION SETUP */

$(function(){
	// Run the animate opening when the dom is ready
	//animateOpening();
	searchFieldHandler();
});




/* END OF HOME PAGE ANIMATION SETUP */


/* TOP NAVIGATION ENABLER */

/**
 * Enables the pulldown top nav on dom ready
 */
$(function(){
	// Create the configuration for the sub nav hoverIntent
	var config = {    
		     over: openSubNav, 	// function = onMouseOver  
		     timeout: 300, 		// number = milliseconds delay before onMouseOut    
		     out: closeSubNav 	// function = onMouseOut    
		};
	// Now apply these to the topnav li's
	$('#topnav li.nav-company, #topnav li.nav-services, #topnav li.nav-sectors').hoverIntent(config);
});

/**
 * CALLBACK FUNCTION
 * Function to open a subnav
 */
function openSubNav(){
	// Find the first a and make 'active' so that is displays a hovered background
	$(this).find('a:first').addClass('active');
	// Fade in the ul child fast
	$(this).find('ul').fadeIn('fast');
	// If it's sectors menu lets hide the service ref thing
	if($(this).hasClass('nav-sectors')){
		$('#serviceRef').css({"visibility":"hidden"});
	}
}

/**
 * CALLBACK FUNCTION
 * Function to close a subnav
 */
function closeSubNav(){
	// Now turn off the active class on the first a
	$(this).find('a:first').removeClass('active');
	// And fade out the ul child
	$(this).find('ul').fadeOut('fast');
	// If it's sectors menu lets show the service ref thing
	if($(this).hasClass('nav-sectors')){
		$('#serviceRef').css({"visibility":"visible"});
	}
}

/* END OF TOP NAVIGATION ENABLER */

/**
 * ONLOAD
 * Run functions necessary
 */
$(function(){
	enableDates();
	enableSectors();
	enableServices();
	enableCombined();
	enableNews();
	enableEmailForm();
	enableRelated();
});

/**
 * Enabled the project by date dropdown actions and page numbers
 */
function enableDates(){
	// Set a bunch of options so we have a pre-send function and a post-send action
	var options = {
			target: 		'#projectsByDate', 		// This is where we want the ajax.php result to end up
			beforeSubmit: 	showDateRequest,		// Before the form fires we need to validate with a function
			success: 		showDateResponse,		// Once it's fired we need to add animation handlers
			clearForm: 		false,					// We are not clearing a form because we're firing on dropdowns
			url: 			'/pages/ajax/ajax.php'	// This is the url of the ajax.php page (might .htaccess this later)
		};
	// Now add the ajaxForm handlers with the options to the form
	$('#project-by-date-form').ajaxForm(options);
	// Dropdown
	$('#fromDate').change(function(){
		$('#datePageNumber').val(1);
		$('#project-by-date-form').submit();
	});
	// Next Button
	$('.date-next').click(function(){
		$('#datePageNumber').val(1*$('#datePageNumber').val()+1);
		$('#project-by-date-form').submit();
	});
	// Previous Button
	$('.date-previous').click(function(){
		$('#datePageNumber').val(1*$('#datePageNumber').val()-1);
		$('#project-by-date-form').submit();
	});
}

/**
 * Date Change pre-send callback (here for debugging)
 * @param formData
 * @param jqForm
 * @param options
 * @return
 */
function showDateRequest(formData, jqForm, options){
	var queryString = $.param(formData);
	// Debug code // Remember to turn this off
	// alert ('About to submit: \n\n' + queryString);
	return true;
}

/**
 * Date post-callback
 * @param responseText
 * @param statusText
 * @return
 */
function showDateResponse(responseText, statusText){
	enableDates();
	enableProjectPreviews();
}

/**
 * Enable sector dropdown
 */
function enableSectors(){
	// Set a bunch of options so we have a pre-send function and a post-send action
	var options = {
			target: 		'#projectsBySector', 	// This is where we want the ajax.php result to end up
			beforeSubmit: 	showSectorRequest,		// Before the form fires we need to validate with a function
			success: 		showSectorResponse,		// Once it's fired we need to add animation handlers
			clearForm: 		false,					// We are not clearing a form because we're firing on dropdowns
			url: 			'/pages/ajax/ajax.php'	// This is the url of the ajax.php page (might .htaccess this later)
		};
	// Now add the ajaxForm handlers with the options to the form
	$('#project-by-sector-form').ajaxForm(options);
	// Dropdown
	$('#sectorRef').change(function(){
		$('#sectorPageNumber').val(1);
		$('#project-by-sector-form').submit();
	});
	// Next Button
	$('.sector-next').click(function(){
		$('#sectorPageNumber').val(1*$('#sectorPageNumber').val()+1);
		$('#project-by-sector-form').submit();
	});
	// Previous Button
	$('.sector-previous').click(function(){
		$('#sectorPageNumber').val(1*$('#sectorPageNumber').val()-1);
		$('#project-by-sector-form').submit();
	});
}

/**
 * Sector Pre-send callback (debugging)
 * @param formData
 * @param jqForm
 * @param options
 * @return
 */
function showSectorRequest(formData, jqForm, options){
	var queryString = $.param(formData);
	// Debug code // Remember to turn this off
	// alert ('About to submit: \n\n' + queryString);
	return true;
}

/**
 * Sector post-callback
 * @param responseText
 * @param statusText
 * @return
 */
function showSectorResponse(responseText, statusText){
	enableSectors();
	enableProjectPreviews();
}

function enableRelated(){
	
	// Setup a new get
	$('#relatedProjectsDropdown').live("change",function(){
		
		// Get and store our value
		var $serviceRef = $('#relatedProjectsDropdown option:selected').val();
		var $projectRef = $('#projectReference').val();
		// We have changed so we need to go get the new data
		$.get("/pages/ajax/ajax.php", {ajax : "relatedProjectByService", serviceRef:$serviceRef, projectRef:$projectRef},function(data){
			// replace the current div with the data
			$('div.relatedProjects-panel').replaceWith(data);
		});
		
	});
	
}

/**
 * Enable sector dropdown
 */
function enableServices(){
	// Set a bunch of options so we have a pre-send function and a post-send action
	var options = {
			target: 		'#projectsByService', 	// This is where we want the ajax.php result to end up
			replaceTarget:	true,
			beforeSubmit: 	showServiceRequest,		// Before the form fires we need to validate with a function
			success: 		showServiceResponse,		// Once it's fired we need to add animation handlers
			clearForm: 		false,					// We are not clearing a form because we're firing on dropdowns
			url: 			'/pages/ajax/ajax.php'	// This is the url of the ajax.php page (might .htaccess this later)
		};
	// Now add the ajaxForm handlers with the options to the form
	$('#project-by-service-form').ajaxForm(options);
	// Dropdown
	$('#serviceRef').change(function(){
		$('#servicePageNumber').val(1);
		$('#project-by-service-form').submit();
	});
	// Next Button
	$('.service-next').click(function(){
		$('#servicePageNumber').val(1*$('#servicePageNumber').val()+1);
		$('#project-by-service-form').submit();
	});
	// Previous Button
	$('.service-previous').click(function(){
		$('#servicePageNumber').val(1*$('#servicePageNumber').val()-1);
		$('#project-by-service-form').submit();
	});
}

/**
 * Sector Pre-send callback (debugging)
 * @param formData
 * @param jqForm
 * @param options
 * @return
 */
function showServiceRequest(formData, jqForm, options){
	var queryString = $.param(formData);
	// Debug code // Remember to turn this off
	// alert ('About to submit: \n\n' + queryString);
	return true;
}

/**
 * Sector post-callback
 * @param responseText
 * @param statusText
 * @return
 */
function showServiceResponse(responseText, statusText){
	enableServices();
	enableProjectPreviews();
}

/**
 * Enable sector dropdown
 */
function enableCombined(){
	// Set a bunch of options so we have a pre-send function and a post-send action
	var options = {
			target: 		'#combinedProjects', 	// This is where we want the ajax.php result to end up
			replaceTarget:	true,
			beforeSubmit: 	showCombinedRequest,	// Before the form fires we need to validate with a function
			success: 		showCombinedResponse,	// Once it's fired we need to add animation handlers
			clearForm: 		false,					// We are not clearing a form because we're firing on dropdowns
			url: 			'/pages/ajax/ajax.php'	// This is the url of the ajax.php page (might .htaccess this later)
		};
	// Now add the ajaxForm handlers with the options to the form
	$('#combined-filter-form').ajaxForm(options);
	// Dropdown
	$('#reference').change(function(){
		$('#pageNumber').val(1);
		$('#combined-filter-form').submit();
	});
	// Next Button
	$('.combined-next').click(function(){
		$('#pageNumber').val(1*$('#pageNumber').val()+1);
		$('#combined-filter-form').submit();
	});
	// Previous Button
	$('.combined-previous').click(function(){
		$('#pageNumber').val(1*$('#pageNumber').val()-1);
		$('#combined-filter-form').submit();
	});
}

/**
 * Sector Pre-send callback (debugging)
 * @param formData
 * @param jqForm
 * @param options
 * @return
 */
function showCombinedRequest(formData, jqForm, options){
	var queryString = $.param(formData);
	// Debug code // Remember to turn this off
	// alert ('About to submit: \n\n' + queryString);
	return true;
}

/**
 * Sector post-callback
 * @param responseText
 * @param statusText
 * @return
 */
function showCombinedResponse(responseText, statusText){
	enableCombined();
	enableProjectPreviews();
}

/**
 * Enable sector dropdown
 */
function enableNews(){
	// Set a bunch of options so we have a pre-send function and a post-send action
	var options = {
			target: 		'#newsWrapper', 	// This is where we want the ajax.php result to end up
			beforeSubmit: 	showNewsRequest,	// Before the form fires we need to validate with a function
			success: 		showNewsResponse,	// Once it's fired we need to add animation handlers
			clearForm: 		false,					// We are not clearing a form because we're firing on dropdowns
			url: 			'/pages/ajax/ajax.php'	// This is the url of the ajax.php page (might .htaccess this later)
		};
	// Now add the ajaxForm handlers with the options to the form
	$('#news-form').ajaxForm(options);

	// Next Button
	$('.news-next').click(function(){
		$('#newsPageNumber').val(1*$('#newsPageNumber').val()+1);
		$('#news-form').submit();
	});
	// Previous Button
	$('.news-previous').click(function(){
		$('#newsPageNumber').val(1*$('#newsPageNumber').val()-1);
		$('#news-form').submit();
	});
}

/**
 * Sector Pre-send callback (debugging)
 * @param formData
 * @param jqForm
 * @param options
 * @return
 */
function showNewsRequest(formData, jqForm, options){
	var queryString = $.param(formData);
	// Debug code // Remember to turn this off
	// alert ('About to submit: \n\n' + queryString);
	return true;
}

/**
 * Sector post-callback
 * @param responseText
 * @param statusText
 * @return
 */
function showNewsResponse(responseText, statusText){
	enableNews();
	enableProjectPreviews();
}


/**
 * Enables the emailForm if present on a page
 */
function enableEmailForm(){
	// When we click the email to friend button
	$('#emailToFriend').click(function(){
		// Unbind the click
		$(this).unbind('click');
		// Set ajaxForm options
		var options = {
				target: 		'#email-form', 			// This is where we want the ajax.php result to end up
				beforeSubmit: 	showEmailRequest,		// Before the form fires we need to validate with a function
				success: 		showEmailResponse,		// Once it's fired we need to add animation handlers
				clearForm: 		false,					// We are not clearing a form because we're firing on dropdowns
				url: 			'/pages/ajax/ajax.php'	// This is the url of the ajax.php page (might .htaccess this later)
			};
		// Apply to form
		$('#emailToFriendForm').ajaxForm(options);
		// And slide down the email form
		$('#email-form').slideDown('fast');
		// Add a handler to the cancel button
		$('#cancelbutton').click(function(){
			$(this).unbind('click'); 					// Unbind this click
			$('#email-form').slideUp('fast');			// Animate
			enableEmailForm();							// Re-run parent function
			return false;								// Return false
		});
	});
}

/**
 * Pre-send Validator for email form
 * @param formData
 * @param jqForm
 * @param options
 * @returns {Boolean}
 */
function showEmailRequest(formData, jqForm, options){
	// Run some basic validation
	if($('#name').val() == ""){
		alert("Please enter your name");
		return false;
	}
	if($('#emailAddress').val() == ""){
		alert("Please enter the email address of the person you wish to send this news story to.");
		return false;
	}
}

/**
 * Post-send callback
 * @param responseText
 * @param statusText
 */
function showEmailResponse(responseText, statusText){
	// Animate close the form
	$('#email-form').delay('2000').slideUp('slow');
}

/**
 * Function to create the 'modal' windows for the projects
 */
$(function(){
	enableProjectPreviews();
});

/**
 * Enable the project preview modal windows
 */
function enableProjectPreviews(){
	// Create the options
	var config = {    
		     over: showProjectPreview, 	// function = onMouseOver  
		     timeout: 300, 				// number = milliseconds delay before onMouseOut    
		     out: nothing 				// function = onMouseOut    
		};
	// Apply to the image rollovers
	//$('.small-feature-image').hoverIntent(config);
	
	var config = {    
		     over: nothing, 	// function = onMouseOver  
		     timeout: 300, 		// number = milliseconds delay before onMouseOut    
		     out: fadeMeOut 	// function = onMouseOut 
		};
	// Add the hoverIntent
	//$('.project_summary').hoverIntent(config);
	
//	$('.project_close').click(function(){
//		$(this).parent().fadeOut('250');
//		return false;
//	});
}

/**
 * This finds the parents first project small div
 * then finds it's child project summary
 * then fades it in over 250ms
 */
function showProjectPreview(){
	// Fade out all currently open previews
//	$('.project_summary').fadeOut('100');
//	// This, parents .project_small's child .project_summary animation (Not to make that sound too complicated)
//	$(this).parents('.project_small')
//		   .find('.project_summary')
//		   .fadeIn('250');
}

/**
 * This 'ere funkshun do-be-do nuthin
 */
function nothing(){
	// This is hear so the hoverout of the image does nothing
	// and so the hover in of the modal does nothing
}

/**
 * This nicely fades whatever is passed to it out nicely over 250ms
 * (can be re-used in whatever)
 */
function fadeMeOut(){
	// Whatever this is, fade it out over 250ms!
	$(this).fadeOut('250');
}

/**
 * Enable the hover states of the footer images in a nice ie friendly way!
 * UDPATE:09/11/2010 - Which now validates! HA!
 */
$(function(){
	$('.jsAccred').hover(function(){ 	// Swap rel and src
		var rel = $(this).parent().attr('rel');
		var src = $(this).attr('src');
		$(this).parent().attr('rel',src);
		$(this).attr('src',rel);
	},function(){						// Swap them back
		var rel = $(this).parent().attr('rel');
		var src = $(this).attr('src');
		$(this).parent().attr('rel',src);
		$(this).attr('src',rel);
	});
});

$(function(){
	// Apply Lightbox to all a.lightbox objects
	//$('a.lightbox').lightbox();
});

$(function(){
	$('.gallery_image').click(function(){
		var image = $(this).attr('href');
		$('#large_image').attr('src',image);
		// Override default behaiviour
		return false;
	});
});

/**
 * This enables the small project hovers from the right hand Side-Nav
 */
$(function(){
	// Set a bunch of options for the hoverIntent
	var options = {
		over:openSmallProject,
		out:nothing,
		timeout:300,
		interval:200
	};
	// Apply these to the small project holder items
	$('.jsSmallProjectHolder').hoverIntent(options);
	
	$('.project_close_button').click(function(){
		$(this).parent().hide('250', function(){
			// Set the css to be the same size and placement as it should be when it starts
			$(this).css({right:0,width:220});
			$(this).unbind('mouseover');
			$(this).find('.jsProjectItemContent').hide();
		});
		return false;
	});
});

/**
 * Open small project item
 * 
 */
function openSmallProject(){
	var item = $(this).find('.jsSmallProjectItem');
	// Hide all others
	$('.jsSmallProjectItem').not(item).hide('250',function(){
		// Set the css to be the same size and placement as it should be when it starts
		$(this).css({right:0,width:220});
		$(this).unbind('mouseover');
		$(this).find('.jsProjectItemContent').hide();
	});
	
	// Set a bunch of options for the hoverIntent
	var options = {
		over:stopClosing,
		out:nothing,
		timeout:300,
		interval:200
	};
	// First animate open the small projectItem and then add the new hover bit for it
	$(this)	// Begin stack
	// Find it's content item
	.find('.jsSmallProjectItem')
	// Animate it and callback
	.animate({opacity: "show", right:221, width:482}, 250, function(){
		// now animate the content!
		$(this)
		.find('.jsProjectItemContent')
		.slideDown('250');
	});// .hoverIntent(options);
	// Going to turn the hover off for this to stop crazy animation stuff happening
}

/**
 * Hide a project preview
 */
function closeSmallProject(){
	// Hide all others
	$('.jsSmallProjectItem').hide('250', function(){
		// Set the css to be the same size and placement as it should be when it starts
		$(this).css({right:0,width:220});
		$(this).unbind('mouseover');
		$(this).find('.jsProjectItemContent').hide()
	});
}

function stopClosing(){
	$(this).stop();
}

/**
 * Function to create the 'modal' windows for the projects
 */
$(function(){
	enableHomepageProjectPreviews();
});

/**
 * Enable the project preview modal windows
 */
function enableHomepageProjectPreviews(){
	// Create the options
	var config = {    
		     over: showHomepageProjectPreview, 	// function = onMouseOver  
		     timeout: 300, 						// number = milliseconds delay before onMouseOut    
		     out: nothing 						// function = onMouseOut    
		};
	// Apply to the image rollovers
	$('.small-homepage-feature-image').hoverIntent(config);
	
	var config = {    
		     over: nothing, 	// function = onMouseOver  
		     timeout: 3000, 	// number = milliseconds delay before onMouseOut    
		     out: fadeMeOut 	// function = onMouseOut 
		};
	// Add the hoverIntent
	$('.homepage_project_summary').hoverIntent(config);
	
	$('.project_close').click(function(){
		$(this).parent().fadeOut('250');
		return false;
	});
}

/**
 * This finds the parents first project small div
 * then finds it's child project summary
 * then fades it in over 250ms
 */
function showHomepageProjectPreview(){
	// Fade out all currently open previews
	$('.homepage_project_summary').fadeOut('100');
	// This, parents .project_small's child .project_summary animation (Not to make that sound too complicated)
	$(this).parents('.homepage_project')
		   .find('.homepage_project_summary')
		   .fadeIn('250');
}


function searchFieldHandler(){
	$('input#search').focus(function(){
		if($(this).val()=="Search Projects")
		{
			$(this).val("");
		}	
	});
	
	$('input#search').blur(function(){
		if($(this).val()=="")
		{
			$(this).val("Search Projects");
		}	
	});
}

// Add new function to pickup the homepage click events
$(function(){
	$('ul.tabs li a').click(function(){
		$('ul.tabs li a').removeClass('selected');
		$(this).addClass('selected');
		$.get("/ajax-banner/",{section:$(this).attr('id')}, function(data){
			if(data!=""){
				$('.homepage-banner').append(data);
				$('.banner-content1').delay('500').fadeOut('1000',function(){
					$('.banner-content1').detach();
					$('.banner-content2').attr('class','banner-content1');
				});
			}
		});
		
		return false;
	});
});
