//***************************** Lightbox *****************************//


jQuery(document).ready(function(){
	jQuery("a[rel^='prettyPhoto']").prettyPhoto({
		theme: "facebook"
	});
});


//***************************** Menu *****************************//


function mainmenu(){
	jQuery("#nav ul li a").removeAttr("title");
	jQuery("#nav ul a").removeAttr("title");
	jQuery("#nav ul ul ").css({display: "none"}); // Opera Fix
	jQuery("#nav ul li").hover(function(){
		jQuery(this).find('ul:first').css({visibility: "visible",display: "none"}).show(400);
		},function(){
		jQuery(this).find('ul:first').css({visibility: "hidden"});
		});
}
 
jQuery(document).ready(function(){					
	mainmenu();
});


//***************************** Tabs *****************************//


jQuery(document).ready(function(){
	// We can use this object to reference the panels container
	var panelContainer = jQuery('div#panels');
	// Create a DIV for the tabs and insert it before the panel container
	jQuery('<div id="tabs"></div>').insertBefore(panelContainer);
	
	// Find panel names and create nav
	// -- Loop through each panel
	panelContainer.find('div.panel').each(function(n){
		// For each panel, create a tab
		jQuery('div#tabs').append('<a class="tab" href="#' + (n+1) + '">' + jQuery(this).attr('title') + '</a>');
	});
	
	// Determine which tab should show first based on the URL hash
	var panelLocation = location.hash.slice(1);
	if(panelLocation){
		var panelNum = panelLocation;
	}else{
		var panelNum = '1';
	}
	// Hide all panels
	panelContainer.find('div.panel').hide();
	// Display the initial panel
	panelContainer.find('div.panel:nth-child(' + panelNum + ')').fadeIn('slow');
	// Change the class of the current tab
	jQuery('div#tabs').find('a.tab:nth-child(' + panelNum + ')').removeClass().addClass('tab-active');
	
	// What happens when a tab is clicked
	// -- Loop through each tab
	jQuery('div#tabs').find('a').each(function(n){
		// For each tab, add a 'click' action
		jQuery(this).click(function(){
			// Hide all panels
			panelContainer.find('div.panel').hide();
			// Find the required panel and display it
			panelContainer.find('div.panel:nth-child(' + (n+1) + ')').fadeIn('slow');
			// Give all tabs the 'tab' class
			jQuery(this).parent().find('a').removeClass().addClass('tab');
			// Give the clicked tab the 'tab-active' class
			jQuery(this).removeClass().addClass('tab-active');
		});
	});
});



//***************************** Scrollable Content *****************************//


jQuery(function() {
	jQuery("#scrollslider").smoothDivScroll({
	scrollingSpeed: 4,
	mouseDownSpeedBooster: 1,
	autoScroll: "",
	autoScrollDirection: "endlessloop",
	autoScrollSpeed: 2,
	visibleHotSpots: "always",
	startAtElementId: "startslider1"
	});
	jQuery("#scrollportfolio").smoothDivScroll({
	scrollingSpeed: 4,
	mouseDownSpeedBooster: 1,
	autoScroll: "",
	autoScrollDirection: "endlessloop",
	autoScrollSpeed: 2,
	visibleHotSpots: "always",
	startAtElementId: "startportfolio1"
	});		
});


//***************************** Image Hover Effect *****************************//


jQuery(document).ready(function(){
    jQuery('.image-thumb').css({'opacity':'0'});
	jQuery('.jFlowControl, .portfolio-thumbnails').hover(
		function() {
			jQuery(this).find('.image-thumb').stop().fadeTo(500, 0.8);
		},
		function() {
			jQuery(this).find('.image-thumb').stop().fadeTo(500, 0);
		}
	)
});	


//***************************** Thumbnail Slide Up Effect *****************************//


