var ShowGoods = {
  MODEL_MODE : false
  ,
  _modelViewNumber : 1
  ,
  _modelColorNumber : 1
  ,
  items : []
  ,
  activeIndex : -1
  ,
  _pageSize : 13
  ,
  customStartIndex : -1
  ,
  collectionPhoto : ''
  ,
  _showTimer : null
  ,
  showTimeout : 6000
  ,
  firstShowTimeout : 6000
  ,

  MouseOver : function(index)
  {
    var itemIndex = this.GetRangeIndex() + index;
    var item = this.items[itemIndex];
    if (!item) return;
    var src = item.ListPhoto1;
    if (item.ListPhoto2) src = item.ListPhoto2;
    if (this.MODEL_MODE) {
      var numSrc = "/i/nums/" + (itemIndex + 1) + "c.gif";
      $("#shwg" + index).attr("src", numSrc).css("background", "url(" + src + ") left top no-repeat");
    } else {
      $("#shwg" + index).attr("src", src);
    }
  }
  ,
  MouseOut : function(index)
  {
    var itemIndex = this.GetRangeIndex() + index;
    var item = this.items[itemIndex];
    if (!item) return;
    var src = item.ListPhoto1;
    if (itemIndex == this.activeIndex && item.ListPhoto2) src = item.ListPhoto2;
    
    if (this.MODEL_MODE) {
      var numSrc = "/i/nums/" + (itemIndex + 1) + ".gif";
      $("#shwg" + index).attr("src", numSrc).css("background", "url(" + src + ") left top no-repeat");
    } else {
      $("#shwg" + index).attr("src", src);
    }
    
  }
  ,
  Draw : function()
  {
    rangeIndex = this.GetRangeIndex();
    endIndex = this.GetEndRangeIndex();
    for (i = 0; i < this._pageSize; i++) {
      id = "shwg" + i;
      src = "/i/p.gif";
      alt = "";
      isItem = false;
      if (this.items.length > i + rangeIndex) {
        var realIndex = i + rangeIndex;
        var item = this.items[realIndex];
        src = item.ListPhoto1;
        if (i == this.activeIndex-rangeIndex && item.ListPhoto2) {
          src = item.ListPhoto2;
        }
        alt = item.Note;
        isItem = true;
      }
      var modelNumberSrc = "/i/nums/" + (realIndex+1) + ".gif";

      if (this.MODEL_MODE) {      
        $("#" + id).attr("src", modelNumberSrc).attr("alt", alt).attr("title", alt).css("background", "url(" + src + ") left top no-repeat");
      } else {
        $("#" + id).attr("src", src).attr("alt", alt).attr("title", alt);
      }
      if (isItem) {
        $("#" + id).css("cursor", "pointer");
      } else {
        $("#" + id).css("cursor", "normal");
      }
    }
    this.DrawArrows();
    this.DrawPhoto();
  }
  ,
  NextPage : function()
  {
    this.customStartIndex = this.CalcRangeIndex(this.GetRangeIndex() + this._pageSize, this._pageSize, this.items.length);
    this.Draw();
  }
  ,
  PrevPage : function()
  {
    this.customStartIndex = this.CalcRangeIndex(this.GetRangeIndex() - this._pageSize, this._pageSize, this.items.length);
    this.Draw();
  }
  ,
  DrawPhoto : function(altText)
  {
    if (!altText) altText = '';
    if (this.activeIndex < 0) {
      $("#shwPic").css("background-image", "url('" + this.collectionPhoto +"')");
    } else {
      var item = this.items[this.activeIndex];
      $("#shwPic").css("background-image", "url('" + item.MainPhoto +"')");
    }
    $("#shwPic").attr("title", altText);
  }
  ,
  Select : function(num)
  {
    if (!this.MODEL_MODE) this.StopShow();
    itemIndex = num + this.GetRangeIndex();
    this.customStartIndex = -1;
    this.activeIndex = itemIndex;
    this.Draw();
    if (this.MODEL_MODE) this.LoadSelectedModel();
  }
  ,
  LoadSelectedModel : function()
  {
    if (this.activeIndex < 0 || this.activeIndex >= this.items.length) return;
    var item = this.items[this.activeIndex];
    $("#modelPlace").load("?ax=model&m=" + item.ID);
  }
  ,
  DrawArrows : function()
  {
    rangeIndex = this.GetRangeIndex();
    endIndex = this.GetEndRangeIndex();
    if (rangeIndex > 0) { $("#catLeft").show(); } else { $("#catLeft").hide(); }
    if (endIndex+1 < this.items.length) { $("#catRight").show(); } else { $("#catRight").hide(); }
  }
  ,
  AddItem : function(id, mainPhoto, photo1, photo2, note, isActive) 
  {
    var item = new ShowGoodInfo();
    item.ID = id;
    item.ListPhoto1 = photo1;
    item.ListPhoto2 = photo2;
    item.MainPhoto = mainPhoto;
    item.Note = note;
    this.items[this.items.length] = item;
    item.Preload();
    if (isActive) this.activeIndex = this.items.length - 1;
  }
  ,
  GetRangeIndex : function()
  {
    rangeIndex = this.CalcRangeIndex(this.activeIndex, this._pageSize, this.items.length);
    if (this.customStartIndex >= 0) rangeIndex = this.customStartIndex;
    return rangeIndex;
  }
  ,
  GetEndRangeIndex : function()
  {
    endIndex = this.GetRangeIndex() + this._pageSize - 1;
    if (this.items.length <= endIndex) endIndex = this.items.length - 1;
    return endIndex;
  }
  ,
  CalcRangeIndex : function(index, pageSize, itemsCount)
  {
    if (index < 0) return 0;
    rangeIndex = index - Math.floor(pageSize / 2);
    //alert(rangeIndex + "|" + index);
    if (rangeIndex < 0) rangeIndex = 0;
    endIndex = rangeIndex + pageSize - 1;
    if (rangeIndex > 0 && itemsCount <= endIndex) {
      rangeIndex = itemsCount - pageSize;
      if (rangeIndex < 0) rangeIndex = 0;
    }
    return rangeIndex;
  }
  ,
  StopShow : function()
  {
    if (this._showTimer) {
      clearInterval(this._showTimer);
    }
    this._showTimer = null;
  }
  ,
  RunShow : function()
  {
    this.StopShow();
    this.customRangeIndex = -1;
    this.activeIndex = 0;
    this.Draw();
    this._showTimer = setInterval(function() { ShowGoods.ShowIteration() }, this.showTimeout);
  }
  ,
  ShowIteration : function()
  {
    if (this.activeIndex+1 < this.items.length) {
      this.activeIndex++;
    } else {
      this.activeIndex = -1;
      this.StopShow();
    }
    this.Draw();
  }
  ,
  InitRunShow : function()
  {
    this.StopShow();
    this._showTimer = setInterval(function() { ShowGoods.RunShow() }, this.firstShowTimeout);
  }
  ,
  SelectModelView : function(url, number, item)
  {
    this._modelViewNumber = number;
    this._modelColorNumber = -1;
    this.ResetModelPhotoSelectors();
    $(item).css('color', '#E60101');
    $("#modelPhoto").css("background-image", "url(" + url + ")");
  }
  ,
  OverModelViewLink : function(item, number)
  {
    $(item).css('color', '#E60101');
  }
  ,
  OutModelViewLink : function(item, number)
  {
    if (number != this._modelViewNumber) $(item).css('color', '#636363');
  }
  ,
  ResetModelPhotoSelectors : function()
  {
    $("#modelViews span").css('color', '#636363');
    $("#modelColors div.modelColorText span").css('color', '#636363');
  }
  ,
  SelectModelColor : function(url, number, item)
  {
    this._modelViewNumber = -1;
    this._modelColorNumber = number;
    this.ResetModelPhotoSelectors();
    $(item).css('color', '#E60101');
    $("#modelPhoto").css("background-image", "url(" + url + ")");
  }
  ,
  OverModelColor : function(item, number)
  {
    $(item).css('color', '#E60101');
  }
  ,
  OutModelColor : function(item, number)
  {
    if (number != this._modelColorNumber) $(item).css('color', '#636363');
  }
}

