﻿/// <reference path="/js/jquery-1.6-vsdoc.js"/>

// remap jQuery to $
(function ($) { })(window.jQuery);

/* optional triggers

$(window).load(function() {
  
});

$(window).resize(function() {
  
});

*/


function formatText(index, panel) {
  return index + "";
}

//CMS class

var CMS = {

  init: function () {
    this.search();
    /*this.slider();*/
    this.headerClick();
    this.initNews();

    $(".inner").each(function () {
      if ($.trim($(this).html()) == "") {
        $(this).remove();
      }
    });

    //ovo bi trebalo srediti glavni menu
    $(".mainMenu A.selected").each(function () {
      var $li = $(this).parent();
      $li.prev("LI.sep").css("background", "none");
    });

    //ZAC
    //$(".inner-right").css("height", $("#main-column").height() + "px");

    $("#header-prijava .prijava").click(function () {
      $("#header-prijava .prijavaform").slideToggle(100);
    });

    $("UL.tabs").each(function () {
      var $this = $(this);
      var $wrap = $this.parent().find(".tabwrap");
      var $wraps = $this.parent().find(".tabwrap DIV");
      var $tabs = $this.find("LI A");
      $this.find("LI A").click(function () {
        if (!$(this).hasClass("selected")) {
          $wraps.hide();
          $tabs.removeClass("selected");
          $(this).addClass("selected");
          $wrap.find("[class='" + $(this).attr("id") + "']").show();
        }
      });

    });

    //ie7 fix
    if ($.browser.msie) {
      if ($.browser.version>6) {
        $("#header").css({ "text-align": "left" });
        $("#menu").css({ "text-align": "left" });
        $("#header-prijava").css({ "z-index": "10000" });  
      }
      

    }
  },

  headerClick: function () {
    $("header h1").click(function () {
      location.href = $(this).attr("rel");
    });
    $("header h1").css({ "cursor": "pointer" });
  },

  doSearch: function (q, $input) {
    if (q.length > 2) {
      var qs = "?q=" + encodeURIComponent(q);

      //alert($input.hasClass("l1"));
      //console.log($input);

      //return;

      if ($input.hasClass("l1")) {
        window.location.href = "/hr/trazi/" + qs;
      }
      else {
        window.location.href = "/en/search/" + qs;
      }


    }
  },

  initNews: function () {

    var nn_mouseover = false;
    var nn_currpage = 0;
    var nn_changePage = function (index) {
      nn_currpage = index;
      $("#novosti-naslovna IMG.main").hide();
      $("#novosti-naslovna IMG.main").eq(index).show();

      $("#novosti-naslovna #novosti-holder .inner DIV.novost").hide();
      $("#novosti-naslovna #novosti-holder .inner DIV.novost").eq(index).show();

      $("#novosti-naslovna #novosti-pager A").removeClass("selected");
      $("#novosti-naslovna #novosti-pager A").eq(index).addClass("selected");

      if (index == 4)
        nn_currpage = -1;
    };

    $("#novosti-naslovna #novosti-pager A").each(function (index) {
      $(this).click(function () {
        nn_changePage(index);
        return false;
      });
    });

    $("#novosti-naslovna").mouseenter(function () {
      nn_mouseover = true;
    }).mouseleave(function () {
      nn_mouseover = false;
    });

    setInterval(function () { if (!nn_mouseover) { nn_changePage(nn_currpage + 1); } }, 4000);

  },

  /*slider: function () {
  $(document).ready(function () {
  $('#slider').jcarousel({
  auto: 5,
  wrap: "both",
  scroll: 1,
  buttonNextHTML: '<div></div>',
  buttonPrevHTML: '<div></div>',
  initCallback: function (jc, state) {
  if (state == 'init') {
  jc.startAutoOrig = jc.startAuto;
  jc.startAuto = function () {
  if (!jc.paused) {
  jc.startAutoOrig();
  }
  }
  jc.pause = function () {
  jc.paused = true;
  jc.stopAuto();
  };
  jc.play = function () {
  jc.paused = false;
  jc.startAuto();
  };
  $('li.jcarousel-item').mouseover(function () {
  jc.pause();
  });
  $('li.jcarousel-item').mouseout(function () {
  jc.play();
  });
  }
  jc.play();
  }
  });
  });
  },*/

  search: function () {
    $("#search INPUT").keypress(function (e) {
      if (e.which == 13) {
        CMS.doSearch($(this).val(), $(this));
      }
    });

    $("#search A").click(function (e) {
      $inp = $(this).parent().find("INPUT").eq(0);
      CMS.doSearch($inp.val(), $inp);
      return false;
    });
  }


};

/* trigger when page is ready */
$(document).ready(function () {
  CMS.init();

  if ($().nivoSlider) {
    $('#slider').nivoSlider({
      effect: 'fade',
      controlNav: false,
      directionNavHide: true,
      pauseTime: 2000
    });
  }
  
  if ($().adGallery) {
  var galleries = $('.ad-gallery').adGallery({
    effect: 'fade',
    loader_image: '/js/plugins/gallery.1.2.4/loader.gif'
  });
}

$(".sponzori IMG")
        .mouseover(function () {
          var src = $(this).attr("src").match(/[^\.]+/) + "-boja.png";
          $(this).attr("src", src);
        })
        .mouseout(function () {
          var src = $(this).attr("src").replace("-boja", "");
          $(this).attr("src", src);
        });

});

$(function () {
  
  //$(".jclite").jCarouselLite({
  //        btnNext: ".next",
  //        btnPrev: ".prev"
  //});

  //simple menu
  //  $(window).bind("onload", function(){
  //    var menu = new menu.dd("menu");
  //	  menu.init("menu","menuhover");
  //  });

  //  $(".mainMenu > LI > UL").each(function(){
  //      console.debug($(this).parent());
  //      $(this).css("width", $(this).parent().width());
  //    });

  //$(".jclite UL").liScroll();

  //  $('ul.mainMenu').superfish({
  //    autoArrows: false,
  //    delay: 500,
  //    speed: 'fast',
  //    animation: { opacity: 'show', height: 'show' }
  //  });

});
