var RPOST;
if (!RPOST) {
RPOST = {
	  useHistory:false
	, appletRoot: "http://applet.racingpost.com/HTMLpb_9.4c/"
	, betslipAffs: {
                                        B11:"4070"
                                        ,B2:"a_22452b_3423"
                                        ,B3:"310"
                                        ,B4:"154"
                                        ,B6:"252"
                                        ,B7:"30"
                                        ,B8:"10001"
                                        ,B10:"2311"
                                        ,B12:"RAP10"
                                        ,B14:"262"
                                        ,B0:"0"
                                        ,B16:"0"
                                        ,B17:"3631"
                                        ,B15:"101990"
                                    }
	, setSubdomain: function() {
		var URL = document.location.toString();
		var domS = URL.indexOf('.');

		if (domS != -1) {
			var s = URL.substr(domS+1);
			var domE = s.indexOf('/');
			if (domE != -1) {
				s = s.substr(0, domE);
			}
			document.domain = s;
		}
	}
	, userPrefs: {
		  reg_uid: 0
		, edition: 4
		, reg_md5: ""
		, bookmakerPreferences: "2|3|6|7|8|10|11|12|17|15"
		, isModerator: false
		, isLoggedIn: false
		, isFreePackage: true
		, thisDomain: ".racingpost.com"
		, MembersPage: "http://www.racingpost.com/shared/members_landing.sd"
		
		, getSingleBookie: function() {
			return 'Best';
		}
		, getBookies: function() {
			return;
		}

		, getUserIDs: function() {
			return {
				  reg_uid:			this.reg_uid
				, reg_md5:			this.reg_md5
				, edition:		 	this.edition
				, bookmakerPreferences:		this.bookmakerPreferences
				, isLoggedIn:			this.isLoggedIn
				, isFreePackage:		this.isFreePackage
				, isModerator:			this.isModerator
			};
		}
		, getFormat: function() {
			try {
				if (typeof(myPrefodds) === 'string') {
					return myPrefodds=='Decimal'? 'DECS':'ODDS';
				}
			}
			catch(e) {}
			return 'ODDS';
		}
		, joinMembersClub: function(inCategory) {
			if (UserPrefs.getUserIDs().isLoggedIn) {
				openUserPreferences('SUBSCRIPTIONS',inCategory);
			} else {
				callLogin(inCategory);
			}
		}
		, barrierClick: function(inSubCatId) {
			if (UserPrefs.getUserIDs().isLoggedIn == false) {
				callLogin(inSubCatId);
			} else {
				openUserPreferences('SUBSCRIPTIONS',(inSubCatId?inSubCatId:''));
			}
		}		
		, isFree: function(inBarrier, inSubCatId, inAdd, inJoin, inLogin) {
			var useGraphic = 1;
			var output = "<span id=\"ruli_"+inSubCatId+"\">";
			var title = '';
			if (inAdd.indexOf('.gif') == -1 
				&& inJoin.indexOf('.gif') == -1 
				&& inLogin.indexOf('.gif') == -1) 
			{
				useGraphic = 0;
			}
			if (useGraphic == 1) {
				output += "<img src=\"/img/members/";
			}
			if (this.isLoggedIn == false) {
				output += inLogin;
				title = 'Log in';
			} else {
				if (this.isFreePackage) {
					output += inJoin;
					title = 'Join The Club';
				} else {
					output += inAdd;
					title = 'Add to subscription';
				}
			}
			if (useGraphic == 1) {
				output += "\"  style=\"cursor:pointer;\" alt=\""+title+"\" title=\""+title+"\" />";
			}
			output += "</span>";
			$(document).ready(
				function(){ 
					$('#'+inBarrier).html(output);
				}
			);
		}
		, isFreeText: function(inBarrier, inSubCatId, inAdd, inJoin, inLogin) {
			var output = "";
			if (this.isLoggedIn == false) {
				output += inLogin;
			} else {
				if (this.isFreePackage) {
					output += inJoin;
				}
				else {
					output += inAdd;
				}
			}
			$(document).ready(
				function(){ 
					$('#'+inBarrier).html(output);
				}
			);
		}
	}
	, refreshList: []
	, maxSyncRetries: 150
	, horsePredictorRunnersQty: 6
	, siteReference: "rac-rac-racingpost"
	, ua: navigator.userAgent
	, _isIE: (navigator.userAgent.indexOf("MSIE") != -1)
	, isIE: function() { return this._isIE; }
	, _isFF: (navigator.userAgent.indexOf("Firefox") != -1)
	, isFF: function() { return this._isFF; }
	, _isSafari: (navigator.userAgent.indexOf("Safari") != -1 && navigator.userAgent.indexOf("Chrome") == -1)
	, isSafari: function() { return this._isSafari; }
	, _isChrome: (navigator.userAgent.indexOf("Chrome") != -1)
	, isChrome: function() { return this._isChrome; }
	, lastTime:0
	, debug: function(s) {
		if (RPOST.ua.indexOf("JONAH") == -1) {
			return;
		}
		if ((!RPOST.isFF() && !RPOST.isChrome()) || !window.console) {
			return;
		}
		var d = new Date(), elapsed="", t = {msecs: d.getMilliseconds().toString(), secs:d.getSeconds().toString(), mins :d.getMinutes().toString(), hours :d.getHours()}, a=arguments;
		t.msecs = t.msecs.length == 1 ? ("00" + t.msecs) : (t.msecs.length == 2 ? ("0" + t.msecs) : t.msecs);
		t.secs = t.secs.length == 1 ? ("0" + t.secs) : t.secs;
		t.mins = t.mins.length == 1 ? ("0" + t.mins) : t.mins;
		if (this.lastTime > 0 && (elapsed = d.getTime() - this.lastTime) > 0) {
			if (elapsed < 10000) {
				elapsed = " -- "+elapsed;
			} else { 
				elapsed = ""; 
			}
		}
		window.console.log(t.hours+":"+t.mins +":"+t.secs+"."+t.msecs+elapsed, s,a[1]?a[1]:"",a[2]?a[2]:"",a[3]?a[3]:"",a[4]?a[4]:"",a[5]?a[5]:"",a[6]?a[6]:"",a[7]?a[7]:"",a[8]?a[8]:"",a[9]?a[9]:"",a[10]?a[10]:"");
		this.lastTime = d.getTime();
	}
	, imageRoot: "/images/"
	, webRoot: "http://www.racingpost.com"
	, mainSiteDomain: "http://www.racingpost.com"
	, uiDomain: "http://ui.racingpost.com"
	, secureRoot: "https://reg.racingpost.com"
	, linkvoid: function() { }
	, loadingImage: "<img src='http://ui.racingpost.com/img/all/loading.gif' alt='Loading...' title='Loading...'/>"
	, Loading: "<div class='loading'><img src='http://ui.racingpost.com/img/all/loading.gif' alt='Loading...' title='Loading...'/></div>"
	, horseForm: {}
	, showPanelTab: function (curr, idOn, idOff)
	{
		$(curr).parents('.panel:first').find('.btnArrow').addClass('btnArrowOff');
		$(curr).removeClass('btnArrowOff');
		$('#'+idOn).show();
		$('#'+idOff).hide();
	}
	, currentBackedObject:null
	, changeEntityBacked: function(id) {
		if (id=='-1') return false;
		id = parseInt(id, 10);
		if (isNaN(id) || id == 0) id = 3;
		else id += 9990000;
		if (this.currentBackedObject === null) {
			this.currentBackedObject = "gm_"+Math.random();
			$('#mbMainOutput').html('<span id="'+this.currentBackedObject+'" class="posRelative"></span>' );
		}
		else {
			this.applet.destroyApplet(this.currentBackedObject);
		}
		this.currentBackedObject = this.applet.createGeneralMessage(
		{
			  targetId:this.currentBackedObject
			, marketId:id
			, dir:"AT_START"
			, max:10
			, style:"comments_1"
		});
		return false;
	}

	, currentCommentary:null
	, switchCommentaryHead: function(inFlag)
	{
		if (typeof inFlag == 'undefined') inFlag = 'Normal';

		if (inFlag == 'FirstTime')
		{
			initPulldowns();
			setActivePulldownLink('tcPulldown','#'+tcRaceId);
			pulldownOnChangeFunctions['comGridPulldownTd']='RPOST.comChangeRace';
		}
		else
		{
			if (RPOST.currentCommentary !== null) RPOST.applet.destroyApplet(RPOST.currentCommentary);
			comHeadOutput=$('#tcHeadOutput')
			comHeadOutput.load(
				"/horses/text_commentary_head.sd?r_date="+tcResultDay+"&race_id="+tcRaceId,
				function()
				{
					initPulldowns();
					setActivePulldownLink('tcPulldown','#'+tcRaceId);
					pulldownOnChangeFunctions['comGridPulldownTd']='RPOST.comChangeRace';
				}
			);
		}
	}
	, switchCommentaryMode: function(resultDayIn, raceIdIn) {
		tcResultDay=resultDayIn;
		tcRaceId=raceIdIn
		if(tcRaceId && tcResultDay) {
			openUrl = "/horses/text_commentary.sd?r_date="+tcResultDay+"&race_id="+tcRaceId;
		}
		else {
			openUrl = "/horses/text_commentary.sd?race_id="+tcRaceId;
		}
		RPOST.switchCommentaryHead();

		$('#tcMainOutput')[0].innerHTML = RPOST.Loading;
		$('#tcMainOutput').load(openUrl);
		return false
	}
	, comChangeRace: function(id) {
		tcRaceId=id!=0?id:''
		RPOST.switchCommentaryMode(tcResultDay,tcRaceId)
	}
	, linkvoid: function() { }

	, liveReporter:	{
		  changeCourse: function(){}
		, changeRace: function(){}
	}
	, liveReporterScores: {
		  course: function(){}
		, quotes: function() {
			RPOST.fireScore({sc:"101",ds:"Quotes",lc:"117",sp:"2"});
		}
	}
	,scoreNames: {
		 event: "ev"
		,section:"sc"
		,location:"lc"
		,sport:"sp"
		,description:"ds"
		,bookmaker:"bk"
		,sportIds:{
			 FOOTBALL:4
			,HORSES:2
			,GREYHOUNDS:3
		}
	}
	,fireScore: function(params) {
		var xml = "<"+"?xml version=\"1.0\" encoding=\"UTF-8\"?><sl><list __isArrayHolder='Score'>";
		xml += "<sd ";
		var desc = '';
		for (p in params) {
			if (p == "ds") {
				desc = params[p];
			}
			else {
				xml += p + "=\"" + params[p] + "\" ";
			}
		}
		xml += ">"+desc+"</sd>";
		xml += "</list></sl>";
		$.ajax({
			type: "POST"
			, url: "/public_gateway/scorecard.sd"
			, contentType: "text/xml"
			, dataType: "text"
			, data:(xml)
		});
	}
	, mainMenu: {
		score: function(type) {
			RPOST.fireScore({
				  sc:109
				, lc:105
				, ds: type
			});
		},
		activeTab:'home'
	}
	, siteType: "default"
};
RPOST.setSubdomain();
RPOST.diffusion = {iframeUrl:'http://push0.' + document.domain + '/rp_iframes/rp_2.html' }
var calledByMC = 0;
var oTidyBrowser = []; 

var isLoggedIn = RPOST.userPrefs.isLoggedIn;
var isFreePackage = RPOST.userPrefs.isFreePackage;

RPOST.refreshList.push(
	function(state, category) {
		var cat = (category == null) ? "0" : category;   	
		var success = 0;
		if(state == 0 || calledByMC==1) {
			return false;		
		}
		$.ajax({
			type: "POST",
			url: "/public_gateway/retrieve_customer_preferences.sd",
			dataType: "html",
			data: ' ',
			cache: false,
			success: function(data) {
				try {
					x = document.body;
					while(x.hasChildNodes()) {
						x.removeChild(x.childNodes[0])
					}            
				}
				catch(err) {}
			    
				var winLocation = new String(window.location);
				winLocation = winLocation.split('#', 1);            
				window.location = winLocation;
			}
		});
		return true;
	}
);		
function componentRefresh(state, category) {
	var cat = (category == null) ? "0" : category;  
	for (i in RPOST.refreshList) {
		if (RPOST.refreshList[i](state,cat)) {
			break;
		}
	}
}
}
var server;
if (!server) {
	server = {};
}
server.ui = "http://ui.racingpost.com";
