$(document).ready(function(){ 

  //get hash value
  var hash = window.location.hash;
  hash = hash.substring(1); 
  var nicename = hash.split( '/' );
  var nivo0 = nicename[0];
  var nivo1 = nicename[1];
  var nivo2 = nicename[2];
  var nivo3 = nicename[3];
  var nivo4 = nicename[4];
  var nivo5 = nicename[5];
  
  
  //nastavi jezik v js
  var jezik = $.url.segment(0);
  if(jezik == null) jezik = 'si';
 
  //nastavimo id sklopa za različne jezike
  var gradbenistvoID = new Array();
  gradbenistvoID['si'] = 6;
  gradbenistvoID['en'] = 33;
  
  var kmetijstvoID = new Array();
  kmetijstvoID['si'] = 7;
  kmetijstvoID['en'] = 34;
  
  var trgovinaitID = new Array();
  trgovinaitID['si'] = 8;
  trgovinaitID['en'] = 35;



//logo
  $("#logo .logo").hover(
    function(){  
       $(this).attr("src", "/images/logo_h.gif");
    },
    function(){ 
       $(this).attr("src", "/images/logo.gif");
    }
  );   
   
//animacija na vstopni strani
 // $(".trigger_button").hover(function(){
   $(".trigger_button").bind("mouseenter",function(){
    var getVal1 = $(this).attr("tval");
//alert(getVal1);
    ////////////////////////
    var animationSpeed = 650;
    var containerWidth = 1022; 
    ////////////////////////

    var width = Math.floor(containerWidth / 3); //sirina elementa
    var longElement = Math.floor(width*2)+3; //aktivni element
    var shortElement = Math.ceil(width / 2)+3; //neaktivni element
    var fixDif = Math.floor(width / 2); //pomik v levo pri spemembi širine

    if(getVal1 == gradbenistvoID[jezik])  { 
      getVal2 = kmetijstvoID[jezik]; getVal3 = trgovinaitID[jezik]; 
      leftFix1 = 0;
      leftFix2 = width + (fixDif * 2);
      leftFix3 = (width * 2) + fixDif;
    }
    else if(getVal1 == kmetijstvoID[jezik])  { 
      getVal2 = gradbenistvoID[jezik];  getVal3 = trgovinaitID[jezik];
      leftFix1 = width - fixDif;
      leftFix2 = 0;
      leftFix3 = (width * 2) + fixDif;
    }
    else if(getVal1 == trgovinaitID[jezik])  { 
      getVal2 = kmetijstvoID[jezik];  getVal3 = gradbenistvoID[jezik]; 
      leftFix1 = (width * 2) - (fixDif * 2);
      leftFix2 = width - fixDif;
      leftFix3 = 0;
    }

    var idfix;
    if(jezik == 'si') idfix = 5;
    else if(jezik == 'en') idfix = 32;
    // alert(jezik);
    //alert(getVal3 - idfix);
    if((getVal3-idfix) != 3) $("#kwick_" + (getVal3-idfix)).animate({ width: shortElement + "px", left: leftFix3 }, animationSpeed, 'swing');
    if((getVal2-idfix) != 3) $("#kwick_" + (getVal2-idfix)).animate({ width: shortElement + "px", left: leftFix2 }, animationSpeed, 'swing');
    if((getVal1-idfix) != 3) $("#kwick_" + (getVal1-idfix)).animate({ width: longElement + "px", left: leftFix1}, animationSpeed, 'swing');    


  }).bind("mouseleave",function(){
     
      $("#menu_vstopna").mouseleave(function(){  
        var animationSpeed = 650;
        $("#kwick_1").stop().animate({ width: "341px", left: "0px" }, animationSpeed , 'swing');
        $("#kwick_2").stop().animate({ width: "341px", left: "341px" }, animationSpeed , 'swing');
        //$("#kwick_3").stop().animate({ width: "341px", left: "682px" }, animationSpeed , 'swing');
      });
  }

  );
  

//scroller
  $('#poteza_meseca_box .inner').jScrollPane({scrollbarMargin: 14});
  $('#nagovor_vodstva_box .inner').jScrollPane({scrollbarMargin: 14});
  $('#menu2 .inner').jScrollPane({scrollbarMargin: 14});

  
  //popravek, ker če div ni viden se ne naloži javascript za scroller
  var cssObj = {'z-index' : '100','display' : 'none'}
  $("#poteza_meseca_box").css(cssObj);
  $("#nagovor_vodstva_box").css(cssObj);
  $("#menu2").css(cssObj);

//nagovor vodstva
  $("#nagovor_vodstva").click(function(){
    //skrijemo potezo meseca
    $("#poteza_meseca_box").fadeOut();
    $("#pm_plus").attr("src", "/images/plus.png");
    
    if($("#nv_plus").attr("src") == "/images/plus.png") 
    {
      $("#nv_plus").attr("src", "/images/minus.png");
      $("#nagovor_vodstva_box").fadeIn();
    }
    else
    {
      $("#nv_plus").attr("src", "/images/plus.png");
      $("#nagovor_vodstva_box").fadeOut();
    } 
  });
  
//poteza meseca
  $("#poteza_meseca").click(function(){
    //skrijemo nagovor vodstva
    $("#nagovor_vodstva_box").fadeOut();
    $("#nv_plus").attr("src", "/images/plus.png");
    
    if($("#pm_plus").attr("src") == "/images/plus.png") 
    {
      $("#pm_plus").attr("src", "/images/minus.png");
      $("#poteza_meseca_box").fadeIn();
    }
    else
    {
      $("#pm_plus").attr("src", "/images/plus.png");
      $("#poteza_meseca_box").fadeOut();
    } 
  });

//klik na logo  
  $("#logo").click(function(){
    $(".elementi_vstopna").fadeIn();
    $(".elementi_inner").fadeOut();
    $("#menu1").fadeOut();
    $("#menu2").fadeOut();
    $("#menu_vstopna").fadeIn();
    $("#menu_inner").fadeOut();
  });

//preusmeritev na podstran 
 
  $(".trigger_button").live('click', function(){
    //var selectedElement = $(this).attr("content");
    var selectedElement;
    if(nivo1 && nivo1 != 59 && nivo1 != 5)  selectedElement = nivo1;
    //if(hash) selectedElement = nivo1;
    else selectedElement = $(this).attr("tval");
    var idfix_cb;
    if(jezik == 'si') idfix_cb = 6;
    else if(jezik == 'en') idfix_cb = 33;
    //alert(selectedElement);
    $(".trigger_button_inner").removeAttr("style");
    $('.kwicks').kwicks({  
      min : 253,  
      spacing : 0,
      sticky : true, 
      event : 'click',
      defaultKwick:(selectedElement-idfix_cb)  
    });

    //ozadje
    
    if(selectedElement == gradbenistvoID[jezik])
    {
        $("#inner_content").css("backgroundPosition", "0px 0px");
    }
    else if (selectedElement == kmetijstvoID[jezik])
    {
        $("#inner_content").css("backgroundPosition", "0px -335px");
    }
    else if (selectedElement == trgovinaitID[jezik])
    {
        $("#inner_content").css("backgroundPosition", "0px -670px");
    }

    //menu
    $("#menu_vstopna").fadeOut();
    $("#menu_inner").fadeIn();

    $("#nagovor_vodstva_box").fadeOut();
    $("#poteza_meseca_box").fadeOut();
    $(".elementi_vstopna").fadeOut();
    ////////////////
    $(".elementi_inner").fadeIn();
    
    //vsebina
    $.get("/include/ajax/ajax.inner_content.php", { eid: selectedElement},
    function(data){ $("#inner_content").html(data); });
    
    //sponzorji
    $.get("/include/ajax/ajax.supporters2.php", { eid: selectedElement, url: $(this).attr("url")},
    function(data){ $("#supporter_bar").html(data); });

  });
  
    
  //gumbi na notranji strani
  $(".trigger_button_inner").live('click', function(){
    var selectedElement = $(this).attr("tval");
     
    $("#menu1").fadeOut();
    $("#menu2").fadeOut();
       
    $(".elementi_inner").fadeOut('fast', function() {
      if(selectedElement == gradbenistvoID[jezik])
      {
          $("#inner_content").css("backgroundPosition", "0px 0px");
      }
      else if (selectedElement == kmetijstvoID[jezik])
      {
          $("#inner_content").css("backgroundPosition", "0px -335px");
      }
      else if (selectedElement == trgovinaitID[jezik])
      {
          $("#inner_content").css("backgroundPosition", "0px -670px");
      } 
      
      //vsebina
      $.get("/include/ajax/ajax.inner_content.php", { eid: selectedElement},
      function(data){ 
        $("#inner_content").html(data); 
      });
      
      

    });
    
    //sponzorji
      $.get("/include/ajax/ajax.supporters2.php", { eid: selectedElement, url: $(this).attr("url")},
      function(data){ 
        $("#supporter_bar").html(data); 

        $('.elementi_inner').fadeIn('fast', function() {
          ///
        });
      });

  });
  

  $("#supporter_bar .supporter").live('click', function(){
    var idb = $(this).attr("rel");
    clickSupporter(idb)
  });
    
  
  $('#supporter_bar .supporter').live('mouseover mouseout', function(event) {
    if (event.type == 'mouseover') {
      $(this).attr("src", "/images/logo/logo_" + $(this).attr("rel") + ".jpg");
    } else {       // alert(nivo3);
      //if($(this).attr("rel") == nivo3) $(this).attr("check", "true");
      if($(this).attr("check") == 'false'){
       $(this).attr("src", "/imagemergegray.php?image=/images/logo/logo_" + $(this).attr("rel") + ".jpg");
      }
    }
  });
  
  function clickSupporter(idb)
  {
     // var idb = $(this).attr("rel");
     var idb = idb;
      //  alert("ok");
    /*nastavimo, da gumb ob kliku ostane označen - start*/
    $("#supporter_bar .supporter").each(function(index) {     
      $(this).attr("check", "false");   //alert($(this).attr("rel") +' - ' + idb);
      if($(this).attr("rel") != idb)
      {    //alert($(this).attr("rel"));
        $(this).attr("src", "/imagemergegray.php?image=/images/logo/logo_" + $(this).attr("rel") + ".jpg");
      }
    });
  
    $(this).attr("check", "true");
    /*end*/
    
      $("#menu2").fadeOut();
      $("#menu1").fadeOut('fast', function() {
        var hash = window.location.hash;
        hash = hash.substring(1);    //alert(hash);
        var nicename = hash.split( '/' );
        var nivo0 = nicename[0];
        var nivo1 = nicename[1];
        var nivo2 = nicename[2];
        var nivo3 = nicename[3];
        //var _url = nivo0+'/'+nivo1+'/'+nivo2+'/'+nivo3;
        var _url = nivo0+'/'+nivo1;
        $.get("/include/ajax/ajax.menu_content.php", { id: idb, url: _url},
        function(data){ 
          $("#menu1").html(data);
          $("#menu1").fadeIn(); 
        });
      });
  }
  
  $("#menu1 li").live('click', function(){
    $("#menu1 li").removeClass('active');
    $(this).addClass('active');
    var id = $(this).attr("rel");
    $("#menu2").fadeOut('fast', function() {
    
     var hash = window.location.hash;
        hash = hash.substring(1); 
        var nicename = hash.split( '/' );
        var nivo0 = nicename[0];
        var nivo1 = nicename[1];
        var nivo2 = nicename[2];
        var nivo3 = nicename[3];
      var _url = nivo0+'/'+nivo1+'/'+nivo2+'/'+nivo3;
  		$("#menu2 .inner").load('/include/ajax/ajax.menu_content2.php?id=' + id + '&url=' +  _url, '', reinitialiseScrollPane);
    }); 
  });
  
  
  /****************************************************************************/
  
  //ajax - crawling
  if(hash)
  {
    if(nivo1 && nivo1 != 59 && nivo1 != 5)
    {
      selectedElement = nivo1;
      $('.trigger_button').trigger('click');
    }
    
    if(nivo3)
    {
        clickSupporter(nivo3);
    }
    
    if(nivo5)
    {
      reinitialiseScrollPane = function()
    	{
        $("#menu2").fadeIn('fast',function() {   });
        $('#menu2 .inner').jScrollPane();
    	}
      $("#menu2 .inner").load('/include/ajax/ajax.menu_content2.php?id=' + nivo5 , '', reinitialiseScrollPane);
    }
    
    if(nivo1 && (nivo1 == 59 || nivo1 == 5))
    {
      $('#nagovor_vodstva').trigger('click');
    } 

    if(nivo0 == '!poteza-meseca' || nivo0 == '!move-of-the-month')
    {  
      //  $("#poteza_meseca").click(function(){
       $('#poteza_meseca').trigger('click');
    }
    
    
  }
  
     

});
