$ = YAHOO.util.Dom.get;
var loaders = function() {
  rewrite_links();
  GlobalBox.init();
  CommentBox.init();
  if (document.getElementById("global_news_sites2")) { selectLocation_onload(); }
  print_onload();
  Popup.init();
}
YAHOO.util.Event.onDOMReady(loaders);

function toggledisplay(e) {
	var e = $(e);
	e.style.display = (e.style.display != 'block') ? 'block' : 'none';
}
function rewrite_links() {
  // rewrite filter page links to point to JS versions:
  var links = YAHOO.util.Dom.getElementsByClassName('filter', 'a');
  if (!links) return false;
  for (x=0; x<links.length; x++) {
    var param = links[x].href.split('?')[1].split('&');
    links[x].href = param[0].split('=')[1] + "_filter.html?sort=date&filter=" + param[1].split('=')[1] + "&dates=1,12";
  }
}

var CommentBox = {
  overlay: null,
  trigger: null,
  
  init: function(){
    if (typeof YAHOO == 'undefined') return;
    this.trigger = document.getElementById('comments');
    if (!this.trigger) return;
    this.trigger.onclick = function(){
      CommentBox.show();
      return false;
    }
    
    this.overlay = new YAHOO.widget.Panel("feedback-box", 
      {
        width:"410px",
        fixedcenter: true,
        close: true,
        modal: true,
        underlay: "none",
        visible: false,
        effect: { effect: YAHOO.widget.ContainerEffect.FADE, duration:0.45 }
      }
    );
    this.overlay.render();
  },
  
  show: function(){
    if ((window.location.href).match(/\/contactSearch\//)) {
      $('send_feedback').src='http://tools.cisco.com/newsroom/contactSearch/jsp/email_feedback_form.html';
    } else {
      $('send_feedback').src=setBase('../Newsroom/nc3.0/snippets/email_feedback_form.html');
    }
    CommentBox.overlay.show();
  }
}

var GlobalBox = {
  overlay: null,
  trigger: null,
  
  init: function(){
    if (typeof YAHOO == 'undefined') return;
    this.trigger = document.getElementById('globalbox-link');
    if (!this.trigger) return;
    this.trigger.onclick = function(){
      GlobalBox.show();
      return false;
    }
    
    this.overlay = new YAHOO.widget.Panel("globalbox", 
      {
        width:"693px",
        height:"502px",
        xy: [20,150],
        close: true,
        modal: true,
        underlay: "none",
        visible: false,
        effect: { effect: YAHOO.widget.ContainerEffect.FADE, duration:0.45 }
      }
    );
    this.overlay.render();
  },
  
  show: function(){
    GlobalBox.overlay.show();
    gbox_onload();
  }
}

function gbox_onload() {
  if (!(document.getElementById('globalbox'))) return;
  var gbox_nav_links = document.getElementById('globalbox-nav').getElementsByTagName('li');
  for (x=0; x<gbox_nav_links.length; x++) {
    gbox_nav_links[x].onmouseover = function() { gbox_swap(this); }
  }
  window.gbox_swapcontent = new Array();
  gbox_swapcontent.push(document.getElementById("swapcontent-apac"));
  gbox_swapcontent.push(document.getElementById("swapcontent-emerging"));
  gbox_swapcontent.push(document.getElementById("swapcontent-european"));
}

function gbox_swap(gbox_nav_link) {
  var gbox_nav_link_id = gbox_nav_link.id.replace("swapnav-", "");
  gbox_nav_link.parentNode.className = gbox_nav_link_id;
  // show selected content, hide others:
  for (x=0; x<gbox_swapcontent.length; x++) {
    if (gbox_swapcontent[x].id == ("swapcontent-" + gbox_nav_link_id)) {
      gbox_swapcontent[x].className = "selected";
    } else {
      gbox_swapcontent[x].className = "";
    }
  }
}
// News + World News pages dropdowns
function selectLocation_onload() {
  if (document.getElementById("global_news_sites2"))
    document.getElementById("global_news_sites2").onchange = function(){selectLocation(this[this.selectedIndex].value);}
  if (document.getElementById("find-news")) {
    var find_news_select = document.getElementById("find-news").getElementsByTagName("select");
    // Technology drop-down:
    find_news_select[0].onchange = function() { selectLocation(this[this.selectedIndex].value); }
    // Market Segment drop-down:
    find_news_select[1].onchange = function() { selectLocation(this[this.selectedIndex].value); }
  }
}
function selectLocation(link) {
  if (!(link != "select")) return;
  if (!(link != "none")) return;
  window.location = setBase(link);
}
function setBase(url) {
  var pageBase = document.getElementsByTagName("base")[0];
  if (pageBase && !url.match(/^http/))
    return pageBase.href + url;
  else
    return url;
}
// Print code
function print_onload() {
	// if it locates &print=true in the URL...
	if (!(getURLResult("print") == "true")) return;

	// loop through all LINK stylesheets:
	var stylesheets = document.getElementsByTagName("link");
	for (x=0; x<stylesheets.length; x++) {
		if (stylesheets[x].href.match("cisco/styles/styles.css")) {
			// swap the default stylesheet with the print preview stylesheet:
			stylesheets[x].href = "cisco/styles/printpreview.css";
			// show some text that tells them they're viewing a print preview page:
			var wrapper = document.getElementById("wrapper");
			var printInfo = document.createElement("div");

			printInfo.appendChild(document.createTextNode("This is the printer-friendly version of this page."));
			printInfo.className = "printinfo";
			wrapper.parentNode.insertBefore(printInfo, wrapper);
			// print the page:
			window.print();
		}
	}
}
function getURLResult(name){
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
	var regexS = "[\\?&]"+name+"=([^&#]*)";  
	var regex = new RegExp( regexS );  
	var results = regex.exec( window.location.href );
	
	if( results == null )    
		return "";  
	else    
		return results[1];
}
//Cookie Functions
function getCookie( name ) {
  var start = document.cookie.indexOf( name + "=" );
  var len = start + name.length + 1;
  if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
    return null;
  }
  if ( start == -1 ) return null;
  var end = document.cookie.indexOf( ';', len );
  if ( end == -1 ) end = document.cookie.length;
  return unescape( document.cookie.substring( len, end ) );
}
function setCookie( name, value, expires, path) {
  var today = new Date();
  today.setTime( today.getTime() );
  if ( expires ) {
    expires = expires * 1000 * 60 * 60 * 24;
  }
  var expires_date = new Date( today.getTime() + (expires) );
  document.cookie = name+'='+escape( value ) +
    ( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) +
    ( ( path ) ? ';path='+path : '');
}
function deleteCookie( name, path, domain ) {
  if ( getCookie( name ) ) document.cookie = name + '=' +
    ( ( path ) ? ';path=' + path : '') +
    ( ( domain ) ? ';domain=' + domain : '' ) +
    ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

var Popup = {
  init: function() {
    var urlpop = window.location.href;
    if (urlpop.match(/\/\d{4}\//) || urlpop.match(/\/podcasts\//))
      this.popup('http://newsroom.cisco.com/Newsroom/nc3.0/snippets/newswireImage.html','NewWire',321,357,500,700);
  },
  popup: function(mypage,myname,w,h,top,left) {
    var popped = getCookie('popup');
    if (popped == null) {
      var settings = 'width='+ w + ',height='+ h + ',top=' + top + ',left=' + left + ',scrollbars=no,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
      var popupWindow=window.open('',myname,settings);
      if(popupWindow) {
        popupWindow.focus();
        popupWindow.location=mypage;
        setCookie('popup', 1, 5, '/');
      }
    }
  }
}


