/***************************************************
 |      ___           ___           ___          _____          ___           ___
 |     /  /\         /  /\         /  /\        /  /::\        /  /\         /__/\
 |    /  /:/_       /  /::\       /  /::\      /  /:/\:\      /  /:/_        \  \:\
 |   /  /:/ /\     /  /:/\:\     /  /:/\:\    /  /:/  \:\    /  /:/ /\        \  \:\
 |  /  /:/_/::\   /  /:/~/::\   /  /:/~/:/   /__/:/ \__\:|  /  /:/ /:/_   _____\__\:\
 | /__/:/__\/\:\ /__/:/ /:/\:\ /__/:/ /:/___ \  \:\ /  /:/ /__/:/ /:/ /\ /__/::::::::\
 | \  \:\ /~~/:/ \  \:\/:/__\/ \  \:\/:::::/  \  \:\  /:/  \  \:\/:/ /:/ \  \:\~~\~~\/
 |  \  \:\  /:/   \  \::/       \  \::/~~~~    \  \:\/:/    \  \::/ /:/   \  \:\  ~~~
 |   \  \:\/:/     \  \:\        \  \:\         \  \::/      \  \:\/:/     \  \:\
 |    \  \::/       \  \:\        \  \:\         \__\/        \  \::/       \  \:\
 |     \__\/         \__\/         \__\/                       \__\/         \__\/
 |
 |
 | JavaScript main file
 | --------------------
 | The Garden
 |
 | Contents:
 |  =general
 |  =functions
 |  =plugins
***************************************************/

/***************************************************
 | =general
***************************************************/
jQuery(function() {
  $('ul#gallery_garden').fktvGalleryFader({
    operation: 'start',
    fade_time: 500,
    delay: 5000
  }).fktvGalleryBrowser({
    fade_time: 1000,
    fdr_fade_time: 2000,
    fdr_delay: 5000,
    hide: 'ul#menu, ul#events, div#footer, ul#brands, div#subcontent, div#clippings, ul#news, div#news_music, div#tweet, div#music_player',
    show_text: 'Pokaži galeriju',
    hide_text: 'Sakrij galeriju'
  });
  repairBrowsers();

  // hovers
  $('ul#events li, form#newsletter p.button button, li.all a, a#clippings_all, a#press_all, form.item p.button input')
    .mouseover (function () {
      $(this)
        .css ({'backgroundPosition' : '0 100%'});
    })
    .mouseout (function () {
      $(this)
        .removeAttr ('style');
    });

    // filter news
   $('p#filter a#filterAll').click (function () {
     if (!$(this).hasClass ('active')) {
       $('ul#news li.item').show ();
       restoreNewsListing ();
       $('p#filter a.active').removeClass ('active');
       $(this).addClass ('active');
     }
     return false;
   });
   $('p#filter a#filterNews').click (function () {
     if (!$(this).hasClass ('active')) {
       $('ul#news li.item.music').hide ();
       $('ul#news li.item:not(li.music)').show ();
       restoreNewsListing ();$('p#filter a.active').removeClass ('active');
       $(this).addClass ('active');
     }
     return false;
   });
   $('p#filter a#filterMusic').click (function () {
     if (!$(this).hasClass ('active')) {
       $('ul#news li.item:not(li.music)').hide ();
       $('ul#news li.item.music').show ();
       restoreNewsListing ();$('p#filter a.active').removeClass ('active');
       $(this).addClass ('active');
     }
     return false;
   });

   if (window.location.hash == '#music') {
     $('p#filter a#filterMusic').click ();
   }
});

/***************************************************
 | =functions
***************************************************/

function restoreNewsListing () {
  var $listing = $('ul#news li');
  for (i = 0; i < $('ul#news li').length; i++) {
    if (i % 2 == 0) {
      $($listing[i]).removeClass ('odd');
    }
    else {
      $($listing[i]).addClass ('odd');
    }
  }
}

function repairBrowsers() {
  // Promjene samo za webkit ili IE
  if ($.browser.webkit || $.browser.msie) {
    // Izbjegni Chrome jer on prikazuje u tedu
    if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {}
    else {
      // Provjeriti postoji li h2
      if ($('div#header h2').length == 0) {
        $('div#header h1').css('padding-bottom', '7px');
      } else {
        $('div#header h1').css('padding-bottom', '6px');
      }
      $('div#header h2').css('padding-bottom', '5px');
    }
  }

}

/*
  Fiktiv Gallery Browser v1.0

  Makes the browsing of a gallery possible.

  Example:
    $('ul#gallery').fktvGalleryBrowser({
      fade_time: 1000,
      fdr_fade_time: 2000,
      fdr_delay: 5000,
      hide: 'ul#menu, ul#events, div#footer, ul#brands, div#subcontent, div#clippings, ul#news',
      show_text: 'Pokaži galeriju',
      hide_text: 'Sakrij galeriju'
    });

  Parameters:
    @param options
           fade_time: time to fade in the control elements (prev and next) and fade out other elements
           fdr_fade_time: fade time which will be used when continuing gallery fader
           fdr_delay: delay between fades when continuing gallery gade
           hide: elements to hide when starting the gallery browser
           show_text: Text of the show button when gallery browser is inactive
           hide_text: Text if the hide button when gallery browser is active
 */
