function collapseDiv(id, prefix)
{
    $('#'+id).slideToggle('slow', function() {
        // Animation complete.
    });	

    $.post(prefix+"ajax.php?div_change="+id);
}

function searchCharacters(event)
{
    if(event.keycode=='13' || event.which=='13' || event == 'search')
        {
        val = $('#search').val();
        $.post("ajax.php", { request: "searchcharacters", val: val, search_male: $('#search_male').val() , search_female: $('#search_female').val(), chartype: $('#type').val()},
        function(data){
            $('#character_box').replaceWith(data);

        });	
    }
}

function deleteCharacter(id)
{
    jConfirm("Are you certain you wish to delete this character?",'Confirm Delete',function(r)
    {
        if(r)
            {
            $.post("ajax.php", { request: "deleteCharacter", id: id },
            function(data){
                eval(data);
            });	
        }	
    }
    );
}

function setFullHeight(id)
{
    h = $(document).height() + 'px';
    $("#"+id).height(h);
}

function imageFadeIn(id)
{
    $('#'+id).fadeIn('slow', function(){});
    $('#pageFade').fadeTo('slow', 0.9 , function(){});
}

function imageFadeOut(id)
{
    $('#'+id).fadeOut('slow', function(){});
    $('#pageFade').fadeOut('slow', function(){});
}

function popupFadeIn(id)
{
    $('#'+id).fadeIn('fast', function(){});
}

function popupFadeOut(id)
{
    $('#'+id).fadeOut('fast', function(){});
}    
function skillAlreadyMax(id)
{
    $('#skill_'+id).click(
    function()
    {
        jAlert('You already have this move.', 'Error');
    }	
    );
}         

function skillNotEnoughGold(id)
{
    $('#skill_'+id).click(
    function()
    {
        jAlert('Not enough gold to purchase.', 'Error');	
    }	
    );	
}

function evolutionNotEnoughGold(id)
{
    $('#evolution_'+id).click(
    function()
    {
        jAlert('Not enough gold to purchase.', 'Error');	
    }	
    );	
}            

function selectSizeChart(id)
{
    $('.sizechart').addClass('greyed');
    $('#'+id).removeClass('greyed');

    $('.sizepopup').hide();
    $('#popup_'+id).show();
}

function createJournal()
{
    id = $('#character').val();

    if(id == 0)
        {
        jAlert("You must select a character.", "Wait!");       
    }
    else
        {
        $.post("ajax.php", { request: "createJournal", id: id },
        function(data){
            eval(data);
        });         
    }

}

function deleteJournal(id)
{
    jConfirm("Are you certain you wish to delete this journal?",'Confirm Delete',function(r)
    {
        if(r)
            {
            $.post("ajax.php", { request: "deleteJournal", id: id },
            function(data){
                eval(data);
            });    
        }    
    }
    );
}

function selectGender(id)
{
    if(id == 1)
        {
        $('#sex_m').removeClass('greyed');
        $('#sex_f').addClass('greyed');
        $('#gender').attr('value', '1');
    }
    else
        {
        $('#sex_f').removeClass('greyed');
        $('#sex_m').addClass('greyed');
        $('#gender').attr('value', '2');		
    }	
}

function selectRace(id)
{
    $('.race').addClass('greyed');
    $('#race_'+id).removeClass('greyed');
    $('#race').attr('value', id);
}

function skillBuy(id, sname, cost, charId)
{

    $('#skill_'+id).click(
    function()
    {
        jConfirm("Are you sure you wish to buy <b>"+sname+"</b>?<br>This will cost you "+cost+" gold.",'Confirm Purchase',function(r)
        {
            if(r)
                {
                $.post("ajax.php", { request: "buyskill", skillid: id, character: charId },
                function(data){
                    eval(data);
                });
            }	
        }
        );	
    }	
    );		
}

function evolutionBuy(id, sname, cost, charId)
{

    $('#evolution_'+id).click(
    function()
    {
        jConfirm("Evolve into <b>"+sname+"</b>?<br>This will cost you "+cost+" gold.",'Confirm Purchase',function(r)
        {
            if(r)
                {
                $.post("ajax.php", { request: "buyevolution", evolution: id, character: charId },
                function(data){
                    eval(data);
                });
            }	
        }
        );	
    }	
    );		
}


function makeForumsCollapsable()
{
    $('#category2 .main-item .item-subject p').slideUp(0);

    $('#category2 .main-item').mouseenter(
    function()
    {

        $('#category2 #'+this.id+' .item-subject p').slideDown(0);
    }
    );

    $('#category2 .main-item').mouseleave(
    function()
    {
        $('#category2 #'+this.id+' .item-subject p').slideUp(0);
    }
    );
}

