var player_money = parseInt(document.getElementById('game_coins').innerHTML);
var items_cnt = new Array();
var items_ids = new Array();
var total_cost=0;
var $total_cost;
var buying_limit=999;
var buy_standard_eq_started = false;
var buy_horses_amount = 0;
var buy_items_amount = 0;
var buy_eqs_amount = 0;
var buy_tools_amount = 0;
var seller_id = -1;
var buy_eq_started = false;
var update_eq_started = false;
var sell_started = false;


function has_enough_money(m){
    return player_money >= m;
}


/*
 * sell items
 */






function show_item_info(item,item_type){
    var item_price = eval("item_type+'_price'");
    $(item_price).innerHTML = get_price(item.value,item_type);
    return;
    new Ajax.Updater('item_info',
    '/item/get_info/'+item.value +'?partial=true',
    {
        asynchronous:true,
        evalScripts:true
    });
}

function get_price(id,item_type){
    var ids = eval(item_type +'_ids');
    var prices = eval(item_type + '_prices');
    for(var i = 0;i<ids.length;i++){
        if(id == ids[i])
            return prices[i];
    }
    return 0;
}

function move_select_items(source,dest,how_many,add_money,item_type){
    var src_select = $(source);
    var dest_select = $(dest);
    var si = src_select.selectedIndex;
    var sell_money=	eval("'sell_' + item_type + '_money'")
    var money = parseInt($(sell_money).innerHTML);
    if(how_many == 1){//One item
        if(si == -1)
            return;
        money+=get_price(src_select[si].value,item_type)*add_money;
        dest_select.appendChild(src_select[si]);
    }
    else {//All items
        while(src_select.length>0){
            money+=get_price(src_select[0].value,item_type)*add_money;
            dest_select.appendChild(src_select[0]);
        }
    }

    $(sell_money).innerHTML = money;
}

/*
 * buy standard eq
 *
 */


function set_cart(position, value, cost){
    if(value==items_cnt[position] )
        return;
    if(isNaN(value))
        value=0;
    if(value>buying_limit){
        value=buying_limit;
        $('item_'+position).value=value;
    }
    if(!items_cnt[position])
        items_cnt[position]=0;
    total_cost+=(value-items_cnt[position])*cost;
    items_cnt[position]=value;
    items_ids[position] = $('item_'+position);

    set_cost(total_cost);
}

function change_cart(id,cost, sign,idn){
    if(items_cnt[idn]>=buying_limit && sign==1){
        alert("Come on, you cannot carry that much!");
        return false;
    }

    if(items_ids[idn] == null)
        items_ids[idn] = $(id);


    $id = items_ids[idn];

    if(sign == -1 && (total_cost==0 || items_cnt[idn]==null || items_cnt[idn]==0)){
        return;
    }

    total_cost+=cost*sign;
    set_cost(total_cost);
    
    items_cnt[idn]=items_cnt[idn]==null?sign:items_cnt[idn]+sign;
    $id.value = items_cnt[idn];
}

function set_cost(total_cost){
    if(!$total_cost)
        $total_cost = $('total_cost');
    if(!$total_cost)
        return;
    var color = total_cost<=player_money?"black":"red";
    $total_cost.innerHTML = "<font color=" + color + ">" + total_cost + "</font>";
    $('total_cost').innerHTML = $total_cost.innerHTML;

}

function empty_cart(init)
{
    for(var i=0;i<items_ids.length;i++){
        items_cnt[i] = 0;
        if(items_ids[i]!=null)
        //items_ids[i].innerHTML = 0;
            items_ids[i].value = 0;
    }
    total_cost = 0;
    set_cost(0)
    //$total_cost.innerHTML = 0;
}
function clear_cart(){
    items_cnt = new Array();
    items_ids = new Array();
    total_cost=0;
    $total_cost = $('total_cost');
}
function buy_eq(){
    if(buy_standard_eq_started){
        alert("Transaction already in progress, please wait...");
        return;
    }

    if(total_cost == 0){
        alert("You have not selected any items!");
        return false;
    }
    if(total_cost > player_money){
        alert("You do not have enough coins! (You need "+ (total_cost-player_money) + " more coins)");
        return false;
    }
    if(!confirm('Are you sure you want to buy the selected items?'))
        return false;

    ret="";
    for(var i=0;i<items_cnt.length;i++){
        if(items_cnt[i]!=null && items_cnt[0]!=0)
            ret+="eq["+i+"]="+items_cnt[i]+"&";
    }

    set_result_message("Buying the selected items, please wait...");
    buy_standard_eq_started = true;
    var t = new Date().getTime();
    new Ajax.Updater('control_buy', '/market/buy_items?'+ ret+"when="+t+"&" , {
        asynchronous:true,
        evalScripts:true,
        onComplete:function(request){
            clear_cart();buy_standard_eq_started = false;
        },
        parameters:ret,
        method:'GET'
    });
    empty_cart();
    return false;
}







