YAHOO.namespace("lebow");

YAHOO.lebow.init = function () { 
	// Define various event handlers for Dialog 
	var handleRegister = function() { 
	    alert('register');
	}; 
	var handleCancel = function() { 
	    alert('cancel');
	}; 

	YAHOO.util.Event.onAvailable('timeLeft', function () {
	   updateTimeLeft();
	   setInterval('updateTimeLeft()', 1000); 
	}); 
	
	YAHOO.lebow.event = new YAHOO.widget.Panel('lbShowEvent', 
														  { width: "500px",
														    fixedcenter: true, 
														    visible: false, 
														    constraintoviewport: true, 
														    modal: true
														   });
	
	YAHOO.lebow.event.render();
	
	/** SET UP OUR MAIN CONTENT TABS 
	YAHOO.lebow.tabView = new YAHOO.widget.TabView('MainContent');
	YAHOO.lebow.tabView.getTab(0).set('cacheData', true); 
	YAHOO.lebow.tabView.getTab(0).set('dataSrc', '/tabcontent.php?content=focus'); 
	YAHOO.lebow.tabView.getTab(1).set('cacheData', true); 
	YAHOO.lebow.tabView.getTab(1).set('dataSrc', '/tabcontent.php?content=news'); 
	YAHOO.lebow.tabView.getTab(2).set('cacheData', true); 
	YAHOO.lebow.tabView.getTab(2).set('dataSrc', '/tabcontent.php?content=life'); 
	YAHOO.lebow.tabView.getTab(3).set('cacheData', true); 
	YAHOO.lebow.tabView.getTab(3).set('dataSrc', '/tabcontent.php?content=research'); 
	YAHOO.lebow.tabView.getTab(4).set('cacheData', true); 
	YAHOO.lebow.tabView.getTab(4).set('dataSrc', '/tabcontent.php?content=podcasts'); 
	YAHOO.lebow.tabView.set('activeTab', tabView.getTab(0));                     
	
	
	YAHOO.util.History.register('tabs', initState, function (state) { 
                                                      tabView.set('activeIndex', state.substr(3); 
                                                    });
	YAHOO
   var hdlTVchange = function (e) { 

   };      
                                                    
   YAHOO.lebow.tabView.addListener('activeTabChange', hdlTVchange);                                                 
   **/ 
	//YAHOO.lebow.initTabs();
	
	try {
      YAHOO.util.History.initialize('yui-history-field', 'yui-history-iframe'); 
   } 
   catch (e) { 
     YAHOO.lebow.initTabs(); 
   } 
                                                    
	/** SET UP OUR EVENTS TABS **/ 
	var EventTabs = new YAHOO.widget.TabView('EventTabs'); 
	EventTabs.getTab(0).set('cacheData', true); 
	EventTabs.getTab(0).set('dataSrc', '/Newsroom/rest/event.php?action=today');
	EventTabs.getTab(1).set('cacheData', true); 
	EventTabs.getTab(1).set('dataSrc', '/Newsroom/rest/event.php?action=upcoming');
	EventTabs.set('activeTab', EventTabs.getTab(0));
	
	
	var tabLoaded = function(e) { 
	  var actTab = e.newValue; 
	  if (actTab.search('There are no events today') > 0) { 
	        EventTabs.set('activeIndex', 1);
	  } 
   	  els = YAHOO.util.Dom.getElementsByClassName('dyEventLink');
   	  var showEvent = function (e) { 
   	     var eid = this.id; 
   	     rx = /^Event_(\d*)$/; 
   	     eid = eid.replace(rx, '$1'); 
   	     YAHOO.lebow.getEvent(eid);
   	  } 
   	  
   	  YAHOO.util.Event.addListener(els, 'click', showEvent);   
	} 
	EventTabs.getTab(0).addListener('contentChange', tabLoaded);
	EventTabs.getTab(1).addListener('contentChange', tabLoaded);
	
	//YAHOO.lebow.loadEvents('/Newsroom/Events/xml.php?action=list&sDate=12-11-2007&eDate=12-11-2007', 'Today'); 
	
} 

	function hdlTVChange(e) { 
   var newState, currentState;
   var tabInx = this.getTabIndex(e.newValue);
   newState = "tab" + tabInx;
   
   try { 
      currentState = YAHOO.util.History.getCurrentState('tabs'); 
      
      if (newState != currentState) { 
         YAHOO.util.History.navigate('tabs', newState);  
      }   
   }
   catch (e) { 
      tabView.set('activeIndex', newState.substr(3));
   }
} 

