/* SiteCatalystWrapper */
var SCW = {

  //object containing list of values
  ContentProperties: null
	, EVars: null
	, Props: null

  //Flag to activate "flash only" population of the s object
  //if you set this to true, the s object will be populated with
  //all the data collected as usual, BUT it won't execute the s.t();
  //that does the actual tracking.
  //This allows the Flash application to read all the properties of the s
  //object to do the tracking by itself.
	, FlashPage: false

  //set to false to disable all the tracking functionalities of SCW
	, Active: true
	
  //Array of events
	, Events: null

  //Array of js properties that can be set via SCW.AddContent();
	, AllowedJSContent: null

	, Initialize: function(properties) {

	  SCW.Events = new Array();
	  SCW.EVars = {};
	  SCW.Props = {};

	  //these two are optional, you may or may not use them
	  //according to the project you are working on
	  SCW.ContentProperties = {}; 	//the CustomSCW will know how to handle them
	  SCW.AllowedJSContent = new Array(); 	//this list the content properties that can be set via js
	}

	, Execute: function() {
	  //get out if not active ;)
	  if (!SCW.Active) return;

	  CustomSCW.Execute();
	  if (!SCW.FlashPage) {
	    //THIS CODE WAS TAKEN FROM OMNITURE ITSELF
	    //					DO NOT CHANGE IT!
	    //****************************************
	    var s_code = s.t();
	    if (s_code)
	      document.write(s_code);
	    //****************************************
	  }
	}

  //The AddContent function works in conjunction with the PageContent and ValidProperties field.
  //The usage of this function is optional.
	, AddContent: function(contentName, contentValue, ignoreCheck) {
	  if ((ignoreCheck) || (SCW.AllowedJSContent.indexOf(contentName) > -1))
	    SCW.ContentProperties[contentName] = contentValue;
	}

	, AddEvent: function(name) {
	  //name: the FULL NAME of the event (eg: "event8", "products", etc.)
	  if (SCW.Events.indexOf(name) < 0)
	    SCW.Events.push(name);
	}

	, GetEventString: function() {
	  var retVal = "";
	  for (var index = 0, len = SCW.Events.length; index < len; ++index) {
	    retVal += SCW.Events[index];
	    retVal += ",";
	  }
	  return retVal.substring(0, retVal.length - 1);
	}

	, AddEVar: function(n, val) {
	  //n  : the evar NUMBER (eg: "8", "34", etc.)
	  //val: the value of the evar (any string)
	  SCW.EVars[n] = val;
	}

	, AddProp: function(n, val) {
	  //n  : the prop NUMBER  (eg: "8", "34", etc.)
	  //val: the value of the property (any string)

	  SCW.Props[n] = val;
	}

	, TL: function(linkObject, kind, friendlyName, options) {
	  /*
	  * SAMPLE USAGE:
	  * <a href="someurl.html" 
	  *    onClick="SCW.TL(this,'o','User Did Something',{linkTrackVars:'events',linkTrackEvents:'event7',events='event7'});">
	  *	Click Here
	  * </a>
	  */

	  //get out if not active ;)
	  /*if (!SCW.Active) return;*/

	  if (!options) options = {};
	  var stl = s_gi(s_account);
	  for (key in options) { stl[key] = options[key]; }
	  //debugger;
	  stl.tl(linkObject, kind, friendlyName);
	}

}

