jQuery(document).ready(function() {

// About Us Toggle
	$('.about_link').click(function() {
		if( $('.about_link').hasClass("current") ){
			$('li .about_link').removeClass("current");
			$('#about').fadeOut(500,function() {$('#featured').fadeIn(500);});
		}else{
			$('li .about_link').addClass("current");
			$('#featured').fadeOut(500,function() {$('#about').fadeIn(500);});
		}
	});

// Navigation Hover
	$('.nav ul li').hover(function(){
		$(this).toggleClass('hover');
		if($(this).hasClass('first')){ 
			$(this).parents('.nav').toggleClass('hover');
		}else if( $(this).hasClass('last')){
			$(this).parent('ul').toggleClass('hover');
		}else if( $(this).hasClass('solo') ){
			$(this).parents('.nav').toggleClass('hover');
			$(this).parent('ul').toggleClass('hover');
		}
	}, function() {
		$(this).toggleClass('hover');
		if($(this).hasClass('first')){ $(this).parents('.nav').toggleClass('hover'); }else if( $(this).hasClass('last')){ $(this).parent('ul').toggleClass('hover'); }else if( $(this).hasClass('solo') ){ $(this).parents('.nav').toggleClass('hover'); $(this).parent('ul').toggleClass('hover'); }
	});

// Video and Event Menu
	$(".toggle").click(function(){ var menu_class = $(this).attr("title"); toggle_lists(menu_class); toggle_menu($(this)); });
	
	function toggle_lists(area){
		$("."+area+".box").slideToggle();
		$("."+area+".box").toggleClass("current");
		if ( $("."+area+".box").siblings().hasClass("current") ){
			$("."+area+".box").siblings().slideToggle();
			$("."+area+".box").siblings().toggleClass("current");
		}
	};
	
	function toggle_menu(x){
		$(x).parent().hide();
		$(x).parent().siblings().show();
		$(x).parent().siblings(".opened").toggleClass("current");
		if ($(x).parent().parent().siblings().children().hasClass("current")) {
			$(x).parent().parent().siblings().children(".current").hide();
			$(x).parent().parent().siblings().children(".current").siblings().show();
			$(x).parent().parent().siblings().children(".current").removeClass("current");
		}
		if ($(x).parent().hasClass("current")) $(x).parent().removeClass("current")
	};
	
	$(".videos ul li a").click(function() { close_video_list(".videos.box"); toggle_menu(".opened.current .toggle"); });
	function close_video_list(x){ $(x).slideUp(); $(x).removeClass("current"); };
	
//Featured Event Rotator
	$(".videos").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 0, true);
	$(".events").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 10000, true);
	$(".events").hover(
		function() { $(".events").tabs("rotate",0,true); },
		function() { $(".events").tabs("rotate",10000,true); }
	);
	
// Partners & Sponsors Slider
	var autoscrolling = true;
	$('.infiniteCarousel').infiniteCarousel().mouseover(function () { autoscrolling = false; }).mouseout(function () { autoscrolling = true;});
	setInterval(function () { if (autoscrolling) { $('.infiniteCarousel').trigger('next'); } }, 10000);
	
	// Diagnal Sliding Cover for Slider
		$('.boxgrid.thecombo').hover(function(){
			$(".cover", this).stop().animate({top:'260px', left:'325px'},{queue:false,duration:300});
		},function() {
			$(".cover", this).stop().animate({top:'0px', left:'0px'},{queue:false,duration:300});
		});
		
		$('.tri_box').hover(function(){
			$(".cover", this).stop().animate({top:'-260px'},{queue:false,duration:300});
		},function() {
			$(".cover", this).stop().animate({top:'0px'},{queue:false,duration:300});
		});
		
		$('.boxgrid.mission').hover(function(){
			$(".cover", this).stop().animate({top:'78px'},{queue:false,duration:300});
		},function() {
			$(".cover", this).stop().animate({top:'0px'},{queue:false,duration:300});
		});
	
