(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

var imgFrame = function (target_elem) {
    $(target_elem).wrapInner("<div class=\"lt\"><div class=\"rt\"><div class=\"rb\"><div class=\"lb\"><div class=\"top\"><div class=\"bottom\"><div class=\"left\"><div class=\"right\"><div class=\"inner\"></div></div></div></div></div></div></div></div></div>");
    
    jQuery.preLoadImages(
        "/css/img_catalogue/tab_cnt_lt.png",
        "/css/img_catalogue/tab_cnt_rt.png",
        "/css/img_catalogue/tab_cnt_rb.png",
        "/css/img_catalogue/tab_cnt_lb.png",
        "/css/img_catalogue/tab_cnt_top.png",
        "/css/img_catalogue/tab_cnt_bottom.png",
        "/css/img_catalogue/tab_cnt_left.png",
        "/css/img_catalogue/tab_cnt_right.png"
    );

    if (navigator.userAgent.indexOf("MSIE 6") > 0) {
        DD_belatedPNG.fix('.lt,.rt,.rb,.lb,.top,.bottom,.left,.right');
        /* string argument can be any CSS selector */
        /* .png_bg example is unnecessary */
        /* change it to what suits you! */
    }
}

var setTabMenu = function () {
    imgFrame("div.img_frame");
    
    $("#nav_global .tab").click(function () {
        if ($(this).parent().hasClass("selected")) {
            $(this).parent().removeClass("selected");
            $("#header").css("position","static");
        } else {
            $("#header").css("position","relative");
            $(this).parent().siblings().removeClass("selected");
            $(this).parent().addClass("selected");
        }
    });
    
    $("body").click(function() {
            $('#nav_global .tab_unit').removeClass("selected");
            $("#header").css("position","static");
    });
    $('#nav_global .tab_unit').click(function(event){
        event.stopPropagation();
    });
}

var setGallery = function (node) {
    var thumbs = $(node).children("ul.thumbs").children("li");
    var photos = $(node).children("ul.photos").children("li");
    
    thumbs.each(function ($i) {
		$(this).hover(
			function() {
				$(this).children("img:first").css("border-color","#ff9191");
			},
			function() {
				$(this).children("img:first").css("border-color","#cccccc");
			}
		);
        $(this).click(function () {
          if($(photos[$i]).hasClass("selected")) {
              return;
          } else {
              photos.removeClass("selected");
              $(photos[$i]).addClass("selected");
          }
        });
    });
  
}

$(document).ready(function(){
    
    setTabMenu();
    setGallery("#gallery");
    
});
