/* Author: 
	
*/

$(document).ready(function() {	
	
	var notMobile;
	if (screen.width > 480)
	//if (document.body.offsetWidth >= 480)
		notMobile = true;
	else
		notMobile = false;
	
	if(notMobile) {
	
		// Main Nav
		$('.slogan').after('<div id="bubble"></div>');
		$('#mainNav').delegate("a", "hover", function(event){
			if(event.type === 'mouseenter')
				$('#bubble').html($(this).attr('data-tidbit')+'<b></b>').addClass('hover');
			else
				$('#bubble').removeClass('hover');
			//$(this).toggleClass("hover");
		});
		
		
		// Move the highlight behind the active slide, set and remove the active class
		function slideActive(currNum, nextNum) {
			$('#promoNav li:nth-child(' + currNum +')').removeClass('active');
			$('#promoNav li:nth-child(' + nextNum +')').addClass('active');
			
			$('#promoNavActive').stop().animate({
				height: $('#promoNav li:nth-child(' + nextNum +')').height(),
				top: $('#promoNav li:nth-child(' + nextNum +')').position().top
			}, 500, function() { });
		}
		
		if ($('ul#promoImages li').length > 0) {
			var first = false;
			
			var mp = false;
			if ($('ul#promoImages li').length > 1)
				mp = true;
			
			function promoStart() {
				// Promo nav and highlight
				$('#promos').after('<ul id="promoNav"></ul>');
				var promoNavLinks ='';
				$('#promoImages li').each(function() { 
					promoNavLinks += '<li>' + $(this).children('h2').html() + '</li>';
				});
				$('#promoNav').append(promoNavLinks);
				$('#promoNav').append('<li id="promoNavActive"></li>');
				slideActive(1,1);
				
				//$('#promos li:first-child img:first-of-type').hide().attr({id:'mainPromo',class:'promoSmall'}).show();
				
				$('#promos').fadeIn(100, function() { 
					// Zoom Effect for browsers with no CSS transitions
					if (($('html').hasClass('no-csstransitions'))&&($('#mainPromo').hasClass('promoSmall'))) {
						var imgW = $('#mainPromo').width();
						var imgH = $('#mainPromo').height();
						$('#mainPromo').height(0);
						$('#mainPromo').width(0);
						$('#mainPromo').css({visibility: "visible", marginTop: imgH/2, opacity: 0});
						$('#mainPromo').animate({ 
							height:imgH+'px',
							width:imgW+'px',
							marginTop:($('#mainPromo').height()/2)+'px',
							opacity:1
						}, 1000);
					}
					// Triggers spinning transition
					$('#mainPromo').removeClass('promoSmall');
				});
			}
			
			promoStart();
			
			if (mp) {
				function onBefore(curr, next, opts) {
					if (first) {
						nextNum = opts.nextSlide + 1;
						currNum = opts.currSlide + 1;
						slideActive(currNum, nextNum);
					} else
						first = true;
				}
				
				// Add in the next/prev controls if there is more than one promo
				//$('#promoImages').after('<ul id="controls"><li><a href="#" id="promoPrev" title="Previous">&#9664</a></li><li><a href="#" id="promoNext" title="Next">&#9654</a></li></ul>');
				$('ul#promoImages').cycle({
					fx: 'scrollHorz',
					delay: 2000,
					//next: '#promoNext', 
					//prev: '#promoPrev',
					before: onBefore
					//pager: '#promoNav',
					/*pagerAnchorBuilder: function(idx, slide) { 
						return '<li>' + $(slide).children('h2').eq(0).text() + '</li>'; 
					}*/
					//after: onAfter
				});
				
				function onAfter(curr, next, opts) {
					//currNum = opts.currSlide + 1;
					//$('#promoNav li:nth-child(' + currNum +')').addClass('active');
					//if (curNum <= $('#promoNav').length)
				}
				
				$('#promoNav li').each(function(idx) {
					$(this).bind('click', function(e) {
						$('ul#promoImages').cycle(idx);
						$('ul#promoImages').cycle('pause');
					});
				});
	
				// Pause on Prev/Next
				//$('#promoNext').click(function() { $('ul#promoImages').cycle('pause'); return false; });
				//$('#promoPrev').click(function() { $('ul#promoImages').cycle('pause'); return false; });
			}
		}
		
		
		// Gallery
		if ($('ul#gallery').length ) {
			// Slideshow with thumbnails
			$('ul#gallery').cycle({
				fx: 'fade'
			});
		}
	
	} //Not mobile
	
	
	if (!notMobile) {
		// If promo images are not linked, link them to the image source to make them easier to read
		if ($('ul#promoImages li').length > 0) {
			/*$('#promoImages li').each(function(idx) {
				if ($(this).find('img').length > 0) {
					if ($(this).find('img').parent()[0].nodeName != 'A') {
						$(this).find('img').wrap('<a href="'+ $(this).find('img').attr('src') +'" target="_blank" />');
					}
				}
			});*/
			$('#promoImages').children('li').delegate('img','click',function(){
				if ($(this).parent()[0].nodeName != 'A')
					window.open($(this).attr('src'), '_blank');
			});
		}
		
		$('#mainNav li:nth-child(n+5)').hide();
		//$('#mainNav li:nth-child(4)').after('<li id="more"><a href="#more">More &raquo;</a></li>');
		$('#mainNav li:last-child').after('<li id="more"><a href="#more">More &raquo;</a></li>');
		$('#mainNav').delegate('#more a','click',function(e){
			$('#mainNav li:nth-child(n+5)').slideDown();
			$('#more').hide();
			
			e.preventDefault();
		});
		
		/*$('#more').bind('click', function(e) {
			$('#mainNav li:nth-child(n+5)').slideDown();
			$(this).slideUp();
			
			e.preventDefault();
		})*/
	}// Mobile
	
	
	var ieVer = msieversion();
	
	if (ieVer < 9) {
		$('body').append('<div class="bottemBird"></div>');	
		$('#container').append('<div class="leaves leavesBG"></div>');
		if (ieVer < 8) {
			$('#container').append('<div class="leaves leavesFG"></div>');
			$('header').append('<div class="birdTop"></div>');
		}
	}
	
	// Get IE version
	function msieversion() {
		var ua = window.navigator.userAgent;
		var msie = ua.indexOf ( "MSIE " );
		if ( msie > 0 ) {
			return parseInt (ua.substring (msie+5, ua.indexOf (".", msie )))
		}
	}
	
	// Mobile
	//$('#mainNav li:nth-child(4)').after('</ul><a class="more" href="#more">More &raquo;</a><ul>');

});
