/*
 * Special event for image load events
 * Needed because some browsers does not trigger the event on cached images.

 * MIT License
 * Paul Irish     | @paul_irish | www.paulirish.com
 * Andree Hansson | @peolanha   | www.andreehansson.se
 * 2010.
 *
 * Usage:
 * $(images).bind('load', function (e) {
 *   // Do stuff on load
 * });
 * 
 * Note that you can bind the 'error' event on data uri images, this will trigger when
 * data uri images isn't supported.
 * 
 * Tested in:
 * FF2-3.6
 * IE6-8
 * Chromium4 Developer
 * Opera 9-10
 */ 
(function ($) {

$.event.special.load = {
	setup: function(data, namespaces, hollaback) {
		var retVal = false;
		
		if (this.tagName.toLowerCase() === 'img' && this.src !== "") {
			// Image is already complete, fire the hollaback (fixes browser issues were cached
			// images isn't triggering the load event)
			if (this.complete || this.readyState === 4) {
				$(this).bind('load', data || {}, hollaback).trigger('load');
				retVal = true;
			}
			
			// Check if data URI images is supported, fire 'error' event if not
			else if (this.readyState === 'uninitialized' && this.src.indexOf('data:') >= 0) {
				$(this).trigger('error');
				retVal = true;
			}
		}
		
		return retVal;
	}
}

}(jQuery));




// Innitiate Main Menu
$(document).ready(function() { 
	$('ul#menu').superfish(); 
}); 

//Homepage boxes
function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}
$('.equalize img').bind('load', function() {
	equalHeight($(".equalize"));
	equalHeight($(".equalize2"));
});
$(document).ready(function() {
	equalHeight($(".equalize"));
	equalHeight($(".equalize2"));
});


//News Scroller (Widget)
$(document).ready(function() {
 
 	$('.news-scroller').cycle({ 
	    fx: 'scrollVert',
		speed: 2000,
		rev: true,
		timeout: 15000,
		next: '.news-next', 
	    prev: '.news-previous'
	 });      
 
});


// Social Drop Down Panel 
$(document).ready(function() {
	$(".btn-slide").click( function() {
		if ($("#openCloseIdentifier").is(":hidden")) {
			$("#social").animate( {top: "0"} , 650 );
			$(this).addClass("active");
			$("#openCloseIdentifier").show();
		} else {
			$("#social").animate( {top: "-46px"} , 650 );
			$(this).removeClass("active");
			$("#openCloseIdentifier").hide();
		}
	});  
});


//Innititate Pretty Photo
$(document).ready(function(){
	$("a[rel^='lightbox']").prettyPhoto({theme:'light_rounded'});
});


//Contact Form
$(document).ready(function() {

	//Define URL to PHP mail file
	var url = "/cms/public/custom/sendmail.php"; // Modificato by Marco
	
	//Activate jQuery form validation
	$("#jaybich-contact").validate({
	
		submitHandler: function() {
		
			//Define data string
			var datastring = $("#jaybich-contact").serialize();
			
			//Submit form
			$.ajax({
				type: "POST",
				url: url,
				data: datastring,
				success: function(){
					$('#jaybich-contact').slideUp();
					$('#sent').fadeIn();
				}
			});
		}
	
	});
			
});

//Comments Form
$(document).ready(function() {
	
	//Activate jQuery form validation
	$(".comments-form").validate();
			
});

//Portfolio thumbnail
$(document).ready(function(){

	$('.portfolio-box a').hover(function() {
		
		//Show darkenned hover over thumbnail image
		$(this).find('img').stop(true, true).animate({opacity:0.5},400);

	}, function() {
		
		//Hide darkenned hover over thumbnail image
		$(this).find('img').stop(true, true).animate({opacity:1},400);
			
	});

});



//Cufon text
Cufon.replace('h1, h2, h3');



// Addedd by Marco - target blank nei credit
$(document).ready(function(){
	$('#credits a').attr('target', '_blank');
});