jQuery(document).ready(function(){

	jQuery(function() {
		jQuery("#slider-container-large, #slider-container-medium, #slider-container-small").hover(function(){			jQuery(this).find(".pop_box_lower").animate({top:"-100px"},{queue:false,duration:500});},
			function(){ jQuery(".pop_box_lower").animate({top:"0px"},{queue:false,duration:500});
		});
		
	});

	jQuery(function() {
	
		jQuery("#slider-container-large").hover(function(){
			jQuery(this).find(".pop_box_upper_large").animate({top:"-525px"},{queue:false,duration:500});},
			function(){ jQuery(".pop_box_upper_large").animate({top:"-650px"},{queue:false,duration:500});
		});
		
		/*jQuery("#slider-container-medium").hover(function(){
			jQuery(this).find(".pop_box_upper_medium").animate({top:"-300px"},{queue:false,duration:500});},
			function(){ jQuery(".pop_box_upper_medium").animate({top:"-425px"},{queue:false,duration:500});
		});*/
		
		jQuery("#slider-container-small").hover(function(){
			jQuery(this).find(".pop_box_upper_small").animate({top:"-418px"},{queue:false,duration:500});},
			function(){ jQuery(".pop_box_upper_small").animate({top:"-553px"},{queue:false,duration:500});
		});	
		
	});
		
});


/*************************** Image Preloader ***************************/


jQuery(function () {
	jQuery('.preload').hide();//hide all the images on the page
});

var i = 0;//initialize
var int=0;//Internet Explorer Fix
jQuery(window).bind("load", function() {//The load event will only fire if the entire page or document is fully loaded
	var int = setInterval("doThis(i)",500);//500 is the fade in speed in milliseconds
});

function doThis() {
	var images = jQuery('.preload').length;//count the number of images on the page
	if (i >= images) {// Loop the images
		clearInterval(int);//When it reaches the last image the loop ends
	}
	jQuery('.preload:hidden').eq(0).fadeIn(500);//fades in the hidden images one by one
	i++;//add 1 to the count
}


/*************************** Contact Form ***************************/


jQuery(document).ready(function(){
	
	jQuery('#contactform').submit(function(){
	
		var action = jQuery(this).attr('action');
		
		jQuery("#message").slideUp(750,function() {
		jQuery('#message').hide();
		
 		jQuery('#submit')
			.after('<div class="loader"> </div>')
			.attr('disabled','disabled');
		
		jQuery.post(action, { 
			name: jQuery('#name').val(),
			email: jQuery('#email').val(),
			subject: jQuery('#subject').val(),
			comment_box: jQuery('#comment_box').val(),
			verify: jQuery('#verify').val()
		},
			function(data){
				document.getElementById('message').innerHTML = data;
				jQuery('#message').slideDown('slow');
				jQuery('#contactform div.loader').fadeOut('slow',function(){jQuery(this).remove()});
				jQuery('#contactform #submit').attr('disabled',''); 
				if(data.match('success') != null) jQuery('#contactform').slideUp('slow');
				
			}
		);
		
		});
		
		return false; 
	
	});
	
});

/* NEW SLIDER */
	var theInt = null;
		var $crosslink, $navthumb;
		var curclicked = 0;
		
		theInterval = function(cur){
			clearInterval(theInt);
			
			if( typeof cur != 'undefined' )
				curclicked = cur;
			
			$crosslink.removeClass("active-thumb");
			$navthumb.eq(curclicked).parent().addClass("active-thumb");
				$(".stripNav ul li a").eq(curclicked).trigger('click');
			
			theInt = setInterval(function(){
				$crosslink.removeClass("active-thumb");
				$navthumb.eq(curclicked).parent().addClass("active-thumb");
				$(".stripNav ul li a").eq(curclicked).trigger('click');
				curclicked++;
				if( 5 == curclicked )
					curclicked = 0;
				
			}, 7000);
		};
		
		$(function(){
			
			$("#main-photo-slider").codaSlider();
			
			$navthumb = $(".nav-thumb");
			$crosslink = $(".cross-link");
			
			$navthumb
			.click(function() {
				var $this = $(this);
				theInterval($this.parent().attr('href').slice(1) - 1);
				return false;
			});
			
			theInterval();
		});

