Cufon.replace('.promo h2, .promo a, .promo-social-media li', { fontFamily: 'swiss-light' });
Cufon.replace('#home_container #pri_message h1', { fontFamily: 'avenir-heavy' });

var IE6 = (navigator.userAgent.indexOf("MSIE 6") >= 0) ? true : false,
	IE7 = (navigator.userAgent.indexOf("MSIE 7") >= 0) ? true : false,
	homeFlashWidthMin = 1013, //minimum width of flash
	homeFlashHeightMin = 460, // minimum height of flash
	poppedOpacity = 0.75, // inactive tab opacity
	poppedOpacityOver = 1.0, // active/hover tab opacity
	opacityAnimationDuration = 200, // speed for opacity animation on tabs
	slideTransitionDuration = 500, // speed to transition between slides when a tab is clicked
	tabsWrapperHeightClosed = 33, // height of tab wrapper when closed
	tabsWrapperHeightOpenMin = 310, // height of tab wrapper when open
	tabsWrapperHeightOpenMin, // we'll use this later to stop tabs from getting hidden underneath the header on window resize
	tabsWrapperOpenDuration = 300, // speed at which tab wrapper will open to reveal slides
	cycleTimeout = 15000, // milliseconds between slide transitions (0 to disable auto advance) 
	cycleSpeed = 2000, // speed of the transition (any valid fx speed value)
	whyCienaNonFlashURL = 'http://www.ciena.com/corpinfo.htm'; // URL to load when 'why ciena?' tab is clicked in non-flash version

function get_cookie(Name) {
	var search = Name + "="
	var returnvalue = "";
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search)
		// if cookie exists
		if (offset != -1) { 
			offset += search.length
			// set index of beginning of value
			end = document.cookie.indexOf(";", offset);
			// set index of end of cookie value
			if (end == -1) {
				end = document.cookie.length;
			}
			returnvalue=unescape(document.cookie.substring(offset, end))
		}
	}
	return returnvalue;
}

