$.fn.extend({
	addFavorite: function(){
		$(this).bind('click',function(){
			try{
				window.external.addFavorite(window.location.href,document.title)
			}catch(e){
				window.sidebar.addPanel(document.title,window.location.href,'')
			}finally{
			}
		})
	},
	flush: function(opacity){
		opacity = opacity || 0.8;
		$(this).css({opacity:opacity});
		$(this).hover(function(){$(this).css({opacity:1});},function(){$(this).css({opacity:opacity});})
	}
})


$(function(){
	var gamekey = $('#gamename').html(); 
	$('#server ul li').each(function(){
		if( $(this).children('a').html() == gamekey )
			$(this).children('a').css({'color':'black','fontWeight':'bold'});
	})
})

$(function(){
	$('#fastorder h4.main_block_head').html('Custom Fast Order');
	$('#orderarea').show(300);
	var currencyID=parseInt(getCookie("currencyID"));
	$('input[name=moneytype]').removeAttr('checked');
	if($('input[name=moneytype]')[currencyID-1]){
		$('input[name=moneytype]')[currencyID-1].checked = true;
	}
	$('#serverlist option[value='+window.location.href+']').attr('selected','selected');
	$('#favimg').addFavorite();
	//$('#hotgame img').flush(0.6);
	initlg();
	initregf();
	initodr();
	initcur();
	prep();
	$(':text,:password').focus(function(){$(this).css('backgroundColor','#FFEEFF');}).blur(function(){$(this).css('backgroundColor','white');})
})


$(function(){
	var game_select    =  $('#fr_game');
	var server_select  =  $('#fr_server');
	var product_select =  $('#fr_product');
	var money_choose   =  $('input[name=moneytype]');
	var fastorderbt    =  $('#fastorderbt');
	$.getJSON('inc/fastorder.asp?action=getsort&sortid=0', function(data) {
		for(var key in data){
			game_select.append('<option value="'+data[key]+'">'+key+'</option>');
		}
	})
	game_select.change(function(){
		server_select.children('option:gt(0)').remove();
		$.getJSON('inc/fastorder.asp?action=getsort&sortid='+$(this).val(), function(data) {
			for(var key in data){
				server_select.append('<option value="'+data[key]+'">'+key+'</option>');
			}
		})	
	})
	server_select.change(function(){
		product_select.children('option:gt(0)').remove();
		$.getJSON('inc/fastorder.asp?action=getproduct&sortid='+$(this).val()+'&moneytype='+$('input[name=moneytype]:checked').val(), function(data) {
			for(var key in data){
				product_select.append('<option value="'+data[key]+'">'+key+'</option>');
			}
		})	
	})
	money_choose.click(function(){
		var value = product_select.val();
		setCookie("currencyID",1+Number($(this).val()));
		product_select.children('option:gt(0)').remove();
		$.getJSON('inc/fastorder.asp?action=getproduct&sortid='+server_select.val()+'&moneytype='+$(this).val(), function(data) {
			for(var key in data){
				product_select.append('<option value="'+data[key]+'">'+key+'</option>');
			}
			//alert( product_select.children('option').length + '|' +value )
			product_select.children('option[value='+value+']').attr('selected','selected');
		})	
	})
	fastorderbt.click(function(){
		location.href = '/shoppingcart.asp?pid='+product_select.val();
	})
})

$(function(){
	if($('#arealist').length>0){
		$.getJSON('inc/gamearea.asp?action=getarea&gameid='+$('#gameid').html(), function(data) {
			var arealist = '<ul id="arealist">'
			for(var key in data){
				arealist += '<li><a href="#'+data[key]+'">'+key+'</a></li>';
				showserverlist(data[key],key);
			}
			arealist += '</ul>';
			$('#arealist').replaceWith($(arealist));
		})
		//$('#arealist li').hover(function(){$(this).css('color','red')},function(){$(this).css('color','#cccccc')})
	}
})

function showserverlist(areaid,areaname){
	$.getJSON('inc/gamearea.asp?action=getserver&areaid='+areaid, function(data) {
		var areaserverlist = '<ul class="areaserverlist"><hr /><h4><a name="'+areaid+'">'+areaname+'</a></h4>'
		for(var key in data){
			areaserverlist += '<li><a href="/'+data[key]+'.html">'+key+'</a></li>';
		}
		areaserverlist += '</ul>';
		$('#arealist').after($(areaserverlist));
		//alert(areaserverlist);
	})
}
