// JavaScript Document

$(function(){
		    
//PHOTO GALLERY
/*$('#slideshow').cycle({ 
    fx:     'fade', 
    speed:  2000, 
    timeout: 3500, 
    pager:  '#nav', 
	//after: onAfter,
    pagerAnchorBuilder: function(idx, slide) { 
        // return selector string for existing anchor 
        return '#nav li:eq(' + idx + ') a'; 
    } 
});*/

//CHANGE CAPTION AFTER FADE --NOT USED
function onAfter() { 
    $('#leftgallerynav').html('<h3>'+this.alt+'</h3>');
}
/*
//Get our elements for faster access and set overlay width
var div = $('div#nav-wrapper'),
ul = $('ul#nav'),
// unordered list's left margin
ulPadding = 15;

//Get menu width
var divWidth = div.width();

//Remove scrollbars
div.css({overflow: 'hidden'});

//Find last image container
var lastLi = ul.find('li:last-child');

//When user move mouse over menu
div.mousemove(function(e){

	//As images are loaded ul width increases,
	//so we recalculate it each time
	var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;
	
	var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
	div.scrollLeft(left);
});

$('#pauseButton').click(function() { 
    $('#slideshow').cycle('pause'); 
});
$('#resumeButton').click(function() { 
    $('#slideshow').cycle('resume'); 
});

*/
		   
//SIDE MENU
var sliderSpeed = 400; //ADJUST THE SPEED OF THE SLIDING LEFT MENU//////////////////////////////////
$('#sideMenu ul').hover(function(){
	$(this).animate({left : '400'}, sliderSpeed);
},function(){
	$(this).animate({left : '0'}, sliderSpeed);
});


//SIDE BAR HOME CATEGORIES
$('#leftgallerynav li a').css({'background-color':'#000', 'opacity':.3});
$('#leftgallerynav li').hover(function(){
	$(this).find('a').stop().animate({opacity: 0}, 'fast');
	$(this).find('.category').css({'color':'#BEFE92', 'text-shadow':'2px 2px 3px #000'});
}, function(){
	$(this).find('a').stop().animate({opacity:.3}, 'fast');
	$(this).find('.category').css({'color':'#FDB434', 'text-shadow':'1px 1px 1px #666'});
});


/*  
//ON CLICK CHANGE CONTENT
	var slideDownSpeed = 500; //ADJUST THE SLIDE DOWN SPEED//////////////////////////////////////////////
	var fadeInSpeedNewContent = 800; //ADJUST FADE IN SPEED OF LOADING CONTENT/////////////////////////
	$('#sideMenu ul li a').click(function(){
		var pageFile = $(this).attr('href');
		$('#interchange').animate( { top : 500 }, slideDownSpeed, function(){
			$('<img src="images/load.gif" alt="Loading..." id="loading" />').hide().appendTo('#homeright').fadeIn(1200);
			if(pageFile == 'index.php')
			{
				setTimeout(function(){
					$('#loading').fadeOut(500, function(){$(this).remove()});					
				},1500);
				setTimeout(function(){
					$('#interchange2').css('top', 0).fadeIn(fadeInSpeedNewContent).attr('id', 'interchange');
				}, 2000);
					$(this).parent().remove();
			}
			else
			{
				setTimeout(function(){
					$('#loading').fadeOut(500, function(){$(this).remove()});					
				},1500);
				setTimeout(function(){
					$('<div />').load(pageFile+' #servicesright #interchange', function(){
						$(this).hide().appendTo('#servicesright').fadeIn(fadeInSpeedNewContent);
					});
				}, 2000);
				if($('#interchange #fadeShow').length > 0)
					$(this).attr('id', 'interchange2').hide();
				else
					$(this).remove();
			}
		});
		return false;
	});
	
//PHOTO GALLERY EFFECTS
	$('ul.slideshowNav li.image').click(function(){
		if($('img.galleryImage').css('opacity') >= '1')
		{
			$('#play').html('Play');
			var intervalID = '';
			if(intervalID.length > 0)
				clearInterval(intervalID);
			var size = $('#gallery img').size();
			var value = $(this).attr('value');
			$('img[title="'+value+'"]').show();
			$('li.image').css('color', '#FFFFFF');
			$(this).css('color', '#0000FF');
			$('#gallery img#topItem').hide(function(){
				$(this).attr('id', '');
				$('img[title="'+value+'"]').attr('id', 'topItem');
			});
			
		}
	});
	
	//PLAY BUTTON
	$('#play').click(function(){
		var size = $('#gallery img').size();
		var innerText = $(this).html();
		if(innerText == 'Play')
		{
			$('.galleryImage').show();
			$('#gallery img').attr('id', '');
			$('li.image').css('color', '#FFFFFF');
			$('#item_1').css('color', '#0000FF');
			$('img[title="1"]').attr('id', 'topItem');
			$('.galleryImage').show();
			$(this).html('Stop');
			var img = $('#topItem').attr('title');
			$('#item_'+img).css('color', '#0000FF');
			intervalID = setInterval(function()
			{
				img++;
				$('#gallery img:last').show();
				$('li.image').css('color', '#FFFFFF');
				$('#item_'+img).css('color', '#0000FF');
				$('#topItem').hide(function(){
					$(this).attr('id', '');
					$('img[alt="Image '+img+'"]').attr('id', 'topItem');
					
					if(img == (size+1))
					{
						img = 1;
						$('.galleryImage').show();
						$('img[title="1"]').attr('id', 'topItem');
					}
				});
			}, 1000);
		}
		else
		{
			$(this).html('Play');
			clearInterval(intervalID);
			$('.galleryImage').show();
			$('#gallery img').attr('id', '');
			$('li.image').css('color', '#FFFFFF');
			$('#item_1').css('color', '#0000FF');
			$('img[title="1"]').attr('id', 'topItem');
		}
	});*/
	
	//FADED NAVIGATION
	$('.fadedNav').css('opacity', '0.5');
	
	if($('.contact-form').length > 0)
	{
		$('.send-email-btn').hover(function(){
			$(this).attr('src', 'images/sendemailbtnhover.png');											
		}, function(){
			$(this).attr('src', 'images/sendemailbtn.png');
		});
	}
	
	if($('.fadeIn').length > 0)
	{
		$('.fadeIn').fadeIn(2000);	
	}
	
	//LIGHTBOX
	$('.lightbox a').lightBox();
	
	//Execute the slideShow
	slideShow();
	
	$('#photos').galleryView({
		panel_width: 568,
		panel_height: 358,
		frame_width: 140,
		frame_height: 100
	});
	
	//GALLERY PAGE
	/*$('.galleria').addClass('gallery_demo'); // adds new class name to maintain degradability
	
	$('ul.galleria').galleria({
		history   : true, // activates the history object for bookmarking, back-button etc.
		clickNext : true, // helper for making the image clickable
		insert    : '#main_image', // the containing selector for our main image
		onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
			
			// fade in the image & caption
			if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
				image.css('display','none').fadeIn(1000);
			}
			caption.css('display','none').fadeIn(1000);
			
			// fetch the thumbnail container
			var _li = thumb.parents('li');
			
			// fade out inactive thumbnail
			_li.siblings().children('img.selected').fadeTo(500,0.6);
			
			// fade in active thumbnail
			thumb.fadeTo('fast',1).addClass('selected');
			
			// add a title for the clickable image
			image.attr('title','Next image >>');
		},
		onThumb : function(thumb) { // thumbnail effects goes here
			
			// fetch the thumbnail container
			var _li = thumb.parents('li');
			
			// if thumbnail is active, fade all the way.
			var _fadeTo = _li.is('.active') ? '1' : '0.6';
			
			// fade in the thumbnail when finnished loading
			thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
			
			// hover effects
			thumb.hover(
				function() { thumb.fadeTo('fast',1); },
				function() { _li.not('.active').children('img').fadeTo('fast',0.6); } // don't fade out if the parent is active
			)
		}
	});*/

	
});