function toggleMaleSearch()
{
    current = $('#search_male').val();
    if(current == 0)
        {
        $('#male_icon').removeClass('greyed');
        $('#search_male').val(1);
    }
    else
        {
        $('#male_icon').addClass('greyed');
        $('#search_male').val(0);
    }
}

function toggleFemaleSearch()
{
    current = $('#search_female').val();
    if(current == 0)
        {
        $('#female_icon').removeClass('greyed');
        $('#search_female').val(1);
    }
    else
        {
        $('#female_icon').addClass('greyed');
        $('#search_female').val(0);
    }
}

function showDescription()
{
    val = $('#description_select').val();
    $('.desc').hide();
    $('#desc_'+val).show();
}

function showPokestats()
{
    val = $('#pokemon_stats').val();
    $('.pokestats').hide();
    $('#pokestats_'+val).show();
}


function map_click()
{
    $("#map_click").click(function(e){

        var x = e.pageX - this.offsetLeft;
        var y = e.pageY - this.offsetTop;

        $("#yourcity").show();
        $("#yourcity").css('margin-left', (x-6) + 'px');
        $("#yourcity").css('margin-top', (y-6) + 'px');
        
        $('#x').val(x);
        $('#y').val(y);  
        
    });
}

function hunt_click()
{
    $(".hunt_click").click(function(e){

        var x = e.pageX - this.offsetLeft;
        var y = e.pageY - this.offsetTop;

        $("#hunt").show();
        $("#hunt").css('margin-left', (x-80) + 'px');
        $("#hunt").css('margin-top', (y-80) + 'px');
        
        $('#x').val(x);
        $('#y').val(y); 
        
        city_x = $('#city_x').val();
        city_y = $('#city_y').val();    

        varx = parseInt(x) - parseInt(city_x);
        varx = varx * varx; 
 
        vary = parseInt(y) - parseInt(city_y);
        vary = vary * vary;        
        
        toroot = varx + vary;
        
        distance = Math.sqrt(toroot);
        cost = parseInt(distance / 10);
        
        $('#distance').html(cost);
        
    });
}


function validateCity()
{
    if($('#x').val() == '0' || $('#y').val() == '0')
    {
        jAlert('Please pick a location for your city.', 'Error');
        return false; 
    }        
    if($('#name').val() == '')
    {
        jAlert('Please name your city.', 'Error');
        return false; 
    }
    if($('#description').val() == '')
    {
        jAlert('Please enter a description for your city.', 'Error');
        return false; 
    }
    
    return true;
}

function updatePokemonMap()
{
    id = $('#pokemon').val();
    $('.pokelocations').hide();
    $('.pokelocation_'+id).show();        
}

function removeHuntingSpot(pokemon, x, y)
{
    $.post("ajax.php", { request: "removeHuntingSpot", pokemon: pokemon, x: x, y: y },
    function(data){
        eval(data);
        });    
}


function pokemon_map_click()
{
    $("#map_click").click(function(e){

        var x = e.pageX - this.offsetLeft;
        var y = e.pageY - this.offsetTop;

        var pokemon = $("#pokemon").val();
    
    if(pokemon == '0' || pokemon == 0)
    {
        alert("Please select a Pokémorph first");
        return false;
    }
      
    xx = (x-6) + 'px';
    yy = (y-6) + 'px'; 
      
    $('#map_overlay').append("<img class='pokelocations pokelocation_"+pokemon+"' onclick=' $(this).remove(); removeHuntingSpot("+pokemon+","+x+","+y+");' src='img/city/yourcity.png' style='position: absolute; margin-left: "+xx+"; margin-top: "+yy+"; '>");  
       
    $.post("ajax.php", { request: "addHuntingSpot", pokemon: pokemon, x: x, y: y },
    function(data){
        eval(data);
        });           
       
        
    });
}


function selectType(type)
{
    $('#type_1').addClass('greyed');
    $('#type_2').addClass('greyed');
    $('#type_3').addClass('greyed');    
    $('#type_'+type).removeClass('greyed');
    
    $('.costs').hide();
    $('#cost_'+type).show();
    
    
    $('#type_input').val(type);
}

function selectGender(id)
{
    $('#gender_1').addClass('greyed');
    $('#gender_2').addClass('greyed');
    $('#gender_'+id).removeClass('greyed');
    $('#gender_input').val(id);
}


function pickCity(city)
{
    if(city != 1 && city != 2)
    {
        jConfirm("This city is owned by another player, who must first confirm your application<br>This could take some time, depending on how active they are.<br>Do you still wish to pick this city?",'Confirm City',function(r)
        {
           if(r)
           {
                
                $('.steps').hide();
                $('#step2').show();
                $('#city_input').val(city);
            }    
        })
    }    
    else
    {
                $('.steps').hide();
                $('#step2').show();
                $('#city_input').val(city);      
    }
}
