
window.BarResult = {
	'user' : {
		'username':'',
		'filename':'',
		'region':''
	}, 
	'url' : {
		'staticFile':'user.cherrycredits.com',
		'user':'user.cherrycredits.com',
		'base':'cherrycredits.com',
		'exchange':'https://exchange.cherrycredits.com/myaccount/summary.aspx',
		'exchangeSignup':'http://user.cherrycredits.com/signup',
		'v2':'http://www.cherrycredits.com'
	}
};


window.CherryBar = {};


window.CherryBar = function() {
	window.CherryBar.serverResponse = function (rs) {
		window.BarResult.user = rs;
	
		var target = jQuery('#nav');
		if (BarResult.user) {
			if(BarResult.user.region != '') {
				var country = document.getElementById('country-click2');
				var logo = document.getElementById('logo');
				var cherrydemall = document.getElementById('dropnav_cherrydemall');
				country.className = BarResult.user.region;
				logo.setAttribute('href', BarResult.url.v2+'/' + BarResult.user.region);
				cherrydemall.setAttribute('href', BarResult.url.v2+'/' + BarResult.user.region + '/cherrydemall');
			}
			if(BarResult.user.username != '') {
				var img = '<img src="//'+BarResult.url.staticFile+'/img/user.png" alt="userpic" />';
				if(BarResult.user.filename != '') {
					var img = '<img src="//'+BarResult.url.user+'/uploads/users/thumb/' + BarResult.user.filename + '" alt="userpic" />';
				}
				target.html('<li><a href="//'+BarResult.url.user+'/profile/">' + img + 'Hi ' + BarResult.user.username + '!</a><ul><li><a href="//'+BarResult.url.user+'/settings/">Settings</a></li><li><a href="https://exchange.cherrycredits.com/myaccount/summary.aspx">Check Balance</a></li><li><a href="https://exchange.cherrycredits.com/mall/mall.aspx">Redemption</a></li><li><a href="https://exchange.cherrycredits.com/Messenger/Messagelist.aspx">Support</a></li><li><a class="" href="//'+BarResult.url.user+'/logout/?referer=' + window.location + '">Sign Out</a></li></ul></li><li class="bar-topup"><a href="https://exchange.cherrycredits.com/topup/methods.aspx">Top Up</a></li>');
			}
		}
		target.css('display','block');
	}

	function getBarHTML() {
		
		var region = [
			'<a class=en href="http://user.cherrycredits.com/region/en"><em></em>Global</a>',
			'<a class=au href="http://user.cherrycredits.com/region/au"><em></em>Australia</a>',
			'<a class=my href="http://user.cherrycredits.com/region/my"><em></em>Malaysia</a>',
			'<a class=ph href="http://user.cherrycredits.com/region/ph"><em></em>Philippines</a>',
			'<a class=sg href="http://user.cherrycredits.com/region/sg"><em></em>Singapore</a>',
			'<a class=th href="http://user.cherrycredits.com/region/th"><em></em>Thailand</a>'
		];

		return '<div id="bar"><div id="bar-inner">'+
			'<a id="logo" href="http://www.cherrycredits.com/"><img src="//'+BarResult.url.staticFile+'/img/bar/logo.png" alt="Cherry Credits" /></a>'+
			'<div id="dropnav"><a id="mainlink" class="cherryexchange" href="javascript:void()">Bring me to:</a><div id="dropnav-links"><ul><li><a href="javascript:void()">Game Channel</a><ul><li><a href="http://sd.cherrycredits.com/">SD Gundam</a></li><li><a href="http://dn.cherrycredits.com/">Dragon Nest</a></li><li><a href="http://storm.cherrycredits.com/" >Storm Online</a></li><li><a href="http://bto.cherrycredits.com/" >Business Tycoon Online</a></li><li><a href="http://ta.cherrycredits.com/" >Tactics Anthem</a></li><li><a href="http://mochi.cherrycredits.com/" >Mochi</a></li></ul></li><li><a href="'+BarResult.url.exchange+'">Cherry Exchange</a></li><li><a href="http://www.cherrycredits.com/sg/cherrydemall" id="dropnav_cherrydemall" >Cherry De Mall</a></li><li><a href="http://forum.'+BarResult.url.base+'/">Forum</a></li></ul></div></div>'+
			'<div id="search"><form onsubmit="return false" id="SearchIndexForm" method="post" action="/searches" accept-charset="utf-8"><div style="display:none;"><input type="hidden" name="_method" value="POST" /></div><input name="data[Search][query]" type="text" onkeyup="lookup(this.value);" onfocus="if(this.value==this.defaultValue)this.value=&#039;&#039;" onblur="if(this.value==&#039;&#039;)this.value=this.defaultValue" value="Search" class="text" id="SearchQuery" autocomplete="off" /><div class="submit"><input type="image" src="//'+BarResult.url.staticFile+'/img/search-btn.png" class="search_btn" alt="Search" onclick="this.submit()" value="Search" /></div></form><div id="suggestions-wrap"><ul id="suggestions"></ul></div></div>'+
			'<div id=country2><a id=country-click2><em></em><strong>Region</strong></a><div id=country-links><span>Select Region</span><ul><li>'+region.join('</li><li>')+'</li></ul><a class=close>Close</a><span class=point></span></div></div>'+
			'<ul id="nav"><li><a href="//'+BarResult.url.user+'/login/?referer=' + window.location + '">Login</a></li><li><a href="'+BarResult.url.exchangeSignup+'">Signup</a></li></ul>'+
			'</div></div>';
	}
	jQuery('body').prepend(getBarHTML());
	jQuery('head').append( '<script src="//user.cherrycredits.com/api/bar.php" type="text/javascript" charset="utf-8"></script>' );
	jQuery('head').append( '<link type="text/css" rel="stylesheet" href="//'+BarResult.url.staticFile+'/css/bar.css'+'" />' );

}


function lookup(inputString) {

	if(inputString.length == 0) {
		jQuery('#suggestions-wrap').fadeOut(); // Hide the suggestions box
	} else {
		jQuery.getScript('//'+BarResult.url.user+'/ajax/search/' + inputString);
	}
}

function noenter() {
	return !(window.event && window.event.keyCode == 13);
}

jQuery(function(jQuery) {
	CherryBar();
	jQuery("#dropnav").hover(function () {
		jQuery(this).addClass("selected");
	},function () {
		jQuery(this).removeClass("selected");
	});

	// COUNTRY DROPDOWN
	jQuery('#country-click2, #country-links .close').click(function() {
		jQuery('#country-links').toggle();
		return false;
	});
	
	// Fade out the suggestions box when not active
	jQuery(".text").blur(function(){
		jQuery('#suggestions-wrap').fadeOut();
	});

});


