/*

*/

// Navigation
function mainmenu(){
jQuery(" #nav ul ").css({display: "none"}); // Opera Fix
jQuery(" #nav li").hover(function(){
		jQuery(this).find('ul:first').css({visibility: "visible",display: "none"}).fadeIn(250);
		},function(){
		jQuery(this).find('ul:first').css({visibility: "hidden"});
		});
}

jQuery(document).ready(function(){
	mainmenu();
	showLogin();
});

function showLogin(){
	jQuery('#RegisterLog').ready(function() {
	 if(readCookie('spurs_logged_in'))
	 {
     jQuery('#RegisterLog').html('<div><strong>WELCOME</strong> ' + unescape(readCookie('spurs_name')) + ' - <div class="logButtonWrap"><a class="welcomeBut" href="/account"><span>Account</span></a><a class="welcomeBut" href="/logout"><span>Logout</span></a></div></div>');
	 }
	 

	 jQuery('#RegisterLog').show();
	});
}

function switch_tabs(unactive_tabs, active_tab, unactive_content,  active_content)
{
  //show chosen tab
  jQuery(unactive_tabs).removeClass("active");

  //hide all tabs
  jQuery(active_tab).addClass("active");

	jQuery(active_tab).parent().removeClass();
  jQuery(active_tab).parent().addClass(active_tab.replace('#',''));

  //hide content
  jQuery(unactive_content).hide();

  //show content by id
  jQuery(active_content).show();
}

// Global
function openWindow(theURL,winName,features) {
  window.open(theURL,winName,features);
}

var notext = new Array(); function doclear(f)  { if (notext[f.id] != true) { f.value = ''; notext[f.id]=true; } }

function MT_emailPage()
{
	var mailit = "mailto:?subject=Visit this page on tottenhamhotspur.com";
	mailit += "&body=%0D%0A%0D%0AHi, below is a link to a page on tottenhamhotspur.com that I thought you might be interested in%0D%0A%0D%0A";
	mailit += "To view the page, visit: "+document.location.href;
	document.location.href = mailit;
	return false;
}

function linkEmail(linkID){
 if($(linkID)){
		$(linkID).onclick = MT_emailPage;
	}
	return false;
}



// top_search
function loadTopLinks()
{

  if(typeof mapkey != 'undefined')
  {
  	for (var i=0; i<mapkey.length; i++)
  	{
  	  var keywords= mapkey[i].keywords.split(", ");
  	  var toplink;
  	  var k;
  	  var re = /(^\w)/;
  	  var uckeyword;

  	  for (var j=0; j<keywords.length; j++)
  	  {
  			k = j + 1;
  			toplink = document.getElementById("top_" + k);

  			if(toplink) {
  			  url = "http://uk.search.yahoo.com/yhs/search?fr=yhs-tottenham-hotspots&partnerid=yhs-tottenham-hotspots&lyt=st&p="+ keywords[j];
  				toplink.href = url;
  				uckeyword = keywords[j].substr(0,1).toUpperCase() + keywords[j].substr(1,keywords[j].length - 1);
  				toplink.innerHTML = uckeyword;
  			} else {
  				return;
  			}
  	  }
  	}
  }
}