YAHOO.lebow.initTabs = function () { 
   YAHOO.lebow.tabView = new YAHOO.widget.TabView('MainContent');
   
	YAHOO.lebow.tabView.getTab(0).set('cacheData', true); 
	YAHOO.lebow.tabView.getTab(0).set('dataSrc', '/tabcontent.php?content=focus');
	YAHOO.lebow.tabView.getTab(1).set('cacheData', true); 
	YAHOO.lebow.tabView.getTab(1).set('dataSrc', '/tabcontent.php?content=news'); 
	YAHOO.lebow.tabView.getTab(2).set('cacheData', true); 
	YAHOO.lebow.tabView.getTab(2).set('dataSrc', '/tabcontent.php?content=life'); 
	YAHOO.lebow.tabView.getTab(3).set('cacheData', true); 
	YAHOO.lebow.tabView.getTab(3).set('dataSrc', '/tabcontent.php?content=research'); 
	YAHOO.lebow.tabView.getTab(4).set('cacheData', true); 
	YAHOO.lebow.tabView.getTab(4).set('dataSrc', '/tabcontent.php?content=podcasts');
	
	//YAHOO.lebow.tabView.addListener('activeTabChange', Videobox);
	
	YAHOO.lebow.tabView.addListener('activeTabChange', hdlTVChange);
	YAHOO.lebow.tabView.addListener('beforeActiveTabChange', function (e) { 
	                                                           var newInx = this.getTabIndex(e.newValue); 
	                                                           var prevInx = this.getTabIndex(e.prevValue);
	                                                           if (newInx != prevInx) { 
   	                                                            var track = '/hp/tabs/' +  YAHOO.lebow.trackLabel[newInx]; 
                                                                  pageTracker._trackPageview(track); 
	                                                              } 
	                                                           }); 
	YAHOO.lebow.trackLabel = new Array('Focus', 'InTheNews', 'Lifestyles', 'Research', 'Podcasts'); 
	
} 
   

   var bmState= YAHOO.util.History.getBookmarkedState('tabs'); 
   var initState = bmState || "tab0";

   YAHOO.util.History.register('tabs', initState, function (state) { 
                                                      YAHOO.lebow.tabView.set('activeIndex', state.substr(3)); 
                                                    });
   
   YAHOO.util.Event.addListener(window, "load", YAHOO.lebow.init); 
   
   YAHOO.util.History.onReady(function () { 
         var currentState; 
         YAHOO.lebow.initTabs();
         currentState  = YAHOO.util.History.getCurrentState('tabs'); 
         YAHOO.lebow.tabView.set('activeIndex', currentState.substr(3)); 
      });
   
   
   

YAHOO.lebow.loadEvents = function (url, container) { 
   var todayURL = '/Newsroom/rest/event.php?action=list&sDate=12-11-2007&eDate=12-11-2007';
   
   var todaySucc = function (o) { 
        if (o.responseText !== 'undefined') { 
            var xml = o.responseXML; 
            var body = '<div>'; 
            
            var events = xml.getElementsByTagName('Event'); 
            for (var i = 0; i < events.length; i++) { 
               eID = 'Event_' + events[0].getAttribute('EventID');
               body += '<p id="' + eID + '">' + YAHOO.lebow.xmlValue(events[0], 'Name') + '</p>';
               YAHOO.util.Event.addListener(eID, "click", YAHOO.lebow.getEvent); 
            } 
            
            body += '</div>'; 
            
            document.getElementById(container).innerHTML = body;
        } 
   } 
   
   var failed = function  (o)  { 
      alert('failed');  
   } 
   var req =  YAHOO.util.Connect.asyncRequest('GET', url, {success: todaySucc, failure: failed}); 
   
}; 


YAHOO.lebow.showEvent = function (id) { 

	YAHOO.lebow.getEvent(); 
};  

	YAHOO.lebow.xmlValue = function(xml, node) { 
		var tags = xml.getElementsByTagName(node); 
		if (tags.length == 1 && tags[0].hasChildNodes()) { 
			return tags[0].firstChild.nodeValue;
		} 
		else
			return false;
	} ; 
 

var sURL = '/Event.xml';

