$.blockUI.defaults.message = '<div id="pop_small"><div id="pop_content_small"><p class="moment">'+TEXT1+'</p></div></div>';
$.extend($.blockUI.defaults.css, {border:0,width:'256px',height:'106px'});
$.extend($.blockUI.defaults.overlayCSS,{background:'#000',cursor:'default'});
var ie6=$.browser.msie&&($.browser.version == "6.0");
var close=function(hash) { hash.w.fadeOut('2000',function(){ hash.o.remove(); }); };

var localGameID = 0;
var localGameUsername = '';
var localItemCategory = 'Premiums'; // default
var localItemCategoryID = 0; // default

$(function(){
	$('.playVideoLink').click(function(){
		$('#modal').empty().html('<div id="popup" class="videoWindow"></div>');
		$('#popup').jqm({ajax:'video.php?'+$(this).attr('rel'),overlay:50,toTop:true}).jqmShow();
	});
	
	// choose between paying with G1C or $USD
	$('.choose_payment_type').change(function() {
		var itemID = $(this).attr('name');
		var payment_type = $(this).val();
		
		$('.i_want_this_'+itemID).hide();
		if (payment_type == 'g1c')
			$('#'+itemID+'g1c').show();
		else if (payment_type == 'usd')
			$('#'+itemID+'usd').show();
	});

	$('input.itemBuy').click(function() {  
		var values = $(this).attr("name");
		var split_vals = values.split("|", 4);
		var itemID = split_vals[0];
		var rmtBuyType = split_vals[3];
		$(this).hide();
		
		if (rmtBuyType == 'premium') {
			$('.fmPurchase' + itemID).submit();
		}
		else{
			buyItem(values, 'itemBlock', itemID, 'marketplace');
		}
	});
	$('input.itemBuyGame').unbind('click').bind('click', function() {
		var values = $(this).attr("name");
		var split_vals = values.split("|", 4);
		var itemID = split_vals[0];
		var rmtBuyType = split_vals[3];

        if(rmtBuyType == 'premium' && $('#no-game-account').length) {
            var text = $('#no-game-account').text();
            $(this).parents('.itemButton').empty().append('<span class="changeError">' + text + '</span>');
            return;
        }

		$(this).hide();
		$('#loading-'+itemID).show();
		if (rmtBuyType == 'premium') {
			$('.fmPurchase' + itemID).submit();
		}
		else{
			buyItem(values, 'itemBlock', itemID, 'drupal_site');
		}
	});
});

