$(document).ready(function(){

	$('header nav > ul > li').each(function () {
		if ($(this).children('ul').length > 0 ) {
			
			$(this).hover(
				function () {
					$(this).children('a').addClass('menu-active');
					$(this).children('ul').stop(true, true).slideDown(150);
				},
				function () {
					$(this).children('ul').delay(150).slideUp(150, function () {
						$(this).parent('li').children('a').removeClass('menu-active');
					});
				}
			);
			
			$(this).children('ul').mouseover(function () {
				$(this).parent('li').children('a').addClass('menu-active');
			});
			
		}
	});
	
	var image_width = 1350;
	var total_images = $('#slides > div').size();
	var canvas_width = image_width * total_images;
	
	$('#slides').css({'width' : canvas_width});
	$('#slides > div').each(function () {
		$(this).css('background-image', 'url(' + $(this).attr('rel') + ')');
	});
	
	$('#slides > div:first').addClass('slider-active');
	
	slide = function(){
		var current_id = $active.attr('title') - 1;
		var canvas_position = current_id * image_width;
		
		$('#slides > div').removeClass('slider-active');
		$active.addClass('slider-active');
	
		$('#slides').animate({
			left: -canvas_position
		}, 2000);
	
	}; 

	slideSwitch = function(){
		play = setInterval(function(){
			$active = $('#slides > div.slider-active').next();
			if ($active.length === 0) {
				$active = $('#slides > div:first-child');
			}
			slide();
		}, 6000);
	};
	
	slideSwitch();
	
	$('#slides > div').hover(function() {
		clearInterval(play);
	}, function() {
		slideSwitch();
	});	
	
	$('#right-arrow').click(function() {
		$active = $('#slides > div.slider-active').next();
		if ($active.length === 0) {
			$active = $('#slides > div:first-child');
		}
		
		
		clearInterval(play);
		slide();
		slideSwitch();
		return false;
	});
	
	$('#left-arrow').click(function() {
		$active = $('#slides > div.slider-active').prev();
		if ($active.length === 0) {
			$active = $('#slides > div:last');
		}
		
		
		clearInterval(play);
		slide();
		slideSwitch();
		return false;
	});
	
	$('div.latest-news ul').find('li').not(':first-child').hide();
	
	newsSwitch = function(){
		newsInterval = setInterval(function(){
			$currentNews = $('div.latest-news ul').find('li:visible');
			$nextNews = $('div.latest-news ul').find('li:visible').next();
			
			if ($nextNews.length === 0) {
				$nextNews = $('div.latest-news').find('li:first');
			}
			
			$currentNews.fadeOut(400);
			$nextNews.fadeIn(400);
		}, 5000);
	};
	
	newsSwitch();
	
	$('div.tabs > div > div:first').show();
	$('div.tabs > ul li:first-child a').addClass('tabs-active');
		
	$('div.tabs > ul a').click(function() {
		$('div.tabs > ul a').removeClass('tabs-active');
		$(this).addClass('tabs-active');
		$('div.tabs > div > div').hide();
		
		var nth = $(this).attr('rel');
		$('div.tabs > div > div:nth-child(' + nth + ')').fadeIn('fast');
		
		return false;
	});
	
	$('.calendar').datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: 'mm-dd-yy',
		beforeShow: function()
      {
          setTimeout(function()
           {
               $(".ui-datepicker").css("z-index", 999);
           }, 10);
        } 
	});
	$("div#ui-datepicker-div").css("z-index", "1001" );
	$("#ui-datepicker-div").css("z-index", "1003" );


	
	
$('#slides > div h3').css('left', $('#canvas').width()/2 - $('#slides > div h3').width()/2);
$(window).resize(function() {
	if ($('#canvas').width() > 1004) {
		$('#slides > div h3').css('left', $('#canvas').width()/2 - $('#slides > div h3').width()/2);
	}
});
	
});

