/* Author: 

*/
(function($){
var ProSight = {
	init: function() {
		// collapsible area for subnav
		$('#container').prepend('<div class="collapsible"></div>');
		
		if($('.partbg').length) {
			ProSight.resizeGray();	
		}
		
		// selectmenu ui
		if ($(".dropdown").length) {
			$(".dropdown select").selectmenu();	
		}
		
		// ie fixes
		$('.ielt9 .bucket:last').addClass('last')
		$('.ielt9 aside .related .box:last, .ielt9 aside .related .WidgetHolder:last').addClass('last');
		$('.ielt9 aside .related .box:first, .ielt9 aside .related .WidgetHolder:first').addClass('first');
		$('.ielt9 .borders:last').addClass('last');
		$('.ielt9 .livefeed li:odd').addClass('even');
		$('.ielt9 #subnav-bg .links ul li::nth-child(3n)').addClass('last');
		$('.ielt9 .assets img:first').addClass('first');
		
		// generate logo carousel for solutions list page
		if($(".logoview").length) { 
			ProSight.logoCarousel();
		}
		
		// prosight twitter
		if($('.prosighttweet').length) {
			ProSight.twitterFeed();
		}
		
		// prosight homepage dynamic element heights
		if($('.homebg').length) {
			ProSight.setTallest($('#main .bucket'));
			ProSight.setTallest($('#slidewrap .hero'));
		}
		
	},
	// partial background image fix when content is shorter than window size
	resizeGray: function() {
		$container = $('#container');
		$container.removeAttr('style');
		$containerHeight = $container.height();
		$viewHeight = $(window).height();
		if($containerHeight <= $viewHeight) {
			$container.css({'height': $viewHeight});	
		}	
	},
	// collapse subnav
	collapse: function() {
		$('#subnav-bg').slideUp(300, function(){
			$('#subnav-bg div').removeClass('active').removeAttr('style');
		});
		$('#main-nav a').removeClass('selected');
	},
	// expand subnav
	expand: function(e) {
		e.preventDefault();
		var $navitem = $(this),
		$present = $('#subnav-bg div.active'),
		$show = $($navitem).attr('href')+'-sub'; 

		$($navitem).addClass('selected');
		if($present.length) { // if subnav is already open, swap in new content
			if('#'+$present.attr('id') === $show) { // if user clicks on same subnav, close
				ProSight.collapse();
				return(false);
			}
			$('#main-nav a').removeClass('selected');
			$($navitem).addClass('selected');
			$($present).fadeOut(300, function(){
				$($present).removeClass('active');
				$($show).fadeIn(300, function(){
					$($show).addClass('active');
				});
			})
		}
		else {
			$($show).addClass('active');
			$('#subnav-bg').slideDown(300);
		}
	},
	// solutions list page logo carousel
	logoCarousel: function() {
		$('.solutions > li').each(function(index) {
			var $currLi = $(this),
			i = index,
			$logoLen = $currLi.find('.logoview ul li').length;
			
			// assign each logo list a unique class
		    $currLi.find('.logowrap').addClass('logo-'+i); 
		    // if more than one logo image in list then add carousel buttons and generate carousel
		    if($logoLen > 1) {
		    	$('.logo-'+i).append('<span class="prev-logo"></span><span class="next-logo"></span>');
		    	$('.logo-'+i+' ul').carouFredSel({
					circular: true,
					auto : true,
					prev : {
						button	: '.logo-'+i+' .prev-logo',
						easing: 'linear'
					},
					next : {
						button	: '.logo-'+i+' .next-logo',
						easing: 'linear'
					}
				});	
		    }		    
		});	
	},
	twitterFeed: function() {
		/*$('.prosighttweet').rssfeed('http://twitter.com/statuses/user_timeline/ProSightCorp.rss', {
		    limit: 3,
		    header: false,
		    content: false,
		    date: false,
		    linktarget: '_blank'
		});*/
		// load twitter feed
	    $(".prosighttweet").tweet({
		    join_text: "auto",
		    username: "ProSightCorp",
		    count: 3,
		    auto_join_text_default: "",
		    auto_join_text_ed: "",
		    auto_join_text_ing: "",
		    auto_join_text_reply: "",
		    auto_join_text_url: "",
		    loading_text: "loading tweets...",
		    template: "{text}"
	  	}).bind("loaded",function(){
	  		$(this).find("a").attr("target","_blank");	
	  		if($('.partbg').length) {
				ProSight.resizeGray();	
			}
  		});
	},
	setTallest: function(elem) {
		console.log(elem);
		var tallest = 0,
		temp = 0;
		
		elem.each(function(index) { 
			temp = $(this).height();
			if(temp > tallest) {
				tallest = temp;
			}
		});
		$(elem).css({'height': tallest});
	}
}

$(function(){
	// init
	ProSight.init();

	// partial background image fix when content is shorter than window size
	$(window).resize(function() {
		if($('.partbg').length) {
			ProSight.resizeGray();	
		}
	});
		
	// homepage slideshow
	if($('.home-bg').length) {
		// push main content area down if hero taller than initial css
		var heroHeight = $('.hero').height();
		if(heroHeight > 230) {
			$('#main').css({'padding-top': heroHeight+80});
			$('#slide-control').css({'top': heroHeight+40});	
		}
		
		// set variables
		var $backgrounds = $('.home-bg img'),
			$heroes = $('.hero'),
			$prev = $('.background-prev'),
			$next = $('.background-next');
			
		// dynamically generate chicklets
		for (var i = 0; i < $backgrounds.length; i++) {
			$('#slide-control').append($('<a />').attr('href', '#')).append(' ');
		}
		$('#slide-control a:first').addClass('selected');
		
		var $chicklets = $('#slide-control a');
		
		// Swap Background Images
			
		$prev.click(function(e) {
			if ($(':animated').length) {
				return false;
			}
			e.preventDefault();
			clearInterval(carousel);
			swap(-1);
		});
		$next.click(function(e) {
			if ($(':animated').length) {
				return false;
			}
			e.preventDefault();
			clearInterval(carousel);
			swap(1);
		});
		
		// chicklets
		$chicklets.click(function(e) {
			if ($(':animated').length) {
				return false;
			}
			e.preventDefault();
			$(this).addClass('selected').siblings().removeClass('selected');
			clearInterval(carousel);
			var index = Math.max($(this).index('#slide-control a'), 0); // swap slide index
			swap(0, index);
		});
		
		// autoscroll through slides once
		var tabnum = $('.home-bg img').length;
	
		if (tabnum) {
			var CAROUSEL_INTERVAL = 6000,
				i=0;
			carousel = setInterval(function () {
				if (i < tabnum) {
					swap(-1);
					i++;
				} 
			}, CAROUSEL_INTERVAL);
		}	
	} // end if statement
	
	function swap(dir, chicklet) {
		// find the current img
		var $curr = $backgrounds.filter('.current');
		// find the current hero
		var $currhero = $heroes.filter('.active');

		// current slide index
		var index = Math.max($curr.index('img'), 0),
		swap;
		
		// make sure one of the backgrounds is current
		if (!$curr) {
			$curr = $backgrounds.eq(index);
			$curr.addClass('current');
		}
		// make sure one of the heroes is active
		if (!$currhero) {
			$currhero = $heroes.eq(index);
			$currhero.addClass('active');
		}
		
		// find the next/prev img or wrap to first/last
		if (dir === -1) {
			// next
			swap = index + 1 < $backgrounds.length ? index + 1 : 0;
		}
		else if (dir === 1) {
			// prev
			swap = index - 1 > -1 ? index - 1 : $backgrounds.length - 1;
		}
		else {
			// slide controlled
			swap = chicklet;
		}
		
		// find the fade img 
		var $swap = $backgrounds.eq(swap),
		// find fade hero
			$swaphero = $heroes.eq(swap),
		// find swap chicklet
			$swapchick = $chicklets.eq(swap);
			
		// highlight selected chicklet
		$swapchick.addClass('selected').siblings().removeClass('selected');
		
		// set initial css
		$swap.addClass("swap");
		$curr.animate({opacity: 0}, 800, function() {
			// when animation completes, remove all additional styles
			$curr.removeClass();
			$curr.removeAttr('style');
			
			$swap.removeClass().addClass("current");
			$swap.removeAttr('style');
		});
		
		$currhero.fadeOut(800, function() {
			$swaphero.fadeIn(800, function() {
				$currhero.removeClass("active");
				$currhero.removeAttr('style');
				
				$swaphero.addClass("active");
				$swaphero.removeAttr('style');
			})
		});
	} // end swap function
	
	// navigation
	$('#main-nav').delegate('a.expand', 'click', ProSight.expand);
	
	$('.collapse, .collapsible, .graybg, #main').bind('click', function(){
		ProSight.collapse();
	});
	
});
})(jQuery);