/* NL's Stuff */
/* 5 */
(function($) {
$.extend({
    hasClass: new function(className) {
        var classAttr = $(this).attr('class');
        if (classAttr != null && classAttr != undefined) {
            var classList = classAttr.split(/\s+/);
            for(var ix = 0, len = classList.length;ix < len;ix++) {
                if (className === classList[ix]) {
                    return true;
                }
            }
        }
        return false;
    }
}); })(jQuery);

jQuery(document).ready(function(){
	$(".boxes-5").mouseover(function() {
		$(this).css('border', '1px black dashed');
		$(this).children('h2').css('margin-top', ($(this).children('h2').css('margin-top').substr(0, ($(this).children('h2').css('margin-top').lenght) - 2) - $(this).children('h2').height() - 3) + 'px');
			var classList = $(this).children('h2').attr('class').split(/\s+/);
			var oggetto = $(this);
			$.each( classList, function(index, item){
			if (item === 'boxed-5-alt2') {
				 $(oggetto).children('h2').css('margin-left', (-(200 - (490 - $(oggetto).children('h2').width()))) + 'px');
				 var baloon = $(oggetto).children('h2');
				 $('h2.boxed-5-alt2 > .arrow-5-alt2').css('left', ($(baloon).width() - 154));
			}
		});
		$(this).children('h2').css('display', 'block');
	}).mouseout(function() {
		$(this).css('border', '1px darkGrey solid');
		$(this).children('h2').css('display', 'none');
	});
	$(".boxes-5-middle").mouseover(function() {
		$(this).children('h2').css('margin-left', '-' + ((($(this).children('h2').width() + 30 - 310) / 2) - 5) + 'px');
		$(this).children('h2').children('#baloon-arrow').css('left', (($(this).children('h2').width() / 2) - 11) + 'px');
	});
	if (!$.browser.msie) {
		$(".boxes-5").mousemove(function(e){
		
			x = e.pageX - this.offsetLeft;
			y = e.pageY - this.offsetTop;
			var pixelX = 0;
			if ($.client.os == "Mac")  pixelX = 222; else pixelX = 462;
			if ((x > 773) ||(y > 705) || (x <= pixelX) || (y <= 422)) {
				$(this).children('h2').css('display', 'none');
			} else {
				$(this).children('h2').css('display', 'block');
			}
		});
	}
	$(".nav-thumb").each(function() {
		$(this).attr("src", "http://www.goodgame.it/thumbnail-slider.php?img=" + $("#fortunate" + $(this).attr('id').substr(9, 1)).children('img').attr("src"));
	});
});

/*
	//jquery image loader based on QueryLoader by Gaya Design
	//author: jsaade
	updates & fixes:
	- supports custom arguments no need for external CSS
	- can have several loaders/divs being loaded running not limited to only one
	- if the div itself has a background-image or if it is an image it is now taken into consideration.
	- resize when window is resized.
	
	arguments:
		- the id of the div, if not defined it is set to the body
		- an object defining the options to be used:
				- overlayColor: hex
				- userCallback: function to be called when the loader is done
				- showProgress: true/false
				- showProgressText: true/false
				- progressColor: hex
				- textColor: hex
				- textSize: in px
				- loaderPosition{top%,left%} => repositions the loader according to the overlay
				
	NB:
	- if there is a flash element, its wmode should be set to transparent, else it will be visible ontop of the overlay (might fix it using js soon).
	- make sure the div you are loading is visible else, its dimensions won;t be correct.		

	Usage Example:
	1-
	var loader= new Loader("#content", {userCallback:showContent, showProgress:true, showProgressText:true, textSize:15});
	loader.Start();
	
	2-
	var loader= new Loader("#content", {userCallback:showContent, showProgress:true, showProgressText:true, textSize:15, loaderPosition:{top:20,left:50}});
	loader.Start();
	
	the object has a Start fnction actually start the loading process.
	it will get all the images and background images, add them to a hidden div and after
	they are all loaded, call the doneLoading function
*/

