var flashComplete = false,
	feedComplete = false;

$(function() {
	setUpTopMenu();
	
	//if we're on the homepage, set up the hover effects
	if ($('#center_area.home').length > 0) {
		if (!has_viewed_flash)
			establishSplashFlash();
		else
			removeFlashSplash();
		
		establishManagementHover();
		establishLeavittHover();
		establishFlowplayer();
		establishFeed();
	}
});

/**
 * Sets up the top menu hover effects
 */
function setUpTopMenu() {
	var menuItems = {
			aboutUs: {
				$menuItem: $('#header').find('.menu_left a.about_us'),
				$dropdownItems: $('#header').find('.about_us_bg, .about_us_menu'),
				timer: 0
			},
			oncologyCompounds: {
				$menuItem: $('#header').find('.menu_left a.oncology_compounds'),
				$dropdownItems: $('#header').find('.oncology_compounds_bg, .oncology_compounds_menu'),
				timer: 0
			},
			investorRelations: {
				$menuItem: $('#header').find('.menu_right a.investor_relations'),
				$dropdownItems: $('#header').find('.investor_relations_bg, .investor_relations_menu'),
				timer: 0
			},
			newsEvents: {
				$menuItem: $('#header').find('.menu_right a.news_events'),
				$dropdownItems: $('#header').find('.news_events_bg, .news_events_menu'),
				timer: 0
			}
		},
		$headerLogo = $('#header img.logo');
	
	$.each(menuItems, function(i, el) {
		el.$menuItem.bind({
			mouseover: function() {
				clearTimeout(el.timer);
				
				$.each(menuItems, function(i2, el2) {
					if (i2 != i)
						el2.$dropdownItems.stop(true, true).hide();
				});
				
				el.$dropdownItems.stop(true, true).slideDown(350);
			},
			mouseout: function() {
				el.timer = setTimeout(function() {
					el.$dropdownItems.fadeOut(150);
				}, 150);
			}
		});
		
		
		el.$menuItem.hover(function() {
			el.$dropdownItems.slideDown(350);
		});
		
		el.$dropdownItems.bind({
			mouseover: function() {
				clearTimeout(el.timer);
			},
			mouseout: function() {
				el.timer = setTimeout(function() {
					el.$dropdownItems.fadeOut(150);
				}, 300);
			}
		});
	});
}

var flashSet = false;

/**
 * Sets the splash page flash into motion if the user has flash
 */
function establishSplashFlash() {
	var flashvars = {
			assets: base_url + "flash/resources/"
		},
		params = {
			play: "true",
			menu: "false",
			quality: "best",
			scale: "noScale",
			salign: "top",
			devicefont: "false",
			allowfullscreen: "true",
			allowscriptaccess: "always",
			allownetworking: "all",
			wmode: "transparent"
		},
		attributes = {
			align: "top",
			bgColor: "#FFFFFF"
		};
	
	if (swfobject.hasFlashPlayerVersion("10.0.0")) {
		swfobject.embedSWF(
			base_url + "flash/cytrx.swf", "flash_splash", "1280", "754", "10.0.0", null, flashvars, params, attributes
		);
		
		flashSet = true;
		
		$('#center_area').hide();
		
		setTimeout(function() {
			$('#flash_splash').css({'margin-top': '-44px'});
		}, 100);
	} else
		$('#center_area').show();
}

/**
 * Kills the flash splash
 */
function removeFlashSplash() {
	swfobject.removeSWF('flash_splash');
	$('#center_area').show();
	flashComplete = true;
	
	if (flashComplete && feedComplete) {
		setTimeout(establishPipelineHover, 500);
	}
}

/**
 * Sets up the homepage hover effect for the pipeline
 */