jQuery.fn.fktvGalleryBrowser = function(options) {

  var settings = jQuery.extend({
    fade_time: 1500,
    fdr_fade_time: 1500,
    fdr_delay: 5000,
    hide: '',
    show_text: 'Pokaži galeriju',
    hide_text: 'Sakrij galeriju'
  }, options);

  gallery_name = this.attr('id').split('_')[1];
  gallery_obj = this;

  var prev_height = 0;
  $('a#show_' + gallery_name).click(function() {
    if ($(this).hasClass('showing')) {
      $('div#overlay').fadeOut(settings.fade_time/2, function() {$(this).remove()});
      $('a#prev_' + gallery_name + ', a#next_' + gallery_name).fadeOut(settings.fade_time, function() {
        $('div#content').stop().animate({
            height: prev_height
          },
          'slow',
          function() {
              if (!$.browser.msie) {
                var to_show = settings.hide + ', pre.subtext, div#header h2, h1';
              } else {
                var to_show = settings.hide;
              }
              $(to_show).fadeIn(settings.fade_time);
              if ($.browser.msie) {
                $('pre.subtext, div#header h2, h1').show();
              }
              $('ul#lang_chooser').fadeTo(settings.fade_time, 1);
              gallery_obj.fktvGalleryFader({
                operation: 'start',
                fade_time: settings.fdr_fade_time,
                delay: settings.fdr_delay
            });
          }
        );
      });
      $(this).removeClass('showing').html(settings.show_text);
    }
    else {
      prev_height = $('div#content').height();
      gallery_obj.fktvGalleryFader({
        operation: 'stop'
      });
      $(this).addClass('showing').html(settings.hide_text);
      $('ul#lang_chooser').fadeTo(settings.fade_time, 0.01);
      if (!$.browser.msie) {
        var to_hide = settings.hide + ', pre.subtext, div#header h2, h1';
      } else {
        var to_hide = settings.hide;
      }
      $('div#content').height(prev_height);
      $(to_hide).fadeOut(settings.fade_time, function() {
        if ($.browser.msie) {
          $('pre.subtext, div#header h2, h1').hide();
        }
        $('div#content').stop().animate({
            height: '580px'
          },
          'slow',
          function() {
            $('a#prev_' + gallery_name).fadeIn(settings.fade_time);
            $('a#next_' + gallery_name).fadeIn(settings.fade_time);
            if (!$.browser.msie) {
              $('<div id="overlay"></div>').prependTo('body').fadeTo(settings.fade_time/2, 0.8);
            }
          }
        );
      });
    }
  });

  $('a#next_' + gallery_name).click(function() {
    img_num = gallery_obj.children('li').length;

    current = gallery_obj.children('li.active').index('ul#' + gallery_obj.attr('id') + ' li');
    gallery_obj.children('li.active').fadeOut(settings.fdr_fade_time);
    if (current == (img_num-1)) {
      gallery_obj.children('li.active').removeClass('active');
      gallery_obj.children('li:first').addClass('active');
    } else {
      gallery_obj.children('li.active').removeClass('active').next().addClass('active');
    }
    gallery_obj.children('li.active').fadeIn(settings.fdr_fade_time);
  });

  $('a#prev_' + gallery_name).click(function() {
    img_num = gallery_obj.children('li').length;

    current = gallery_obj.children('li.active').index('ul#' + gallery_obj.attr('id') + ' li');
    gallery_obj.children('li.active').fadeOut(settings.fdr_fade_time);
    if (current == 0) {
      gallery_obj.children('li.active').removeClass('active');
      gallery_obj.children('li:last').addClass('active');
    } else {
      gallery_obj.children('li.active').removeClass('active').prev().addClass('active');
    }
    gallery_obj.children('li.active').fadeIn(settings.fdr_fade_time);
  });

  return this;
}


/*
  Fiktiv Gallery Fader v1.0

  Fades a list of images.

  Example:
    $('ul#gallery').fktvGalleryFader({
      operation: 'start',
      fade_time: 1500,
      delay: 5000
    });

    To stop this gallery:
    $('ul#gallery').fktvGalleryFader({
      operation: 'stop'
    });

  Parameters:
    @param options
           operation: 'start' or 'stop',
           fade_time: duration of the fade effects,
           delay: time between transitions
*/
jQuery.fn.fktvGalleryFader = function(options) {

  settings = jQuery.extend({
    operation: 'start',
    fade_time: 1500,
    delay: 5000
  }, options);

  if (settings.operation == 'stop') {
    interval_id = this.attr('title');
    clearInterval (interval_id);
  }
  else {
    gallery_obj = this;

    img_num = gallery_obj.children('li').length;
    gallery_obj.children('li').not('ul#' + gallery_obj.attr('id') + ' li.active').hide();

    interval_id = setInterval(function() {
      current = gallery_obj.children('li.active').index('ul#' + gallery_obj.attr('id') + ' li');
      gallery_obj.children('li.active').fadeOut(settings.fade_time);
      if (current == (img_num-1)) {
        gallery_obj.children('li.active').removeClass('active');
        gallery_obj.children('li:first').addClass('active');
      } else {
        gallery_obj.children('li.active').removeClass('active').next().addClass('active');
      }
      gallery_obj.children('li.active').fadeIn(settings.fade_time);
    }, settings.delay);

    gallery_obj.attr('title', interval_id);
  }

  return this;
}