/* UefaCom Specific */
var CustomSCW = {

	subsectionCount: 4

	, fillUserDetails: function() {
		var cookie = readCookie('manSession');
		if (cookie) {
			var userId = readCookieParam('manSession', 'UserId');
			s.prop24 = userId; //userid
			s.prop25 = 'Logged in';
			s.prop26 = 'web';
			s.prop33 = 'no'; //Newsletter yes or no

			s.prop43 = 'web';
		}
		else {
			s.prop25 = 'Not logged in';
		}
	}

	, chainProps: function(ht, count, separator) {

	  if (!separator)
	    separator = ":";

	  var retVal = ht["sec"] + separator;

	  for (var ix = 1; ix <= count; ix++) {
	    if (ht["subsec" + ix]) {
	      retVal += ht["subsec" + ix];
	      retVal += separator;
	    }
	  }
	  return retVal.substring(0, retVal.length - 1);
	}

	, fillCommonPageProperties: function(ht) {
	  s.channel = ht["sec"];

	  //Add PageType for ErrorPage
	  s.pageType = ht["pagetype"];

	  for (var ix = 1; ix <= CustomSCW.subsectionCount; ix++) {
	    if (ht["subsec" + ix]) {
	      var val = CustomSCW.chainProps(ht, ix);
	      val = this.onPropSave(ix, val);
	      s["prop" + ix] = val;
	    } else {
	      break;
	    }
	  }

	  s.prop5 = ht["type"];

	  s.hier1 = CustomSCW.chainProps(ht, CustomSCW.subsectionCount, ",");
	  s.hier1 = ANSICharConverter(s.hier1);

	  //pageName
	  s.pageName = CustomSCW.chainProps(ht, CustomSCW.subsectionCount);
	  if (ht["type"]) {
	    s.pageName += ":";
	    s.pageName += ht["type"];
	  }
	  s.pageName = ANSICharConverter(s.pageName);

	  s.prop11 = ht["lang"].toUpperCase();
	}

	, Execute: function() {
		/*
		var c = $.query.get('c');
		var competition = null;

		if (c == 3) { competition = 'euro'; }
		if (c == 5) { competition = 'futsal'; }
		if (c == 27) { competition = 'futsalcup'; }
		if (c == 9) { competition = 'supercup'; }
		if (c == 1) { competition = 'ucl'; }
		if (c == 14) { competition = 'uel'; }
		if (c == 23) { competition = 'under17'; }
		if (c == 24) { competition = 'under19'; }
		if (c == 13) { competition = 'under21'; }
		if (c == 28) { competition = 'womencup'; }
		if (c == 101) { competition = 'wunder17'; }
		if (c == 25) { competition = 'wunder19'; }
		if (c == undefined || c == null || c == '') { competition = 'video'; }

		if (competition) SCW.AddContent("sec", competition.toLowerCase(), true);
		*/
		CustomSCW.fillUserDetails();
		CustomSCW.fillCommonPageProperties(SCW.ContentProperties);

		if (SCW.ContentProperties["title"]) {
			s.prop8 = SCW.ContentProperties["title"];
			s.pageName += ":";
			s.pageName += SCW.ContentProperties["title"];
		}
		if (SCW.ContentProperties["id"])
			s.prop7 = SCW.ContentProperties["id"];
		if (SCW.ContentProperties["author"])
			s.prop9 = SCW.ContentProperties["author"];
		if (SCW.ContentProperties["comp"])
			s.prop13 = SCW.ContentProperties["comp"];
		if (SCW.ContentProperties["matchday"])
			s.prop15 = SCW.ContentProperties["matchday"];
		if (SCW.ContentProperties["round"])
			s.prop17 = SCW.ContentProperties["round"];
		if (SCW.ContentProperties["match"])
			s.prop16 = SCW.ContentProperties["match"];
		if (SCW.ContentProperties["club"])
			s.prop19 = SCW.ContentProperties["club"];
		if (SCW.ContentProperties["season"])
			s.prop14 = SCW.ContentProperties["season"];
		if (SCW.ContentProperties["group"])
			s.prop18 = SCW.ContentProperties["group"];
		if (SCW.ContentProperties["player"])
			s.prop20 = SCW.ContentProperties["player"];

		//the event string
		var sEvents = SCW.GetEventString();
		if (sEvents.length > 0) {
			if (s.events)
				s.events += ",";
			else
				s.events = "";
			s.events += sEvents;
		}

		//set all evars and props so far
		//note that with our settings, all EVar and Props set before can be overridden by these ones
		for (key in SCW.EVars) { s["eVar" + key] = SCW.EVars[key]; }
		for (key in SCW.Props) { s["prop" + key] = SCW.Props[key]; }
		}

		,onPropSave: function(n, val) {
		if (n == 2) val = ANSICharConverter(val);
		return val;
		}
		
		,sendOmniture: function() {
			var s_code = s.t();
			if (s_code)
				document.write(s_code);

		}
}

function ANSICharConverter(s) {
  var charList = new Array(206, 238, 234, 202, 158, 138, 192, 193, 196, 197, 229, 224, 225, 227, 228, 198, 230, 201, 203, 200, 232, 233, 235, 204, 205, 207, 236, 237, 239, 338, 211, 214, 216, 243, 246, 248, 244, 220, 218, 252, 250, 268, 269, 199, 231, 262, 263, 209, 241, 353, 352, 223, 221, 253, 381, 382, 351);
  var mappingList = new Array("I", "i", "e", "E", "z", "S", "A", "A", "A", "A", "a", "a", "a", "a", "a", "AE", "ae", "E", "E", "E", "e", "e", "e", "I", "I", "I", "i", "i", "i", "OE", "O", "O", "O", "o", "o", "o", "o", "U", "U", "u", "u", "C", "c", "C", "c", "c", "c", "N", "n", "s", "S", "ss", "Y", "y", "Z", "z", "s");

  s = '' + s;
  var stringBuilder = '';
  //for (var i=0; i<charList.length; i++) {
  //		var sChar = String.fromCharCode( charList[i] );
  //		stringBuilder = stringBuilder.replace( sChar, mappingList[i] );
  //}

  for (var n = 0; n < s.length; n++) {
    var c_old = s.charCodeAt(n);
    var pos = charList.indexOf(c_old);
    var c_new = String.fromCharCode(c_old);
    if (pos != -1) c_new = mappingList[pos];
    stringBuilder += c_new;
  }
  return stringBuilder;
}
