var Krka = {
  init: function () {
    $('#images').imageSlider();        
    $('.filter form select').change(Krka.filter);
    if($('#album-images li div a').length != 0){
      $('#album-images li div a').lightBox();
    }
    if($('#images li a').length != 0){
      $('#images li a').lightBox();
    }
    $('#header .search form input').hint();
    $($.date_input.initialize);
    Krka.business_network.setup();
    Krka.financial_reports.setup();    
    Krka.ny_greeting.setup();
    if (window.location.href.indexOf("?cestitka") > -1){
      Krka.ny_greeting.display();
    }
	Krka.unicodeize();
  },
  
  unicodeize: function(){
    var el = $(".org-network .map-data .data address p.address");
    if (el.length > 0){
	  if(el.html().indexOf("Mukusalas") > -1){
        el.html(el.html().replace("Mukusalas", "Mūkusalas"));
	  }
    }
  },

  filter: function () {
    $(this).parents('form').submit();
  },

  business_network: {
    setup: function() {
      $('#org-network-multiple .map-data').hide();
      $('#org-network-multiple .units li h4 a').click(Krka.business_network.office_click);
      $('#org-network-multiple .units li:first h4 a').click();
    },

    office_click: function() {
      $(this).parent().parent().siblings().removeClass('active');
      $(this).parent().parent().addClass('active');
      var office_id = $(this).parent().parent().attr('id').replace('menu-', '');
      $('#org-network-multiple .map-data').hide();
      $('#item-' + office_id).show();
    }
  },

  financial_reports: {
    setup: function(){
      $('.yearly-reports ul.content li').hide();
      $('.yearly-reports .tab li a').click(Krka.financial_reports.year_click);
      $('.yearly-reports .tab li:first a').click();
    },

    year_click: function(){
      $(this).parent().siblings().removeClass('active');
      $(this).parent().addClass('active');
      var year = $(this).parent().attr('id').replace('tab-year-', '');
      if(year != 'archive'){
        $('.yearly-reports .content li').hide();
        $('#year' + year).show();
        $('#year' + year + ' ul').children().show();
        $('#year' + year + " h3").hide();
      } else {
        $('.yearly-reports .content li').hide();
        $('.yearly-reports .content li').each(function (){
          if ($(this).attr('id').indexOf('archive') > -1){
            var itemID = '#' +  $(this).attr('id');
            $(itemID).show();
            $(itemID + ' ul').children().show();
          }
        });
      }
    }
  },

  ny_greeting : {
    setup: function(){
      $("#ny-greeting").hide();
      if($("a#inline").length != 0){
        $("a#inline").fancybox({
          'hideOnContentClick': true,
          'frameWidth': 720,
          'frameHeight': 420,
          'overlayOpacity': 0.5,
          'overlayColor': "#000000"
        });
      }
    },
    
    display: function(){
      $("a#inline").click();
    }
  }
};

//var j = 1;
//$(function() {
//	$('#keyvisual ul li').hide();
//	$('#keyvisual ul li:first-child').fadeIn('2500');
//	i = $('#keyvisual ul li').length;
//	toggleKeyvisual = function() {
//		$('#keyvisual ul li').fadeOut('1500');
//		$($('#keyvisual ul li')[j]).fadeIn('1500');
//		j = j + 1;
//		if(j == i) j = 0;
//	}
//	var t = setInterval('toggleKeyvisual()',7000);
//});

/* style and stripe all tables in .cms container */
$().ready(function () {
	$('.cms table thead th:first-child').addClass('first');
	$('.cms table thead tr:first-child').addClass('first');
	$('.cms table thead td:first-child').addClass('first');
	$('.cms table thead th:last-child').addClass('last');
	$('.cms table thead tr:last-child').addClass('last');
	$('.cms table thead td:last-child').addClass('last');
	$('.cms table tbody tr:nth-child(2n)').addClass('odd');
});

