$(document).ready(function(){
    $('.action_button').tipsy();
    $('.location').tipsy();
    $('.location').fancybox();
    $('.convention_name').fancybox();
    $('#country_dropdown').change(function(){
        var value = $('#country_dropdown').val();
        if(value == 'any')
        {
            $('#city_dropdown').html('<option value="any" selected="selected">any city</option>').attr('disabled', 'disabled');
        }
        else
        {
            $.post('framework/controllers/load_cities.php', {'country':value}, function(data){
                $('#city_dropdown').html(data).removeAttr('disabled');
            });
        }
    });

    $('.action_button').live('click', function(){
        var user_id = $(this).attr('rev');
        var action_button = $(this);
        if($(this).hasClass('like') || $(this).hasClass('unlike'))
        {
            $.post('framework/controllers/add_like.php', {'id':user_id}, function(data){
                if(data == 'success')
                {
                    var total = $(action_button).attr('rel');
                    total = parseInt(total);
                    total++;
                    $(action_button).attr('rel', total);
                    $(action_button).removeClass('like').addClass('unlike').html("<span class='glyph_container'></span><span class='glyph'></span>"+total);
                }
                else if(data == 'fail')
                {
                    alert('Sorry, an error occured. Please try again!');
                }
                else
                {
                    alert('You have to login to like something!');
                }
            });
        }
        else if($(this).hasClass('favourite') || $(this).hasClass('unfavourite'))
        {
            $.post('framework/controllers/add_favourite.php', {'id':user_id}, function(data){
                if(data == 'artist_success_add' || data == 'shop_success_add')
                {
                    data = data.split('_');
                    var total = $(action_button).attr('rel');
                    total = parseInt(total);
                    total++;
                    $(action_button).attr('rel', total);
                    $(action_button).removeClass('favourite').addClass('unfavourite').html("<span class='glyph_container'></span><span class='glyph'></span>"+total);

                    var image = $(action_button).parent().parent().parent().find('img:first').attr('src');
                    image = image.replace('user_images/', 'user_images/thumb/');

                    var the_link = $(action_button).parent().parent().parent().find('a:first').attr('href');

                    var item = $('<li><a href="'+the_link+'"><img src="'+image+'" /></a></li>');

                    if($('#footer_user_bar').hasClass('currently_hidden'))
                    {
                        sliding = true;
                        $('#footer_user_bar').animate({bottom:'+=100'}, 500, 'swing', function(){
                            sliding = false;
                            $('#footer_user_bar').removeClass('currently_hidden');
                            $('#hide_button').html('Hide');
                            $.post('framework/controllers/save_userbar.php', {'status':'show'});
                        });
                    }
                    if(data[0] == 'artist')
                    {
                        var carousel_id = '#carousel_2';
                    }
                    else
                    {
                        var carousel_id = '#carousel';
                    }
                    if($(carousel_id).css('display') == 'none')
                    {
                        $('.carousel_button').each(function(){
                            $(this).find('span').remove();
                        });

                        $('.carousel_button[href="'+carousel_id+'"]').append('<span></span>');

                        $('.es-carousel-wrapper').each(function(){
                            $(this).hide();
                        });

                        $(carousel_id).show();
                    }

                    setTimeout(function(){
                        $('#fav_'+data[0]+'s ul').append(item);
                        $(carousel_id).elastislide('add', item);
                    }, 250);
                }
                else if(data == 'artist_success_delete' || (data == 'shop_success_delete'))
                {
                    data = data.split('_');
                    var total = $(action_button).attr('rel');
                    total = parseInt(total);
                    total--;
                    $(action_button).attr('rel', total);
                    $(action_button).removeClass('unfavourite').addClass('favourite').html("<span class='glyph_container'></span><span class='glyph'></span>"+total);

                    var image = $(action_button).parent().parent().parent().find('img:first').attr('src');
                    image = image.replace('user_images/', 'user_images/thumb/');

                    var the_link = $(action_button).parent().parent().parent().find('a:first').attr('href');

                    var item = $('<li><a href="'+the_link+'"><img src="'+image+'" /></a></li>');

                    if($('#footer_user_bar').hasClass('currently_hidden'))
                    {
                        sliding = true;
                        $('#footer_user_bar').animate({bottom:'+=100'}, 500, 'swing', function(){
                            sliding = false;
                            $('#footer_user_bar').removeClass('currently_hidden');
                            $('#hide_button').html('Hide');
                            $.post('framework/controllers/save_userbar.php', {'status':'show'});
                        });
                    }
                    if(data[0] == 'artist')
                    {
                        var carousel_id = '#carousel_2';
                    }
                    else
                    {
                        var carousel_id = '#carousel';
                    }
                    if($(carousel_id).css('display') == 'none')
                    {
                        $('.carousel_button').each(function(){
                            $(this).find('span').remove();
                        });

                        $('.carousel_button[href="'+carousel_id+'"]').append('<span></span>');

                        $('.es-carousel-wrapper').each(function(){
                            $(this).hide();
                        });

                        $(carousel_id).show();
                    }

                    setTimeout(function(){
                        $('#fav_'+data[0]+'s ul li a[href="'+the_link+'"]').parent().remove();
                        $(carousel_id).elastislide('remove_item');
                    }, 250);
                }
                else if(data == 'fail')
                {
                    alert('Sorry, an error occured. Please try again!');
                }
                else
                {
                    alert('You have to login to add something to your favourites!');
                }
            });
        }

        return false;
    });
});
