/* =============================================================================== */
/*        Copyright (C) VNU Business Publications LTD. All rights reserved.        */
/* ------------------------------------------------------------------------------- */
/*             Internal use only. Do not publish and/or redistribute.              */
/* =============================================================================== */

//  Grapeshot adtags GS_CATEGORY value
var GS_CATEGORY="DEFAULT";

//Serve any component to third party websites
function VNUComponentServer() {
  this.host = "tools.vnu.net";  			//tools.vnu.net
  this.remoteLanguageDefault = "fr";		//bad name, should be remoteLanguage
  this.remoteLanguage = null;						//bad name, should be remoteLanguageDefault
  this.remoteCountryDefault = "GB";
  this.remoteCountry = null;
  this.remoteSiteKeyDefault = "vnuTest"; 
  this.remoteSiteKey =  null;
  this.remoteSkinDefault = "vnu";
  this.remoteSkin = null;
  this.remoteCss = null;
  this.globalLevelCss = null;
  this.countryLevelCss = null;
  this.siteLevelCss = null;
  this.defaultCssFile = "includer";
  
  
  //before anything else if remote server defines a vnuRemoteLocale variable 
  //	which should be called 'vnuRemoteLanguage'
  //	use it to override default locale
  try {
    if (vnuRemoteLocale != null) this.remoteLanguage = vnuRemoteLocale;
  } catch(error) {
    //no vnuRemoteLocale defined on remote server, use default
    if (this.remoteLanguage == null) this.remoteLanguage = this.remoteLanguageDefault;  
  }
  
  //set the remoteCountry variable
  this.setRemoteCountry(); 
  
  //now determine the remote site key or set default
  try {
    if (vnuRemoteSiteKey != null) this.remoteSiteKey = vnuRemoteSiteKey;
  } catch(error) {
    //no vnuRemoteSiteKey defined on remote server, use default
    if (this.remoteSiteKey == null) this.remoteSiteKey = this.remoteSiteKeyDefault;
  }

  //now read siteVariables for remote site
  //if found, do dynamic include of js file defining variables
  try {
    if (this.remoteSiteKey != null) {
      var tmp_url = "http://" + this.host + "/tools/componentServer/siteVariablesMapping/" + getMappingFolderName(this.remoteCountry) + "/" + this.remoteSiteKey;
      document.write("<SCR" + "IPT type=\"text/javascript\" src=\"" + tmp_url + "\"></SCR" + "IPT>");
    }
  } catch(error) {
    //do nothing on error
  }
  var sitesMetadataURL = "http://" + this.host + "/tools/componentServer/sitesMetadata/" + this.remoteLanguage + "/" + this.remoteCountry;
  document.write("<SCR" + "IPT type=\"text/javascript\" src=\"" + sitesMetadataURL + "\"></SCR" + "IPT>");
}


VNUComponentServer.prototype.prepare = function() {
  try {
    if (vnuRemoteSkin != null) this.remoteSkin = vnuRemoteSkin;
  } catch(error) {
    //use this.remoteSkinDefault
    if (this.remoteSkin == null) this.remoteSkin = this.remoteSkinDefault;
  }
  
  //now we have the opportunity to override the css irrespective of skin
  try {
    if (vnuRemoteCss != null) this.remoteCss = vnuRemoteCss;
  } catch(error) {
    //just leave the remoteCss as null
  }
  
  //include required css files for this component
  this.includeCSS(null);
  
  //include custom remote defined webtrends metatags
  this.includeWebtrendsMeta();
}


VNUComponentServer.prototype.inspect = function() {
  //for now just alert host to test if we have the object
  var outStr = "host is : " + this.host + "\n";
  outStr += "locale is : " + this.remoteLanguage + "\n";
}


//a function which renders components according to variables
//set up in mapping for the remote site
VNUComponentServer.prototype.prepareComponent = function(path) {
  this.path = path;
  
  var thirdPartyFlag = "?thirdPartyParam=true";
  
  var vnuTabSelect = (vnuHeaderTabSelected != null ? "&tabSelect=" + vnuHeaderTabSelected : "");
  
  //the layout generation url
  this.component_url = "http://" + this.host + "/tools/componentServer/serve/" + this.remoteLanguage + "/" + this.remoteCountry + "/" + this.remoteSkin + "/" + this.path + thirdPartyFlag + vnuTabSelect;
  document.write("<SCR" + "IPT type=\"text/javascript\" src=\"" + this.component_url + "\"></SCR" + "IPT>");
  
}


