 $(document).ready(function()
 {
    //Avataan submenu
    $('.category_item:not(.open)').live('click', function(event) {
        event.preventDefault();
        // Animate box as wanted and add a class to indicate that the box is open.
        $(this).addClass('open');
        $(this).siblings(".submenu").fadeIn("fast");
    });

    //Suljetaan submenu
    $('.open').live('click', function(event) {
        event.preventDefault();
        // Reverse animation and remove class
        $(this).removeClass('open');
        $(this).siblings(".submenu").fadeOut("fast");
    });     
 });

 $(window).load(function()
 {
    $id = $('.active').siblings('input').val();
    if($('#EIFace')[0])
    {
        $('#EIFace')[0].sendId($id);
    }
    else
    {
        $('#EIFace').sendId($id);
    }    

    //TYÖLINKIT
     $('.work_link').click(function(event)
     {
         event.preventDefault();
         $('.active').removeClass('active');
         $(this).addClass('active');

         $('.work_info').children('p').text("");
         $('.work_info').children('h3').text("");

         var work_id = $(this).siblings('input').val();

         if($('#EIFace')[0])
         {
             $('#EIFace')[0].sendId(work_id);
         }
         else
         {
             $('#EIFace').sendId(work_id);
         }

         //HAETAAN TYON KUVAUS AJAXILLA
         $.post('category/description/',{id : work_id, type : 'noview'},
             function(data)
             {
                $('.work_info').children('p').append(data);
             });

         //HAETAAN TYON NIMI AJAXILLA
         $.post('category/name/',{id : work_id, type : 'noview'},
             function(data)
             {
                $('.work_info').children('h3').append(data);
                $('.work_info').fadeIn("fast");
             });
     });

     //Nuolinäppäimien klikkailu
     $(document.documentElement).keyup(function(event)
     {
         if (event.keyCode == 37)
         {
            // go left
            if($('#EIFace')[0])
             {
                 $('#EIFace')[0].moveLeft();
             }
             else
             {
                 $('#EIFace').moveLeft();
             }
         }
         else if (event.keyCode == 39)
         {
            // go right
            if($('#EIFace')[0])
             {
                 $('#EIFace')[0].moveRight();
             }
             else
             {
                 $('#EIFace').moveRight();
             }
         }
     });
});
