// Make calls to other operations from this file. 

addLoadEvent(onPageLoad);

// fired at the end of the page once all html has loaded
function onDOMload () {
	
	// sIFR Flash Text 
	if(typeof sIFR == "function"){
		switchFonts();
	};
	
	// Image Transitions / Effects
	if(typeof buildTransitions == "function") {
		buildTransitions();
	};
	
	if(typeof do_homepage_flash_replacements == "function") {
	// Flash Replacements
		do_homepage_flash_replacements();
	}
	
};

// Fired once all content has loaded
function onPageLoad () { };

function do_homepage_flash_replacements() {
	
	var body = document.getElementsByTagName('body')[0];
	if (!hasClass(body, 'home')) return false;
	
	replace_jockey_colours();
	replace_word_search();
	replace_foals_callout();
	replace_high_scores();
}

/* Flash Replacements */



function replace_jockey_colours() {

	var replace_id = 'jockey_colours_callout';
	var swf_path = 'jockey_colours_callout.swf';
	var width = 272;
	var height = 147;

	do_flash_replacement(replace_id, swf_path, height, width);
}

function replace_word_search() {
	
	var replace_id = 'word_search_callout';
	var swf_path = 'playbtn_wordsearch-2.swf';
	var width = 272;
	var height = 147;

	do_flash_replacement(replace_id, swf_path, height, width);
}

function replace_foals_callout() {
	
	var replace_id = 'foals_callout';
	var swf_path = '/darleykids/_assets/flash/foal_diary_images.swf';
	var width = 170;
	var height = 170;
	var version = '7.0.0';
	var background = '#ffffff';	

	//do_flash_replacement(replace_id, swf_path, height, width, false, '#ff0000');
	var f = new FlashObject(swf_path, replace_id, width, height, version, background);
	f.addVariable("images_path", "/darleykids/_assets/flash/images");
	f.write(replace_id);
}

function replace_high_scores() {

	var replace_id = 'high_scores';
	var swf_path = '/darleykids/_assets/flash/top10_scorers.swf';
	var width = 192;
	var height = 160;
	var version = '7.0.0';
	var background = '#ffffff';

	var scores = document.getElementById(replace_id);
	var scores_xml = scores.innerHTML;
	scores.removeChild(scores.getElementsByTagName('ol')[0]);

	// Create replacement object
	var f = new FlashObject(swf_path, replace_id + "_flash", width, height, version, background);
	
	// Default params
	f.addParam("quality", "high");
	f.addParam("wmode", "transparent");
//	f.addParam("FlashVars", "scores='" + scores_xml + "'");
	f.addVariable("scores", escape(scores_xml));
	// Insert flash
	f.write(replace_id);
}

function addLoadEvent(func) {
	
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
		if (oldonload) {
			oldonload();
		}
			func();
		}
	}
}

function do_flash_replacement(replace_id, swf_path, height, width, version, background) {

	// Check the element ID exists before trying to replace
	if (!document.getElementById(replace_id)) return false;

	// Base flash dir
	var flash_path_base = '/darleykids/_assets/flash/';
	
	// Defaults
	version = version || '7.0.0';
	background = background || '#FFFFFF';
	
	// SWF file path
	swf_path = flash_path_base +  swf_path;

	// Create replacement object
	var f = new FlashObject(swf_path, replace_id + "_flash", width, height, version, background);
	
	// Default params
	f.addParam("quality", "high");
	f.addParam("wmode", "transaprent");
	
	// Insert flash
	f.write(replace_id);
}
//***Returns true if the object has the class assigned, false otherwise.
function hasClass(obj,cName){ return (!obj || !obj.className)?false:(new RegExp("\\b"+cName+"\\b")).test(obj.className) }