	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 userLoggedIn()
	{
		if (readCookie('ATVOEmail') && !isNaN(readCookie('ATVOEmail'))) return false;
		
		//return readCookie('userloggedin');
		return (readCookie('userloggedin')  || readCookie('venda') != null || readCookie('VLUID') != null);
	}

	function textSize()
	{
		//return readCookie('userloggedin');
		var iTextSize = readCookie('tsz');

		if (iTextSize != null && iTextSize > 0)
		{
			document.write('<style type="text/css" media="screen"><!--body { font-size: '+iTextSize+'px; }--></style>');
		}

	}

	function getProfileLink(link)
	{
		var href = ''; var text = '';

		if (link == 0)
		{
			href = (userLoggedIn()) ? '/myprofile?mode=logout':'/login';
			text = (userLoggedIn()) ? 'Logout':'Login';
		}
		else if (link == 1)
		{
			href = (userLoggedIn()) ? '/myprofile':'/register';
			text = (userLoggedIn()) ? 'Profile':'Register';
		}else if (link == 2)
        {
			href = (userLoggedIn()) ? '/usa/myprofile?mode=logout':'/usa/login';
			text = (userLoggedIn()) ? 'Logout':'Login';
        }
		else if (link == 3)
		{
			href = (userLoggedIn()) ? '/usa/myprofile':'/usa/register';
			text = (userLoggedIn()) ? 'Profile':'Register';
        }
		document.write('<a id="loginLink" href="'+href+'" class="login">'+text+'</a>');
	}

	function toggleShowHide(sDiv)
	{
		if (document.getElementById(sDiv).style.display == "block")
		{
			document.getElementById(sDiv).style.display = "none";
		}else{
			document.getElementById(sDiv).style.display = "block";
		}
	}

	function popup(name, sUrl, width, height) {
		window.open(sUrl, name,'width='+width+'px,height='+height+'px,menubar=no,status=yes,scrollbars=no,toolbar=no,location=no,directories=no,resizable=yes,top=25,left=30');
		return false; //necessary so it doesn't follow href
	}

	function externalLinks()
	{
		 if (!document.getElementsByTagName) return;
		 var anchors = document.getElementsByTagName("a");

		 for (var i=0; i<anchors.length; i++) {
		   var anchor = anchors[i];
		   if (anchor.getAttribute("id") == 'loginLink' || anchor.getAttribute("id") == 'registerLink') continue;

			//if (anchor.getAttribute("href") == 'http://tv.arsenal.com' || anchor.getAttribute("href") == 'http://tv.arsenal.com/'){
			if (anchor.getAttribute("href") && (anchor.getAttribute("href").toLowerCase().indexOf('http://tv.arsenal.com') > -1 || anchor.getAttribute("href").toLowerCase().indexOf('http://usatv.arsenal.com') > -1)){
		   		anchor.setAttribute('onclick', "return popup('arsenaltv', '"+anchor.getAttribute("href")+"', 990, 691);");
		   		//IE 6 requires this.
		   		anchor.parentNode.innerHTML = anchor.parentNode.innerHTML
		    }
		    else if (anchor.getAttribute("href") && anchor.getAttribute("href").toLowerCase().indexOf('http://www.arsenal.com/webforms') > -1) {
		   		anchor.setAttribute('onclick', "return popup('webforms','"+anchor.getAttribute("href")+"', 800, 455);");
		   		//IE 6 requires this.
		   		anchor.parentNode.innerHTML = anchor.parentNode.innerHTML
		    }
		    else if (anchor.getAttribute("href") && anchor.getAttribute("href").toLowerCase().indexOf('http://www.arsenal.com/christmasgiftguide') > -1) {
		   		anchor.setAttribute('onclick', "return popup('christmasgiftguide','"+anchor.getAttribute("href")+"', 760, 600);");
		   		//IE 6 requires this.
		   		anchor.parentNode.innerHTML = anchor.parentNode.innerHTML
		    }
		    else if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external"){
		     anchor.target = "_blank";
		   }//end if else
		 }//end for
	}//end function


    function hasVoted(pageid)
    {
        return readCookie('ilike_'+pageid);
    }

    function getILikeThis(pageid, numLikes)
    {
        if (hasVoted(pageid))
            {
                if (numLikes>=2)
                {
                    document.write('<li><p>You and <strong>'+formatNum(numLikes)+'</strong> other people like this</p></li>');
                }else{
                    document.write('<li><p>You like this page</p></li>');
                }
            }else{
                if (numLikes>=2)
                {
                    document.write('<li><a href="javascript:iLikeThis('+pageid+','+numLikes+')">I like this</a></li><li><p><strong>'+formatNum(numLikes)+'</strong> people like this</p></li>');
                }else{
                    document.write('<li><a href="javascript:iLikeThis('+pageid+','+numLikes+')">I like this</a></li>');
                }
            }

    }

    function formatNum(nStr)
    {
        nStr += '';
        x = nStr.split('.');
        x1 = x[0];
        x2 = x.length > 1 ? '.' + x[1] : '';
        var rgx = /(\d+)(\d{3})/;
        while (rgx.test(x1)) {
            x1 = x1.replace(rgx, '$1' + ',' + '$2');
        }
        return x1 + x2;
    }



    function iLikeThis(pageid, numLikes)
    {
    	var $j = jQuery.noConflict();
        try{

           var onSuccess = function(text)
           {
                if (numLikes >= 2)
                {
                    $j("#like-this").html('<li><p>You and <strong>'+formatNum(numLikes)+'</strong> other people like this</p></li>');
                }else{
                    $j("#like-this").html('<li><p>You like this page</p></li>');
                }
           }

             $j.ajax({
              type: "GET",
              url: "/_scripts/ajax/likethis.ajax.php",
              data: "page_id="+pageid,
              async: false,
              success: onSuccess
            });

        }catch (err){
        }

    }


	window.onload =  externalLinks;