// Web trends
function DcsInit(){
	this.dcsid="dcsm1991x00000wgunemm34ht_7i2o";
	this.domain="statse.webtrendslive.com";
	this.enabled=true;
	this.exre=(function(){
		if (window.RegExp){
			return(new RegExp("dcs(uri)|(ref)|(aut)|(met)|(sta)|(sip)|(pro)|(byt)|(dat)|(p3p)|(cfg)|(redirect)|(cip)","i"));
		}
		else{
			return("");
		}
	})();
	this.fpc="WT_FPC";
	this.fpcdom="";
	this.i18n=false;
	this.images=[];
	this.index=0;
	this.qp=[];
	this.re=(function(){
		if (window.RegExp){
			return(this.i18n?{"%25":/\%/g}:{"%09":/\t/g,"%20":/ /g,"%23":/\#/g,"%26":/\&/g,"%2B":/\+/g,"%3F":/\?/g,"%5C":/\\/g,"%22":/\"/g,"%7F":/\x7F/g,"%A0":/\xA0/g});
		}
		else{
			return("");
		}
	})();
	this.timezone=0;
	this.trackevents=true;
	var t=this;
    (function(){
        if (t.enabled&&(document.cookie.indexOf(t.fpc+"=")==-1)&&(document.cookie.indexOf("WTLOPTOUT=")==-1)){
            document.write("<scr"+"ipt type='text/javascript' src='"+"http"+(window.location.protocol.indexOf('https:')==0?'s':'')+"://"+t.domain+"/"+t.dcsid+"/wtid.js"+"'><\/scr"+"ipt>");
        }
	})();
}
var DCS={};
var WT={};
var DCSext={};
var dcsInit=new DcsInit();

// Cookiereader

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 switchLoginAreaDisplay() {

/*	if(readCookie('spurs_logged_in')){

		document.getElementById('tr1').className = 'log_show';

		document.getElementById('tr0').className = 'log_hide';

	} else {

		document.getElementById('tr0').className = 'log_show';

		document.getElementById('tr1').className = 'log_hide';


	}
	checkCartStatus();
*/
}


function shopSwitchLoginAreaDisplay() {

/*	if(readCookie('spurs_logged_in')){

		document.getElementById('tr0').className = 'log_hide';
		document.getElementById('tr6').className = 'log_show';
		document.getElementById('tr1').className = 'log_hide';
		document.getElementById('tr3').className = 'log_show';

	} else {

		document.getElementById('tr0').className = 'log_show';
		document.getElementById('tr6').className = 'log_hide';
		document.getElementById('tr1').className = 'log_show';
		document.getElementById('tr3').className = 'log_hide';

	}
*/
}


function checkCartStatus(){

	if(document.getElementById){
		if(readCookie('spurs_logged_in')){

			if($('#spurs_name')){
			$('#spurs_name').html(unescape(readCookie('spurs_name')));
			}
		}
			var cartprice = "";
			if(readCookie('spurs_cart_total') != null){
				cartprice = unescape(readCookie('spurs_cart_total'));
			} else {
				cartprice = '0.00';
			}

			if($('#spurs_cart_total')){
			$('#spurs_cart_total').html(cartprice);
			}
		}
}


function logout() {

	if(readCookie('spurs_logged_in')){
	document.cookie = 'spurs_logged_in=logout;expires=Thu, 01-Jan-70 00:00:01 GMT path=/';
	window.location.reload(true);

	}

}

  function loadXML(xmlFile, handleResponse){

    $.ajax({
     type: "GET",
     url: xmlFile,
     dataType: "xml",
     success: handleResponse
    });

	}
	function getRoot(RO){
		//returnVal = RO.responseXML.childNodes[1] || RO.responseXML.childNodes[0];
		returnVal = RO.responseXML.childNodes[0];
		return returnVal;
	}

	function getText(xmlNode){
		var returnVal = xmlNode.text;
		if(!returnVal){
			returnVal = xmlNode.textContent
		}
		return returnVal;
	}


// Overture
function loadAds()
{

  try
  {
    // jQuery
    $.get('/overture.php', {} , displaySponsoredLinks);
  } catch(e) {
    // Prototype
    new Ajax.Request('/overture.php', {onSuccess:displaySponsoredLinks});
  }
}

function loadAdsHome()
{

  try
  {
    $.get('/overture.php', { home: 1  }, displaySponsoredLinks);
  } catch(e) {

  }
}

function displaySponsoredLinks(data) {

  var output = "";
  td_width = "33%";

  try // jQuery
  {
    if($(data).find('Listing').length == 4)
    {
      td_width = "25%";
    }

    output = "<tr>";

    // Display links
    $(data).find('Listing').each(function() {

      output += "<td width=\"" + td_width + "%\">\r\n"
    	output += "<a href=\""+$(this).find('ClickUrl').text()+"\" class=\"body_0_9 body_mid_blue\" target=\"blank\">"
    	output += "<strong>"+$(this).attr('title')+"</strong></a><br /><br />\r\n"
    	output += "<a href=\""+$(this).find('ClickUrl').text()+"\" target=\"blank\" class=\"body_0_9 body_grey\">"
    	output += $(this).attr('description')+"</a><br /><br />\r\n";
    	output += "<strong><a href=\""+$(this).find('ClickUrl').text()+"\" class=\"body_0_9 body_mid_blue\" target=\"blank\">"
    	output += $(this).attr('siteHost')+"</a></strong>\r\n</td>"

    })

    output += "</tr>";

    $('#sponsored_links table tr:eq(1)').remove();
    $('#sponsored_links table').append(output);
  }

  catch(e){ // Normal Javascript

    var response = data.responseXML.documentElement;
    var listing = response.getElementsByTagName('Listing');

    if(listing.length == 4)
    {
      td_width = "25%";
    }

    var tr = document.getElementById('sponsored_links').getElementsByTagName('tr');

    for(var i=0; i<listing.length; i++)
    {
      var td = (i==0) ? tr[1].cells[0] : tr[1].insertCell(i);
      var clickurl = response.getElementsByTagName('ClickUrl')[i].firstChild.nodeValue;

      //output = "<td width=\"" + td_width + "%\">\r\n"
    	output = "<a href=\""+clickurl+"\" class=\"body_0_9 body_mid_blue\" target=\"blank\">"
    	output += "<strong>"+listing[i].getAttribute("title")+"</strong></a><br /><br />\r\n"
    	output += "<a href=\""+clickurl+"\" target=\"blaClickUrlnk\" class=\"body_0_9 body_grey\">"
    	output += listing[i].getAttribute("description")+"</a><br /><br />\r\n"
    	output += "<strong><a href=\""+clickurl+"\" class=\"body_0_9 body_mid_blue\" target=\"blank\">"
    	output += listing[i].getAttribute("siteHost")+"</a></strong>\r\n"
    	//output += "</td>"'

      td.innerHTML = output;
    }

  }
}


	function Request(arg) {

		var obj = this;

	   obj.strUri = "";
	   var requestHandler = null;
	   var strSendMethod = "GET";

	   var objVariables = {};
	   var async = true;
	   var user = "";
	   var password = "";
	   var requestObj;

	  if (window.XMLHttpRequest) {
	      requestObj = new XMLHttpRequest();
	  }

	  if(!window.XMLHttpRequest) {
	      requestObj = new ActiveXObject("Microsoft.XMLHTTP");
	  }

	  /* Pubic Methods */

	  obj.setSendMethod = function(strMeth) {

	   strMeth = strMeth.toUpperCase();

	   if(strMeth === "GET" || strMeth === "POST") {
	    strSendMethod = strMeth;
	    return true;
	   }
	   raiseWarning("No invalid Send method passed");
	   return false;

	  };

	  obj.setUri = function(uri) {

	    if(uri !== "") {
	      obj.strUri = uri;
	      return true;
	    }
	    raiseWarning("No uri passed");
	    return false;

	  };

	  // Wrapper for setVariable
	  obj.setMethod = function(strMeth) {

	    if(strMeth !== "") {
	      objVariables.method = strMeth;
	      return true;
	    }
	    raiseWarning("No method passed");
	    return false;

	  };

	  // Sets value
	  // setRequestHeader
	  obj.setVariable = function(theVar,theValue) {

	    if(theVar !== "") {
	      objVariables[theVar] = escape(theValue);
	      return true;
	    }
	    raiseWarning("No variable name passed");
	    return false;

	  };

	  obj.setErrorHandler = function(theHander) {

	   if(typeof(theHandler) === 'function') {
	     	errorHandler = theHandler;
	      return true;
	   } else {
	   		raiseWarning("Error handler is not a function");
	   		return false;
	   }

	  };

	  obj.setAuthentication = function(theUser,thePass) {

	  	if(user !== "") {
	  		user = theUser;
	  		password = thePass;
	  		return true;
	  	} else {
	  		raiseWarning("No username passed");
				return false;
	  	}

	  };

	  obj.send = function(uri,handler) {

			obj.strUri = uri;
			if(typeof(handler) !== 'function') {
			  raiseError("Handler is not a function");
			  return false;
			}

	    var finalUri = getUri();
	    var headers = getHeaders();

		  try {

					requestObj.onreadystatechange = processReqChange;
//		    EventManager.add(requestObj,"onreadystatechange",processReqChange,false);

		  } catch(e) {
		  	alert(e.message);
		    raiseError("Unable to create XMLHttpRequest object");
		    return;

		  }

	    obj.requestHandler = handler;
	    requestObj.open(strSendMethod,finalUri,async,user,password);
	    requestObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	    requestObj.send(headers);
	    return true;
	  };


	  /* Private Function */
	  function processReqChange() {

				// Remove the event handler


	      // only if req shows "complete"
	      if (requestObj.readyState === 4) {
	          // only if "OK"
	          if (requestObj.status === 200) {
	              // Now call handler function
	              obj.requestHandler(requestObj);
	              requestObj = null;
	              return requestObj;
	          } else {
	          		requestObj = null;
	              raiseError("There was a problem retrieving the XML data:\n" + requestObj.statusText);
	              return false;
	          }
	      }

	  }

	  function buildVariableString() {
	    var tempArr = [];
	    for(theVar in objVariables) {
	      tempArr[tempArr.length] = theVar + "=" + objVariables[theVar];
	    }
	    return tempArr.join("&");

	  }

	  function getUri() {

			var strVariableString = buildVariableString();
	    if(strSendMethod === "GET" && strVariableString !== "") {
	      return obj.strUri + "?" + strVariableString;
	    } else {
	      return obj.strUri;
	    }

	  }

	  function getHeaders() {

	   if(strSendMethod === "GET") {
	     return null;
	   } else {
	     return buildVariableString();
	   }

	  }

	  function raiseError(strError) {

	   if(typeof(errorHandler) !== 'function') {
	      alert(strError);
	   } else {
	   	 errorHandler(strError);
	   }
	  }

	  function raiseWarning(strWarning) {

	   if(typeof(errorHandler) !== 'function') {
	      alert(strWarning);
	   } else {
	   	 errorHandler(strError);
	   }

	  }

	}