YAHOO.lebow.getEvent = function (EventID) { 
	var eventSuccess = function (o) { 
		if (o.responseText !== 'undefined') {
			var xml = o.responseXML; 
			var eBody = '<table>';
			
			var allowRegister = false;
			var registrations = xml.getElementsByTagName('RegistrationTypes'); 
			if (registrations.length == 1) {  
				if (registrations[0].getAttribute('allowRegister') == 'true') 
					allowRegister = true;
			}
			
			var EventID = YAHOO.lebow.xmlValue(xml, 'EventID');  
			
			if (allowRegister) { 
				eBody += '<tr><td colspan="2" class="register"><a href="/Event/' + YAHOO.lebow.xmlValue(xml, 'ShortName') + '/register" class="register">register</a><a href="/Event/' + YAHOO.lebow.xmlValue(xml, 'ShortName')  + '/sendtofriend" class="sendToFriend">send to friend</a></td></tr>'; 
			} 
			eBody += '<tr><td class="label">Event Name</td><td class="value">' + YAHOO.lebow.xmlValue(xml, 'EventName')  + '</td>'; 
			eBody += '<tr><td class="label">Event Type</td><td class="value">';
			
			var types = xml.getElementsByTagName('EventType');
			for (var i = 0; i < types.length; i++) { 
				eBody += types[i].firstChild.nodeValue + '<br>';
			} 
			eBody +=  '</td>'; 
			
			description = YAHOO.lebow.xmlValue(xml, 'Description'); 
			
			eBody += '<tr><td class="label">Event Start</td><td class="value">' + YAHOO.lebow.xmlValue(xml, 'EventStart')  + '</td>'; 
			eBody += '<tr><td class="label">Event End</td><td class="value">' + YAHOO.lebow.xmlValue(xml, 'EventEnd')  + '</td>'; 
         
			if (description.length > 800) 
			   description = description.substr(0, 800) + '...<a href="/Newsroom/Events/viewevent.php?id=' + YAHOO.lebow.xmlValue(xml, 'EventID') + '">more...</a>';
			   
			PhoneBlock = YAHOO.lebow.xmlValue(xml, 'PhoneNumber');
			if(PhoneBlock.length==11)
				PhoneBlock = PhoneBlock.substr(0,1) + '.' + PhoneBlock.substr(1,3) + '.' + PhoneBlock.substr(4,3) + '.' + PhoneBlock.substr(7,4)
			else if(PhoneBlock.length==10)
				PhoneBlock = PhoneBlock.substr(0,3) + '.' + PhoneBlock.substr(4,3) + '.' + PhoneBlock.substr(6,4)
			else if(PhoneBlock.length==7)
				PhoneBlock = PhoneBlock.substr(0,3) + '.' + PhoneBlock.substr(3,4)
			   
			eBody += '<tr><td class="label">Event End</td><td class="value">' + description  + '</td>'; 
			eBody += '<tr><td class="label">Location Name</td><td class="value">' + YAHOO.lebow.xmlValue(xml, 'LocationName')  + '</td>'; 
			eBody += '<tr><td class="label">Contact</td><td class="value">' + YAHOO.lebow.xmlValue(xml, 'FirstName') + ' ' + YAHOO.lebow.xmlValue(xml, 'LastName')  + '</td>'; 
			eBody += '<tr><td class="label">Contact Phone</td><td class="value">' + PhoneBlock  + '</td>'; 
			eBody += '<tr><td class="label">Contact Email</td><td class="value">' + '<a href="mailto:' + YAHOO.lebow.xmlValue(xml, 'Email') + '">' + YAHOO.lebow.xmlValue(xml, 'Email') + '</a></td>'; 
			if (allowRegister) { 
				eBody += '<tr><td colspan="2" class="register">'; 
				eBody += '<a href="/Event/' + YAHOO.lebow.xmlValue(xml, 'ShortName') + '/register" onClick="pageTracker._trackPageview(\'/hp/showEvent/register/' + YAHOO.lebow.xmlValue(xml, 'ShortName')  + '\');" class="register">register</a>';
				eBody += '<a href="/Event/' + YAHOO.lebow.xmlValue(xml, 'ShortName')  + '/sendtofriend" onClick="pageTracker._trackPageview(\'/hp/showEvent/sendToFriend/' + YAHOO.lebow.xmlValue(xml, 'ShortName')  + '\');" class="sendToFriend">send to friend</a></td></tr>'; 
			} 
			
			eBody += '</table>';
			
			document.getElementById('lbShowEventContent').innerHTML = eBody;
			
			YAHOO.lebow.event.setHeader('Event Details'); 
			YAHOO.lebow.event.setFooter('<a href="/Newsroom/Events/index.php">View all upcoming events</a>'); 
			YAHOO.lebow.event.show();
			
			pageTracker._trackPageview('/hp/showEvent/view/' + YAHOO.lebow.xmlValue(xml, 'ShortName'));
			
		} 
	}; 
	
	var eventFail = function (o) { 
		alert('failed');
	};
   
	
	sURL = '/Newsroom/rest/event.php?action=EventDetails&EventID=' + EventID;
	var req =  YAHOO.util.Connect.asyncRequest('GET', sURL, {success: eventSuccess, failure: eventFail}); 
	};

	
	function adRotateClick(url, linkTarget, adName) {  
	   var trackName = '/hp/ads/' + adName; 
      pageTracker._trackPageview(trackName); 
	   document.location = url;
	}
	