/// <reference path="../../js/base.js" />
;;;(function ()
{
	Letao.Page.Index = {
		RollImages: (function ()
		{
			var __ImageArray__ = {};
			return {
				Set: function (index, arr)
				{
					__ImageArray__[index] = arr;
				},
				Get: function (index)
				{
					return __ImageArray__[index] || [];
				}
			}
		})(),
		BindBrandTab: function (obj)
		{
			obj.each(function (i)
			{
				var _this = this;
				$(_this).find("a").click(function ()
				{
					if ($(_this).hasClass("chos"))
						return;

					obj.removeClass("chos").eq(i).addClass("chos");
					$("#ltbrandcontent ul").hide()

					if ($("#ltbrandcontent_" + i).length == 1)
					{
						$("#ltbrandcontent_" + i).show();
						$("#ltbrandcontent").css("background-position", "-1000px -1000px");
					}
					else
					{
						$("#ltbrandcontent").css("background-position", "50% 50%");
						var callback = function (al_return)
						{
							if (al_return == null || al_return[0].toString() != "SUCCESS")
							{
								return false;
							}
							else
							{
								var _BrandContent = al_return[1];
								$("#ltbrandcontent").append(_BrandContent).css("background-position", "-1000px -1000px");
								$("#ltbrandcontent_" + i + " li").each(function ()
								{
									var $img = $(this).find("img");
									Letao.UI.LazyLoad.Add($img, "Image", $img.attr("lazyload"));
								});
							}
						};
						$.LT_AJAX("get_brand_content", i + "", callback, "user_letao_index");
					}
				});
			})
		}
	};

	var rollImage = function (imgdata, outer, hover, uuid, fnpre, fnload)
	{
		if (!rollImage.init)
		{
			//如果还没有执行过函数，就设定init方法，构造所有需要用到的函数
			rollImage.init = (function ()
			{
				var F = function (imgdata, outer, hover, uuid, fnPreload, fnLoad)
				{
					//起码得有imagedata,outer,hover三个定义参数
					if (arguments.callee.length < 3)
					{
						return null;
					}
					//如果没有唯一索引，就给一个随机值
					if (typeof uuid != "string" && typeof uuid != "number")
					{
						uuid = Math.floor(new Date().getTime() / 1000);
					}
					//特权函数，取得UUID的值
					this.getUUID = function ()
					{
						return uuid;
					}
					this.images = Letao.Page.Index.RollImages.Get(imgdata) || [];
					this.outer = outer;
					this.hover = hover;
					this.fnPreload = fnPreload || null;
					this.fnLoad = fnLoad || function () { };
					this._index = 0;
					this._loaded = {};
					this._hovers = {};
					this._timer = null; //计时器
					this._isauto = 0; //是否自动轮播
					this._isstop = 0; //是否停止
				}

				F.prototype.loadImage = function (index)
				{
					//如果已经读取过这个图片了，就返回这个图片
					if (this._loaded.hasOwnProperty(index) && this._loaded.index != "preload")
					{
						if (typeof this._loaded[index] != "undefined" && this._loaded[index].length != 0 && typeof this._loaded[index].fadeIn != "undefined")
						{
							this._loaded[index].fadeIn(1000, this.fnLoad);
						}
					}
					else
					{
						var imgid = "rollimage_" + this.getUUID() + "_" + index, _this = this;
						this.outer.append("<img id=\"" + imgid + "\" src=\"http://i.ltimg.cn/images/e.gif\" style=\"display:none\" />");
						var imgobj = $("#" + imgid);
						//将图片对象添加到loaded对象中，并设置标志位表示正在准备读取图片
						this._loaded[index] = "preload";
						var img = new Image();
						img.src = this.images[index];
						this.stoproll();
						if (img.complete)
						{
							if (this._isauto && this._isstop)
							{
								this.autoroll();
							}
							imgobj.attr("src", img.src);
							this._loaded[index] = imgobj;
							if (this._index == index)
							{
								this.fnLoad(this);
								imgobj.fadeIn(1000);
							}
						}
						else
						{
							img.onload = function ()
							{
								if (_this._isauto && _this._isstop)
								{
									_this.autoroll();
								}
								imgobj.attr("src", img.src);
								_this._loaded[index] = imgobj;
								if (_this._index == index)
								{
									_this.fnLoad(_this);
									imgobj.fadeIn(1000);
								}
							}
						}
					}
				}

				F.prototype.hideImage = function ()
				{
					//停掉当前的图像动画效果并且隐藏掉
					var imgid = "rollimage_" + this.getUUID() + "_" + this._index;
					$("#" + imgid).stop(true, true).fadeOut(500);
				}

				F.prototype.initFirstImage = function (func)
				{
					var images = this.outer.find("img");
					if (images.length == 1)
					{
						images.attr("id", "rollimage_" + this.getUUID() + "_0");
						this._loaded["0"] = images;
					}
					if (typeof func == "function")
					{
						func();
					}
				}

				F.prototype.roll = function (index)
				{
					var _this = this;
					//在hover的时候执行Preload事件
					_this.fnPreload && _this.fnPreload(_this, _this._hovers[index], index);
					//把前一个图片hide掉
					_this.hideImage();
					//设置当前的图片索引
					_this._index = index;
					_this.loadImage(index);
				}

				//自动轮播
				F.prototype.autoroll = function ()
				{
					var _this = this;

					if (_this._timer != null)
						return false;

					_this._timer = setInterval(function ()
					{
						if (!_this._isstop)
						{
							_this.stoproll();
							return false;
						}

						var index = _this._index + 1;
						if (index >= _this.hover.length)
							index = 0;
						_this.roll(index);
					}, 7000);
				};

				//停止轮播
				F.prototype.stoproll = function ()
				{
					var _this = this;
					if (_this._timer == null)
						return false;

					window.clearInterval(_this._timer);
					_this._timer = null;
				};

				F.prototype.run = function (isauto)
				{
					var _this = this;
					_this._isauto = isauto || _this._auto;
					this.hover.each(function (i)
					{
						var index = i;
						var hovertimer = null;
						_this._hovers[index] = $(this);
						$(this).hover(function ()
						{
							if (_this._index != index)
							{
								hovertimer = setTimeout(function ()
								{
									_this.roll(index);
								}, 200);
							}
						}, function ()
						{
							window.clearTimeout(hovertimer);
							hovertimer = null;
						});
					});

					if (isauto)
					{
						this.outer.parent().hover(function ()
						{
							_this._isstop = 0;
							_this.stoproll();
						}, function ()
						{
							_this._isstop = 1;
							_this.autoroll();
						});

						_this._isstop = 1;
						_this.autoroll();
					}
				}
				return F;
			})();
		}

		return new rollImage.init(imgdata, outer, hover, uuid, fnpre, fnload);
	}

	$(function ()
	{
		$("#ltnoticeinfo span").each(function (i)
		{
			$(this).hover(function ()
			{
				if (!$(this).hasClass("chos"))
				{
					$("#ltnoticeinfo span").removeClass("chos");
					$(this).addClass("chos");
					$("#ltnoticeinfo ul").hide().eq(i).show();
				}
			}, function () { });
		});

		var toprollfnpre = function ()
		{
			var o = arguments[0],
			    hover = arguments[1],
			    index = arguments[2];
			o.outer.attr("href", hover.attr("href"));
			o._hovers[o._index].removeClass("chos").css("opacity", 0.7);
			hover.addClass("chos").css("opacity", 1);
			if (typeof o._loaded[index] == "undefined") { o.outer.parent().css("background-position", "50% 50%"); }
		};

		var arearollfnpre = function ()
		{
			var o = arguments[0],
			    hover = arguments[1],
			    index = arguments[2];
			o.outer.attr("href", hover.attr("href"));
			o._hovers[o._index].removeClass("chos");
			hover.addClass("chos");
			if (typeof o._loaded[index] == "undefined") { o.outer.parent().css("background-position", "50% 50%"); }
		};

		var rollload = function ()
		{
			if (typeof arguments[0] != "undefined")
			{
				var o = arguments[0];
				o.outer.parent().css("background-position", "-1000px -1000px");
			}
		};

		Letao.UI.LazyLoad.Add($("#ltimageroll"), "Function", "IndexTopRollImage", function ()
		{
			var obj = $("#ltimageroll");
			var toproll = new rollImage("topRollImage", obj.find("a:first"), obj.find("div a"), "top", toprollfnpre, rollload);
			toproll.initFirstImage(function ()
			{
				obj.find("div a").css("opacity", 0.7);
				obj.find("div a:first").addClass("chos").css("opacity", 1);
			});
			toproll.run(1);
		});

		Letao.UI.LazyLoad.Add($("#IndexRollImageArea0"), "Function", "IndexRollImageArea0", function ()
		{
			var obj = $("#IndexRollImageArea0");
			var arearoll0 = new rollImage("IndexRollImageArea0", obj.find("a:first"), obj.find("div a"), "_0_", arearollfnpre, rollload);
			arearoll0.initFirstImage(function () { obj.find("div a:first").addClass("chos"); });
			arearoll0.run(1);
		});

		Letao.UI.LazyLoad.Add($("#IndexRollImageArea1"), "Function", "IndexRollImageArea1", function ()
		{
			var obj = $("#IndexRollImageArea1");
			var arearoll1 = new rollImage("IndexRollImageArea1", obj.find("a:first"), obj.find("div a"), "_1_", arearollfnpre, rollload);
			arearoll1.initFirstImage(function () { obj.find("div a:first").addClass("chos"); });
			arearoll1.run(1);
		});

		Letao.UI.LazyLoad.Add($("#IndexRollImageArea2"), "Function", "IndexRollImageArea2", function ()
		{
			var obj = $("#IndexRollImageArea2");
			var arearoll2 = new rollImage("IndexRollImageArea2", obj.find("a:first"), obj.find("div a"), "_2_", arearollfnpre, rollload);
			arearoll2.initFirstImage(function () { obj.find("div a:first").addClass("chos"); });
			arearoll2.run(1);
		});

		Letao.UI.LazyLoad.Add($("#IndexRollImageArea3"), "Function", "IndexRollImageArea3", function ()
		{
			var obj = $("#IndexRollImageArea3");
			var arearoll3 = new rollImage("IndexRollImageArea3", obj.find("a:first"), obj.find("div a"), "_3_", arearollfnpre, rollload);
			arearoll3.initFirstImage(function () { obj.find("div a:first").addClass("chos"); });
			arearoll3.run(1);
		});

		Letao.UI.LazyLoad.Add($("#IndexRollImageArea4"), "Function", "IndexRollImageArea4", function ()
		{
			var obj = $("#IndexRollImageArea4");
			var arearoll4 = new rollImage("IndexRollImageArea4", obj.find("a:first"), obj.find("div a"), "_4_", arearollfnpre, rollload);
			arearoll4.initFirstImage(function () { obj.find("div a:first").addClass("chos"); });
			arearoll4.run(1);
		});

		Letao.Page.Index.BindBrandTab($("#ltbrandtab ul li"));

		Letao.Page.HeaderFooter.BindLogo.isRun = false;
	});
})();

