var backgroundImageRatio;// = 1024 / 644;
var topMenuMap = new Array();			
var topMenuMapKeyset = new Array();			
var jsonString;

function debugOut(out) {
	alert(out);
}

function initWheel() {
	var firstLevelCount = $j('.wheel > .menu > li').length;
	
	//sample from ActionScript
	//loadMenuStructureFromJavascript('[["Menu 0","http://1",[["Menu 0.1","http://2"],["Menu 0.2", "http://3"]]], ["Menu 1","http://4",[["Menu 1.1","http://5"],["Menu 1.2", "http://6"]]]]);
	
	jsonString = '[';
	
	for (var i = 0; i < firstLevelCount; i++) {
		jsonString += '["' + $j('.wheel > .menu > li > a > span').eq(i).text() + '",';		
		jsonString += '"' + $j('.wheel > .menu > li > a').eq(i).attr("href") + '",[';
		
		var secondLevelCount = $j('.wheel > .menu > li').eq(i).find('ul > li').length;
		
		for (var j = 0; j < secondLevelCount; j++) {
			jsonString += '["' + $j('.wheel > .menu > li').eq(i).find('ul > li > a > span').eq(j).text() + '",';
			jsonString += '"' + $j('.wheel > .menu > li').eq(i).find('ul > li > a').eq(j).attr("href") + '"]';
			if (j < (secondLevelCount - 1))
				jsonString += ',';
		}				
		jsonString += ']]';
		if (i < (firstLevelCount - 1))
			jsonString += ',';				
	}
	
	jsonString += ']';

	//alert(jsonString);
	
	$j('.wheel').remove();	
}

function getMenuStructureFromJavascript() {
	if (isIE)
		getFlashMovie('SeyesCircle').sendMenuStructureToFlash(jsonString);
	else 
		$j('#flashDiv').flash(function(){this.sendMenuStructureToFlash(jsonString);});
}			

function getHighlightWordFromJavascript() {
	var path = window.location.href;
	if (indexOf2(path, "?", 0) != -1) //the top url is not trimmed
		path = trimURL(path);
	highlightMenu(path + "seyes/");
}				

function ajaxLoadNewPage(newPageUrl) {				
	//alert(newPageUrl);					
	$j('#ajaxLoader').css('display','');
	var newPageUrl = "" + newPageUrl;
	
	$j.ajax({
		url: newPageUrl,
		cache: false,
		success: function(xml) {				
			var contentString = getTagString(xml, "content");
			contentString = swfObjectizeStep1(contentString);			
			var bannerString = getTagString(xml, "banner");
			ajaxSwitchContent(contentString, "content");	
			swfObjectizeStep2();			
			ajaxSwitchContent(bannerString, "banner");
			ajaxifyAnchors();
			highlightMenu(newPageUrl);
			$j('#content').scrollTop(0,0);
			$j('#ajaxLoader').css('display','none');						
		}
	});

}

function ajaxSwitchContent(xml, id) {	
	$j('#' + id).html($j(xml).html());			
}

function getTagString(xml, id) {
	var start = indexOf2(xml, '<div id="' + id + '"', 0);		
	xml = xml.substring(start, xml.length);
	var end = indexOf2(xml, '<!--end_' + id, 0);
	xml = xml.substring(0, end);	
	return xml;
}

function highlightMenu(thisUrl) {
	if (isIE)
		getFlashMovie('SeyesCircle').highlightWord(thisUrl);
	else 
		$j('#flashDiv').flash(function(){this.highlightWord(thisUrl);});		
		
	for (var i = 0; i < topMenuMapKeyset.length; i++)
		topMenuMap[topMenuMapKeyset[i]].removeClass("selected");
	
	if (topMenuMap[thisUrl] != null)
		topMenuMap[thisUrl].addClass("selected");			
}

function ajaxifyAnchors() {	
	for (var i = 0; i < $j("a").length; i++) {
		if (!$j("a").eq(i).hasClass("ajaxified") && !$j("a").eq(i).hasClass("external_link")) {
			if ($j("a").eq(i).parent().parent().hasClass("menu_top")) {
				topMenuMap[$j("a").eq(i).attr("href")] = $j("a").eq(i).parent();			
				topMenuMapKeyset.push(trimURL($j("a").eq(i).attr("href")));
			}
			$j("a").eq(i).attr("storedLink", trimURL($j("a").eq(i).attr("href")));
			$j("a").eq(i).click(function(){ajaxLoadNewPage($j(this).attr("storedLink"));});
			$j("a").eq(i).removeAttr("href");
			$j("a").eq(i).addClass("ajaxified");						
		}
	} 		
}

function trimURL(url) {
	if ((url.length > 4) && (url.substring(0, 4) == "http")) {
		var startOfPath = indexOf2(url, "/", 7);
		url = url.substring(startOfPath, url.length);
	}
	return url;
}

function createWheelNonIE(swfURL) {	
	$j('#flashDiv').flash({ swf: swfURL, 
							width: '100%', 
							height: '100%', 
							params: { 
								id: 'SeyesCircle',
								width: '100%', 
								height: '100%', 									
								menu: 'false', 
								bgcolor: '#ffffff', 
								src: swfURL,
								quality: 'high',
								align: 'middle',
								wmode: 'transparent',
								allowScriptAccess: 'sameDomain',
								FlashVars: {
									textRad1: '219',
									textRad2: '201',
									textSize1: '18',
									textSize2: '14',
									textColour1: '0x303030',
									textColour2: '0xf0f0f0'	
								}
							} });	  	
}

function displayFlashWheel() {
	$j('#flashDiv').css("display","");
}	

function getFlashMovie(movieName) {
	return window[movieName];
}			

function resizeBackground() {
	var windowRatio = $j(window).width() / $j(window).height();

	if (windowRatio > backgroundImageRatio) {
		//normalised height of image is greater than normalised height of window
		//so make width of image equal window width
		$j('#backgroundImage').css('width', $j(window).width());
		$j('#backgroundImage').css('height', $j(window).width() / backgroundImageRatio);
	} else {
		//normalised width of image is greater than normalised width of window
		//so make height of image equal window height
		$j('#backgroundImage').css('height', $j(window).height());
		$j('#backgroundImage').css('width', $j(window).height() * backgroundImageRatio);
	}		
}	