﻿Vehix$Gallery$Module = function(confg)
{
	this._container = $("#" + confg.Container);
	this.ManufacturerImages = confg.ManufacturerImages;
	this.ExteriorSpin = confg.ExteriorSpin;
	this.InteriorSpin = confg.InteriorSpin;
	this.AdInfo = { "BodyStyle": confg.BodyStyle, "Make": confg.Make, "Model": confg.Model };
	this._render();
}

Vehix$Gallery$Module.prototype =
{
	_render: function()
	{
		var num = 0;
		if (this.ManufacturerImages && this.ManufacturerImages.length)
			num = this.ManufacturerImages.length;
		var p = Vehix$Web$UI;
		var d = this._container;
		var tp = p.div("galleryModuleTitlePanel");
		var ps = p.span(null, null, "Photos:");
		var ml = p.anchor("javascript:void(0)", "Manufacturer", "galleryType");
		var el = p.anchor("javascript:void(0)", "360 Exterior", "galleryType");
		var il = p.anchor("javascript:void(0)", "360 Interior", "galleryTypeRight");
		var icd = p.div("right", "galleryModuleImageIndexPanel");
		var hid = p.div("galleryModuleHeroPanel");
		var hi = p.image("galleryModuleHeroImage");
		var pai = p.actionImage("Previous_68x18", "javascript:void(0)", false);
		var nai = p.actionImage("Next_68x18", "javascript:void(0)", false);
		var tic = p.div("galleryModuleThumbsContainer", "galleryModuleThumbsContainer");
		var tid = p.div("galleryModuleThumbsPanel", "galleryModuleThumbsPanel");
		var ntl = p.anchor("javascript:void(0)", "Next 6 of " + num + " images >>", "thumbsNext");
		var ptl = p.anchor("javascript:void(0)", "<< Previous 6 of " + num + " images", "thumbsPrevious");
		var mip = p.div("galleryModuleImagesPanel");
		var sp = p.div("galleryModuleSpinPanel");

		p.Hide(ptl);
		p.Hide(pai);
		p.Hide(mip);
		p.Hide(sp);

		p._ac(tp, [ps, ml, el, il, icd]);
		p._ac(hid, [hi, pai, nai]);

		$(ml).bind("click", this, this._manufacturerLinkClick);
		$(el).bind("click", this, this._exteriorLinkClick);
		$(il).bind("click", this, this._interiorLinkClick);
		$(ml).bind("gallerySourceChanged", this, this._titlePanelUpdate);
		$(el).bind("gallerySourceChanged", this, this._titlePanelUpdate);
		$(il).bind("gallerySourceChanged", this, this._titlePanelUpdate);
		$(ntl).bind("click", { "This": this, "links": [ptl, ntl] }, this._nextThumbsClick);
		$(ptl).bind("click", { "This": this, "links": [ptl, ntl] }, this._previousThumbsClick);
		$(pai).bind("click", { "This": this, "links": [ptl, ntl], "buttons": [pai, nai] }, this._previousButtonClick);
		$(nai).bind("click", { "This": this, "links": [ptl, ntl], "buttons": [pai, nai] }, this._nextButtonClick);

		this._mtl = ml;
		this._etl = el;
		this._itl = il;
		this._hi = hi;
		this._tic = tid;
		this._icd = icd;
		this._icp = 1;
		this._mip = mip;
		this._sp = sp;
		this._pai = pai;
		this._nai = nai;

		d.empty();
		d.append(tp);
		d.append(mip);
		d.append(sp);
		$(tic).append(tid);
		p._ac(mip, [hid, tic, ptl, ntl]);

		if (this.ManufacturerImages && this.ManufacturerImages.length)
			$(ml).trigger($.Event("click"));
		else
		{
			p.Hide(ml);
			if (this.ExteriorSpin && this.ExteriorSpin.Src)
				$(el).trigger($.Event("click"));
			else
			{
				p.Hide(el);
				if (this.InteriorSpin && this.InteriorSpin.Src)
					$(il).trigger($.Event("click"));
				else
					p.Hide(il);
			}
		}
	},

	_manufacturerLinkClick: function(e)
	{
		var p = Vehix$Web$UI;
		$(this).trigger($.Event("gallerySourceChanged"));
		if (e.data.ManufacturerImages && e.data.ManufacturerImages.length > 0)
		{
			var gal = e.data.ManufacturerImages;
			e.data._hi.src = gal[0].Src;
			var tid = e.data._tic;
			$(tid).empty();

			for (var i = 0; i < gal.length; i++)
			{
				var thumb = p.image("thumb", gal[i].Thumbnail);
				var tanch = p.anchor("javascript:void(0)", thumb)
				$(tanch).bind("click", { "This": e.data, "Src": gal[i].Src, "Pos": i + 1 }, e.data._thumbClick);
				$(tid).append(tanch);
			}
			$(e.data._sp).empty();
			p.Hide(e.data._sp);
			p.Show(e.data._mip);
		}
	},

	_exteriorLinkClick: function(e)
	{
		$(this).trigger($.Event("gallerySourceChanged"));
		var p = Vehix$Web$UI;
		var lg = e.data.ExteriorSpin.Src.indexOf("exterior_036_swf_480") > 0;
		var sf = p.iframe("exteriorspinframe", null, "no", e.data.ExteriorSpin.Src, lg ? 480 : 360, lg ? 360 : 240, "0");

		e.data._sp.style.padding = lg ? "20px 80px" : "20px 140px";
		p._replaceChildren(e.data._sp, sf);
		p.Hide(e.data._mip);
		p.Show(e.data._sp);
	},

	_interiorLinkClick: function(e)
	{
		$(this).trigger($.Event("gallerySourceChanged"));
		var p = Vehix$Web$UI;
		var lg = e.data.InteriorSpin.Src.indexOf("exterior_036_swf_480") > 0;
		var sf = p.iframe("interiorspinframe", null, "no", e.data.InteriorSpin.Src, lg ? 480 : 360, lg ? 360 : 240, "0");

		e.data._sp.style.padding = lg ? "20px 80px" : "20px 140px";
		p._replaceChildren(e.data._sp, sf);
		p.Hide(e.data._mip);
		p.Show(e.data._sp);
	},

	_titlePanelUpdate: function(e)
	{
		var t = e.data;
		$(t._etl).removeClass("active");
		$(t._itl).removeClass("active");
		$(t._mtl).removeClass("active");
		$(this).addClass("active");
		if ($(t._mtl).hasClass("active"))
			Vehix$Web$UI._replaceChildren(t._icd, Vehix$Web$UI.textNode("1 of " + t.ManufacturerImages.length + " images"));
		refreshAds(t.AdInfo.BodyStyle, t.AdInfo.Make, t.AdInfo.Model);
	},

	_updateHeroImage: function(img)
	{
		var pimg = Vehix$Web$UI.image("galleryModuleHeroImage", img.Src)
		var hi = this._hi;
		hi.src = img.Src
		$(pimg).load(function()
		{
			hi.src = $(this).attr("src");
		});
	},

	_thumbClick: function(e)
	{
		var t = e.data.This;
		var p = Vehix$Web$UI;
		t._updateHeroImage(e.data);
		p._replaceChildren(t._icd, p.textNode(e.data.Pos + " of " + t.ManufacturerImages.length + " images"));
		t._icp = e.data.Pos;
		p.Show(t._pai);
		p.Show(t._nai);
		if (e.data.Pos == 1)
			p.Hide(t._pai);
		else if (e.data.Pos == t.ManufacturerImages.length)
			p.Hide(t._nai);
	},

	_nextButtonClick: function(e)
	{
		var t = e.data.This;
		var p = Vehix$Web$UI;
		var pos = Number(t._icp);
		var lngth = t.ManufacturerImages.length;
		if (pos >= 0 && pos < lngth)
		{
			var img = t.ManufacturerImages[pos];
			t._updateHeroImage(img);
			$(t._icd).text(pos + 1 + " of " + lngth + " images");
			t._icp = pos + 1;
		}
		if (pos % 6 == 0)
		{
			refreshAds(t.AdInfo.BodyStyle, t.AdInfo.Make, t.AdInfo.Model);
		}
		p.Show(e.data.buttons[0]);
		if (t._icp >= t.ManufacturerImages.length)
			p.Hide(e.data.buttons[1]);
	},

	_previousButtonClick: function(e)
	{
		var t = e.data.This;
		var p = Vehix$Web$UI;
		var pos = Number(t._icp) - 2;
		if (pos >= 0 && pos < t.ManufacturerImages.length)
		{
			var img = t.ManufacturerImages[pos];
			t._updateHeroImage(img);
			p._replaceChildren(t._icd, p.textNode(pos + 1 + " of " + t.ManufacturerImages.length + " images"));
			t._icp = pos + 1;
		}
		if (t._icp % 6 == 0)
		{
			refreshAds(t.AdInfo.BodyStyle, t.AdInfo.Make, t.AdInfo.Model);
		}
		p.Show(e.data.buttons[1]);
		if (t._icp <= 1)
			p.Hide(e.data.buttons[0]);
	},

	_nextThumbsClick: function(e)
	{
		var t = e.data.This;
		var p = Vehix$Web$UI;
		t._tic.style.top = (t._tic.offsetTop - 75) + "px";
		p.Show(e.data.links[0]);
		if (t._tic.offsetTop == (0 - t._tic.offsetHeight) + 75)
			p.Hide(e.data.links[1]);
		else
			t._setNextLinkText(e);
		refreshAds(t.AdInfo.BodyStyle, t.AdInfo.Make, t.AdInfo.Model);
	},

	_previousThumbsClick: function(e)
	{
		var t = e.data.This;
		var p = Vehix$Web$UI;
		t._tic.style.top = (t._tic.offsetTop + 75) + "px";
		p.Show(e.data.links[1]);
		t._setNextLinkText(e);
		if (t._tic.offsetTop == 0)
			p.Hide(e.data.links[0]);
		refreshAds(t.AdInfo.BodyStyle, t.AdInfo.Make, t.AdInfo.Model);
	},

	_setNextLinkText: function(e)
	{
		var t = e.data.This;
		var p = Vehix$Web$UI;
		var lngth = t.ManufacturerImages.length;
		if (t._tic.offsetTop == (0 - t._tic.offsetHeight) + 150 && lngth % 6 > 0)
			p._replaceChildren(e.data.links[1], p.textNode("Next " + lngth % 6 + " of " + lngth + " images >>"));
		else
			p._replaceChildren(e.data.links[1], p.textNode("Next 6 of " + lngth + " images >>"));
	}
}