/* Author: J-Dizzle Fo' Shizzle. */

$(document).ready(function(){
	
    // Set the temperature and turn on the oven
    var rotate_speed = 4000;
    var feature_interval = setInterval("slideSwitch()", rotate_speed);
	var selection_interval = setInterval("selectionSwitch()", rotate_speed);
	
	//General feature Slider Bits
	var $slide_wrapper = $('#slider-wrapper');
	var $primary_tabs = $('#slider-tabs li');
	var $all_sections = $('.slider-content');
	
	// Happening Now Section
	var $happening_section = $($all_sections).siblings('#happening-content');
	var $all_slides = $('.slide');
	var $all_features = $('.feature');
	
	//Top Picks Bits
	var $top_pick_section = $($all_sections).siblings('#top-pick-content');
	var $all_selection_tabs = $($top_pick_section).children('ul').children('li');
	var $all_selections = $('.selection-wrapper div.selection');
	
	//Preview Bits
	var $preview_section = $($all_sections).siblings('#preview-content');
	
	// Video Box Bits
	var $thumb_strip = $('#thumb-strip');
	var $vid_up = $('#prev-video');
	var $vid_down = $('#next-video');
	var $vid_all_thumbnails = $('.video-thumbnail'); $vid_all_thumbnails.css({'opacity' : 0.9});
	var $all_videos = $('.featured-video');
	
	//Calander Bits
	var $all_events = $('.calendar-event');
	
	// Condiments
	var thumb_strip_height = $thumb_strip.height();
	var top_bound = 0;
	var bottom_bound = -(thumb_strip_height - 300);
	var thumb_strip_increment = 100;
	var can_click = true;
	
	//console.log($thumb_strip);
	//console.log(thumb_strip_height);
	
	
	// EVENT: User clicks on a content change tab on the slider
	$($primary_tabs).click(function(e){
		
		e.preventDefault();
		var $this_tab = $(this);
		
		if(!$this_tab.hasClass('current')){
			
			var section = $this_tab.attr('name');
			
			$($all_sections).stop().fadeOut();
			$($primary_tabs).removeClass('current');
			$this_tab.addClass('current');
			
			switch(section){
				
				case 'happening-now':
					$($happening_section).stop().fadeIn();
					break;
				case 'top-picks':
					$($top_pick_section).stop().fadeIn();
					break;
				case 'preview':
					$($preview_section).stop().fadeIn();
					break;
				
			}
			
		}
		
	})
	
	// EVENT: User hovers mouse over the feature slideshow
	$($slide_wrapper).mouseenter(function(event){
        clearInterval(feature_interval);
		clearInterval(selection_interval);
    }).mouseleave(function(event){
        feature_interval = setInterval( "slideSwitch()", rotate_speed);
		selection_interval = setInterval( "selectionSwitch()", rotate_speed);
    })
	
	// EVENT: User hovers over an inactive slide in the 'Happening Now' slideshow
	$($all_slides).mouseenter(function(event){
		
		if(!$(this).hasClass('current')){
			$(this).stop().animate({'background-color' : '#cccccc'});
		}
		
    }).mouseleave(function(event){
		if(!$(this).hasClass('current')){
			$(this).stop().animate({'background-color' : '#999999'});
		}
    })
	
	// EVENT: User clicks on a slide in the 'Happening Now' slideshow
	$($all_slides).click(function(e){
		
		//Dom objects
		var $this_slide = $(this);
		
		//Get the index of this slide
		var index = $(this).index();
		
		//If the slide clicked is not already the current slide
		if(!$(this).hasClass('current')){
			
			//Grab the related feature based on the index of the slide clicked
			var $related_feature = $all_features[index];
	
			var $active_feature = $('#feature-wrapper .active');
			
			$all_slides.stop().css({'background-color' : '#999999'}).removeClass('current');
			$this_slide.stop().animate({'background-color' : '#D2232A'}).addClass('current');
			
			//Give the active elements the last-active class to give them a lower z-index than the next elements to be displayed
			$active_feature.addClass('last-active');
			
			//Animate the opacity of the next image to be displayed and give it the '.active' class to bring it to the front
			$($related_feature).css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 300, function() {
				$active_feature.removeClass('active last-active');
			});
			
		}
		
	})
	
	
	
	
	// EVENT: User hovers over an inactive slide in the 'Happening Now' slideshow
	$($all_selection_tabs).mouseenter(function(event){
		
		if(!$(this).hasClass('selected')){
			$(this).stop().animate({'background-color' : '#cccccc'});
		}
		
    }).mouseleave(function(event){
		if(!$(this).hasClass('selected')){
			$(this).stop().animate({'background-color' : '#999999'});
		}
    })
	
	// EVENT: User clicks on a slide in the Top Picks slideshow
	$($all_selection_tabs).click(function(e){
		
		console.log('hello');
		
		e.preventDefault();
		
		//Dom objects
		var $this_slide = $(this);
		
		//Get the index of this slide
		var index = $(this).index();
		
		//If the slide clicked is not already the current slide
		if(!$(this).hasClass('selected')){
			
			console.log($all_selections);
			//Grab the related feature based on the index of the slide clicked
			var $related_selection = $all_selections[index];
	
			var $active_selection = $('.selection-wrapper .active');
			
			$all_selection_tabs.stop().css({'background-color' : '#999999'}).removeClass('selected');
			$this_slide.stop().animate({'background-color' : '#D2232A'}).addClass('selected');
			
			//Give the active elements the last-active class to give them a lower z-index than the next elements to be displayed
			$active_selection.addClass('last-active');
			
			//Animate the opacity of the next image to be displayed and give it the '.active' class to bring it to the front
			$($related_selection).css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 300, function() {
				$active_selection.removeClass('active last-active');
			});
			
		}
		
	})
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	// EVENT: User clicks the 'up' button on the video thumbnail slider
	$vid_up.click(function(e){
		
		e.preventDefault();
		
		var current_position = $thumb_strip.position();
		current_position = current_position.top;
		
		if(current_position != top_bound && can_click){
			
			$vid_down.removeClass('disabled');
			$vid_up.removeClass('disabled');
			
			can_click=false;
			var new_position = current_position + thumb_strip_increment + 'px';
			
			$thumb_strip.animate({'top': new_position}, 'fast', function(){
				can_click=true;
			});
			
			if(new_position==top_bound+'px'){
				$vid_up.addClass('disabled');
			}
			
		}
		
	})
	
	// EVENT: User clicks the 'down' button on the video thumbail slider
	$vid_down.click(function(e){
		
		e.preventDefault();
		
		var current_position = $thumb_strip.position();
		current_position = current_position.top;

		if(current_position != bottom_bound && can_click){
			
			$vid_down.removeClass('disabled');
			$vid_up.removeClass('disabled');
			
			can_click=false;
			var new_position = current_position - thumb_strip_increment + 'px';
			
			$thumb_strip.animate({'top': new_position}, 'fast', function(){
				can_click=true;
			});
			
			if(new_position==bottom_bound+'px'){
				$vid_down.addClass('disabled');
			}
			
		}
		
	})
	
	// EVENT: User hovers over a video thumbail
	$($vid_all_thumbnails).mouseenter(function(){
		if(!$(this).hasClass('selected-vid')){
			$(this).stop().animate({'opacity' : 1});
		}
	}).mouseleave(function(){
		if(!$(this).hasClass('selected-vid')){
			$(this).stop().animate({'opacity' : 0.9});
		}
	});
	
	// EVENT: User clicks on a video thumbnail
	$($vid_all_thumbnails).click(function(e){
		
		e.preventDefault();
		
		if(!$(this).hasClass('selected-vid')){
			var index = $(this).index();
			var $selected_vid = $all_videos[index];
			
			$vid_all_thumbnails.removeClass('selected-vid').stop().animate({'opacity' : 0.9},100);
			$(this).addClass('selected-vid').stop().animate({'opacity' : 1},100);;
			
			$($all_videos).fadeOut(100);
			$($selected_vid).fadeIn(100);
		}
		
	})
	
	//EVENT: User hovers on an event in the calendar
		
	$($all_events).mouseenter(function(){
		
		$(this).stop().animate({'background-color' : '#D2232A'}, 'fast').addClass('hover');
		
	}).mouseleave(function(){
		
		$(this).stop().animate({'background-color' : '#999999'}, 'fast').removeClass('hover');
		
	});

})




