$('html').addClass('js');

$(function(){
 $('a[rel="external"]').click(function(){      this.target = '_blank';  });  
//************************ FLASH NAVIGATION ***/
var flashvars = {};
var params = {
  wmode: "transparent"
};
var attributes = {};
swfobject.embedSWF(phrasebook.URL_ROOT+"/flash/navigation.swf?url_root="+phrasebook.URL_ROOT+"&currPage="+$("#nav-main li.active").prevAll("li").length, "nav-main", "250", "250", "9.0.0", "expressInstall.swf", flashvars, params, attributes);

//************************ INFIELD LABEL ***/
$('#form-contact label').inFieldLabels({ fadeOpacity:0, fadeDuration:100 });

//************************ LIGHTBOX GALLERY ***/  
$('#list-t01 .img-gallery-link').click(function(){
  var imgGalleryLink = $(this).attr('href');
  $('body').append('<div id="lightbox-overlay"></div><div id="lightbox"></div>');
  transp = $('#container').outerHeight(true);
  $('#lightbox-overlay').css({'width':'100%','height':transp,'opacity':.25});
  
  
  $('#lightbox').css({ 'top':$(window).scrollTop()+100, 'margin-left':-($('#lightbox').outerWidth(true)/2) });
  $(document).bind('click keydown',function(e){
    var code = (e.keyCode ? e.keyCode : e.which);
    var clicked = $(e.target);
    if (clicked.is('#lightbox-overlay, #img-gallery-close') || code === 27) { $('#lightbox-overlay, #lightbox').fadeOut(300,function(){ $(this).remove(); }); }
  });
  $('#lightbox-overlay, #lightbox').hide().fadeIn(300,function(){
    $.ajax({
      url: imgGalleryLink,
      cache: false,
      success: function(data){
        $('#lightbox').html($(data).find('#content'));
        if( $(data).find('#content #slides li').length >1 ){
            imgGallery();
        }
      }
    });
  });
  return false;
});




//************************ GALLERY PAGE SLIDESHOW ***/
var gallslide = '#list-t02 ul';
var gallslideActive = '#list-t02 .active';
var gallslideWidth = $(gallslide).outerWidth(true);
var gallslideSum = $(gallslide).length;

$('#list-t02').css({ 'width': gallslideSum*gallslideWidth }).parent().css({ 'width':gallslideWidth }).after('<div id="list-t02-amount"><span />/'+gallslideSum+'</div><div id="list-t02-ctrl" class="ctrl-t02"><a class="prev">Prethodna stranica</a><a class="next">Slijedeća stranica</a></div>');

gallslideMovement($(gallslideActive), 0);

$('#list-t02-ctrl a').click(function(){ gallslideCtrl($(this).attr('class')) });

function gallslideCtrl(controlClass) {
  if (controlClass === 'next'){
    if ($(gallslideActive).nextAll().length !== 0) {
      $(gallslideActive).next().addClass('active').siblings().removeClass('active');
    } else {
      $(gallslide).first().addClass('active').siblings().removeClass('active');
    }      
  } else if (controlClass === 'prev'){
    if ($(gallslideActive).prevAll().length !== 0) {
      $(gallslideActive).prev().addClass('active').siblings().removeClass('active');
    } else {
      $(gallslide).last().addClass('active').siblings().removeClass('active');
    }
  }
  gallslideMovement($(gallslideActive), $(gallslideActive).position().left);
}

function gallslideMovement(gallSlideActiveVal, gallSlideActivePos) {
  $('#list-t02').animate({ 'margin-left': -(gallSlideActivePos) });
  $('#list-t02-amount span').html(gallSlideActiveVal.index()+1);
}




//************************ IMAGE GALLERY ***/
imgGallery();
function imgGallery(){
  var slide = $('#img-gallery li').outerWidth(true);
  var slideContainer = $('#img-gallery ul');
  var slideTitle = $('#img-gallery').parent().siblings('h1');
  
  slideContainer.css({'width':slide*$('#img-gallery li').length});
  
  $('#img-gallery li:first-child').addClass('active first-visible');
  $('#img-gallery li:nth-child(8)').addClass('last-visible');
  $('#img-gallery li a').click(function(){
    $(this).parent().addClass('active').siblings().removeClass('active');
    updateHolder();
    return false;
  });
  
  $('#img-gallery').parent().after('<div id="img-gallery-ctrl" class="ctrl-t01"><a class="prev">Prethodna</a><a class="next">Sljedeća</a></div><a id="img-gallery-close">Zatvori</a>');
  
  if($('#img-gallery-close').siblings().is('#introduction')) { $('#img-gallery-close').remove(); }
  
  if(!$('#img-gallery-close').parents().is('#lightbox')) {
    $('#img-gallery-close').click(function(){
      history.go(-1);
    });
  }
  
  $('#img-gallery-ctrl a').click(function(){ sliderControls( $(this).attr('class') ) });
  
  function updateHolder() {
    var imgLink = $('#img-gallery .active a');
    var imgBig = $('#img-holder img');
    $('#img-holder').addClass('loading');
    if ( imgBig.attr('src') !== imgLink ) {
      slideTitle.animate({ opacity: 0 });
      imgBig.animate({ opacity: 0 }, function(){
        var img = new Image();
        $(img).load(function(){   
          $(this).css({opacity: 0});
          $('#img-holder').removeClass('loading').html(this);
          $(this).animate({opacity: 1});
          slideTitle.html($('#img-gallery .active img').attr('alt')).animate({opacity: 1});
        }).error(function(){}).attr({src: imgLink.attr('href'), alt: imgLink.children().attr('alt') });
      });
    }
  }

  function sliderControls(sliderControl){
    if (sliderControl === 'next') {
      $('#img-gallery .active').removeClass('active').next().addClass('active');
      if ( $('#img-gallery .active').prev().hasClass('last-visible') ) {
        $('#img-gallery .last-visible').removeClass('last-visible').next().addClass('last-visible');
        $('#img-gallery .first-visible').removeClass('first-visible').next().addClass('first-visible');
        slideContainer.animate({ marginLeft: -(slide-parseInt(slideContainer.css('margin-left'))) });
      } else if ( $('#img-gallery .active').prevAll().length === 0 ) {
        $('#img-gallery li:first-child').addClass('active first-visible').siblings().removeClass('active first-visible');
        $('#img-gallery li:nth-child(8)').addClass('last-visible').siblings().removeClass('last-visible');
        slideContainer.animate({ marginLeft: 0 });
      }
      updateHolder();
    } else if (sliderControl === 'prev') {
      $('#img-gallery .active').removeClass('active').prev().addClass('active');
      if ( $('#img-gallery .active').next().hasClass('first-visible') ) {
        $('#img-gallery .first-visible').removeClass('first-visible').prev().addClass('first-visible');
        $('#img-gallery .last-visible').removeClass('last-visible').prev().addClass('last-visible');
        slideContainer.animate({ marginLeft: parseInt(slideContainer.css('margin-left'))+slide });
      } else if ( $('#img-gallery .active').nextAll().length === 0 ) {
        $('#img-gallery li:last-child').addClass('active last-visible').siblings().removeClass('active last-visible');
        $('#img-gallery li:eq('+($('#img-gallery li').prevAll().length-(8-1))+')').addClass('first-visible').siblings().removeClass('first-visible');
        slideContainer.animate({ marginLeft: -(slide*($('#img-gallery li:last-child').prevAll().length-(8-1))) });
      }
      updateHolder();
    }
  }
}




//************************ IE6 ***/
if(typeof document.body.style.maxHeight === 'undefined') {
  DD_belatedPNG.fix('#content, #content .box-bottom, #content .box-repeat');
}

});

