// Just a 'closure' $ becomes jQuery
(function($)
{
	// extend a new function (fn)
	$.fn.fadeElements = function()
	{
		// Do your library code here :)
		var siblings = $(this).slice(1);
		
		$(this).eq(0).fadeIn('400');
		if (siblings.length)
		{				
			setTimeout(function(){ siblings.fadeElements();}, 75);
		}
		
		// Make it chainable!
		return $(this);
	}
})(jQuery);

function signupFocus() {
 	if (document.forms.signup.email.value == 'Enter your email address') {
		document.forms.signup.email.value = '';
	}
}

// get current active image number
function activeImage(){
	//return $(".projectNav ul li a.selected").parent().prevAll().length;
	return $(".projectImages #scroll li.selected").prevAll().length;
}

//get total number of images
function totalImages(){
	//return $('.projectNav ul li').length;
	return $('.projectImages #scroll li').length;
}


//does the sliding
function slideshow(num)
{	
	if ($('#scroll:animated').length)
	{
		return;
	}
	
	var viewport = 970;

	// About to loop
	if (num ==  0 && activeImage() == totalImages()-1)
	{
		// Clone the first element and add to the end
		$('#scroll li:first').addClass('selected').clone().appendTo($('#scroll'));

		// Scroll the 'first' (cloned) element into the viewport
		$("#scroll").stop().animate({left:-(viewport*$('#scroll li').prevAll().length)},1000, 'easeInOutCubic', function()
		{
			// Move to the real first element instantly and remove the cloned element
			$("#scroll").stop().css({left:0}).find('li:last').remove();
			$(".projectImages #scroll li").removeClass('selected').eq(0).addClass('selected');
		});
	
	}
	else
	{	
		if (activeImage() == 0 && num == totalImages() -1)
		{
			// Clone the first element and add to the end
			$('#scroll li:last').addClass('selected').clone().prependTo($('#scroll'));

			// Scroll the 'first' (cloned) element into the viewport
			$("#scroll").css('left', -viewport).stop().animate({left:0},1000, 'easeInOutCubic', function()
			{
				// Move to the real first element instantly and remove the cloned element
				$("#scroll").stop().css({left:-(viewport*($('#scroll li').prevAll().length-1))}).find('li:first').remove();
				
				$(".projectImages #scroll li").removeClass('selected').eq(totalImages()-1).addClass('selected');
	
			});
		}
		else
		{
			// Width of the viewport and also the images in the portfolio slideshow pages.
			$("#scroll").stop().animate({left:-(viewport*num)},1000, 'easeInOutCubic');
		}
	}
	// Return array of all these a tags
	$(".projectImages #scroll li")
	
	// Remove .selected from all of them
	.removeClass('selected')
	
	// grab just the element at index[num]
	.eq(num)
	
	// Make this one selected
	.addClass('selected');
	
	var hidden = $("#hiddencontent li:eq("+num+")");
	
	$(".projectNav h1").html($("h1", hidden).html());
	$(".projectNav p").html($("p", hidden).html());
	
	$(".projectNav .imagereadmore").attr('href', $(".imagereadmore", hidden).attr("href"));
	$(".projectNav 	.imagereadmore").attr('title', $(".imagereadmore", hidden).attr("title"));
}

//timer for index page
function timescroll()
{
	current = activeImage() + 1;
	if (current >= totalImages())
	{
		current = 0;
	}
	slideshow(current);
}

$(function(){


	viewport = 970;


	// Bind click events too the buttons so that they update
	$('.projectNav ul li a').click(function(){

		//slideshow($(this).parent().prevAll().length);
		
		return false;
	});
	
	// Left clicker
	$('#controlleft').click(function(){
		var current = activeImage();
		if (current !== 0)
		{
			slideshow(current-1);
		}
		else if (current == 0)
		{
			var current = totalImages()-1;
			slideshow(current);
		}
		return false;
	});
	
	// Right clicker
	$('#controlright').click(function(){
		var current = activeImage();
		if (current !== totalImages()-1)
		{
			slideshow(current+1);
		}
		else if (current == totalImages()-1)
		{
			var current = 0;
			slideshow(current);
		}
		
		return false;
	});

   	
   	//to change events list rollovers
   	$(".eventlist").hover(function() {
		$(this).addClass("eventlistover");
   	},function(){
    	$(this).removeClass("eventlistover");
   	});

	//to change top curve 
		$(".eventlist.topevent").hover(function() {
		$("#pagesplitter").addClass("pagesplitterover");
   	},function(){
    	$("#pagesplitter").removeClass("pagesplitterover");
   	});
   	
   	//to change border on gallery images
   	$("#gallery ul li").hover(function(){
   		$(this).addClass("highlight");
   	},function(){
   		$(this).removeClass("highlight");
   	});
});


var timeout	= 100;
var closetimer	= 0;
var ddmenuitem	= 0;

// open hidden layer
function mopen(id)
{	
	// cancel close timer
	mcancelclosetime();

	// close old layer
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

	// get new layer and show it
	ddmenuitem = document.getElementById(id);
	ddmenuitem.style.visibility = 'visible';

}
// close showed layer
function mclose()
{
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

// go close timer
function mclosetime()
{
	closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

$(function(){
	$('#regionheader').mouseover(function()
	{
		mopen('m1');
	}).mouseout(mclosetime);
	
	$('#m1').mouseover(mcancelclosetime).mouseout(mclosetime); 
});
// close layer when click-out
document.onclick = mclose; 
