$(document).ready(function() {
    $(".ajax-fc-container").captcha({
        borderColor: "white",
        text: "Trage <span>foarfecele</span> in cercul din dreapta."
    });
    $("ul.subnav").parent().append("<span></span>");
    $("ul.topnav li.sub").hover(function() {
        $(this).parent().find("ul.subnav").show();
        $(this).parent().hover(function() {
            }, function(){
                $(this).parent().find("ul.subnav").hide();
            });
    });
    $(".progressbar").progressbar({
        value:0
    });
    $(".lightbox").fancybox();
    $(".lightbox[rel=lightbox-gallery]").fancybox();
    $(".comment-item.closeable").prepend('<span class="comment-close"></span>').find(".comment-close").click(function(){
        $(this).parent().fadeOut(function(){
            $(this).remove()
        })
    });
    $("#open-comments").click(function(e) {
        $('#comments-wrapper').toggle('blind');
    });
    $("#open-add-friend").click(function(e) {
        $('#friends-wrapper').toggle('blind');
    });
    $(".tabs").tabs();
    $(".drawing-list li").hover(function() {
        $(this).find(".gallery-buttons").toggle();
    });
    $(".gallery-list li").hover(function() {
        $(this).find(".gallery-buttons").toggle();
    });
    $(".comment-item").hover(function() {
        $(this).find(".gallery-buttons").toggle();
    });
    $(".box").hover(function() {
        $(this).find(".ToolbarBoxRight").toggle();
    });
    $('#top').click(function(){
        $('html, body').animate({
            scrollTop:0
        },'slow');
    });
    $('.tips').tipsy({
        gravity:'s',
        live: true
    });
    $('.tipn').tipsy({
        gravity:'n',
        live: true
    });
    $('.tipe').tipsy({
        gravity:'e',
        live: true
    });
    $('.tipw').tipsy({
        gravity:'w',
        live: true
    });
    $("#SidePanel #Toggle").click(function(e) {
        $('#SidePanelContents').toggle('blind');
    });
    $('#ShopObjects .jcarousel-item').live('click', function() {
        var id = $(this).attr('id');
        var elem = $(this);
        $.ajax({
            type: "POST",
            dataType: 'xml',
            data: "id="+id,
            url: "/xmlrpc/buy_object",
            success: function(xml){
                if ($("status", xml).text() == "1")
                {
                    $('#freeow-tr').freeow('Eroare', $("message",xml).text(), {
                        classes: ["smokey", "alert-error"]
                    });
                }
                else {
                    $('#freeow-tr').freeow('Succes', $("message",xml).text(), {
                        classes: ["smokey", "alert-info"]
                    });
                    elem.show('pulsate');
                    $('#money').html($("money",xml).text());
                }
            }
        });
    });
    $('#InventoryObjects .jcarousel-item').live('click', function() {
        var id = $(this).attr('id');
        var elem = $(this);
        $.ajax({
            type: "POST",
            dataType: 'xml',
            data: "id="+id,
            url: "/xmlrpc/use_object",
            success: function(xml){
                if ($("status", xml).text() == "1")
                {
                    $('#freeow-tr').freeow('Eroare', $("message",xml).text(), {
                        classes: ["smokey", "alert-error"]
                    });
                }
                else {
                    $('#freeow-tr').freeow('Succes', $("message",xml).text(), {
                        classes: ["smokey", "alert-info"]
                    });
                    elem.hide();
                    $("#ProgressHunger span").attr('style', 'text-align: left; width: '+$("hunger",xml).text()+'0%');
                    $("#ProgressHealth span").attr('style', 'text-align: left; width: '+$("health",xml).text()+'0%');
                    $("#ProgressSocial span").attr('style', 'text-align: left; width: '+$("social",xml).text()+'0%');
                    $("#ProgressEducation span").attr('style', 'text-align: left; width: '+$("education",xml).text()+'0%');
                    $("#ProgressXp span").attr('style', 'width: '+$("xp",xml).text()+'%');
                }
            }
        });
    });
    $('#Health').click(function(e) {
        $('#Inventory').empty();
        $('#Inventory').append('<ul id="InventoryObjects" class="jcarousel-skin-tango"></ul>');
        $.ajax({
            type: "POST",
            dataType: 'xml',
            url: "/xmlrpc/get_health_objects",
            success: function(xml){
                $('obj', xml).each(function() {
                    var line = '<li id="'+$(this).attr("invid")+'" title="<b>'+$(this).attr("name")+'</b>" class="tipn"><img width="48" height="48" src="/var/virtu/objects/'+$(this).attr("image")+'" alt="" /></li>';
                    $("#InventoryObjects").append(line);
                });
                $('.tipn').tipsy({
                    gravity:'s',
                    html: true,
                    live: true
                });
                $('#InventoryObjects').jcarousel();
                $('#Inventory').show();
            }
        });
    });
    $('#Hunger').click(function(e) {
        $('#Inventory').empty();
        $('#Inventory').append('<ul id="InventoryObjects" class="jcarousel-skin-tango"></ul>');
        $.ajax({
            type: "POST",
            dataType: 'xml',
            url: "/xmlrpc/get_hunger_objects",
            success: function(xml){
                $('obj', xml).each(function() {
                    var line = '<li id="'+$(this).attr("invid")+'" title="<b>'+$(this).attr("name")+'</b>" class="tipn"><img width="48" height="48" src="/var/virtu/objects/'+$(this).attr("image")+'" alt="" /></li>';
                    $("#InventoryObjects").append(line);
                });
                $('.tipn').tipsy({
                    gravity:'s',
                    html: true,
                    live: true
                });
                $('#InventoryObjects').jcarousel();
                $('#Inventory').show();
            }
        });
    });
    $('#School').click(function(e) {
        $('#GameArea').removeClass();
        $('#GameArea').addClass('Loader');
        $.ajax({
            type: "POST",
            dataType: 'xml',
            url: "/xmlrpc/move_to_zone",
            data: "id=1&name=School",
            success: function(xml){
                $('#GameArea').addClass('School');
                $('#GameArea').removeClass('Loader');
            }
        });
    });
    $('#Coffee').click(function(e) {
        $('#GameArea').removeClass();
        $('#GameArea').addClass('Loader');
        $.ajax({
            type: "POST",
            dataType: 'xml',
            url: "/xmlrpc/move_to_zone",
            data: "id=2&name=CoffeeShop",
            success: function(xml){
                $('#GameArea').addClass('CoffeeShop');
                $('#GameArea').removeClass('Loader');
            }
        });
    });
    $('#Shop').click(function(e) {
        $('#GameArea').removeClass();
        $('#GameArea').addClass('Loader');
        $('#Inventory').empty().append('<ul id="ShopObjects" class="jcarousel-skin-tango"></ul>');
        $.ajax({
            type: "POST",
            dataType: 'xml',
            url: "/xmlrpc/get_all_objects",
            success: function(xml){
                $('obj', xml).each(function() {
                    var line = '<li id="'+$(this).attr("id")+'" title="<b>'+$(this).attr("name")+'</b><br />categorie: '+$(this).attr("category")+'<br />cost: '+$(this).attr("cost")+'lei" class="tipn"><img width="48" height="48" src="/var/virtu/objects/'+$(this).attr("image")+'" alt="" /></li>';
                    $("#ShopObjects").append(line);
                });
                $('.tipn').tipsy({
                    gravity:'s',
                    html: true,
                    live: true
                });
                $('#ShopObjects').jcarousel();
                $('#Inventory').show();
                $('#GameArea').addClass('Mall');
                $('#GameArea').removeClass('Loader');
            }
        });
    });
    $('#Education').click(function(e) {
        $('#Inventory').empty();
        $('#Inventory').append('<ul id="InventoryObjects" class="jcarousel-skin-tango"></ul>');
        $.ajax({
            type: "POST",
            dataType: 'xml',
            url: "/xmlrpc/get_education_objects",
            success: function(xml){
                $('obj', xml).each(function() {
                    var line = '<li id="'+$(this).attr("invid")+'" title="<b>'+$(this).attr("name")+'</b>" class="tipn"><img width="48" height="48" src="/var/virtu/objects/'+$(this).attr("image")+'" alt="" /></li>';
                    $("#InventoryObjects").append(line);
                });
                $('.tipn').tipsy({
                    gravity:'s',
                    html: true,
                    live: true
                });
                $('#InventoryObjects').jcarousel();
                $('#Inventory').show();
            }
        });
    });
    $('#Social').click(function(e) {
        $('#Inventory').empty();
        $('#Inventory').append('<ul id="InventoryObjects" class="jcarousel-skin-tango"></ul>');
        $.ajax({
            type: "POST",
            dataType: 'xml',
            url: "/xmlrpc/get_social_objects",
            success: function(xml){
                $('obj', xml).each(function() {
                    var line = '<li id="'+$(this).attr("invid")+'" title="<b>'+$(this).attr("name")+'</b>" class="tipn"><img width="48" height="48" src="/var/virtu/objects/'+$(this).attr("image")+'" alt="" /></li>';
                    $("#InventoryObjects").append(line);
                });
                $('.tipn').tipsy({
                    gravity:'s',
                    html: true,
                    live: true
                });
                $('#InventoryObjects').jcarousel();
                $('#Inventory').show();
            }
        });
    });
    $(".contentAreaLeft").sortable({
        connectWith: '.contentAreaLeft',
        handle: '.ToolbarBoxLeft',
        items: ".box",
        placeholder: "ui-state-highlight",
        update: function(event, ui) {
            var cooked = new Array;
            $( ".contentAreaLeft" ).each(function(){
                $.cookie('csppos', $(".contentAreaLeft").sortable('toArray'));
            });
        }

    });
    (function($) {
        var cache = [];
        $.preLoadImages = function() {
            var args_len = arguments.length;
            for (var i = args_len; i--;) {
                var cacheImage = document.createElement('img');
                cacheImage.src = arguments[i];
                cache.push(cacheImage);
            }
        }
    })(jQuery)
    jQuery.preLoadImages(
        "/assets/images/site/loader.gif",
        "/assets/images/site/virtu/world/park.png",
        "/assets/images/site/virtu/world/mall.png",
        "/assets/images/site/virtu/world/school.png",
        "/assets/images/site/virtu/world/room.png",
        "/assets/images/admin/success.png",
        "/assets/images/admin/error.png",
        "/assets/images/site/virtu/shop-next.png",
        "/assets/images/site/virtu/shop-next.png",
        "/assets/images/site/virtu/buttons/shop.png",
        "/assets/images/site/virtu/buttons/school.png",
        "/assets/images/site/virtu/buttons/social.png",
        "/assets/images/site/virtu/buttons/health.png",
        "/assets/images/site/virtu/buttons/hunger.png",
        "/assets/images/site/advisor/kat.png",
        "/assets/images/site/pig/bubble.png",
        "/assets/images/admin/back_h2_box_blue.jpg"
        );
});
