/**
 * Universal Header 
 */
(function(){

 var timer = null;
 var yui_build = "2.5.2";

 //Scripts to be loaded. A script will be loaded only if the  
 var scripts1 = [
   ["http://yui.yahooapis.com/"+yui_build+"/build/yahoo-dom-event/yahoo-dom-event.js", function(){return("undefined" !== typeof YAHOO && "undefined" !== typeof YAHOO.util && "undefined" !== typeof YAHOO.env && "undefined" !== typeof YAHOO.util.Event && "undefined" !== typeof YAHOO.util.Dom);}],
   ["http://l.yimg.com/hb/i/sg/news/ec/js/uh_utils.js", function(){return("undefined" != typeof YAHOO.one && "undefined" != typeof YAHOO.one.uh.Header);}]
 ];
 
 var scripts2 = [
   ["http://yui.yahooapis.com/"+yui_build+"/build/yahoo/yahoo-min.js", function(){return("undefined" !== typeof YAHOO && "undefined" !== typeof YAHOO.util && "undefined" !== typeof YAHOO.env);}],
   ["http://yui.yahooapis.com/"+yui_build+"/build/dom/dom-min.js", function(){return("undefined" !== typeof YAHOO.util.Dom);}],
   ["http://yui.yahooapis.com/"+yui_build+"/build/event/event-min.js", function(){return("undefined" !== typeof YAHOO.util.Event);}],
   ["js/uh_utils.js", function(){return("undefined" != typeof YAHOO.one && "undefined" != typeof YAHOO.one.uh.Header);}]
 ];
 
 var getDocRoot = function(){
     if (typeof document !== "undefined"){
         return document;
     }else if (typeof document.documentElement !== "undefined"){
         return document.documentElement;
     }
 };
 
 var eventUtil = function(){
   return {
     addListener : function(oTarget, sEventType, fnHandler, oScope){
         var scopedListener = function(e){
             oScope.tempFn = fnHandler;
         oScope.tempFn(e);
         };
       
         if (oTarget.addEventListener){
             oTarget.addEventListener(sEventType, scopedListener, false);
         }else if(oTarget.attachEvent){
             oTarget.attachEvent("on" + sEventType, scopedListener);
         }else{
         if (!oTarget["on" + sEventType]) {
           oTarget["on" + sEventType] = scopedListener;
         }
         }
     },    
     
     preventDefault : function(e){
       if (e.preventDefault) {
             e.preventDefault();
         }
       e.returnValue = false;
     },
     
     getTarget : function(e){
         var t = e.target || e.srcElement;
         if (t && 3 == t.nodeType) {
             return t.parentNode;
         } else {
             return t;
         }
     }
   };
 }();
 
 var loadScript = function(scriptUrl){
   var head = document.getElementsByTagName("head")[0];
   var scripts = head.getElementsByTagName("script");
   var found = false;
   for (var i=0;i<scripts.length;i++){
     if (1 == scripts[i].nodeType && scriptUrl == scripts[i].getAttribute("src")) {
       found = true;
       break;
     }
   }
   if (!found) {
     var script = document.createElement("script");
     script.setAttribute("type", "text/javascript");
     script.setAttribute("src", scriptUrl);
     head.appendChild(script);
   }
 };
 
 var checkClassLoaded = function(scripts, success, failure, maxWait, scope){
   var totalWait = 0;
   var check = function(){
     var result = true;
     for (var i=0 ;i<scripts.length;i++) {       
       if (!scripts[i][1]()) {       
         result = false;
       }
     }
     return result;
   };
   
   (function(){
     var self = this;
     if (check()) {
       scope.tempFn = success;
       scope.tempFn();
       scope.tempFn = null;
     } else {
       for (var i=0 ;i<scripts.length;i++) { 
         if (!scripts[i][1]()) {
           loadScript(scripts[i][0]);
         }
       }
       if (totalWait <= maxWait) {
         if (timer) {
           clearTimeout(timer);
           timer = null;
         }
         timer = setTimeout(arguments.callee, 500);
         totalWait += 500;
       } else {
         scope.tempFn = failure;
         scope.tempFn();
         scope.tempFn = null;
       }
     }
   })();
 };
 
 var hasClass = function(element, className){
   var re = new RegExp('(?:^|\\s+)' + className + '(?:\\s+|$)');
   return re.test(element.className);

 };
 
 var update = function(event){
   var keyCode = null;
   
   try{
     if ("keydown" == event.type) {
       keyCode = window.event ? event.keyCode : event.which;
     }
     if ("click" == event.type && hasClass(eventUtil.getTarget(event), "stop_nav")){
       eventUtil.preventDefault(event);  
     }
     if ("keydown" == event.type && 13 == keyCode && hasClass(eventUtil.getTarget(event), "stop_key_nav")){
       eventUtil.preventDefault(event);  
     }
   }catch(e){}
   
   
   try{
     if (document.createEventObject) {
       event = document.createEventObject(event);
     }
   }catch(e){}
   
   var failure = function(){
     var target = eventUtil.getTarget(event);
     if (target && hasClass(target, "stop_nav")){
       window.location = target.href;
     }
   };
   
   var success = function(){
     if (timer) {
       clearTimeout(timer);
       timer = null;
     }

     if ('load' != event.type) {
       YAHOO.one.uh.Header.init(event);
     }
   };
   
   var getSuccessCallback = function(arr){
     if (arr.length > 0) {
       return function(){
         checkClassLoaded([arr[0]], getSuccessCallback(arr.slice(1)), failure, 1000, this);
       };
     }else{
       return success;
     }
   };
   
   //alternative utility js files to load, if any
   var altUtils = null;
   if ("undefined" !== typeof ygmaGetUtils){
     altUtils = ygmaGetUtils();
   }
   if (altUtils){
     //This will be used to load any custom js files instead of the default ones. 
     checkClassLoaded([altUtils[0]], getSuccessCallback(altUtils.slice(1)), failure, 3000, this);
   }else if ("undefined" == typeof YAHOO){ 
     //All of YAHOO, YAHOO.util.Event and YAHOO.util.Dom are absent. So load yahoo-dom-event
     checkClassLoaded([scripts1[0]], getSuccessCallback(scripts1.slice(1)), failure, 3000, this);
   }else{
     //Some or all of YAHOO, YAHOO.util.Event and YAHOO.util.Dom present. So try loading yahoo, dom, event separately
     checkClassLoaded([scripts2[0]], getSuccessCallback(scripts2.slice(1)), failure, 3000, this);  
   }
   
 };
 
 var init = function(){

   var m = navigator.userAgent.match(/MSIE\s([^;]*)/);
   if(m && m[1]){
     if(parseInt(m[1],10) < 6){/* disabling JS  for browser lower to IE 6 version */
       return;
     }
   } 

   var hdr = getDocRoot().getElementById("ygma");
   eventUtil.addListener(hdr, "mouseover", update, this, true);
   eventUtil.addListener(hdr, "click", update, this, true);
   eventUtil.addListener(hdr, "keydown", update, this, true);
   var updateAfterLoad = function(event){
     try{
       if (document.createEventObject) {
         var event = document.createEventObject(event);
       }
     }catch(e){

     }
     setTimeout(function(){update(event);}, 1);
   };
   eventUtil.addListener(window, "load", updateAfterLoad, this, true);
 };
 
 if (typeof YAHOO !== "undefined" && YAHOO && typeof YAHOO.util !== "undefined" && typeof YAHOO.util.Event !== "undefined" && YAHOO.util.Event.onContentReady) {    
     YAHOO.util.Event.onContentReady("ygma", init);
 } else {
     (function(){
         if (getDocRoot().getElementById("ygma")) {
       if (timer) {
         clearTimeout(timer);
         timer = null;
       }
             init();
         } else {          
             timer = setTimeout(arguments.callee, 500);
         }
     })();    
 }
 
})();