function establishPipelineHover() {
	var hoverTimer,
		$learn_more = $('#center_area .left_text a.learn_more'),
		lm_position = $learn_more.position(),
		image_offset = ($.browser.msie && (parseInt($.browser.version) < 10)) ? 463 : 462;
	
	$('#center_area .left_text div.pipeline_hover').css({top: (lm_position.top - image_offset) + 'px'});
	
	//pipeline homepage hover
	$('#center_area .left_text a.learn_more').hover(function() {
		$('#center_area .left_text div.pipeline_hover').show();
	});
	
	$('#center_area .left_text div.pipeline_hover').bind({
		mouseover: function() {
			clearTimeout(hoverTimer);
		},
		mouseout: function() {
			var $this = $(this);
			
			hoverTimer = setTimeout(function() {
				$this.fadeOut(150);
			}, 300);
		}
	});
}


/**
 * Sets up the homepage hover effect for the management team
 */
function establishManagementHover() {
	var hoverTimer,
		mtHoverTimer,
		$hoverElements = $('#center_area div.cfo_bottom_hover, #center_area div.cfo_top_hover'),
		$managementHover = $('#center_area div.management_hover'),
		draggerReset = function() {
			$dragger.css('top', '0');
		};
	
	//cfo homepage hover
	$hoverElements.bind({
		mouseover: function() {
			clearTimeout(hoverTimer);
			$managementHover.show();
			$('#center_area div.leavitt_hover').hide();
		},
		mouseout: function() {
			hoverTimer = setTimeout(function() {
				$managementHover.fadeOut(150, draggerReset);
			}, 300);
		}
	});
	
	$managementHover.bind({
		mouseover: function() {
			clearTimeout(hoverTimer);
		},
		mouseout: function() {
			hoverTimer = setTimeout(function() {
				$managementHover.fadeOut(150, draggerReset);
			}, 300);
		}
	});
	
	//set up the dragger
	var $descrip = $managementHover.find('.description p'),
		$dragger = $('#center_area div.management_hover div.dragger'),
		isDragging = false;
	
	$descrip.scroll(function(e) {
		var $this = $(this),
			maxScrollTop = 251,
			draggerEnd = 89,
			currentPosition = $this.scrollTop() / maxScrollTop * draggerEnd;
		
		if (!isDragging) {
			$dragger.animate({top:currentPosition}, 70);
		}
	});
	
	$dragger.draggable({
		axis: 'y',
		containment: 'parent',
		drag: function(e) {
			var maxScrollTop = 251,
				draggerStart = 0,
				draggerEnd = 89,
				currentPosition = maxScrollTop * (parseFloat($dragger.css('top')) / draggerEnd);
			
			isDragging = true;
			
			$descrip.scrollTop(currentPosition);
		},
		stop: function() {
			isDragging = false;
		}
	})
	
	
	//set up the management team hover
	var outsideArea = false;
	
	$('#center_area a.management_team').hover(function() {
		$('#center_area div.management_team_container').show();
	});
	
	$('#center_area div.management_team_container').bind({
		mouseover: function() {
			clearTimeout(hoverTimer);
			
			if (!outsideArea)
				clearTimeout(mtHoverTimer);
		},
		mouseout: function() {
			var $this = $(this);
			
			mtHoverTimer = setTimeout(function() {
				$this.fadeOut(150);
			}, 300);
		},
		mousemove: function(e) {
			var $this = $(this);
			
			if ( e.offsetY > 275 && (e.offsetX < 134 || e.offsetX > 294) ) {
				outsideArea = true;
				
				mtHoverTimer = setTimeout(function() {
					$this.fadeOut(150);
					outsideArea = false;
				}, 300);
			} else {
				outsideArea = false;
			}
		}
	});
}


/**
 * Sets up the homepage hover effect for leavitt
 */