$(function(){
	
	// Cycle promo content		
	$('.promo-first ul').cycle({
		sync: 0,
		timeout: cycleTimeout,	
		speed: cycleSpeed,	
		delay: 0,		// additional delay (in ms) for first transition (hint: can be negative)
		cleartypeNoBg: true
	});
	$('.promo-second ul').cycle({
		sync: 0,
		timeout: cycleTimeout,
		speed: cycleSpeed,
		delay: 400,		// additional delay (in ms) for first transition (hint: can be negative)
		cleartypeNoBg: true
	});
	$('.promo-blog ul').cycle({
		sync: 0,
		timeout: cycleTimeout,
		speed: cycleSpeed,
		delay: 800,		// additional delay (in ms) for first transition (hint: can be negative)
		cleartypeNoBg: true
	});
	
	// Track promo clicks 
	$('.promo-success, .promo-events, .promo-blog').click(function(){
		var promoClass = $(this).attr('class'),
			s=s_gi('cienacom');
		s.linkTrackVars='eVar31,events';
		s.linkTrackEvents='event8';
		s.eVar31=promoClass;
		s.events='event8';
		s.tl(this,'o',promoClass);
	});
	
	var homeContainer = $('#home_container');

	function resizeHomeFlashDiv() {
		var headerHeight = $('#header').height(),
			footerHeight = $('#footer').height(),
			homeFlashWidth = $(window).width(),
			setFlashWidth = (homeFlashWidth > homeFlashWidthMin) ? homeFlashWidth : homeFlashWidthMin,
			homeFlashHeight = $(window).height() - ( headerHeight + footerHeight ),
			setFlashHeight = (homeFlashHeight > homeFlashHeightMin) ? homeFlashHeight : homeFlashHeightMin;
			
		window.tabsWrapperHeightOpen = ((setFlashHeight - 49) > tabsWrapperHeightOpenMin) ? setFlashHeight - 49 : tabsWrapperHeightOpenMin;

		homeContainer.css({
			width: setFlashWidth,
			height: setFlashHeight
		});
	}
	resizeHomeFlashDiv();
	
	function resizeTabsContainer() {
		
		var tabsWrapper = $("#tabs_wrapper");
		
		if (tabsWrapper.attr('class') == 'popped') {
			
			var currentTabsWrapperOffset = tabsWrapper.offset(),
				currentTabsWrapperOffsetTop = currentTabsWrapperOffset.top;
			
			if (currentTabsWrapperOffsetTop < tabsWrapperOffsetMin) {
				tabsWrapper.css('height', window.tabsWrapperHeightOpen - (tabsWrapperOffsetMin - currentTabsWrapperOffsetTop) + 'px');
			} else {
				tabsWrapper.stop().css({ height: window.tabsWrapperHeightOpen });
			}
		}
	}
	
	$(window).resize(function() {
		resizeHomeFlashDiv();
		resizeTabsContainer();
	});
	
	
	
	// Initial sizing and embedding of home flash
	var jwPlayer = '/public/swf/player-licensed.swf',
		playerConfig = '/public/swf/playerconfig.xml',
		videoIds = new Array();

		videoIds['#tab1'] = 'video86';
		videoIds['#tab2'] = 'video84';
		videoIds['#tab3'] = 'video70';
	
	if ($.flash.available) {
		
		homeContainer.empty();
		
		homeContainer.flash({
			id: 'home_flash',
			name: 'home_flash',
			swf: '/public/swf/ciena_home.swf',
			width: '100%',
			height: '100%',
			flashvars: {
				xmlLoc: '/public/swf/ciena_flash.xml'
			}
		});
		
	} else {
		
		homeContainer.addClass('no_flash')
			.find('#no_flash').show()
				.find('#city-messages').cycle({
					sync: 0,
					timeout: 8000,	
					speed: cycleSpeed,	
					delay: 0
				});
				
		$('#city-messages .city-message').children().not('div').click(function(){
			$('#city-messages').cycle('pause');
			$(this).parent().find('.city-message-more').fadeIn('fast');
			return false;
		});
		
		$('#city-messages .city-message-more .close').click(function(){
			$(this).parent().fadeOut('fast', function(){
				$('#city-messages').cycle('resume');
			});
			return false;
		});
		
	}
	
	// tell home flash to go to videos.
	function whyCienaVideos() {
		
		if (!$.flash.available) return; 
		
		homeContainer.flash(
			function() {
				this.whyCiena();
			}
		);
	}
	
	function pauseHomeFlash() {
		
		if (!$.flash.available) return;
		
		homeContainer.flash(
			function() {
				this.deactivate();
			}
		);
	}
	
	function resumeHomeFlash() {
		
		if (!$.flash.available) return;
		
		homeContainer.flash(
			function() {
				this.activate();
			}
		);
	}	
		
	function nonFlashAnimation(arg) {

		if ($.flash.available) return;

		if (arg == 'close') {
			$('#city-messages').hide().cycle('pause');
			$('#spectrum').stop().animate({ height: '38'}, 'fast');
		} else {
			$('#city-messages').show();
			if ($('#city-messages .city-message-more').css('display') != 'block') {
				$('#city-messages').cycle('resume');
			}
			$('#spectrum').stop().animate({ height: '116px'}, 'fast');
		}
	}
		
	$('#tabs a').click(function(){
		
		var newSlideId = $(this).attr('href');
		var currentSlideId = '#' + $('.activeSlide').attr('id');

		// Track clicks
		var tabText = $(this).text().toLowerCase(),
			s=s_gi('cienacom');
		s.linkTrackVars='eVar33,events';
		s.linkTrackEvents='event15';
		s.eVar33=tabText;
		s.events='event15';
		s.tl(this,'o',tabText);
		
		if ($(newSlideId).length > 0) {
			
			pauseHomeFlash();
			
			if ($("#tabs_wrapper").attr('class') != 'popped') {
			
				nonFlashAnimation('close');
				
				$(this).addClass('activeTab');
			
				$("#tabs_wrapper").stop().animate({ height: tabsWrapperHeightOpen + 'px' }, tabsWrapperOpenDuration, function() {
					
					var tabsWrapperOffset = $(this).offset();
					window.tabsWrapperOffsetMin = tabsWrapperOffset.top; // this is the maximum height we want the tabs to rise, even if the window is resized.
					
				    $(this).addClass('popped');
					$(newSlideId).addClass('activeSlide').fadeIn(slideTransitionDuration, function(){
						loadIndustryVideo(newSlideId);
					});
					$('#tabs a').not('.activeTab').animate({ opacity: poppedOpacity }, opacityAnimationDuration );
				 });
				
			} else {
			
				if ( currentSlideId != newSlideId) {
				
					$('.activeTab').removeClass('activeTab').stop().animate({ opacity: poppedOpacity }, opacityAnimationDuration );
					$(this).addClass('activeTab');

					$(currentSlideId).removeClass('activeSlide').fadeOut(slideTransitionDuration, function(){
						$('.video').empty();
					});
					$(newSlideId).addClass('activeSlide').fadeIn(slideTransitionDuration, function(){
						loadIndustryVideo(newSlideId);
					});
				}
			
			}
			
		} else { // This isn't a tab that opens a slide, so it must be 'why ciena?'
			
			if ($('#tabs_wrapper').hasClass('popped')) {
				closeIndustryTabs(newSlideId);
			} else {
				if ($.flash.available) {
					whyCienaVideos();
				} else {
					window.location = whyCienaNonFlashURL;
				}
			}
			
		}
		
		return false;
    });

	// Hover effect on tabs
	$('.popped #tabs a').live('mouseover', function(){
		$(this).not('.activeTab').stop().animate({opacity: poppedOpacityOver}, opacityAnimationDuration);
	});
	$('.popped #tabs a').live('mouseout', function(){
		$(this).not('.activeTab').stop().animate({ opacity: poppedOpacity }, opacityAnimationDuration );
	});
	
	function loadIndustryVideo(newSlideId) {
		if ($.flash.available) {
			$(newSlideId + ' .video').flash({
				swf: jwPlayer,
				id: 'videoplayer' + newSlideId.replace('#', ''),
				name: 'videoplayer' + newSlideId.replace('#', ''),
				width: 513,
				height: 323,
				encodeParams: true,
				wmode: 'opaque',
				allowFullScreen: true,
				bgcolor: '#000000',
				flashvars: {
					config: playerConfig,
					file: '/public/swf/' + videoIds[newSlideId] + '.xml'
					//file: 'http://ciena.com/swf/playlist.php?id=' + videoIds[newSlideId]
				}
			});
		} else {
			$('.home-video-player-noflash-container').fadeIn('fast');
		}
	}

	function closeIndustryTabs(clickedTab) {
		
		if (clickedTab == '#tab4' && !$.flash.available) {
			window.location = whyCienaNonFlashURL;
			return;
		}
		
		$("#tabs_wrapper").stop().animate({ height: tabsWrapperHeightClosed + 'px' }, tabsWrapperOpenDuration, function() {
		    
			$(this).removeClass('popped');
			$('.activeSlide').removeClass('activeSlide').hide();
			
			nonFlashAnimation('open');
			
			$('#tabs a').removeClass('activeTab').animate({ opacity: poppedOpacityOver }, opacityAnimationDuration, function(){
				$('.video').empty();
				if ($.flash.available) {
					resumeHomeFlash();
				}
			});
			
			if (clickedTab == '#tab4' && $.flash.available) {
				whyCienaVideos();
			}
			
		 });
	}
	
	// Close the slides
	$('a.close-tabs').click(function(){
		closeIndustryTabs();
		return false;
    });
	
	// IE6 functionality fixes and alert to upgrade
	if (IE6) {
		
		// Get rid of background image flicker on links
		try {
		  document.execCommand("BackgroundImageCache", false, true);
		} catch(err) {}
		
		// Script for navigation in IE6
		$("#pri_navigation li").hover(function(){
			$("div", $(this)).addClass("js");
			$(this).addClass("ie6hover");
		},function(){
			$(this).removeClass("ie6hover");
			$("div", $(this)).removeClass("js");
		});
		
		// IE6 Alert
		if (get_cookie("closeIE6Alert") != "true") {
            $('#ie6-alert-container').show();
		}

		$('#ie6-alert-container #ie6-alert #ie6-alert-close-container .ie6-alert-close').click(function() {
			$('#ie6-alert-container').hide('fast');
			document.cookie="closeIE6Alert=true";
			return false;
		});
		
	}

	// Boomerang community link puller
	function gotoBoom() {
	    window.location = $('.ptcWidgetRecentBlogEntryLink a').attr('href');
	}
	
	// Track social media link clicks
	$('.social-media-links a').click(function(){
		var socialMedia = $(this).attr('title').toLowerCase(),
			s=s_gi('cienacom');
		s.linkTrackVars='eVar32,events';
		s.linkTrackEvents='event10';
		s.eVar32=socialMedia;
		s.events='event10';
		s.tl(this,'o',socialMedia);
	});

	// Homepage search focus
    $('#w').unbind("focus");
    $('#w').focus();
    $('#w').attr('value', 'search');
    $('#w').select();

	$('#w').keypress(function(event) {
	    if ($(this).attr('value') == "search") {
	        $(this).attr('value', '');
	    }
	});

});