//==============================================================================
function preloadMenu(){
	var sections = ['about', 'trading', 'forex', 'clients'];
	for (var i in sections){
		preloadImage('../img/' + sections[i] + '.gif');
		preloadImage('../img/' + sections[i] + '_active.gif');
	}
}

//==============================================================================
function preloadImage(src){
	tmp = new Image();
	tmp.src = src;
}

//==============================================================================
function externalLinks(){
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external"){
			anchor.target = "_blank";
		}
	}
}

//==============================================================================
function openBlank(obj, width, height){
	window.open(obj.href, "_blank", "height=" + height, "width=" + width, "status=no, toolbar=no, menubar=no, location=yes"); 
}
//==============================================================================
function prepareMenu(){
	$("ul#main_menu > li").filter('[class!=active]').hover(function(){

		$(this).find("img").each(function(i){
			if (this.src.indexOf("_active") == -1){
				var _src = this.src.substring(0, this.src.length-4);
				this.src = _src + '_active.gif';
			}
		});

	}, function(){

		$(this).find("img").each(function(i){
			if (this.src.indexOf("_active") != -1){
				var _src = this.src.substring(0, this.src.length-11);
				this.src = _src + '.gif';
			}
		});

		$(this).find("div.menuNumber").removeClass('rollOver');
	});
}
//==============================================================================
function prepareAccount(){
	$("table.open a").filter('[class!=active]').hover(function(){

		$(this).find("img").each(function(i){
			if (this.src.indexOf("_active") == -1){
				var _src = this.src.substring(0, this.src.length-4);
				this.src = _src + '_active.gif';
			}
		});

	}, function(){

		$(this).find("img").each(function(i){
			if (this.src.indexOf("_active") != -1){
				var _src = this.src.substring(0, this.src.length-11);
				this.src = _src + '.gif';
			}
		});

	});
}
function runQuotes(){
	if (!$('div.quotes').length) {
	return;
	}
	setInterval(function(){
			updateQuotes();
		},
		5000
	);
}

function updateQuotes(){
	$.get("/quotes", {}, doUpdateQuotes);
}

function doUpdateQuotes(data){
	$('div.quotes').find('ul').replaceWith(data);
}

function setCookie(cookieName,cookieValue,nDays) {

	var today = new Date();
	var expire = new Date();

	if (nDays==null || nDays==0) nDays=1;

	expire.setTime(today.getTime() + 3600000*24*nDays);

	document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString() + ";path=/";
}

function showMenu(){
	$('div.top_menu ul li a').mouseover(function(){
		var sub = $(this).parent().attr('id');
		$('div.sub_menu ul').attr('class','');
		$('#sub'+sub).attr('class','active');
	});
}

function runTabs(){
	$('div.tabs_menu a').click(function(){
		$('div.tabs_menu a').attr('class','');
		$(this).attr('class','select_tabs');
		$('#right_side div.tab').hide();
		$('#'+$(this).attr('id')+'_content').show();
	});
}


//==============================================================================
//bootstrap
if (jQuery){
$(document).ready(function(){
	//prepareMenu();
	//preloadMenu();
	prepareAccount();
	externalLinks();
	showMenu();
	runQuotes();
	runTabs();
});
}

//==============================================================================