//include required css files for this component
VNUComponentServer.prototype.includeCSS = function() {
  //first ensure we pull in the required set of stylesheets for the component
  //we follow eurocore's hierarchy of stylesheets, ANY, country/ANY, country/site
  
  var vnuStyleBase = vnu_stylesMetadata[this.remoteSkin];
  
  //test if global level, country level and site level css variables are pulled in. If not default is includer.
  try {
    if (vnuGlobalLevelCss != null) this.globalLevelCss = vnuGlobalLevelCss;
  } catch(error) {
    //use this.defaultCssFile
    if (this.globalLevelCss == null) this.globalLevelCss = this.defaultCssFile;
  }
  
  try {
    if (vnuCountryLevelCss != null) this.countryLevelCss = vnuCountryLevelCss;
  } catch(error) {
    //use this.defaultCssFile
    if (this.countryLevelCss == null) this.countryLevelCss = this.defaultCssFile;
  }
  
  try {
    if (vnuSiteLevelCss != null) this.siteLevelCss = vnuSiteLevelCss;
  } catch(error) {
    //use this.defaultCssFile
    if (this.siteLevelCss == null) this.siteLevelCss = this.defaultCssFile;
  }
  
  
  //add any stylesheet to remote site document head (least specific css). Default is includer.css. If variable is set to "" then css will be omitted
  if (this.globalLevelCss != "") {
    //alert("yes I am including the top level css");
	var anyStylesheet = document.createElement("link");
	anyStylesheet.setAttribute("rel", "stylesheet");
	anyStylesheet.setAttribute("type", "text/css");
	anyStylesheet.setAttribute("href", "http://" + this.host + "/resources/css/" + vnuStyleBase + "styles/ANY/" + this.globalLevelCss + ".css");
	anyStylesheet.setAttribute("media", "screen");
	document.getElementsByTagName("head")[0].appendChild(anyStylesheet);
  }
  
  //add country/any stylesheet to remote site document head
  if (this.countryLevelCss != "") {
  		//alert("i have selected middle level css");
  var countryAnyStylesheet = document.createElement("link");
  countryAnyStylesheet.setAttribute("rel", "stylesheet");
  countryAnyStylesheet.setAttribute("type", "text/css");
  countryAnyStylesheet.setAttribute("href", "http://" + this.host + "/resources/css/" + vnuStyleBase + "styles/" + this.remoteLanguage + "/" + this.remoteCountry + "/ANY/" + this.countryLevelCss + ".css");
  countryAnyStylesheet.setAttribute("media", "screen");
  document.getElementsByTagName("head")[0].appendChild(countryAnyStylesheet);
  }
  
  //finally add country/site stylesheet to remote document head (most specific css)
  if (this.siteLevelCss != "") {
	  var countrySiteStylesheet = document.createElement("link");
	  countrySiteStylesheet.setAttribute("rel", "stylesheet");
	  countrySiteStylesheet.setAttribute("type", "text/css");	  
	  countrySiteStylesheet.setAttribute("href", "http://" + this.host + "/resources/css/" + vnuStyleBase + "styles/" + this.remoteLanguage + "/" + this.remoteCountry + "/" + this.remoteSkin + "/" + this.siteLevelCss + ".css");

	  //countrySiteStylesheet.setAttribute("href", "http://" + this.host + "/resources/css/" + vnuStyleBase + "styles/" + this.remoteLanguage + "/" + this.remoteSkin + "/" + this.siteLevelCss + ".css");
	  countrySiteStylesheet.setAttribute("media", "screen");
	  document.getElementsByTagName("head")[0].appendChild(countrySiteStylesheet);
  }
  
  
  
  //if remote site calls custom css, refer to this.remoteCss
  //it is possible that some sites may require a slightly modified
  //look and feel
  if (this.remoteCss != null) {
	  var customRemoteStylesheet = document.createElement("link");
	  customRemoteStylesheet.setAttribute("rel", "stylesheet");
	  customRemoteStylesheet.setAttribute("type", "text/css");
	  customRemoteStylesheet.setAttribute("href", "http://" + this.host + "/resources/css/" + vnuStyleBase + "styles/" + this.remoteCountry + "/" + this.remoteSkin + "/thirdParty/" + this.remoteCss + ".css");
	  customRemoteStylesheet.setAttribute("media", "screen");
	  document.getElementsByTagName("head")[0].appendChild(customRemoteStylesheet);
  }
}