function buyItem(val, field, itemID, dir) {
	var post_script = "";
	switch(field){
		case 'itemBlock':
			post_script = "../"+dir+"/buy_item.php";break;
		case 'applyItemBlock':
			post_script = "../"+dir+"/applyItem.php";break;
	}
	
    $.post(post_script, {
        postVal: val
    }, function(response){
        var returnVal = unescape(response);
		
		if (field == 'itemBlock' || field == 'applyItemBlock') {
			$('.itemBuy:hidden').show();
			$('.default_i_want_this:hidden').show();
			$('.buyItemClass:visible').hide();
			$('div[id*="itemDefault"]:hidden').show();
			$('div[id*="itemButton"]:hidden').show();
			$('#itemDefault'+itemID).hide();
			$('#itemButton'+itemID).hide();
			$('#buyItem'+itemID).html(returnVal).fadeIn("fast", function(){
				
				if (field == 'itemBlock') {
					$('.applyItem').bind('click', function(){
						$('#applyItem-loading-'+itemID).show();
						var world = $('#applyItemWorld'+itemID).val();
						var character = $('#applyItemCharacter'+itemID).val();
						var updated_post_val = val + '|' + world + '|' + character;
						if (character != 'nocharacter') {
							buyItem(updated_post_val, 'applyItemBlock', itemID, dir);
							$(this).hide();
						}
					});
				}
				$('.closeMoreDetails').click(function() {
					var itemID = $(this).attr("rel");
					$('#buyItem'+itemID).hide();
					$('#loading-'+itemID).hide();
					$('#itemDefault'+itemID).fadeIn();
					$('#itemButton'+itemID).fadeIn();
					$('.default'+itemID).show();
				});
				
				// get g1c link and dropdown for G1C items where
				// user doesn't have enough G1C
				$('#g1c_menu_link_item'+itemID).click(function(){
					$(this).blur().next().next().next().clone(true).prependTo('body').end().remove();
					
					$('#g1cMenu_item'+itemID).slideFadeToggle('fast',function(){
						$('body').unbind('click').bind('click',function(){
							$('#g1cMenu_item'+itemID).hide();
						});
					})
					.bind('contextmenu',function(){
						$('#g1cMenu_item'+itemID).hide();
						return false;
					});
				});
				$('.select_g1c_package_item').click(function(){
					var rel = $(this).attr("rel"); 
						$.post("/buy_g1c_menu_package.php", {
						strPost: rel
					}, function(response){
						var returnVal = jQuery.trim(response);
						$('#g1c_form_item'+itemID).html(returnVal);
						document.buy_g1c.submit();
					});
				});
				
				// user must select server first to determine if
				// inventory is maxed out
				// Games affect: 9Dragons
				$('#applyItemCharacter'+itemID).change(function() {
					var server = $(this).val();
					if (server != 'noserver') {
						$.post('check_inventory_limit.php', {
							postVal : server, postVal2 : itemID
						}, function(response) {
							var returnVal = jQuery.trim(response);
							if (returnVal == 'GOOD')
								$('#applyItemButton'+itemID).show();
							else if (returnVal == 'MAXEDOUT') {
								$('#applyItemButton'+itemID).hide();
								$('#applyItemButton'+itemID).parent().append('<span class="changeError">Maximum inventory limit reached.</span>');
							}
						});
					}
				});				
			});
		}
		else {
			$('#buyItem').html(returnVal).fadeIn("fast", function(){
			});
		}
		
		// Nickname Change
		bindNickChange();
    });
}

function bindNickChange() {
	$('#nickChange').bind('click',function(){
		var val = $(this).attr("rel");
		var str = val.split("|",5);
		var username = str[1];
		var prefix = str[2];
		var nickname = str[3];
		var page = str[4];
		var loadNickChange = function(hash){
			$('#gI').attr('value',str[0]);
			$('#gU').attr('value',username);
			$('#currentNick').attr('value',nickname);
			$('#pageRel').attr('value',page);
		};
		$('#modal').empty().html('<div id="popup" class="jqmWindow"></div>');
		$('#popup').jqm({ajax:'../change_nickname.php',onLoad:loadNickChange,overlay:50,toTop:true}).jqmShow();
	});
}

function updateG1C(val) {
	if (val == '0')
	    $('.credit_count').html('0');
	else
		$('.credit_count').html(val);
}

function switchOutBuyButtonWR(itemID,msg) {
	if (itemID != '0') {
		$('.fmPurchase'+itemID).hide();
		$('#wrLeaseMsg'+itemID).html('<b class="highlight">'+msg+'</b>').show();
	}
	else {
		$('.fmPurchaseClass:visible').hide();
		$('.wrLeaseMsgClass').html('<b class="highlight">'+msg+'</b>').show();
	}
	
}

function switchInBuyButtonWR(itemID) {
	$('#wrLeaseMsg'+itemID).hide();
	$('.fmPurchase'+itemID).show();
	
}

function disableBuyButtonWR() {
	$('.itemBuy').attr("disabled",true);
}

function popupRandomBox(productID,deliveredComponentID) {
	$('#random_box').empty().jqm({ajax:'/marketplace/random_box.php?pid='+productID+'&componentID='+deliveredComponentID,overlay:50,toTop:true,modal:true}).jqmShow();
}
