/*
I&A Intelligence & Art template: Custom Javascript
Author: BCProducties 
*/

jQuery.noConflict();


jQuery(document).ready(function() {

	//Initialise Cufon
	Cufon.replace('h1, h2, h3, h4', { hover : 'true' });
	
	//Initialise Dropdown menu
	jQuery('#navigation').superfish({
		animation:   {opacity:'show',height:'show'},
	});
	
	//Initialise Frontpage Slider	
	jQuery('#featured li img, .caption').css({ opacity : 0 });

	var cyclerLoaded = false;
	var numberOfPreloaded = 0;
	
	jQuery('#featured li img').each(function()
  	{
    	// Get image instance.
	    var image = new Image();
	    image.src = jQuery(this).attr('src');

	    if (image.complete) {       
	      imageLoaded();        
	    } else {       
	      image.onload = imageLoaded;
		}
  	});
	
	function imageLoaded()
	{      
	  numberOfPreloaded++;
	
	  if (numberOfPreloaded >= jQuery('#featured li img').length && !cyclerLoaded)
	  {
	    jQuery('#featured').css({ background : 'none' }).cycle({ pager : "#featured-nav",
																		 pagerAnchorBuilder: function(idx, slide) { 
																							return '<li><a href="#">' + idx + '</a></li>'; 
																							}
																		});
	
		jQuery('#featured li img, .caption').animate({ opacity : 1 }, 1200);
	    cyclerLoaded = true;
	  }
	} // End Frontpage Slider Init
	
	// Preload images in #main area
	jQuery('#main').bc_preloader();
	
	
	// Init jQuery accordion widgets
	jQuery(".acc_widget").accordion({
		autoheight: false,
		header: 'h5'
	});
	
	// Init scrollToTop links
	jQuery(".top").click(function () {
		jQuery('html:not(:animated),body:not(:animated)').animate({scrollTop:0}, 'slow')
	});
	
	init_close();
	portfolio_margins();
	portfolio_titles();
	
	// Init Contact Form
	jQuery.validator.setDefaults({
		submitHandler: function() {

			jQuery('#contactForm').append('<div id="loader" style="margin:10px"></div>');
			jQuery('#loader').empty().append("<img src='img/loader_small.gif' style='border:none' />");

			var name = jQuery("#name").val();  
			var email = jQuery("#email").val(); 
			var message = jQuery("#message").val(); 

			var dataString = 'name='+ name + '&email=' + email + '&message=' + message;  

				jQuery.ajax({

				url: "mail.php",
				data: dataString,
				type: "GET",
				success: function(responseText) {
					jQuery('#loader').empty()
						jQuery('#contactForm').append('<h3>'+ responseText + '</h3>');

						}
				});
		}
	});

	jQuery("#contactForm").validate();
	
});


function init_close() {
	var $close = jQuery('<a href="#">X</a>').css({ float : 'right', lineHeight : '18px' }).prependTo('.info, .alert, .error');
	$close.click(function() {
		jQuery(this).parent().fadeOut(900, function() {
			jQuery(this).remove();
		});
		
		return false;
		
	});
}

function portfolio_margins() {
	jQuery('.portfolio .item:nth-child(3n)').addClass('last');
}

function portfolio_titles() {
	jQuery('.portfolio .item').hover(function() {
		jQuery(this).find('h5').fadeIn();
	}, function() {
		jQuery(this).find('h5').fadeOut();
	});
} 

//
// Custom jQuery Image preloader created by BCProducties
//

(function($)
{
	$.fn.bc_preloader = function(options) 
	{
		var defaults = 
		{
			fadeInSpeed: 1200
		};
		
		var options = $.extend(defaults, options);
		
		return this.each(function()
		{			
				var $imgs = $(this).find('img'),
					imgsLoaded = false,
					numberOfPreloaded = 0,
					num = $imgs.length;
				
				$imgs.css({ opacity : 0 });
				
				if (num > 0) {
					var $loading_images = $('<div class="loading_images"></div>').text('Loading images...');
					$('#container').append($loading_images);
				}			
								
				methods =	{	
					
					imageLoaded: function()
					{	
						numberOfPreloaded++;

						  if (numberOfPreloaded >= num && !imgsLoaded)
						  {
							$imgs.animate({ opacity : 1 }, options.fadeInSpeed, '', function() {
								$loading_images.fadeOut('fast', function() {
									$(this).remove();
								});
							});
							
						    imgsLoaded = true;
						  }
					},
					
					init: function()
					{	
						$imgs.each(function()
					  	{
					    	// Get image instance.
						    var image = new Image();
						    image.src = jQuery(this).attr('src');

						    if (image.complete) {       
						      methods.imageLoaded();        
						    } else {       
						      image.onload = methods.imageLoaded;
							}
					  	});
					}
						
				};
				
				methods.init();
		});
		
	}
})(jQuery);
	