function slideSwitch() {
		
		//Get the currently active feature show tab and main block
		var $active_feature = $('#feature-wrapper .active');
		var $active_description = $('#slide-details .current');
		
		//check to see if we are at the last feature item
		if(!$active_feature.next('div').length){
			
			$next_feature = $('#feature-wrapper').children('div:first');
			$next_description = $('#slide-details').children('div:first');
			
		}
		else{
			
			$next_feature = $active_feature.next('div');
			$next_description = $active_description.next('div');
			
		}
		
		//Give the active elements the last-active class to give them a lower z-index than the next elements to be displayed
		$active_feature.addClass('last-active');
		
		//Animate the opacity of the next image to be displayed and give it the '.active' class to bring it to the front
		$next_feature.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 300, function() {
			$active_feature.removeClass('active last-active');
		});
		
		$active_description.stop().animate({'background-color' : '#999999'}).removeClass('current');
		$next_description.stop().animate({'background-color' : '#D2232A'}).addClass('current');
		
	}
	
function selectionSwitch() {
	
	//console.log('beep');
		
	
	//Get the currently active feature show tab and main block
	var $active_selection = $('.selection-wrapper div.active');
	var $active_selection_tab = $('#top-pick-content ul li.selected');
	
	//check to see if we are at the last feature item
	if(!$active_selection.next('div').length){
		
		$next_selection = $('.selection-wrapper').children('div:first');
		$next_selection_tab = $('#top-pick-content ul').children('li:first');
		
	}
	else{
		
		$next_selection = $active_selection.next('div');
		$next_selection_tab = $active_selection_tab.next('li');
		
	}
	
	//Give the active elements the last-active class to give them a lower z-index than the next elements to be displayed
	$active_selection.addClass('last-active');
	
	//Animate the opacity of the next image to be displayed and give it the '.active' class to bring it to the front
	$next_selection.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 300, function() {
		$active_selection.removeClass('active last-active');
	});
	
	$active_selection_tab.stop().animate({'background-color' : '#999999'}).removeClass('selected');
	$next_selection_tab.stop().animate({'background-color' : '#D2232A'}).addClass('selected');
	
}


$(function(){
      $("#twitter-feed").tweet({
        join_text: "auto",
        username: "TasSymphony",
        avatar_size: 48,
        count: 5,
        auto_join_text_default: "",
        auto_join_text_ed: "w",
        auto_join_text_ing: "",
        auto_join_text_reply: "",
        auto_join_text_url: "",
        loading_text: "loading tweets..."
      });
    });














