$(document).ready(function() {
	$('article.preview').hover(
		function(){
			$(this).addClass('active');
		},
		function(){
			$(this).removeClass('active');
	});
	
	$('.bg_slider').each(function(index){
	    $(this).cycle({ 
			fx: 'fade',
			speed: 1000, /* was 300, which was too jarring */
			timeout: 4500, /* was 0, which did not animate */
			sync:    false,
			prev: '#bg_info_wrapper nav a.prev',
			next: '#bg_info_wrapper nav a.next'
		});
	});
	
	var curActive = $("#scroller .box:first-child");
	var total = $("#scroller .box").size();
	updateNeighbors();
	curActive.removeClass('hidden');
	function slideTo(index) {
		// take care of the bottom
		if($('#scroller .box:nth-child('+ (curActive + 1) +')').hasClass('hidden')) {
			
		}
	}
	
	function unMakeActive() {
		curActive.find('.expanded').slideUp(600);
		curActive.find('.preview').slideDown(600);
	}
	
	function makeActive() {
		curActive.find('.expanded').slideDown(600);
		curActive.find('.preview').slideUp(600);
		curActive.removeClass('notactive');
		
	}
	
	function updateNeighbors() {
		curActive.next().slideDown(500);
		curActive.prev().slideDown(500);
		curActive.next().next().slideDown(500);
		curActive.prev().prev().slideDown(500);
		curActive.prev().prev().prevAll().slideUp(500);
		curActive.next().next().nextAll().slideUp(500);
	}
	
	function slideUp() {
		if(curActive.next().length) {
			unMakeActive();
			
			curActive = curActive.next();
			makeActive();
			
			updateNeighbors();
		}
	}
	
	function slideDown() {
		if(curActive.prev().length) {
			unMakeActive();
				
		//slideTo(curActive-1);
		curActive = curActive.prev();
		makeActive();

		//curActive -= 1;	
		updateNeighbors();
		}
	}
	
	$('#scroller .box:nth-child(1)').removeClass('notactive');
	
	$(".preview").click(function() {
		unMakeActive();
		curActive = $(this).parent();
		makeActive();
		updateNeighbors();
	});
	
	$("#events nav a").click(function() {
		//console.log(curActive);
		if($(this).hasClass('up')) {
			slideUp();
		}
		else {
			slideDown();
		}
		return false;
	});	
	
	$('.ask_zahra a.not_active').hover(
	        function(){
	            $(this).stop(true, true).animate({'padding-right':'25px'}, 200);
	        },
	        function(){
	            $(this).stop(true,true).animate({'padding-right':'15px'}, 300);
	    });

	  $('.ask_zahra a.tab').toggle(
	        function(){
	            $(this).removeClass('not_active').addClass('active');
							$('.ask_zahra_outer').css({'width': '390px', 'height': '315px'});
	      			$('.ask_zahra').animate({'right': '0'}, 300);
	        },
	        function(){
	            $(this).removeClass('active').addClass('not_active');
      			$('.ask_zahra').animate({'right': '-286px'}, 300, function(){
							$('.ask_zahra_outer').css({'width': '95px', 'height': '56px'});
			});
		});
							
		$("#zahra_form").submit(function() {
					$('.ask_zahra h2').text('Thank you!');
		      $('.ask_zahra a.tab').delay(10000).removeClass('active').addClass('not_active');
					$('.ask_zahra').delay(10000).animate({'right': '-128px'}, 300);
		    });

	var standard_message = $('.ask_zahra textarea').val();
	$('.ask_zahra textarea').focus(
	    function() {
	        if ($(this).val() == standard_message)
	            $(this).val("");
	    });
	$('.ask_zahra textarea').blur(
	    function() {
	        if ($(this).val() == "")
	            $(this).val(standard_message);
	    });
	

	var standard_message2 = $('.submit_question textarea').val();
	$('.submit_question textarea').focus(
	    function() {
	        if ($(this).val() == standard_message2)
	            $(this).val("");
	    });
	$('.submit_question textarea').blur(
	    function() {
	        if ($(this).val() == "")
	            $(this).val(standard_message2);
	    });
	
			
	$("select").change(function () {
		    var str = "";
		    $(this).find("option:selected").each(function () {
		      str += $(this).text() + "";
		    });
		    $(this).prev().text(str);
				// alert(str)
		})
		  .trigger('change');
		
	$('.middle header button').click(
			function() {
				$('.submit_question').slideDown();
			});
			
	$('.right header button').click(
			function() {
				$('.submit_question').slideDown();
			});
			
		$('.submit_question_close').click(
			function() {
				$('.submit_question').slideUp();
			});
		
	$('#leftCol ul li ul').parents('li').addClass('has_children');
	
	if ($('nav.pagination a').size() >= 7 ) {
		$('nav.pagination a:last-child').before('<span>...</span>');
		$('nav.pagination a:nth-child(6)').hide();
	}
	
	$('.video').append('<img src="images/play_btn.png" class="play_btn">');
	
	$('#event_slider')
	    .cycle({ 
			fx: 'scrollHorz',
			speed: 600,
			timeout: 0,
			pagerAnchorBuilder: function(idx, slide) { 
				return '#list_carousel a:eq(' + idx + ')'; 
			}
	});

	$("#thumbs").carouFredSel({
		scroll		: 3,
		circular	: false,
		infinite	: false,
		auto : false,
		prev : {	
			button	: "#prev",
			key		: "left"
		},
		next : { 
			button	: "#next",
			key		: "right"
		}
	});

});


