// JavaScript Document

// Allows any dynamic styles to be added right away referencing the 
$('html').addClass('js');

// -- jQuery Timer
jQuery.timer = function (interval, callback)
 {
	var interval = interval || 100;

	if (!callback)
		return false;
	
	_timer = function (interval, callback) {
		this.stop = function () {
		clearInterval(self.id);
	};
		
	this.internalCallback = function () {
		callback(self);
	};
		
	this.reset = function (val) {
		if (self.id)
			clearInterval(self.id);
			
			var val = val || 100;
			this.id = setInterval(this.internalCallback, val);
		};
		
		this.interval = interval;
		this.id = setInterval(this.internalCallback, this.interval);
		
		var self = this;
	};
	
	return new _timer(interval, callback);
 };


jQuery(document).ready(function($){
	var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
	
	
	// Scroller
	
	var proj = 0;
	//var autoSlideSecs = 5;
	var slideCount = $('#slides ul').children('li').length;
	var curMenu = 0;
	
	switchMenu(curMenu);
	function switchMenu(num)
	{
		if(num == 0)
		{
			$('#slideshow #nav_1').show();
			$('#slideshow #nav_2').hide();
		}else{
			$('#slideshow #nav_1').hide();
			$('#slideshow #nav_2').show();
		}
	}
	
	// --- *** Add a new class for the very first element so it can be revealed.
	$('#slides li:first').addClass('first');
	
	// --- *** Begin Slider Animation
	function photoFadeIn(proj) {		
	//	$.timer(1000, function (timer) {
		$('#slides ul').children('li:eq('+proj+')').fadeIn(700, function(){
			if($('#slides ul').children('li:eq('+proj+')').attr('class') != 'nav_2'){
				// pause the menu from appearing until the image has finished fading in.
				$.timer(700, function (timer) {
					switchMenu(0);
					timer.stop();
				});
			}
		});
		if($('#slides ul').children('li:eq('+proj+')').attr('class') == 'nav_2')
			{
				switchMenu(1);
			}	
		//	timer.stop();
	//	});
	}
	
	function photoFadeOut(proj) {
		$('#slides ul').children('li:eq('+proj+')').fadeOut(700);
	}
	
	function photoPutBehind(proj) {
		$.timer(1500, function (timer) {
			$('#slides ul').children('li:eq('+proj+')').css('z-index', 0);
			timer.stop();
		});
	}
	
	function textSlideIn(proj) {
		$.timer(1500, function (timer) {
			//$('.description').animate({ 'opacity' : 1 }, 800);
			timer.stop();
		});
	}
	// ---- Animates Description 
	function textSlideOut(proj) {
		$.timer(1500, function (timer) {
			photoFadeOut(proj);
			photoPutBehind(proj);
			timer.stop();
		});
	}
	
	function moveSlide(where) {
		// Slide exit
	//$('#slides ul').children('li:eq('+proj+')').css("z-index", 2);
		
		// Starts text fade
		textSlideOut(proj);
			
		// Increment and checking if next move forward if prev move back
		if (where == 'next') proj++;
		else if (where == 'prev') proj--;
		else if(where == 'pause') proj = 0;
		
		// the counter = the total amount of items then start over
		if (where == 'next') { 
			var i = slideCount -1;//$('ul.text li').size() - 1;
			if (proj > i) proj = 0;
		}
		
		// Slide entrance
		photoFadeIn(proj)
		textSlideIn(proj);
	}
	
	// Check if the page has the SlideShow ID if so begin rotation
	if(document.getElementById('slideshow')){
var second = false;
		$.timer(5000, function (timer) {
			moveSlide("next");
				//timer.reset(5000);
		});
	}
});