(function ()
{
	var Letao = this.Letao || {};
	Letao.PI = Letao.PI || {};
	Letao.PI.SlipBanner = {
		Elems: {},
		Struct: function ()
		{
			this.ID = null;
			this.HTML = null;
			this.BindTo = null;
			this.Callback = null;
		},
		Add: function (id, html, bindto, callback)
		{
			if (typeof id != "string" || $.trim(id) == "")
			{
				return this;
			}
			if (typeof bindto == "undefined" || bindto == null || bindto.length == 0)
			{
				bindto = $("body");
			}
			if (typeof callback != "function")
			{
				callback = null;
			}
			var o = new Letao.PI.SlipBanner.Struct();
			o.ID = id;
			o.HTML = html;
			o.BindTo = bindto;
			o.Callback = callback;
			o.ElemID = "ltSlipBanner_" + id;

			Letao.PI.SlipBanner.Elems[id] = o;

			return this;
		},
		Run: function (id)
		{
			if (typeof Letao.PI.SlipBanner.Run.Call == "undefined")
			{
				Letao.PI.SlipBanner.Run.Call = function (o)
				{
					var bindTo = o.BindTo;
					if (o && o.HTML)
					{
						bindTo
						.after(
							$("<div>")
							.attr("id", o.ElemID)
							.css({
								"display": "none",
								"position": "relative",
								"z-index":"999",
								"zoom":"1"
							})
							.append(o.HTML)
						)
					}
					o["Elem"] = $("#" + o.ElemID);
					
					setTimeout(function(){
						$("#" + o.ElemID).slideDown(1500, function(){
							o.Callback && o.Callback();
						});
					},2000);
					
					return o;
				}
			}
			var es = Letao.PI.SlipBanner.Elems;
			var o;
			if (typeof id != "string" || $.trim(id) == "")
			{
				for (var i in es)
				{
					o = es[i];
					es[i] = Letao.PI.SlipBanner.Run.Call(o);
				}
			}
			else
			{
				o = es[id];
				es[id] = Letao.PI.SlipBanner.Run.Call(o);
			}
		},
		Close: function (id, func)
		{
			if (typeof Letao.PI.SlipBanner.Close.Call == "undefined")
			{
				Letao.PI.SlipBanner.Close.Call = function (o, func)
				{
					if (typeof func != "function")
					{
						func = null
					}
					if (o && o.Elem && o.Elem.length > 0)
					{
						o.Elem.slideUp(1200, func);
					}
					return true;
				}
			}
			var es = Letao.PI.SlipBanner.Elems;
			var o;
			if (typeof id != "string" || $.trim(id) == "")
			{
				for (var i in es)
				{
					o = es[i];
					Letao.PI.SlipBanner.Close.Call(o, func);
				}
			}
			else
			{
				o = es[id];
				Letao.PI.SlipBanner.Close.Call(o, func);
			}
		}
	};
})();