function Loader(divid, options)
{
	if(!divid) divid = "body";
	this.div_id = divid;
	this.overlayColor = "#000000";
	this.userCallback = 0;
	this.showProgress = false;
	this.showProgressText = false;
	this.progressColor = "#ffffff";
	this.textColor = "#ffffff";
	this.textSize = "15";
	this.loaderPosition = {top:"50",left:"50"};
	if(options)//user has specified options?
	{
		this.overlayColor  = (!options.overlayColor)? this.overlayColor:options.overlayColor;
		this.userCallback  = (!options.userCallback)? this.userCallback:options.userCallback;
		this.showProgress  = (!options.showProgress)? this.showProgress:options.showProgress;
		this.showProgressText  = (!options.showProgressText)? this.showProgressText:options.showProgressText;
		this.progressColor  = (!options.progressColor)? this.progressColor:options.progressColor;
		this.textColor  = (!options.textColor)? this.textColor:options.textColor;
		this.textSize  = (!options.textSize)? this.textSize:options.textSize;
		
		if(options.loaderPosition)
		{
			this.loaderPosition.top  = (!options.loaderPosition.top)? this.loaderPosition.top:options.loaderPosition.top;
			this.loaderPosition.left  = (!options.loaderPosition.left)? this.loaderPosition.left:options.loaderPosition.left;
		}
		//debug
		this.debug = (!options.debug)? false:options.debug;
	}
	
	this.Stop = function()
	{
		if(!this.started) return;
	
		//this.userCallback = 0;
		clearTimeout(this.ieTimeout);
		
		if(!this.toLoadImages) return;
		
		
		for(var i = 0; i < this.toLoadImages.length; ++i)
			this.toLoadImages[i].unbind("load");
		this.toLoadImages = 0;	
		this.started = false;
	}
	
	
	this.Start = function()
	{
		if(this.started) return;
		if (navigator.userAgent.match(/MSIE (\d+(?:\.\d+)+(?:b\d*)?)/) == "MSIE 6.0,6.0") {
			//break if IE6	
			if(this.userCallback) this.userCallback();
			return;
		}
		
		this.images = this.getImages();
		
		if(this.images.length == 0)
		{
			if(this.userCallback) this.userCallback();
			return;
		}
		this.started = true;
		//create a box with height/width of the div and on top of it:
		this.width = $(this.div_id).outerWidth();
		this.height = $(this.div_id).outerHeight();
		this.position = $(this.div_id).offset();
		this.overlay = $("<div></div>").appendTo($(this.div_id));
		var overlyDesc = {
			'background-color':this.overlayColor,
			'z-index': '9999',
			'position':'fixed',
			'top': this.position.top,
			'left': this.position.left,
			'width': this.width + "px",
			'height': this.height + "px"
		};
		$(this.overlay).css(overlyDesc);
		//if(this.debug) alert(this.position.top);

		//attach load events:
		//load the images into a hidden div:
		this.preloader = $("<div></div>").appendTo(this.selectorPreload);
		$(this.preloader).css({
			height: 	"0px",
			width:		"0px",
			overflow:	"hidden"
		});
		this.imagesLoaded = 0;
		var parent = this;
		var callback = this.imgLoaded;
		
		if(this.showProgress)
		{
			this.loadBar = $("<div></div>").appendTo($(this.overlay));
			$(this.loadBar).css({
				'background-color': this.progressColor,
				'height': "1px",
				'position': "relative",
				'top': this.loaderPosition.top+"%",
				'width': "0%"
			});
		}
		if(this.showProgressText)
		{
			this.loadAmt = $("<div>0%</div>").appendTo($(this.overlay));
			$(this.loadAmt).css({
				'color':this.textColor,
				'font-family':'"Trebuchet MS",Arial,Helvetica,sans-serif',
				'font-size': this.textSize+'px',
				'font-weight':'bold',
				'line-height':'50px',
				'height':'50px',
				'width':'100px',
				'margin':'-60px 0 0 -50px',
				'position': "relative",
				'top': this.loaderPosition.top+"%",
				'left': this.loaderPosition.left+"%"
			});
		}
		
		
		//on resize
		var scope = this;
		$(window).resize(function() {
			if(!scope.overlay) return;
			scope.width = $(scope.div_id).outerWidth();
			scope.height = $(scope.div_id).outerHeight();
			scope.position = $(scope.div_id).offset();
			var overlyDesc = {
				'top': scope.position.top,
				'left': scope.position.left,
				'width': scope.width + "px",
				'height': scope.height + "px"
			};
		
		  $(scope.overlay).css(overlyDesc);
			
		 });
		
		
		this.toLoadImages = new Array();
		for (var i = 0; i < this.images.length; i++) {
			var imgLoad = $("<img></img>");
			$(imgLoad).attr("src", this.images[i]);
			$(imgLoad).unbind("load");
			
			$(imgLoad).bind("load", function() {
				callback(parent);
			});
			$(imgLoad).appendTo($(this.preloader));
			this.toLoadImages.push($(imgLoad));
		}
		
		
		
		//help IE drown if it is trying to die :)
		var scope = this;
		if($.browser.msie)
			this.ieTimeout = setTimeout(scope.ieLoadFix(scope), 2000);
	};
	
	this.ieLoadFix= function(scope) {
		while ((100 / scope.images.length) * scope.imagesLoaded < 100) {
			scope.imgLoaded(scope);
		}
	};
	
	this.animateLoader= function(scope) {
		var perc = (100 / scope.images.length) * scope.imagesLoaded;
		if (perc > 99) {
			if(scope.loadAmt)
				$(scope.loadAmt).html("100%");
			if(scope.loadBar)
			{
				$(scope.loadBar).stop().animate({
				width: perc + "%"
			}, 500, "linear", function() { 
				scope.doneLoading(scope);
			});
			}
			else
			scope.doneLoading();
		} else {
		
			if(scope.loadBar)
				$(scope.loadBar).stop().animate({
				width: perc + "%"
			}, 500, "linear", function() { });
			if(scope.loadAmt)
				$(scope.loadAmt).html(Math.floor(perc)+"%");
		}
	}
	this.imgLoaded = function(scope)
	{
		scope.imagesLoaded++;
		scope.animateLoader(scope);
	};
	
	this.doneLoading = function(scope){
		
		$(scope.preloader).remove();
		var cbk = scope.userCallback;
		if(this.started)
		{
			scope.overlay.fadeOut("slow",function(){$(this).remove(); if(cbk) cbk();});
		}else
			$(scope.overlay).remove();
		scope.overlay.fadeOut("slow",function(){$(this).remove(); if(cbk) cbk();});
		scope.overlay = 0;
		this.started = false;
		this.toLoadImages = 0;
		
		
	}
	
	//get all images
	this.getImages = function()
	{
		var imgs = new Array();
		
		//add own back image!
		var url = "";
		if ($(this.div_id).css("background-image") != "none") {
			url = $(this.div_id).css("background-image");
		}
		else if (typeof($(this.div_id).attr("src")) != "undefined" && $(this.div_id).attr("tagName").toLowerCase() == "img") {
				url = $(this.div_id).attr("src");
		}
		
		url = url.replace("url(\"", "");
		url = url.replace("url(", "");
		url = url.replace("\")", "");
		url = url.replace(")", "");
		if (url.length > 0) {
			imgs.push(url);
		}
		
		var p = this;
		var everything = $(this.div_id).find("*:not(script)").each(function() {
			var url = "";
			if ($(this).css("background-image") != "none") {
				url = $(this).css("background-image");
			} else if (typeof($(this).attr("src")) != "undefined" && $(this).attr("tagName").toLowerCase() == "img") {
				url = $(this).attr("src");
			}
			
			url = url.replace("url(\"", "");
			url = url.replace("url(", "");
			url = url.replace("\")", "");
			url = url.replace(")", "");
			
			if (url.length > 0) {
				imgs.push(url);
			}
		});
		return imgs;
	};
}