//include custom remote defined webtrends metatags
VNUComponentServer.prototype.includeWebtrendsMeta = function() {
  //always write the vnu_magazine webtrends tag
  //**** not sure if we are going to use this, need to talk to Manuela ****
  //var vnu_magazine_meta = document.createElement("meta");
  //vnu_magazine_meta.setAttribute("name", "DCSext.vnu_magazine");
  //vnu_magazine_meta.setAttribute("content", vnu_domainMapping["vnu"]);
  //document.getElementsByTagName("head")[0].appendChild(vnu_magazine_meta);
  
  //at this point we write any webtrends meta tags the remote site wants
  //try {
    //if (vnu_webtrends_override != null) {
      //for (var i = 0; i < vnu_webtrends_override.length; i++) {
			  //var vnu_metaEl = document.createElement("meta");
			  //vnu_metaEl.setAttribute("name", vnu_webtrends_override[i].name);
			  //vnu_metaEl.setAttribute("content", vnu_webtrends_override[i].content);
			  //document.getElementsByTagName("head")[0].appendChild(vnu_metaEl);
		  //}
    //}
  //} catch(error) {
    try {
	    if (vnuWebtrendsDef != null) {
	      for (var i = 0; i < vnuWebtrendsDef.length; i++) {
				  var vnu_metaEl = document.createElement("meta");
				  vnu_metaEl.setAttribute("name", vnuWebtrendsDef[i].name);
				  vnu_metaEl.setAttribute("content", vnuWebtrendsDef[i].content);
				  document.getElementsByTagName("head")[0].appendChild(vnu_metaEl);
			  }
	    }
    } catch (error) {
      //if remote site defines no webtrends tags, do nothing
    } 
  //}
}


//we call a placeholder component, to wrap up the content returned nicely
//so we can inject it into the remote page
VNUComponentServer.prototype.prepareAdvert = function(adType) {
  //now inject parameters into component request according to
  //remote definitions
  var vnuWorkingTopad = null;
  var adPropFormat = "";
  var adPropDomain = "";

  //first step, determine if we have a vnu_topad_def object
  //if not use a default param for topad defined here
  var vnuFallbackTopadDef = new Object();
  vnuFallbackTopadDef.url = "forums";
  vnuFallbackTopadDef.cat = "forums";
  
  try {
    if (vnuTopadDef != null) {
      vnuWorkingTopad = vnuTopadDef;
    }
  } catch(error) {
    //hmm, override as in old layouts api?
  }
  
  var adPropNames = "";
	//iterate over all properties but url
  for (prop in vnuWorkingTopad) {
    if ((prop != "url") && (prop != "format") && (prop != "domain")) {
      adPropNames  += "," + prop;
    }
  }
  
  adPropNames = adPropNames.substring(1);
  adPropNames = "?adPropNames=" + adPropNames;
  
  var adPropValues = "";
  for (prop in vnuWorkingTopad) {
    if ((prop != "url") && (prop != "format") && (prop != "domain")) {
      adPropValues  += "," + eval("vnuWorkingTopad." + prop);
    }
  }
  
  adPropValues = adPropValues.substring(1);
  adPropValues = "&adPropValues=" + adPropValues;
  
  //specify format param if a remote one is defined
  try {
    if (vnuWorkingTopad.format != null) adPropFormat += "&adPropFormat=" + vnuWorkingTopad.format;
  } catch(error) {
    //leave param blank
  }
  
  //specify format param if a remote one is defined
  try {
    if (vnuWorkingTopad.domain != null) adPropDomain += "&adPropDomain=" + vnuWorkingTopad.domain;
  } catch(error) {
    //leave param blank
  }
  
  var adPropURL = "&adPropURL=" + vnuWorkingTopad.url;
  
  //temp param into componentServer
  var theParam = "&advertType=" + adType;
  
  //the layout generation url
  var vnuComponentUrl = "http://" + this.host + "/tools/componentServer/serve/" + this.remoteLanguage + "/" + this.remoteCountry + "/" + this.remoteSkin + "/layout/advert" + adPropNames + adPropValues + adPropDomain + adPropFormat + adPropURL + theParam;
  document.write("<SCR" + "IPT type=\"text/javascript\" src=\"" + vnuComponentUrl + "\"></SCR" + "IPT>");
}

VNUComponentServer.prototype.setRemoteCountry = function() {

	//set default
	var remoteCountry = this.remoteCountryDefault;

	try {
		if(vnuRemoteCountry != null) {
			//if vnuRemoteCountry has been set by the 3rd party, return that
			remoteCountry = vnuRemoteCountry;
		}
	}	
	catch(error) {
		//vnuRemoteCountry has not been set by 3rd party, so we'll decide what it should be here
		switch(this.remoteLanguage.toLowerCase()) {
			case "en" :
				remoteCountry = "GB";
				break;
			default :
				//assume that fr language is more likely to be for FR country than BE
				//	and the same for nl language
				remoteCountry = this.remoteLanguage;
				break;			
		}
	}
				
	//set in upper case
	this.remoteCountry = remoteCountry.toUpperCase();	
}

//appalling Eurotidy hack until we change folder names in the /css folder
function getCssFolderName(country) {
	//return variable
	var cssFolderName = "";
	
	switch(country.toUpperCase()) {
		case "GB" :
			cssFolderName = "en";
			break;
		default :
			cssFolderName = country.toLowerCase();
			break;
	}
	
	return cssFolderName;
}

function getMappingFolderName(country) {
	return getCssFolderName(country);
}