function ShowGoodInfo()
{
  this.MainPhoto = '';
  this.ListPhoto1 = '';
  this.ListPhoto2 = '';
  this.ID = 0;
  this.Note = '';
  
  this._img1 = null;
  this._img2 = null;
  this._img0 = null;
  
  this.Preload = function() 
  {
    this._img0 = new Image();
    this._img0.src = this.MainPhoto;
    this._img1 = new Image();
    this._img1.src = this.ListPhoto1;
    this._img2 = new Image();
    this._img2.src = this.ListPhoto2;
  }
}

var OrderForm = {
  ValidateInput : function(inp)
  {
    var val = parseInt(inp.value);
    if (!(val > 0)) val = '';
    inp.value = val;
  }
}

var ModelOrder = {
	Initialize : function()
	{
		$("input[class='ordermodel']").change(function() {
			var j = $(this);
			var count = parseInt(j.val());
			if (isNaN(count) || count <= 0) {
				j.val("");
				count = 0;
			}
			var rel = j.attr("rel");
			var pos = rel.indexOf("|");
			var idModel = parseInt(rel.substr(0, pos));
			var pos2 = rel.indexOf("|", pos + 1);
			var idColor = rel.substr(pos + 1, pos2 - pos - 1);
			var idSize = parseInt(rel.substr(pos2 + 1));
			ModelOrder.Send(idModel, idColor, idSize, count, j);
		});
	}
	,
	Send : function(idModel, idColor, idSize, count, input) 
	{
		$.getJSON("?ax=basket", {m: idModel, c: idColor, s: idSize, cnt: count}, function(json) {
			if (typeof(json.count) != "undefined") {
				input.hide().fadeOut("normal").fadeIn("slow");
				if (json.count == 0) {
					$("#basketCount").hide();
				} else {
//					$("#basketStat").fadeOut();
					$("#basketCount").html(": " + json.count + " " + json.ending).show();
					$("#basketCount").show();
//					$("#basketStat").fadeIn();
				}
			}
		}
		);
	}
}