/* faq toggling */
$().ready(function () {
	/* setup if js */
	$('.faq').addClass('js');
	//$('.faq ol li:first-child').addClass('active');
  //$('.faq ol li:first-child .cms').show();
	/* behaviour */
	$('.faq ol li h3').click(function () {
		$('.faq ol li').removeClass('active');
		$('.faq ol li .cms').hide();
		$(this).next().fadeIn('200');
		$(this).parent().addClass('active');
	});
});

/* standards based external links */
externalLinks = function() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
	var anchor = anchors[i];
	if (anchor.getAttribute("href") &&
		anchor.getAttribute("rel") == "external")
		anchor.target = "_blank";
	}
}

/* language toggling */
$().ready(function(){
	$(".language dt").click(function(){
		$(".language dd").slideToggle("slow");
		$(".language").toggleClass("on");
	});
});

/* tabs */
$().ready(function () {
	// setup if js
	$('.medications').addClass('js');
	$('.files').addClass('js');
	$('.medications .tab li:first-child').addClass('active');
	$('.medications .content li:first-child').addClass('active');
	$('.files .content li:first-child').addClass('active');
	// behaviour
	$('.medications .tab li').click(function () {
		$('.medications .tab li').removeClass('active');
		$(this).addClass('active');
		$('.medications .content li').removeClass('active');
		$('.files .content li').removeClass('active');
		var id = $(this).attr('id').replace('tab-', 'content-');
		$('.medications .content li#' + id).addClass('active');
//    console.log("ADDING ACTIVE TO THIS ITEM: .files .content li#" + id);
		$('.files .content li#' + id +"-files").addClass('active');
	});
});

/* nextUntil plugin by John Resig */
$.fn.nextUntil = function(expr) {
	var match = [];
	this.each(function(){
		for(var i=this.nextSibling;i;i=i.nextSibling) {
			if (i.nodeType != 1) continue;
			if (jQuery.filter( expr, [i] ).length) break;
			match.push(i);
		}
	});
	return this.pushStack( match, arguments );
};

/* collapsing headlines */
$().ready(function () {
	$('.medications .content ul').wrap('<p></p>');
	$('.medications .content h3').each(function () {
		$(this).nextUntil('h3').wrapAll('<div class="js-toggle cms" />');
	});
	$('.js-toggle').hide();
	$('.medications .content h3').click(function () {
		if(this.className != 'active') $('.js-toggle').hide();
		$(this).toggleClass('active');
		$(this).next().slideToggle();
	});
});

/* modal login window */
$().ready(function () {
	$('#footer').after('\n<div class="modal-window" id="win-login">Prosimo počakajte ...</div>');
	var lang = $('html').attr('xml:lang');
	var action = lang == 'sl' ? 'uporabniki/prijava' : 'users/login'
	$('#win-login').jqm({ajax:'/' + lang + '/' + action, trigger: '.login-window'});
});

$().ready(function () {
	$('#footer').after('\n<div class="modal-window" id="win-login">Prosimo počakajte ...</div>');
	var lang = $('html').attr('xml:lang');
	var action = lang == 'sl' ? 'uporabniki/prijava-vademekum' : 'users/login'
	$('#win-login').jqm({ajax:'/' + lang + '/' + action, trigger: '.login-window-vademekum'});
});

/* advanced product search collapse/expand */
$().ready(function () {
  $('.search .expand').toggle(function () {
    $(this).prev().find('li:eq(1)').removeClass('hidden');
    $(this).removeClass('off');
  }, function () {
    $(this).prev().find('li:eq(1)').addClass('hidden');
    $(this).addClass('off');
  });
});

/* autocomplete on product search */
$().ready(function () {
  $('#search_string').autocomplete(window.location.pathname, {
    extraParams: {live: 'true'}
  });
});

/* init & load */
window.oninit = function() {
	if (document.getElementById && document.getElementsByTagName) {
		externalLinks();
	}
}
launchInit = function() {
	if (document.getElementsByTagName && typeof(window.oninit)=='function') {
		if (document.getElementsByTagName('body').length>0) window.oninit();
		else setTimeout('launchInit()',30);
	}
}
launchInit();
$(document).ready(Krka.init);
