﻿
$(function(){
	
	$('.Gallery').cycle({
        fx: 'scrollHorz',
        speed: 500,
        pager: '.Paging',
		timeout: 0,
		next: '.PagesNav .Next',
		prev: '.PagesNav .Prev'
    });
	
	$('.HomeImages ul').cycle({
        fx: 'fade',
        speed: 1000,
        timeout:  10000,
    });
	

	/*
function Idle(config){
	idler = this;
	idler.mouseTimeout = 0;
	idler.state = 1;
	this.Start = function(){
		$(document).bind('mousemove',idler.Move);
		idler.Move();
	};
	this.Stop = function(){
		$(document).unbind('mousemove',idler.Move);
		window.clearTimeout(idler.mouseTimeout);
	};
	this.Move = function(){
		if(idler.state==0)
			config.onactive();
		idle.state = 1;
		window.clearTimeout(idler.mouseTimeout);
		idler.mouseTimeout = setTimeout(idler.Idled,config.timeout);
	};
	this.Idled = function(){
		idler.state = 0;
		config.onidle();
	}
}
	
	var idle = new Idle({
		onidle:function(){
			$('.Header, .SubNav, #OverlayPaging a').animate({opacity:0},2500);
		},
		onactive:function(){
			
			$('.Header, .SubNav, #OverlayPaging a').stop().show().css({opacity:1});
		},
		timeout:1000
	});
	*/
		
	var galleryArray = [];
	var captionArray = [];
	var currentIndex = 0;
	
	var galleryOpenState = 0;
	
	$('.Gallery ul a').click(function(e){
		e.preventDefault();
		imgurl = $(this).attr('href');
		galleryOpenState = 1;
		
		currentIndex = $(this).index('.Gallery a');
		galleryArray = $('.Gallery a').map(function(ind,element){return $(element).attr('href');}).get();	
		captionArray = $('.Gallery a').map(function(ind,element){return $(element).attr('title');}).get();	
		
		$('.MainContent, .Footer, .Nav, .SubNav').fadeOut();
		$('.closebttn').fadeIn();
		$('body').addClass("LargeView");		
		$img = $('#LargeImage');
		
		$('#ImageCaption').text($(this).attr('title'));
		
		$img.attr('src',imgurl).css({width:'',height:''});
		$img.bind('load',function(){
			if(galleryOpenState==1)
			{
				$('#ImageOverlay').fadeIn(function(){
					SetImage();
				});
				setTimeout(function(){SetImage();},100);
				SetImage();
			}
		});
		setTimeout(SetImage,500);
		
		
	});
	
	$('#OverlayPaging .Next').click(function(e){
		e.preventDefault();
		AdvanceImage(1);
	})
	$('#OverlayPaging .Prev').click(function(e){
		e.preventDefault();
		AdvanceImage(-1);
	})
	
	$('#LargeImage, .closebttn').click(function(e){
		e.preventDefault();
		// close image
		$('.MainContent, .Footer, .Nav, .SubNav').fadeIn();
		$('.closebttn').fadeOut();
		$('body').removeClass("LargeView");	
		$('#ImageOverlay').fadeOut();
		$('.Header, .SubNav').stop().css({opacity:1});

		galleryOpenState = 0;
	});
		
	$(window)
		.resize(SetImage)
		.keypress(function(e){
			if(e.keyCode == 39)
				AdvanceImage(1);
			else if (e.keyCode == 37)
				AdvanceImage(-1);
		});;
	
	
	function AdvanceImage(dir)
	{
		if(dir < 0 && currentIndex == 0)
			currentIndex = galleryArray.length-1;
		else if(dir > 0 && currentIndex == galleryArray.length-1)
			currentIndex = 0;
		else
			currentIndex += dir;
		
		$('#ImageOverlay').fadeOut(function(){
			if(galleryOpenState==1)
			{
				$img
					.attr('src',galleryArray[currentIndex])
					.css({width:'',height:''});
				
			$('#ImageCaption').text(captionArray[currentIndex]);
			
		}
		});
		
		$('.SubNav .index2, .SubNav .index3').attr('target','_blank');
		
	}
	
	function SetImage(){
		
		mountDepth = 110;
        jwin = $(window);
		mount = {width:jwin.width()-mountDepth*2,height:jwin.height()-mountDepth*2}
		
        jimg = $('#LargeImage');
		$imgwrap = $('#ImageWrap');
		if(jimg.height() > 0 && jimg.width() > 0)
		{
			scale = jimg.width() / jimg.height() < mount.width / mount.height ?
			  mount.height / jimg.height() : mount.width / jimg.width();
			
			$('#OverlayPaging, #ImageOverlay').width(jwin.width()).height(jwin.height());
		  
			height = jimg.height();
			width = jimg.width()
			jimg.height(height * scale);
			jimg.width(width * scale);
			$imgwrap.css('top', mountDepth-((jimg.height()-mount.height)/2) );
			$imgwrap.css('left', mountDepth-((jimg.width()-mount.width)/2) );
		}
    }
	
});