// Contact List
	$('.close_slider').click(function() {
		toggle_slidebar($(this).attr("title"));
	});

	$('.slider').click(function(){
		$('input.live_search').val('');
		$('input.live_search').focus();
		toggle_slidebar($(this).attr("title"));
		toggle_list($(this).attr("title"));
	});
	
	function toggle_slidebar(x){
		if( ! $('#slidebar').hasClass("current") || x == "close" ){
			$('#slidebar').animate({width: 'toggle'});
			$('#slidebar').toggleClass("current");
			
			if( x == "close" ){
				$('li.slider').removeClass("current");
				$('li.slider').parent('ul').removeClass('current');
				$('li.slider').parents('.nav').removeClass('current');
				$('#list .contacts').removeClass("current");
			}else{
				$('.slider.'+x).addClass("current");
				if( $('.slider.'+x).hasClass('first') ){
					$('.slider.'+x).parents('.nav').toggleClass('current');
				}else if( $('.slider.'+x).hasClass('last') ){
					$('.slider.'+x).parent('ul').toggleClass('current');
				}
			}
		}else{
			if($('.'+x).hasClass("current")){
				$('.'+x).removeClass("current");
				$('#slidebar').animate({width: 'toggle'});
				$('#slidebar').toggleClass("current");
				$('li.slider').parent('ul').removeClass('current');
				$('li.slider').parents('.nav').removeClass('current');
				$('#list .contacts').removeClass("current");
			}else{
				if( $('.slider.'+x).hasClass('first') ){
					$('.slider.'+x).parent('ul').removeClass('current');
					$('.slider.'+x).parents('.nav').toggleClass('current');
				}else if( $('.slider.'+x).hasClass('last') ){
					$('.slider.'+x).parents('.nav').removeClass('current');
					$('.slider.'+x).parent('ul').toggleClass('current');
				}else{
					$('.slider.'+x).parent('ul').removeClass('current');
					$('.slider.'+x).parents('.nav').removeClass('current');
				}
				$('.slider').removeClass("current");
				$('.slider.'+x).addClass("current");
			};
		}
	};
	
	function toggle_list(x){
		var item = '.'+x;
		if( ! $('.link'+item).hasClass("current") ){
			
			$('.link'+item).addClass("current");
			$('.link'+item).siblings('div').removeClass("current");
			
			$('#list '+item).siblings('div').hide();
			$('#list '+item).siblings('div').removeClass("current");
			
			$('#list '+item).toggleClass("current");
			$('#list '+item).show();
		}
	}
	
// Live Search for List

	$('.live_search').focus(function() {
		var live_class = $(this).attr("id");
		$(this).liveUpdate("."+live_class,$(this),$('.live_search_clear'));
	});
	
	
//Scroll the menu on mouse move above the #sidebar layer
	$('#list').mousemove(function(e) {
		//Sidebar Offset, Top value
		var s_top = parseInt($('#slidebar #list').offset().top + 50);
		  
		//Sidebar Offset, Bottom value
		var s_bottom = parseInt($('#slidebar #list').height() + s_top);
		
		//Roughly calculate the height of the menu by multiply height of a single LI with the total of LIs
		var mheight = parseInt($('.contacts.current div.item').height() * $('.contacts.current div.item').length);
		
		//I used this coordinate and offset values for debuggin
		// $('#debugging_mouse_axis').html("X Axis : " + e.pageX + " | Y Axis " + e.pageY);
		// $('#debugging_status').html(Math.round(((s_top - e.pageY)/100) * mheight / 2));
		
		//Calculate the top value
		//This equation is not the perfect, but it 's very close
		var top_value = Math.round(( (s_top - e.pageY) /100) * mheight / 6)
		
		//Animate the #menu by chaging the top value
		$('.contacts.scroll').animate({top: top_value}, { queue:false, duration:500});
	}).mouseleave(function() {
		var s_top = 0;
		var s_bottom = 0;
		var mheight = 0;
		var top_value = 0;
		$('.contacts.current').animate({top: top_value}, { queue:false, duration:500});
		return false;
	});
	
	// if ($(window).width() < 1050) {$("#content").css("padding-top","95px");};
	// 
	// $(window).resize(function() {
	// 	if ($(window).width() > 1024) { 
	// 		$('#content').animate({
	// 			paddingTop: "85px"
	// 		}, { duration: 500, queue: false });
	// 	}else{
	// 		$('#content').animate({
	// 			paddingTop: "95px"
	// 		}, { duration: 500, queue: false });
	// 	}
	// });
	// 
	// $('textarea.expanding').autogrow({
	// 	maxHeight: 250,
	// 	minHeight: 30,
	// 	lineHeight: 0
	// });
});