function establishLeavittHover() {
	var hoverTimer,
		mtHoverTimer,
		$hoverElements = $('#center_area div.leavitt_bottom_hover, #center_area div.leavitt_top_hover'),
		$managementHover = $('#center_area div.leavitt_hover'),
		draggerReset = function() {
			$dragger.css('top', '0');
		};
	
	//cfo homepage hover
	$hoverElements.bind({
		mouseover: function() {
			clearTimeout(hoverTimer);
			$managementHover.show();
			$('#center_area div.management_hover').hide();
		},
		mouseout: function() {
			hoverTimer = setTimeout(function() {
				$managementHover.fadeOut(150, draggerReset);
			}, 300);
		}
	});
	
	$managementHover.bind({
		mouseover: function() {
			clearTimeout(hoverTimer);
		},
		mouseout: function() {
			hoverTimer = setTimeout(function() {
				$managementHover.fadeOut(150, draggerReset);
			}, 300);
		}
	});
	
	//set up the dragger
	var $descrip = $managementHover.find('.description p'),
		$dragger = $('#center_area div.leavitt_hover div.dragger'),
		isDragging = false;
	
	$descrip.scroll(function(e) {
		var $this = $(this),
			maxScrollTop = 251,
			draggerEnd = 89,
			currentPosition = $this.scrollTop() / maxScrollTop * draggerEnd;
		
		if (!isDragging) {
			$dragger.animate({top:currentPosition}, 70);
		}
	});
	
	$dragger.draggable({
		axis: 'y',
		containment: 'parent',
		drag: function(e) {
			var maxScrollTop = 251,
				draggerStart = 0,
				draggerEnd = 89,
				currentPosition = maxScrollTop * (parseFloat($dragger.css('top')) / draggerEnd);
			
			isDragging = true;
			
			$descrip.scrollTop(currentPosition);
		},
		stop: function() {
			isDragging = false;
		}
	})
}

function establishFlowplayer() {
	var execPlayer,
		pipePlayer,
		closeCallback = function() {
			if (execPlayer)
				execPlayer.unload();
			if (pipePlayer)
				pipePlayer.unload();
			
			$('#center_area').show();
			$('#executive_video').hide();
			$('#pipeline_video').hide();
			$('#close_flow_player').hide();
		};
	
	$('#center_area.home div.management_hover a.executive_video').click(function() {
		$('#center_area').hide();
		$('#close_flow_player').show();
		
		if (swfobject.hasFlashPlayerVersion("10.0.0")) {
			$('#executive_video').show();
			
			flowplayer('executive_video_player', {
				src: base_url + 'js/flowplayer/flowplayer-3.2.7.swf',
				wmode: 'transparent'
			}).onFinish(closeCallback);
		} else {
			$('#video_js_executive_video').show();
			
			execPlayer = VideoJS.setup("executive_video_html", {
				controlsBelow: false
			});
			
			execPlayer.play();
			
			execPlayer.onEnded(function() {
				$('#video_js_executive_video').hide();
				$('#center_area').show();
			});
		}
	});
	
	$('#center_area.home div.leavitt_hover a.pipeline_video').click(function() {
		$('#center_area').hide();
		$('#close_flow_player').show();
		
		if (swfobject.hasFlashPlayerVersion("10.0.0")) {
			$('#pipeline_video').show();
			
			flowplayer('pipeline_video_player', {
				src: base_url + 'js/flowplayer/flowplayer-3.2.7.swf',
				wmode: 'transparent'
			}).onFinish(closeCallback);
		} else {
			$('#video_js_pipeline_video').show();
			
			pipePlayer = VideoJS.setup("pipeline_video_html", {
				controlsBelow: false
			});
			
			pipePlayer.play();
			
			pipePlayer.onEnded(function() {
				$('#video_js_pipeline_video').hide();
				$('#center_area').show();
			});
		}
	});
	
	$('#close_flow_player').click(closeCallback);
}

function establishFeed() {
	jQuery.jGFeed('http://phx.corporate-ir.net/corporate.rss%3Fc=187775%26Rule=Cat=news', function(feed) {
		var html = '', date, dateString;
		
		if (feed && feed.entries.length > 0) {
			
			for (var i = 0; i < 3; i++) {
				date = new Date(feed.entries[i].publishedDate);
				dateString = (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear();
				
				html += '<p class="news_and_events">' + dateString + ' ' + feed.entries[i].title + 
						' <a href="' + feed.entries[i].link + '" target="_blank">Learn more</a>';
			}
			
			$('#news_and_events_list').html(html);
		}
		
		feedComplete = true;
	
		if (flashComplete && feedComplete)
			establishPipelineHover();
	}, 5);
}

