/**
 * jQuery.Preload - Multifunctional preloader
 * Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com
 * Dual licensed under MIT and GPL.
 * Date: 3/25/2009
 * @author Ariel Flesler
 * @version 1.0.8
 */
;(function($){var h=$.preload=function(c,d){if(c.split)c=$(c);d=$.extend({},h.defaults,d);var f=$.map(c,function(a){if(!a)return;if(a.split)return d.base+a+d.ext;var b=a.src||a.href;if(typeof d.placeholder=='string'&&a.src)a.src=d.placeholder;if(b&&d.find)b=b.replace(d.find,d.replace);return b||null}),data={loaded:0,failed:0,next:0,done:0,total:f.length};if(!data.total)return finish();var g=$(Array(d.threshold+1).join('<img/>')).load(handler).error(handler).bind('abort',handler).each(fetch);function handler(e){data.element=this;data.found=e.type=='load';data.image=this.src;data.index=this.index;var a=data.original=c[this.index];data[data.found?'loaded':'failed']++;data.done++;if(d.enforceCache)h.cache.push($('<img/>').attr('src',data.image)[0]);if(d.placeholder&&a.src)a.src=data.found?data.image:d.notFound||a.src;if(d.onComplete)d.onComplete(data);if(data.done<data.total)fetch(0,this);else{if(g&&g.unbind)g.unbind('load').unbind('error').unbind('abort');g=null;finish()}};function fetch(i,a,b){if(a.attachEvent&&data.next&&data.next%h.gap==0&&!b){setTimeout(function(){fetch(i,a,1)},0);return!1}if(data.next==data.total)return!1;a.index=data.next;a.src=f[data.next++];if(d.onRequest){data.index=a.index;data.element=a;data.image=a.src;data.original=c[data.next-1];d.onRequest(data)}};function finish(){if(d.onFinish)d.onFinish(data)}};h.gap=14;h.cache=[];h.defaults={threshold:2,base:'',ext:'',replace:''};$.fn.preload=function(a){h(this,a);return this}})(jQuery);

var intTime = 0;
$(document).ready(function(){
						   
	$('#carousel ul').width($('#carousel ul li').size()*131);	
	
	var $links = $('#carousel  li a');
	$links.preload({ threshold:2 }); 

	
	$('#carousel .arr-r').click(function(){
		clearTimeout(intTime);
		if (!$('#carousel ul').is(':animated')) {	
			
			var limit = $('#carousel ul').width() + $('#carousel ul').get(0).offsetLeft - 5;
			
			if (limit <= 655 ) {
				$('#carousel ul').animate({
				'left':'5px'					  
				},400);	
				intTime = setTimeout("gallery_scroll()", 6000);
			} else {
				$('#carousel ul').animate({
					'left':'-=131px'					  
				},400);	
			}
		//	intTime = setTimeout("gallery_scroll()", 6000);
		}
		
									 
	});
	
	$('#carousel .arr-l').click(function(){
		clearTimeout(intTime);
		if (!$('#carousel ul').is(':animated')) {
			
			var limit = $('#carousel ul').get(0).offsetLeft - 5;
			
			if (limit == 0 ) {
				$('#carousel ul').animate({
				'left': '-' + ($('#carousel ul').width() - 655 - 5) + 'px'				  
				},400);	
				intTime = setTimeout("gallery_scroll()", 6000);
			} else {
				$('#carousel ul').animate({
					'left':'+=131px'					  
				},400);	
			}
			
		}
		//intTime = setTimeout("gallery_scroll()", 6000);
									 
	});
	
	
	
	$('#carousel li a').click(function(){
		
		clearTimeout(intTime);
		$('#carousel .overlay').hide();
		$('#carousel li').removeClass('active');
		$(this).parent().addClass('active').children('.overlay').show();
		
		var index = $('#carousel li').index($('#carousel li.active'));
		
		if (index != 0 && index != ($('#carousel li').size()-1)) {
			if (($(this).parent().get(0).offsetLeft + $('#carousel ul').get(0).offsetLeft) == 529) {
				$('#carousel .arr-r').click();
			} else {
				if (($(this).parent().get(0).offsetLeft + $('#carousel ul').get(0).offsetLeft) == 5)
					$('#carousel .arr-l').click();	
			}
		}
		
		$('#aqua-gallery .full-image img').hide().attr('src', $(this).attr('href')).load(function(){
			$(this).show();													 
		});

		if (!$(this).parent().hasClass('.sec')) {
			var id = $(this).parent().prevAll().filter('.sec:not(:last)').attr('id');
			$('.sections li').removeClass('active');
			if ($(this).parent().prevAll('.sec').size() > 1) {
				$('.sections li a[rel="'+id+'"]').parent().addClass('active');
			} else {
				$('.sections li:first').addClass('active');
			}
		} else {
			var id = $(this).parent().attr('id');
			$('.sections li').removeClass('active');
			$('.sections li a[rel="'+id+'"]').parent().addClass('active');
		}
		intTime = setTimeout("gallery_scroll()", 6000);
		return false;
	});
	
	
	$('#aqua-gallery .sections li a').click(function(){
		$('.sections li').removeClass('active');						   
		$(this).parent().addClass('active');
		
		var setSec = $(this).attr('rel');
		var toAnim = ($('#carousel li#' + setSec).prevAll().size() * 131) - 5;
		$('#carousel ul').stop().animate({
			'left': -1*toAnim + 'px'					  
		}, 400);	

		$('#carousel li#' + setSec + ' a').click();
		
		return false;
	});
	
	intTime = setTimeout("gallery_scroll()", 6000);
});


function gallery_scroll () {
	clearTimeout(intTime);
	var $a = $('#carousel li.active').next().children('a');
	$('#carousel li.active').next().children('a').click();
	
	$('#carousel .overlay').hide();
	$('#carousel li').removeClass('active');
	$a.parent().addClass('active').children('.overlay').show();
		
	var index = $('#carousel li').index($('#carousel li.active'));
	
	if (index != 0 && index != ($('#carousel li').size()-1)) {
		if (($a.parent().get(0).offsetLeft + $('#carousel ul').get(0).offsetLeft) == 529) {
			$('#carousel .arr-r').click();
		} else {
			if (($a.parent().get(0).offsetLeft + $('#carousel ul').get(0).offsetLeft) == 5)
				$('#carousel .arr-l').click();	
		}
	}
	
	
	$('#aqua-gallery .full-image img').hide().attr('src', $a.attr('href')).load(function(){
		$(this).show();													 
	});

	if (!$a.parent().hasClass('.sec')) {
		/*var id = $(this).parent().prevAll().filter('.sec:not(:last)').attr('id');
		$('.sections li').removeClass('active');
		if ($(this).parent().prevAll('.sec').size() > 1) {
			$('.sections li a[rel="'+id+'"]').parent().addClass('active');
		} else {
			$('.sections li:first').addClass('active');
		}*/
	} else {
		//var id = $a.parent().attr('id');
		$('.sections li.active').prev().children('a').click();
		//$('#carousel li#' + rel).click();
		//alert($a.parent().prev('.sec').get(0).offsetLeft);
	}
	intTime = setTimeout("gallery_scroll()", 6000);
}



