// Some variables used by the framework for working out portal sizes and grid column sizes etc.


var v_rezx;
var v_rezy;

// All the main variables are stored in here, set them up for defaults....
var global = {
		
	MarketID: '',
	PlcMarketID: '',
	BFmarket: false,
	MarketType: '',
	MarketStatus: '',
	MarketCountry: '',
	RaceDate: new Date().format( 'd-m-Y' ),
	RaceTime: '',
	LastUpdate: '',
	RaceNumber: '',
	TrackCode: '',
	
	//used to turn on auto next race.
	Race_Autoload: false,
	
	//used to filter and build a types of races to be shown
	show_gallops: true,
	show_greyhounds: false,
	show_harness: false,
	//do they want to see the results
	show_results: true,
	//do they want to see the place market prices
	show_place: true,
	show_win: true,
	//do thet want to see the full stats.
	show_fullstats: false,
	show_fgraph: false,
	
	show_bfwap: true,
	show_bflay: true,
	show_stab: true,
	show_nsw: true,
	show_uni: true,
	show_booksp: false,
	show_silk: true,
	show_barrier: true,
	show_WGT: true,
	show_jockey: true,
	
	//advantage settings and default settings, need to be the same as the menu settings.
	// do they want to see it?
	show_advantage: false,
	// compare what with what?
	advantage_type: "BT",
	//these two are used for showing the ADV% based on current pool percentage. one is the stored value of the menu setting, the other is the market.
	advantage_percentage: 120,
	bf_pool_percentage: 0,
	//a variable used to show prices less commission, needs to match menu default.
	my_commission: 0,

	
	show_reductionf: false,
	
	show_scratchings: true,
	
	refresh_timeout: 120000, // A timeout, the default time for relaoding stuff, 2 MINS
	auto_refresh: false, //this is set by the user if they want the data to auto refresh based on refresh interval above.
	refresh_halt: false, //this is used to halt the refresh regardless if the market is closed, resets to false upon new race selection etc.
	refresh_user: false, //sets to true if it was a user refresh, rather than a automated refresh, used to work out Next race or User Race.
	refresh_tag: '' //a variable to store the tag given by refresh interval, used later to stop it of cancel it etc.
};

//now try to get variables from the cookies if they exists.
read_cookie();
//now see if a Market ID was passed, if it was then the person most likely wants to look at it...
m = querySt('marketid');
if (m) global.MarketID = m;
m = querySt('date');
if (m) global.RaceDate = m;

//get somthing passed.
function querySt(ji) {
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for (i=0;i<gy.length;i++) {
		ft = gy[i].split("=");
		if (ft[0] == ji) {
			return ft[1];
		}
	}
}

//Cookie functions....


function write_cookie() {
		setCookie_element("show_gallops", global.show_gallops);
		setCookie_element("show_greyhounds", global.show_greyhounds);
		setCookie_element("show_harness", global.show_harness);
		setCookie_element("show_results", global.show_results);
		setCookie_element("show_place", global.show_place);
		setCookie_element("show_win", global.show_win);
		setCookie_element("show_fullstats", global.show_fullstats);
		setCookie_element("show_advantage", global.show_advantage);
		setCookie_element("show_reductionf", global.show_reductionf);
		setCookie_element("show_scratchings", global.show_scratchings);
		setCookie_element("Race_Autoload", global.Race_Autoload);
		setCookie_element("show_fgraph", global.show_fgraph);
		setCookie_element("show_bfwap", global.show_bfwap);
		setCookie_element("show_bflay", global.show_bflay);
		setCookie_element("show_stab", global.show_stab);
		setCookie_element("show_nsw", global.show_nsw);
		setCookie_element("show_uni", global.show_uni);
		setCookie_element("show_booksp", global.show_booksp);
		setCookie_element("show_silk", global.show_silk);
		setCookie_element("show_barrier", global.show_barrier);
		setCookie_element("show_WGT", global.show_WGT);
		setCookie_element("show_jockey", global.show_jockey);
		
		setCookie_element("advantage_type", global.advantage_type);
		setCookie_element("advantage_percentage", global.advantage_percentage);
		setCookie_element("my_commission", global.my_commission);
		setCookie_element("refresh_timeout", global.refresh_timeout);
}

