// JavaScript Document

// Allows any dynamic styles to be added right away referencing the 
$('html').addClass('js');

function OnClientItemOpening(sender, eventArgs) {
    eventArgs.set_cancel(true);
}

// -- 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($){
	// main menu
	/*
	/* --- *** Add menu arrows for sub navigation  *** ---*/
	$('#header .top-menu .rmLevel1 > .rmItem:has(.rmSlide) > .rmLink').addClass('arrow');
	$('#header .top-menu .rmLevel2 .rmItem .rmLink').removeClass('arrow');

	$('#header .top-menu ul > li').each(function(){		
		/* --- *** Dynamically add the top border for each menu item *** ---*/
		$(this).find('.rmLevel1 li:first-child').css({'border-top' : 'solid #cccccc 1px'});	
		
		$(this).hover(function(){
			//$(this).find('.rmSlide').css({'display' : 'block'});
			$(this).find('.rmLevel1:first-child').css({'display' : 'block'});
		}, function(){
			$(this).find('.rmLevel1:first-child').css({'display' : 'none'});
		});
	});
	
	$('#header .RadMenu ul li ul li').each(function(){
		/* --- *** Dynamically add the top border for each sub-menu item *** ---*/
		$(this).find('.rmLevel2 li:first-child').css({'border-top' : 'solid #cccccc 1px'});
		
		$(this).hover(function(){
			$(this).find('.rmLevel2:first-child').css({'display' : 'block'});
		}, function(){
			$(this).find('.rmLevel2:first-child').css({'display' : 'none'});
		});
		
	});
	// END Menu
	
	// Scroller
	
	var proj = 0;
	var autoSlideSecs = 5;
	var slideCount = $('#slideshow .slides ul').children('li').length;
	
	// --- *** Add a new class for the very first-child element so it can be revealed.
	$('#slideshow .slides li:first-child').addClass('first');
	
	
	// --- *** Begin Slider Animation
	function photoFadeIn(proj) {
		$.timer(1000, function (timer) {
			$('#slideshow .slides ul').children('li:eq('+proj+')').fadeIn(700);
			timer.stop();
		});
	}
	
	function photoFadeOut(proj) {
			$('#slideshow .slides ul').children('li:eq('+proj+')').fadeOut(700);
	}
	
	function photoPutBehind(proj) {
		$.timer(1500, function (timer) {
			$('#slideshow .slides ul').children('li:eq('+proj+')').css('z-index', 0);
			timer.stop();
		});
	}
	
	function textSlideIn(proj) {
		$.timer(1500, function (timer) {
			$('#slideshow .description').animate({ 'top' : '10px', 'opacity' : 1 }, 700);
			timer.stop();
		});
	}
	
	function textSlideOut(proj) {
		$.timer(1500, function (timer) {
			$('#slideshow .description').animate({ 'opacity' : 0 }, 400).animate({ 'top' : '-200px' }, 1, function(){
				photoFadeOut(proj);
				photoPutBehind(proj);
			});
			timer.stop();
		});
	}
	
	function moveSlide(where) {
		// Slide exit
		$('#slideshow .slides ul').children('li:eq('+proj+')').css("z-index", 2);

		textSlideOut(proj);
			
		// Increment and checking
		if (where == 'next') proj++;
		else if (where == 'prev') proj--;
		
		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')){	
		$.timer(10000, function (timer) {
			moveSlide("next");
		});
	}
});