$(function() {
  // Actions
  $(".head-pseudo li a").each(function(i) {
    $(this).click(function() {
      window._c_fbox = new FBox({
          animation: $.browser.msie ? false : true,
          imagesList: false,
          imagesListWidth: 80,
          showOverlay: true,
          overlayOpacity: 0.7,
          useLoaderAnim: true,
          loaderParams: {
              animatioLength: 400,
              animationStep: 50,
              animationTime: 100
          },
          navigationButtons: true,
          navigationButtonsHover: true,
          constraints: {
              minWidth: 730,
              minHeight: 430,
              maxWidth: 730,
              maxHeight: 465
          },
          ajax: {
              url: "/ajax/",
              iface: "special_product_cat",
              object_id: this.parentNode.parentNode.id
          }
      });
      window._c_fbox.htmlIndex = i;
      window._c_fbox.show();
      return false;
    });
  });

  // Gallery animation
  $(".content-gallery a").hover(function() {
      $("ins",this).stop(true,true).animate({opacity: 0},200);
    }, function() {
      $("ins",this).stop(true,true).animate({opacity: 0.4},200);
    });

  if ($.browser.msie) {
    // Transparency
    $(".transparent").each(function() {
      this.style.filter = "alpha(opacity="+this.currentStyle.opacity*100+")";
    });
    // Left part fix
    var lp = $("#left-part");
    if (lp.html() == "") {
      lp.hide();
    }
    // Main menu fix
      $("#head .head-menu .head-menu-item a .item-picture").click(function() {
        document.location.href = this.parentNode.href;
      });

    if ($.browser.version == 6) {
      // PNG fix
      pngFix();
      // Background fix
      var bgs = $("#bgs");
      bgs.height(bgs.parent().height());
    }
  }

  // Head menu animation
  $(".head-menu .head-menu-item a").hover(
    function() {
      var item = $(this).parent();
      item.addClass("active");
      $(".item-picture img",item).stop(true,true).fadeIn(400);
    },
    function() {
      var item = $(this).parent();
      item.removeClass("active");
      $(".item-picture img",item).stop(true,true).fadeOut(400);
    }
  );
});

var pngFix = function() {
  $(".png").each(function() {
    if (this.tagName == "IMG") {
      this.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +
                        this.src +
                        '", sizingMethod="crop")';
      this.src = '/i/null.gif';
    } else {
      var method = (this.currentStyle.backgroundRepeat == 'no-repeat') ? 'crop' : 'scale';
      this.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +
                          this.currentStyle.backgroundImage.replace(/.*?url\(\"?(.*?)\"?\).*$/,"$1") +
                          '", sizingMethod="' + method + '")';
      this.style.backgroundImage = 'none';
    }
  });
}

var Clock = function(selector,offset) {
  this.container = $(selector);
  this.zoneOffset = offset || 6;
  this.setup();
}
Clock.prototype = {
  setup: function() {
    var self = this;
    self.showTime();
    self.timer = window.setInterval(function() { self.showTime(); }, 500);
    self.even = true;
  },
  showTime: function() {
    var self = this;
    var local = new Date();
    var date = new Date(local.valueOf() + (this.zoneOffset * 1000 * 60 * 60));
    var h = date.getUTCHours();
    var m = date.getUTCMinutes();
    if (h < 10)
      h = "0"+h;
    if (m < 10)
      m = "0"+m;
    self.container.html(h+"<span>:</span>"+m).removeClass();
    if (self.even)
      self.container.addClass("colon");
    self.even = !self.even;
  }
}

var Scroll = function(obj) {
  this.container = $(".gallery-thumbs",obj);
  this.arrL = $(".arrow-left",obj);
  this.arrR = $(".arrow-right",obj);
  this.delta = 94;
  this.duration = 400;
  if (this.container.get(0)) {
    this.init();
  }
}
Scroll.prototype = {
  init: function() {
    var self = this;
    self.arrL.click(function() {self.scroll(-1)});
    self.arrR.click(function() {self.scroll(1)});
    self.checkScrolls();
  },
  scroll: function(k) {
    var self = this;
    var el = self.container.get(0);
    var newValue = el.scrollLeft + self.delta*k;
    var maxValue = el.scrollWidth - el.clientWidth;
    if (newValue <= 0) {
      newValue = 0;
      self.arrL.hide();
    } else if (newValue >= maxValue) {
      newValue = maxValue;
      self.arrR.hide();
    }
    self.container.animate({scrollLeft: newValue},{duration: self.duration, complete: function() {self.checkScrolls()}});
  },
  checkScrolls: function() {
    var self = this;
    var el = self.container.get(0);
    var curValue = el.scrollLeft;
    var maxValue = el.scrollWidth - el.clientWidth;
    if (curValue == 0) {
      self.arrL.hide();
    } else {
      self.arrL.show();
    }
    if (curValue == maxValue) {
      self.arrR.hide();
    } else {
      self.arrR.show();
    }
  }
}