function slideShow() {

	//Set the opacity of all images to 0
	$('#slideshow a').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$('#slideshow a:first').css({opacity: 1.0});
	
	//Set the caption background to semi-transparent
	$('#slideshow .caption').css({opacity: 0.7});

	//Resize the width of the caption according to the image width
	$('#slideshow .caption').css({width: $('#slideshow a').find('img').css('width')});
	
	//Get the caption of the first image from REL attribute and display it
	if($('#slideshow').length > 0)
	{
		$('#slideshow .content').html($('#slideshow a:first').find('img').attr('rel'))
		.animate({opacity: 0.7}, 400);
	}
	
	//Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('gallery()',6000);
	
}

function gallery() {
	
	//if no IMGs have the show class, grab the first image
	var current = ($('#slideshow a.show')?  $('#slideshow a.show') : $('#slideshow a:first'));

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('caption'))? $('#slideshow a:first') :current.next()) : $('#slideshow a:first'));	
	
	//Get next image caption
	var caption = next.find('img').attr('rel');	
	
	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	 
	//Set the opacity to 0 and height to 1px
	$('#slideshow .caption').animate({opacity: 0.0}, { queue:false, duration:0 }).animate({height: '1px'}, { queue:true, duration:500 });	
	
	//Animate the caption, opacity to 0.7 and heigth to 100px, a slide up effect
	$('#slideshow .caption').animate({opacity: 0.7},100 ).animate({height: '40px'},500 );
	
	//Display the content
	$('#slideshow .content').html(caption);
	
	
}