/* Declare a namespace for the site */
var Site = window.Site || {};

var Revoplan = {};
var lsection = '';
var bubble_timeout = {};
var menu_timeout = {};
var person_click = false;

/* Create a closure to maintain scope of the '$'
   and remain compatible with other frameworks.  */
//(function($) {
var images_loaded = 0;
var ishcp = false;
	
	
//////////////////////////////////////
// Check cookie
	var opanaCookie = new JavaScriptCookie();
	var opanaHomePage = opanaCookie.get_cookie('opanaHomePage');
	
		if(opanaHomePage != null && opanaHomePage != '') {
			
			if(opanaHomePage == "doctors") {
				ishcp = true;
			}
			/*if(opanaHomePage == "patients") {
				//Do Something
			}
*/

		} else {
			
			//Do Something
			opanaCookie.set_cookie('opanaHomePage', '');
		
		}

	
			  
	 ////////////////////////////////////
	 // Document.ready()
	
	//same as $(document).ready();
	//$(function() {
	$(document).ready(function(){	
							   
			// Preload blur images
			preload([
			    '_/img/people/patient1_blur.png',
			    '_/img/people/patient2_blur.png',
			    '_/img/people/patient2_blur.png',
			    '_/img/people/doctor1_blur.png',
			    '_/img/people/doctor2_blur.png',
			    '_/img/people/doctor3_blur.png'
			]);
			
			if(ishcp) {
				$("#logo").removeClass("patientlogo");
				$("#logo").addClass("hcplogo");
				$("#complextext").show();
			
			}
			
			
	//		$('a').hide().remove('style');
			
			// Activate revoman
			//$('.person').revoMan();
			
		
			
			
			
				
		/*
	$('#a-red-arches').click(function(){
					// // // //////.log('hello');
					slide_screen('-');
					return false;
				
			});
			
			
			$('#a-blue-arches').click(function(){
					
					slide_screen('+');
					return false;
					
									
			});
*/
			
			$('#blue-arches img, #red-arches img').bind('load',function(){
				// // //////.log('Loaded');
				images_loaded++;
				
				if(images_loaded == 2){
					// // //////.log('both images loaded');
					//alert('reveal blue');
					//reveal_blue(Revoplan);
					//reveal_red();

				}
			});
			
			
			reveal_blue(Revoplan);
			
					  	    
	 		init_menu();
	 

	      		init_person();	 
	      		 
	      		 	
	    	      		
	      		 
	    if($.browser.msie && $.browser.version == '7.0')
		{
			$("#HCP").css("background-position","0 -20px");
			$("#shadowstretch").css("width", (($("body").outerWidth() - 990) / 2) + 1);
		}

	}); // end doc.ready



	
		
	var slide_screen = function(ldir,lamt,ldelay){
			$("#intro-overlay").hide();
			$('#blue-arches, #red-arches').stop().delay(ldelay).animate({
						    	left: ldir+'='+lamt
						  	}, 300, function() {
						  	
						  								  		
			});
			
	}
	
	
	function reveal_blue(lobj){
			// // //////.log('function reveal blue:');
			$('#blue-arches').show().animate({
						    	height: 2136+'px'
						  	}, 800, function() {
						  		// // //////.log('calling reveal red');
						  		reveal_red();
						  		
						});
	
	}
	
	var reveal_red = function(){
			// // //////.log('function reveal red');
			
			$('#red-arches').show().animate({
						    	height: 2136+'px'
						  	}, 1600, function() {
						  		//init_everything();
						  		
						  		// $('a').fadeIn(200).removeAttr("style");
							
							
							
							
							
							
							if(opanaHomePage == 'doctors'){
								//show_hcps();
							}else if(opanaHomePage == 'patients' ){
								//show_patients();
							}else{													
								$('#intro').delay(300).ieFade({ 
									fadeSpeed:400,
									fadeType: 'in',
									fadeMethod: 'fadeIn'
								});
							}
							
							$('#logo').ieFade({ 
									fadeSpeed:200,
									fadeType: 'in',
									fadeMethod: 'fadeIn'
								});
							$('#menu').ieFade({ 
									fadeSpeed:200,
									fadeType: 'in',
									fadeMethod: 'fadeIn'
								});
							
						});
	
	}
	
	
	var init_bubbles = function(){
		
		// $('.person').each(function(){
		// 		
		// 		// var lpos = $(this).position();
		// 		// 		
		// 		// 		var bubble = '<div id="patient-resources-bubble" class="bubble" style="top:'+lpos.top+';left:'+lpos.left+'">';
		// 		// 		// Add content-wrap
		// 		// 		
		// 		// 		// Add question
		// 		// 		bubble += $(this).attr('alt');		
		// 		//             // Add link
		// 		// 		bubble += '<a href="#">Resources</a>';
		// 		//             bubble += '</div>';
		// 		// 		
		// 		// 		
		// 		// 		// //////.log(bubble);
		// 		// 		
		// 		// 		$(this).after(bubble);
		// 		
		// 	});
		// 	
		
	}
	
	
	/////////////////////////////////////////////////////////////
	// Show HCPs
	
	var show_hcps = function(){
		
		var lslide = 0;
		
		
		// Set cookies
		opanaCookie.set_cookie('opanaHomePage', 'doctors');
		
		// Swap logo image
		$("#logo").removeClass("patientlogo");
		$("#logo").addClass("hcplogo");
		$("#complextext").show();
		
		// hide intro
		if($('#intro').is(':visible')){
			$('#intro').ieFade({ 
									fadeSpeed:100,
									fadeType: 'out',
									fadeMethod: 'fadeOut'
								});
		}
		
		if(lsection != 'hcp'){
			// exit currnt people
		
			if(lsection == 'patients'){
				hide_bubbles();
				exit_patients();
				lslide = 512;
				ldelay = 800;
				lshow_delay = 1000;
			}else{
				lslide = 281;
				ldelay = 0;
				lshow_delay = 0;
			}
			slide_screen('+',lslide,ldelay);
			
			
				
		}else{
			return false;
		}
		
		// swap z indexes
		$('#red-people').css({'z-index':'31'});
		$('#blue-people').css({'z-index':'30'});	
		
			// show toolbar
			$('#tools-nav').animate({
						    	right: '0px'
						  	}, 400, function() {

			});
		
		lsection = 'hcp';
		
		$('.red-front').delay(lshow_delay).revoMan(
					'animate',
					{
							color: 'red',
							slot: 'slot_1',
							action: 'enter',
							state: 'sele'
						}
			
					
		);
		
		$('.red-slot-2').delay(lshow_delay).revoMan(
				'animate',
				{
						color: 'red',
						slot: 'slot_2',
						action: 'enter',
						state: 'med_doc'
					}


		);
		
			$('.red-slot-3').delay(lshow_delay).revoMan(
					'animate',
					{
							color: 'red',
							slot: 'slot_3',
							action: 'enter',
							state: 'small',
							callBack: 'show_bubbles'
						}


			);
		
		//person_click = false;
	
	}
	
	/////////////////////////////////////////////////////////////
	// Show Patients
	
	var show_patients = function(){
		
		var lslide = 0;
		
		// set cookie
		opanaCookie.set_cookie('opanaHomePage', 'patients');
		
		// hide intro
		if($('#intro').is(':visible')){
				$('#intro').ieFade({ 
									fadeSpeed:100,
									fadeType: 'out',
									fadeMethod: 'fadeOut'
								});
			}
		
		if(lsection != 'patients'){
			// exit currnt people
		
			if(lsection == 'hcp'){
				hide_bubbles();
				exit_doctors();
				lslide = 512;
				ldelay = 800;
				lshow_delay = 1000;
				
			}else{
				lslide = 231;
				ldelay = 0;
				lshow_delay = 0;
			}
			
			slide_screen('-',lslide,ldelay);
				
		}else{
			return false;
		}
			
		// swap z indexes
		$('#red-people').css({'z-index':'30'});
		$('#blue-people').css({'z-index':'31'});
		
		// show toolbar
	//	alert('show patient tools');
		
		
		
	$('#patient-tools-nav').animate({
			right: '0px'
		}, 400, function() {

		});
		
		lsection = 'patients';
	

			
			// Show blue peopel
			
			$('.blue-front').delay(lshow_delay).revoMan(
					'animate',
					{
							color: 'blue',
							slot: 'slot_1',
							action: 'enter',
							state: 'sele'
						}
			
					
			);
			
		//////////////////////////////////////////////////////
		// Slot 2
		$('.blue-slot-2').delay( (lshow_delay + 200) ).revoMan(
				'animate',
					{
							color: 'blue',
							slot: 'slot_2',
							action: 'enter',
							state: 'med'
						}
			);

			
		////////////////////////////////////////////////////////////////////////////////////////
		/// Slot 3
	
		$('.blue-slot-3').delay( (lshow_delay + 400) ).revoMan(
				'animate',
					{
							color: 'blue',
							slot: 'slot_3',
							action: 'enter',
							state: 'small',
							callBack: 'show_bubbles'
							
						}
		);



						  								  		
		//person_click = false;				  	
		$("#logo").addClass("patientlogo");
		$("#logo").removeClass("hcplogo");
		$("#complextext").hide();
	}
	
	
	
	//////////////////////////////////////////////////////////
	// Exit Patients
	var exit_patients = function(){
		
		$('.blue-front').revoMan(
			'animate',
			{
					color: 'blue',
					slot: 'slot_1',
					action: 'exit',
					state: 'sele_out',
					opacity: 0,
					reEntry: 'blue-front-enter'
			}
		);
		
		$('.blue-slot-2').delay(200).revoMan(
			'animate',
			{
				color: 'blue',
				slot: 'slot_2',
				action: 'exit',
				state: 'sele',
				opacity: 0,
				reEntry: 'blue-2-enter'
			}
		);
		
		// Move 3 to slot 2
		$('.blue-slot-3').delay(400).revoMan(
			'animate',
			{
				color: 'blue',
				slot: 'slot_3',
				action: 'exit',
				state: 'med',
				opacity: 0,
				reEntry: 'blue-3-enter'
			}
		);
		
			$('#patient_switch').ieFade({ 
									fadeSpeed:100,
									fadeType: 'out',
									fadeMethod: 'fadeOut'
								});
			
				// hide toolbar
				$('#patient-tools-nav').animate({
							    	right: '-48px'
							  	}, 400, function() {

				});
	
	};
	
	
	//////////////////////////////////////////////////////////
	// Exit Doctors
	var exit_doctors = function(){
			
			$('.red-front').revoMan(
				'animate',
				{
						color: 'red',
						slot: 'slot_1',
						action: 'exit',
						state: 'sele_out',
						opacity: 0,
						reEntry: 'red-front-enter'
				}
			);
			
			
				$('.red-slot-2').delay(200).revoMan(
					'animate',
					{
							color: 'red',
							slot: 'slot_2',
							action: 'exit',
							state: 'sele',
							opacity: 0,
							reEntry: 'red-2-enter'
						}


			);
			
			$('.red-slot-3').delay(400).revoMan(
					'animate',
					{
							color: 'red',
							slot: 'slot_3',
							action: 'exit',
							state: 'med',
							opacity: 0,
							reEntry: 'red-3-enter'
						}


			);
			
			$('#doctor_switch').ieFade({ 
									fadeSpeed:200,
									fadeType: 'out',
									fadeMethod: 'fadeOut'
								});
			
				// hide toolbar
				$('#tools-nav').animate({
							    	right: '-48px'
								}, 400, function() {

				});
			
		
		
	}
	
	
	/////////////////////////////////////////////////////////
	// Show Bubbles
	
	var show_bubbles = function(){
			
			// console.log('show-bubbles');
			if(lsection == 'patients'){
				ljq = '#blue-people .person';
				$('#patient_switch').ieFade({ 
									fadeSpeed:200,
									fadeType: 'in',
									fadeMethod: 'fadeIn'
								});
			}else{
				ljq = '#red-people .person';
				$('#doctor_switch').ieFade({ 
									fadeSpeed:200,
									fadeType: 'in',
									fadeMethod: 'fadeIn'
								});
			}
			
			// console.log(ljq);
			$(ljq).each(function(){

				// Add bubble
				var lpos = $(this).position();
				var lbubble = $('#'+$(this).attr('id')+'-bubble');
				var lb_hidden = lbubble.find('div.q')
				var la_hidden = lbubble.find('div.a');
				//lbubble.attr('d_height',lb_hidden.height());
				lb_hidden.height(0);
				if(la_hidden.length > 0){
					la_hidden.height(0);
				}
				// bottom
				var lbottom = (2136 - ((+lpos.top) + (+$(this).attr('off_bottom'))));
				// left	
				var lleft = (+lpos.left) + (+$(this).attr('off_left'));
			
				if(lbubble.attr('id') == 'doctor_opana-bubble'){
					
					var ltop = (+lpos.top) + (+$(this).attr('off_bottom'));
					
					lbubble.css({
							'top' : ltop,
							'left' : lleft
					});

				}else{
				
					lbubble.css({
							'top' : 'auto',
							'left' : lleft,
							'bottom' : lbottom
					});
				
				}
				
				lbubble.ieFade({ 
									fadeSpeed:100,
									fadeType: 'in',
									fadeMethod: 'fadeTo'
								});;
				
			});
			
			person_click = false;
			
			
	};
	
	var hide_bubbles = function(){
			var ljq = '';
			if(lsection == 'patients'){
				ljq = '#blue-people .bubble';
			}else{
				ljq = '#red-people .bubble';
			}
		$(ljq).ieFade({ 
									fadeSpeed:200,
									fadeType: 'out',
									fadeMethod: 'fadeOut'
								});
	}
	
	
	
	/////////////////////////////////////////////////////////
	// Init person
	
	var init_person = function(){
		
		
		
		
		
		// Add blur
		$('.person').mouseenter(function(){
		
				// Attach blur
				$(this).attr('src',$(this).attr('sele'));
			
				reveal_bubbles($(this));
				
				
					
				
			
				
				
		});
		
	
		
		// Remove blur
		$('.person').mouseleave(function(){
		
			var lbubble = $('#'+$(this).attr('id')+'-bubble');
			var ldiv = lbubble.find('div.q');
			var ldiv = lbubble.find('div.q');
			var lperson = $(this);
			
			bubble_timeout[$(this).attr('id')+'-bubble'] = setTimeout(function(){
					
					//.log('were timing out!');
					
					ldiv.stop().animate({
							height: '0px'
						}, 50, function() {
							
					});
					
					var ladiv = lbubble.find('div.a');
					if(ladiv.length > 0){
								ladiv.stop().animate({
										height: '0px'
									}, 150, function() {
										
								});
						
					}
					
					
				
					
					
			
			},300);
			
			
			
			// console.log('CLICK: %s',person_click);
			if(person_click == false){
				   
				    var ljq = '';
					// Hide the others
					if(lsection == 'patients'){
						ljq = '#blue-people .person';
					}else{
						ljq = '#red-people .person';
					}
					
					$(ljq).not(lperson).each(function(){
						
							$(this).attr('src',$(this).attr('sele'));
							
							// show bubble
							var lbubble = $('#'+$(this).attr('id')+'-bubble');
							
							lbubble.stop().ieFade({ 
									fadeSpeed:200,
									fadeType: 'in',
									fadeMethod: 'fadeTo'
								});
						
						
					});
			}
			
				
			
		
		});
		
		
		$('.person').each(function(){
		
			// Add bubble
			var lpos = $(this).position();
			var lperson = $(this);
				
			var bubble = '<div id="'+$(this).attr('id')+'-bubble" class="bubble">';
				// Add content-wrap
			bubble += '<div class="top"></div>';
			// Add question
			bubble += '<div class="mid">'
			
			// Add question
			bubble += '<div class="q"><a href="'+$(this).attr('href')+'">'+$(this).attr('que')+'</a></div>';
		         // Add link
			bubble += '<a href="'+$(this).attr('href')+'">'+$(this).attr('alt')+'</a>';
			
			// check for special doctor opana
			if($(this).attr('id') == 'doctor_opana'){
				
				bubble += '<div class="a">';
				
				$('#about_opana li').each(function(){
					bubble += $(this).html();
				});
				
				bubble += '</div>';
				
				
			}
			
			
			// add borders and corners
			bubble += '</div>';
			bubble += '<div class="btm"></div>';
		    bubble += '</div>';
			
			
				// //////.log(bubble);
			
			$(this).after(bubble);
			
			var lbubble_sele = '#'+$(this).attr('id')+'-bubble';
			
			$(lbubble_sele).mouseenter(function(){
				
				//.log('enter that bubble');
				clearTimeout(bubble_timeout[$(this).attr('id')]);
				
				var lperson = $(this).attr('id');
				lperson = lperson.split('-');
				// console.log(lperson);
				reveal_bubbles($('#'+lperson[0]));
			
			})
			
			
			
			$(lbubble_sele).mouseleave(function(){
					//.log('leave that bubble');
					
					var lbubble = $(this);
					var ldiv = lbubble.find('div.q');
					var ldiv = lbubble.find('div.q');
					ldiv.stop().animate({
							height: '0px'
						}, 50, function() {
							
					});
					
						var ladiv = lbubble.find('div.a');
					if(ladiv.length > 0){
								ladiv.stop().animate({
										height: '0px'
									}, 150, function() {
										
								});
						
					}
					
					
					var ljq = '';
					// Hide the others
					if(lsection == 'patients'){
						ljq = '#blue-people .person';
					}else{
						ljq = '#red-people .person';
					}
					
					$(ljq).not(lperson).each(function(){
						
							$(this).attr('src',$(this).attr('sele'));
							
							// show bubble
							var lbubble = $('#'+$(this).attr('id')+'-bubble');
							
							lbubble.stop().ieFade({ 
									fadeSpeed:200,
									fadeType: 'in',
									fadeMethod: 'fadeTn'
								});
						
						
					});
					

			});
			
		
		});
		
		
		
	
	
	};
	
	
	/////////////////////////////////////////////////////////
	// Show bubbles
	var reveal_bubbles = function(lperson){
			
					
				
			var lbubble = $('#'+lperson.attr('id')+'-bubble');
			var ldiv_height = (+lperson.attr('d_height'));
			var ldiv = lbubble.find('div.q');
			
			
			ldiv.stop().animate({
					height: (ldiv_height)+'px'
				}, 150, function() {
					// ldiv.animate({
					// 							height: ldiv_height+'px'
					// 						}, 75,function() {
					// 					});
			});
			var ladiv = lbubble.find('div.a');
			if(ladiv.length > 0){
						ladiv.stop().animate({
								height: '135px'
							}, 200, function() {
								// ldiv.animate({
								// 							height: ldiv_height+'px'
								// 						}, 75,function() {
								// 					});
						});
				
			}
			
			
			var ljq = '';
			// Hide the other bubbles
			if(lsection == 'patients'){
				ljq = '#blue-people .person';
			}else{
				ljq = '#red-people .person';
			}
			
			////.log(ljq);
			
			$(ljq).not(lperson).each(function(){
			
				 var lperson = $(this);
				 lperson.attr('src',lperson.attr('blurr'));
				
				// hide bubble
				var lbubble = $('#'+$(this).attr('id')+'-bubble');
				lbubble.stop().ieFade({ 
									fadeSpeed:100,
									fadeType: 'out',
									fadeMethod: 'fadeTo'
								});
			
		
			});
	
	
	}
	
	
	
	
	
	
	/////////////////////////////////////////////////////////
	// Init Menu
	
	var init_menu = function(){
		
		// //////.log('init menu');
		
			// First level mouseover
			$('#nav ul li.menu').find('ul > li:first').css({'margin-top':'20px'});
			
			$('ul li.menu').mouseenter(function(){
				
				
				// clear existing timeout
				
				clearTimeout(menu_timeout[$(this).attr('id')]);
				
				$(this).addClass('sele');
				
				var lheight = $(this).attr('v_height');
				
				var lul = $(this).find('> ul');
				
				lul.stop().animate({
				  	height: lheight+'px'
				  }, 200, function() {
				    // Animation complete.
				  
				});
				
				
			});
			
			$('ul li.menu > ul li:has(ul)').mouseenter(function(){
					//////.log('animate: %s',$(this));
					//////.log($(this));
					clearTimeout(menu_timeout[$(this).attr('id')]);
					var lheight = $(this).attr('m_height');
					$(this).addClass('sele');
					$(this).parent().animate({
					  	width: '470px',
						height: lheight+'px'
					}, 150, function() {
					    // Animation complete.
					   //alert('done');
					});
			});
			
			
			$('ul li.menu').mouseleave(function(){
			
				var lmenu = $(this);
				menu_timeout[$(this).attr('id')] = setTimeout(function(){
										
					lmenu.removeClass('sele');
					
					var lul = lmenu.find('> ul');
					
					lul.stop().animate({
					  	height: '0px'
					  }, 200, function() {
					    // Animation complete.
					   
					 });
				},350);	 
			}); 
			
			$('ul li.menu > ul li:has(ul)').mouseleave(function(){
					//////.log('animate: %s',$(this));
					//////.log($(this));
					var lmenu = $(this);
					menu_timeout[$(this).attr('id')] = setTimeout(function(){
						lmenu.removeClass('sele');
						lmenu.parent().animate({
						  	width: '210px',
							height: '110px'
						}, 150, function() {
						    // Animation complete.
						   //alert('done');
						});
					},350);
			});
		
		
		
		// Add hover to tools menu
		$('#tools-nav, #patient-tools-nav').mouseenter(function(){
				// //////.log('MOUSE OVER TOOLS')
				$(this).stop().animate({
					width: '119px'
				},250);
		});
		
		$('#tools-nav, #patient-tools-nav').mouseleave(function(){
				// //////.log('MOUSE OUT TOOLS')
				$(this).stop().animate({
					width: '48px'
				},75);
		});
		
		
		$('#menu_patient').click(function(){
			//alert('animate');
			show_patients();
			//return false;
		});
		
		$('#menu_hcp').click(function(){
			show_hcps();
			//return false;
		});
		
		
		// Switch people
		$('#doctor_switch, #doctor_switch-bubble, #intro_patient').live('click',function(event){
			event.preventDefault();
			event.stopPropagation();
			person_click = true;
			show_patients();
			return false;			
		});
		
		// Switch people
		$('#patient_switch, #patient_switch-bubble, #intro_physician').live('click',function(event){
			event.preventDefault();
			event.stopPropagation();
			person_click = true;
			show_hcps();
			return false;			
		});

	  
	} // end init_menu
	
	
	
	function preload(arrayOfImages) {
	    $(arrayOfImages).each(function(){
	        $('<img/>')[0].src = this;
	        // Alternatively you could use:
	        // (new Image()).src = this;
	    });
	}

	 
	