function sync_buy_other_amount(check,obj,by, buy_amount){
    buy_amount+= check.checked?by:-by;
    $(obj).style.color = buy_amount<=player_money?"black":"red";
    $(obj).innerHTML = buy_amount;
    return buy_amount;
}

function sync_buy_horses_amount(check,obj,by){
    buy_horses_amount = sync_buy_other_amount(check,obj,by,buy_horses_amount);
    return buy_horses_amount;
}


function sync_buy_tools_amount(check,obj,by){
    buy_tools_amount = sync_buy_other_amount(check,obj,by,buy_tools_amount);
    return buy_tools_amount;
}

function sync_buy_items_amount(check,obj,by){
    buy_items_amount = sync_buy_other_amount(check,obj,by,buy_items_amount);
    return buy_items_amount;
}

function sync_buy_eqs_amount(check,obj,by){
    buy_eqs_amount = sync_buy_other_amount(check,obj,by,buy_eqs_amount);
    return buy_eqs_amount;
}




function buy_other_items(form, buy_amount, itemName, controller, action, updatePanel){
    if(buy_eq_started){
        alert("Transaction already in progress, please wait...");
        return;
    }
    if(buy_amount == 0){
        alert("You have not selected any " + itemName +"!");
        return false;
    }
    if(buy_amount>player_money){
        alert("You do not have enough coins! (You need "+ (buy_amount-player_money) + " more coins)");
        return false;
    }
    if(!confirm('Are you sure you want to buy the selected ' + itemName + '?'))
        return ;
    set_result_message("Buying the selected " + itemName + ", please wait...");
    buy_eq_started = true;
    if(updatePanel)
        new Ajax.Updater(updatePanel, '/' + controller + '/' + action + '/' + seller_id + '?partial=true', {
            asynchronous:true,
            evalScripts:true,
            onComplete:function(request){
                buy_eq_started=false
            },
        parameters:Form.serialize(form)
    })
    else new Ajax.Request('/' + controller + '/' + action + '/' + seller_id + '?partial=true', {
        asynchronous:true,
        evalScripts:true,
        onComplete:function(request){
            buy_eq_started=false
        },
        parameters:Form.serialize(form)
    });
    return true;
}

function buy_other_horses(form, controller, update){    
    if(buy_other_items(form,buy_horses_amount,'horses',controller, 'buy_horses',update))
        buy_horses_amount = 0;
    return false;
}

function buy_other_eqs(form, controller){
    if(buy_other_items(form,buy_eqs_amount,'items',controller, 'buy_eqs','used_eq_content'))
        buy_eqs_amount = 0;
    return false;
}

function buy_items(form, controller){
    if(buy_other_items(form,buy_items_amount,'items',controller, 'buy_items','item_content'))
        buy_eqs_amount = 0;
    return false;
}

function buy_tools(form, controller){
    buy_other_items(form, buy_tools_amount,'tools',controller, 'buy_tools');    
    return false;
}





function update_items_for_sale_list(form,updatePanel,action){
    if(update_eq_started){
        alert("Already updating the list, please wait...");
        return;
    }
    update_eq_started = true;
    new Ajax.Updater(updatePanel, '/shop/' + action + '?partial=true', {
        asynchronous:true,
        evalScripts:true,
        onComplete:function(request){
            update_eq_started = false;
        },
        parameters:Form.serialize(form)
    });
}


