$('document').ready(function() 
{
      /* Contact stuff
      **************************/
      $('#contactForm').submit(function(){
            var templateUrl = $('input#template_url').val();
            var scriptUrl = '/includes/contact/send_form.php';
            var url = templateUrl + scriptUrl;
            var params = 'name=' + $('input#name').val() + '&email=' + $('input#email').val() + '&website=' + $('input#website').val() + '&message=' + $('textarea#message').val() + '&sent=1';

            $('#indicator').ajaxStart(function(){
                  $(this).show();
            }).ajaxStop(function(){
                  $(this).hide();
            });

            $.ajax({
                  url      : url,
                  type     : 'POST',
                  data     : params,
                  dataType : 'html',
                  success  : function(result)
                  {
                        if($('#success').length > 0) $('#success').remove();
                        if($('#failed').length > 0) $('#failed').remove();

                        var el_id = 'success';
                        var responseTxt = result;

                        var response = responseTxt.split(' : ');
                        if(response.length > 1)
                        {
                              el_id = 'failed';
                              responseTxt = response[1];
                        }

                        $('<div id="'+el_id+'">'+responseTxt+'</div>').insertBefore('div#contact form').fadeIn('slow');
                        if('success' == el_id)
                        {
                              setTimeout("$('#success').fadeOut('slow')", 5000);
                        }
                  }
            });
            return false;
      });

      /* Sidebar's gallery stuff
      **************************/
      /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      | MOST COMMENTED GALLERY                                 |
      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
      var tempCommented = $('#mostCommentedInner').html();
      var commentedObj  = $('#mostCommentedInner');

      commentedObj.sliderStart(); // starts animation

      // Toggle the most commented's gallery
      $('#mostCommentedSwitcher').toggle(function() {
            commentedObj.trigger('stop');
            $(this).hide().fadeIn('slow').toggleClass('selected').text('inapoi');
            $(this).parents('div:eq(1)').css({'height':'447px','overflow':'visible'});
            commentedObj.html('').append(tempCommented); // just a fake refresh ...
      }, function() {
            $(this).hide().fadeIn('slow').toggleClass('selected').text('toate');
            $(this).parents('div:eq(1)').css({'height':'163px','overflow':'hidden'});
            commentedObj.html(tempCommented);
            commentedObj.trigger('start');
      });

      /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      | MOST RATED GALLERY                                     |
      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
      var tempRated = $('#mostRatedInner').html();
      var ratedObj  = $('#mostRatedInner');

      ratedObj.sliderStart(); // starts animation

      // Toggle the most rated's gallery
      $('#mostRatedSwitcher').toggle(function() {
            ratedObj.trigger('stop');
            $(this).hide().fadeIn('slow').toggleClass('selected').text('inapoi');
            $(this).parents('div:eq(1)').css({'height':'147px','overflow':'visible'});
            ratedObj.html('').append(tempRated); // just a fake refresh ...
      }, function() {
            $(this).hide().fadeIn('slow').toggleClass('selected').text('toate');
            $(this).parents('div:eq(1)').css({'height':'163px','overflow':'hidden'});
            ratedObj.html(tempRated);
            ratedObj.trigger('start');
      });

});

// Plugin begin
$.fn.sliderStart = function (limit, interval) {

    limit = limit || 3;
    interval = interval || 4000;

    return this.each(function () {

        // capture a cache of all the list items
        var $list = $(this),
            running = true,
            items = [],
            currentItem = limit,
            total = 0, // later ...
            Height = $list.find('> li:first').height();

        // capture the cache...
        $list.find('> li').each(function () {
            items.push('<li>' + $(this).html() + '</li>');
        });

        total = items.length;
        $list.find('> li').filter(':gt(' + (limit - 1) + ')').remove();

        // Bind custom handlers - we'll trigger these when user clicks on the box's controls
        $list.bind('stop', function(){
              running = false;
        }).bind('start', function(){
              running = true;
        });

        // effects
        function animateSlider() {

              if(running)
              {
                    var $insert = $(items[currentItem]).css({
                        height : 0,
                        opacity : 1,
                        display : 'none'
                    }).prependTo($list);

                    $list.find('> li:last').fadeOut('slow', function () {
                          $insert.css({ height : Height }, 1000).fadeIn('slow');
                          $(this).remove();
                    });

                    currentItem++;
                    if (currentItem >= total) {
                          currentItem = 0;
                    }
              }
              setTimeout(animateSlider, interval);

        }
        animateSlider();
    });
};