function setCookie_element(c_name,value)
{
	expiredays = 365;
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" + escape(value) + ";expires=" + exdate.toGMTString();
}


function read_cookie() {
	//read the cookies if they exist.
	r = getCookie_element("show_gallops");
	if (r != null) global.show_gallops = stobool(r); 
	r = getCookie_element("show_greyhounds");
	if (r != null) global.show_greyhounds = stobool(r); 
	r = getCookie_element("show_harness");
	if (r != null) global.show_harness = stobool(r);
	r = getCookie_element("show_results");
	if (r != null) global.show_results = stobool(r);
	r = getCookie_element("show_place");
	if (r != null) global.show_place = stobool(r);
	r = getCookie_element("show_win");
	if (r != null) global.show_win = stobool(r);
	r = getCookie_element("show_fullstats");
	if (r != null) global.show_fullstats = stobool(r);
	r = getCookie_element("show_advantage");
	if (r != null) global.show_advantage = stobool(r);
	r = getCookie_element("show_reductionf");
	if (r != null) global.show_reductionf = stobool(r);
	r = getCookie_element("show_scratchings");
	if (r != null) global.show_scratchings = stobool(r);
	r = getCookie_element("show_fgraph");
	if (r != null) global.show_fgraph = stobool(r);
	r = getCookie_element("advantage_type");
	if (r != null) global.advantage_type = r;
	r = getCookie_element("advantage_percentage");
	if (r != null) global.advantage_percentage = parseFloat(r);
	r = getCookie_element("my_commission");
	if (r != null) global.my_commission = parseFloat(r);
	r = getCookie_element("refresh_timeout");
	if (r != null) global.refresh_timeout = parseFloat(r);
	r = getCookie_element("Race_Autoload");
	if (r != null) global.Race_Autoload = stobool(r);
	r = getCookie_element("show_bfwap");
	if (r != null) global.show_bfwap = stobool(r);
	r = getCookie_element("show_bflay");
	if (r != null) global.show_bflay = stobool(r);
	r = getCookie_element("show_stab");
	if (r != null) global.show_stab = stobool(r);
	r = getCookie_element("show_nsw");
	if (r != null) global.show_nsw = stobool(r);
	r = getCookie_element("show_uni");
	if (r != null) global.show_uni = stobool(r);
	r = getCookie_element("show_booksp");
	if (r != null) global.show_booksp = stobool(r);
	r = getCookie_element("show_silk");
	if (r != null) global.show_silk = stobool(r);
	r = getCookie_element("show_barrier");
	if (r != null) global.show_barrier = stobool(r);
	r = getCookie_element("show_WGT");
	if (r != null) global.show_WGT = stobool(r);
	r = getCookie_element("show_jockey");
	if (r != null) global.show_jockey = stobool(r);
}


function getCookie_element(c_name) {
	if (document.cookie.length > 0)
	  	{
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start != -1)
			{
			c_start = c_start + c_name.length + 1;
			c_end = document.cookie.indexOf(";",c_start);
			if (c_end == -1) c_end = document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
			}
	  	}
		else return null;
	return null;
}


function stobool(v) {
	if (v == "true") return true; else return false; 
}


function GetScreenSizes() {
	// This function is used to determine the browser avalible space, so everything will resize corrently.
	if( typeof( window.innerWidth ) == 'number' ) {
	//Non-IE
		v_rezx = window.innerWidth;
		v_rezy = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
		  v_rezx = document.documentElement.clientWidth;
		  v_rezy = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    //IE 4 compatible
		v_rezx = document.body.clientWidth;
		v_rezy = document.body.clientHeight;
	}
}