var xml;

$.ajaxSetup ({ cache: false });

$(document).ready(function() {

	$.ajax({
		type: "GET",
		url: "cms/data/info.xml",
		dataType: "xml",
		success: xml_loaded
	});

	$(window).resize(function() {
		home_banner_img_resize();
	});
	
	home_banner_img_resize();
});

function home_banner_img_resize() {
		var div_h = $("#home_banner").height();
		var div_w = $("#home_banner").width();
		var div_ratio = div_w / div_h;
		if (div_ratio < 2.461) {
			$('#home_banner img').css('width' , '100%');
			$('#home_banner img').css('height', 'auto');
		} else {
			$('#home_banner img').css('width' , 'auto');
			$('#home_banner img').css('height', '100%');
		}
}

function xml_loaded(data) {
	xml = data;
	
	menu__init();

	Cufon.replace("#copyright2", { fontFamily: 'Regular' });
	Cufon.replace("#client_login", { fontFamily: 'Regular' });
	Cufon.replace("#home_menu ul li a", { fontFamily: 'Light' });
	Cufon.replace("#home_menu ul li span", { fontFamily: 'Regular' });
}

function menu__init() {

	var last_index = $(xml).find("sections > section[status=1]").length - 1;
	$(xml).find('sections > section[status=1]').each(function(index) {

		if ($(this).attr('description') != '') {
			a_href = $(this).attr('description');
			if ( ! /http/.test(a_href) ) {
				a_href = 'http://'+a_href;
			}
		} else {
			var path = menu__link($(this));
			a_href = 'page.php#'+path;
		}
		var a = $("<a />")
			.attr('href', a_href)
			.html($(this).attr('name'));
		var span = $("<span />")
			.html('/')
			.addClass('separator');
		var li = $("<li />");

		a.appendTo(li);
		if (index != last_index) {
			span.appendTo(li)
		}
		li.appendTo("#home_menu ul");

	});
}

function menu__link(section) {
	if (section.find('section[status=1]').length > 0) {
		return  "/" + section.attr('name') + menu__link(section.find('section[status=1]').eq(0));
	} else {
		return  "/" + section.attr('name');
	}
}