/**
 * News Constants
 */
(function() {
  YAHOO.namespace('News');
  YAHOO.News.Resource = {
    /**
     * Category default thumbnails
     */
    DEFAULT_THUMBNAILS: {
      'ts': 'img/thumbs/cat_ts.jpg',
      'bs': 'img/thumbs/cat_bs.jpg'
    },
    
    /**
     * Default News Search Text
     */
    DEFAULT_NEWS_SEARCH_TEXT: 'Search All News'
  };
})();

/**
 * News Util
 */
(function() {
  YAHOO.namespace('News');
  var Dom = YAHOO.util.Dom;
  
  YAHOO.News.Util = {
    toggleClassName: function(id, className) {
      var element = Dom.get(id);
      if (!element) {
        return;
      }
      if (Dom.hasClass(element, className)) {
        Dom.removeClass(element, className);
      } else {
        Dom.addClass(element, className);
      }
    },
    
    stripHTMLComment: function(text) {
      if (!text) {
        return;
      }
      return text.replace(/<!--.+-->/, '');
    },
    
    toggle: function(id) {
      for (var i=0; i<arguments.length; i++) {
        var element = Dom.get(arguments[i]);
        if (!element) {
          return;
        }
        if (element.style.display.match('none')) {
          element.style.display = 'block';
        } else {
          element.style.display = 'none';
        }
      }
    }
  };
})();

/**
 * News Search Module controls default search text
 */
(function() {
  YAHOO.namespace('News');
  
  var Dom = YAHOO.util.Dom,
      Evnt = YAHOO.util.Event,
      Lang = YAHOO.lang,
      News = YAHOO.News;
  
  News.NewsSearch = function() {
    this.init();
  }
  
  News.NewsSearch.prototype = {
    init: function() {
      this.options = {
        inactiveClass: 'inactive',
        searchFieldId: 'news-search-text'
      }
      
      this.elSearchField = Dom.get(this.options.searchFieldId);
      if (!this.elSearchField) { return };
      
      this.initListeners();
      this.deactivateSearchField();
    },
    
    initListeners: function() {
      Evnt.addListener(this.elSearchField, 'focus', this.activateSearchField, this, true);
      Evnt.addListener(this.elSearchField, 'blur', this.deactivateSearchField, this, true);
    },
    
    activateSearchField: function() {
      // Remove inactive style
      if (this.isSearchFieldInactive()) {
        Dom.removeClass(this.elSearchField, this.options.inactiveClass);
      }
      
      // Reset input field
      if (this.isDefaultText()) {
        this.elSearchField.value = '';
      }
    },
    
    deactivateSearchField: function() {
      // Add inactive style
      if (!this.isSearchFieldInactive()) {
        if (this.isDefaultText() || (this.elSearchField.value === '')) {
          if (this.elSearchField.value === '') {
            this.elSearchField.value = News.Resource.DEFAULT_NEWS_SEARCH_TEXT;
          }
          Dom.addClass(this.elSearchField, this.options.inactiveClass);
        }
      }
    },
    
    isSearchFieldInactive: function() {
      return Dom.hasClass(this.elSearchField, this.options.inactiveClass);
    },
    
    isDefaultText: function() {
      return (this.elSearchField.value === News.Resource.DEFAULT_NEWS_SEARCH_TEXT);
    }
  };
  
  Evnt.onContentReady('mod-news-search', function() {
    (new News.NewsSearch());
  });
})();
