var initPage = function() {
	initMenu();
}

var initMenu = function() {
	var nav = document.getElementById('menu');
	if (nav) {
		var lis = nav.getElementsByTagName('li');
		for (var i = 0; i < lis.length; i++){
			lis[i].onmouseover = function() {
				this.className += ' hover';
			};
			lis[i].onmouseout = function() {
				this.className = this.className.replace('hover','');
			};
		}
	}
}



if (window.addEventListener){
	window.addEventListener("load", initPage, false);
} else if (window.attachEvent){
	window.attachEvent("onload", initPage);
}

$(document).ready(function(){
    HackNews();
    $('#aspnetForm ul li a.middlelinks').each(function(){
        if($(this).attr('href') == 'DocumentQuery.aspx?DocumentTypeID=515')
        {
            $(this).attr('href','/News/Email');
        }
    });
    $('#media-scroller li').each(function(){
        var mediaID = $(this).children('a').attr('href').split('=');
        if(mediaID[1] == jQuery.url.param('MediaID'))
        {
            $(this).hide();
        }
    });
    $('h2 .SocialMediaLinks a:external').each(function(){
        var link = $(this).attr('href');
        link = 'http://www.house.gov/htbin/leave_site?ln_url='+link;
        $(this).attr('href',link);
    });
    if(readCookie('popup3') != 'hide')
    {
	    //$('#modalSurveyPopup').click();
	    //createCookie('popup3','hide',4);
	}
    if(readCookie('popupSignup') != 'hide')
    {
	    $('#modalPopup').click();
	    createCookie('popupSignup','hide',60);
	}
});

// Check whether links are external:
// (Only works with elements that have href):
$.extend($.expr[':'],{
    external: function(a,i,m) {
        if(!a.href) {return false;}
        return a.hostname && a.hostname !== window.location.hostname;
    }
});

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function HackNews()
{
    $('table[id$=ExtrasTable] table[bordercolor=#808080] font ~ br').remove();
    $('table[id$=ExtrasTable] table[bordercolor=#808080] img').css('border','1px solid #000');
    $('table[id$=ExtrasTable] table[bordercolor=#808080] img').each(function(i){
        $(this).attr('alt',$('table[id$=ExtrasTable] table[bordercolor=#808080] font:eq('+i+')').remove().text());
        $(this).attr('src',$(this).attr('src').replace(/thumbnails/gi,'lowresolution'));
    });
    $('table[id$=ExtrasTable] table[bordercolor=#808080]').attr('border','0');
}
