﻿var Vehix$Galleries$Media = function()
{
	this.ActiveGallery = null;
	this.SlideInterval = 0;
	this.LockedImageSrc = "";
	this.ImageInterval = 0;
	this.Image = { Width: 0, Height: 0, Orientation: "Landscape", Magnify: false };
	this.CurrentImage = new Image();
	this.Position = 0;
}

Vehix$Galleries$Media.prototype =
{
	Init: function() {
		var obj = this;
		var sscb = function(e) {
			obj.LaunchSlideShow();
		}
		var sspcb = function(e) {
			obj.SetSlideShowPosition(e);
		}
		var cgscb = function(e) {
			obj.ChangeGallerySource(e.data.gal);
		}
		$(".pvGallery .thumbsStrip > .topLinks > a.dealer").bind("click", { gal: "d" }, cgscb);
		$(".pvGallery .thumbsStrip > .topLinks > a.manufacturer").bind("click", { gal: "m" }, cgscb);
		$(".pvGallery .thumbsStrip > .topLinks > a.ext360").bind("click", { gal: "e" }, cgscb);
		$(".pvGallery .thumbsStrip > .topLinks > a.int360").bind("click", { gal: "i" }, cgscb);
		$(".pvGallery > .heroImage > .actionIcons > a.galleryMagnify").click(function(event) {
			$(".fullHeroImage > img").attr("src", $(".heroImage .galleryImage").attr("src"));
			$(".magnifiedParent").toggleClass("hidden");
			$(".detailsContent .leftContent .panelBody .bodyLeft").addClass("hidden");
			$("div.magnifiedParent div.titleBar > div.photoCounter").empty();
			$("div.pvGallery div.slideShowNavigationParent").children().addClass("hidden");
		});
		$(".pvGallery > .heroImage > .actionIcons > a.gallerySlideShow").click(sscb);
		$(".pvGallery .galleryBtnPrevious").bind("click", { posint: -1 }, sspcb);
		$(".pvGallery .galleryBtnNext").bind("click", { posint: 1 }, sspcb);
		$(".pvGallery .titleBar > .closeLink > a.galleryClose_14x14").click(function(event) {
			obj.ClearGallerySlideShow();
			$(".magnifiedParent").toggleClass("hidden");
			$(".detailsContent .leftContent .panelBody .bodyLeft").removeClass("hidden");
		});
		$("div.prevGalleryThumbLinks > a.thumbsAnchor").click(function(event) {
			obj.PrevImageHandler(event);
		});
		$("div.nextGalleryThumbLinks > a.thumbsAnchor").click(function(event) {
			obj.NextImageHandler(event);
		});
		if (typeof (Vehix$Galleries$DealerImages) != "undefined" && Vehix$Galleries$DealerImages.length)
			this.ChangeGallerySource('d');
		else if (typeof (Vehix$Galleries$ManufacturerImages) != "undefined" && Vehix$Galleries$ManufacturerImages.length)
			this.ChangeGallerySource('m');
	},

	anchor: function(href, text, style, containshtml, target) {
		var a = document.createElement("a");
		if (typeof (href) == "string" && href != "javascript:null")
			a.href = href;
		else if (href == "javascript:null")
			a.href = "javascript:void(0)";
		if (typeof (style) == "string")
			a.className = style;
		if (target)
			a.target = target;
		if (typeof (text) == "string") {
			containshtml ? a.appendChild(this.textNode(text)) : a.innerHTML = text;
		}
		else if (text && text.src)
			a.appendChild(text);
		return a;
	},

	image: function(style, src, alt) {
		var img = new Image();
		if (src && src.length) {
			img.src = src;
		}
		else {
			img.src = "/Content/Shared/Img/s.gif";
		}
		if (alt)
			img.alt = alt;
		if (style) img.className = style;
		return img;
	},

	iframe: function(name, style, scroll, src, width, height, brdr) {
		var f = document.createElement("iframe");
		if (style) f.className = style;
		if (scroll) f.scrolling = scroll;
		if (src) f.src = src;
		if (brdr) f.frameBorder = brdr;
		if (width) f.width = width;
		if (height) f.height = height;
		if (name) {
			f.id = name;
			f.name = name;
		}
		return f;
	},

	ChangeGallerySource: function(s) {
		var p = this;
		var actg = null;
		var vsgal = this;
		var populateGallery = function() {
			vsgal.ActiveGallery = actg;
			$("div.pvGallery > div.heroImage > div.spinIframeDiv").empty();
			$("div.pvGallery > div.heroImage > div.spinIframeDiv").css("display", "none");
			var obj = $("div.thumbsStripGallery > div.galleryThumbs > div.thumbsPanel");
			obj.empty();
			obj.css("margin-top", "0");
			obj.css("display", "block");
			if (actg.length > 9) {
				$("div.thumbsStripGallery > div.galleryThumbs").addClass("height");
				$("div.bottomLinks > div.prevGalleryThumbLinks a.thumbsAnchor").addClass("hide");
				$("div.bottomLinks > div.prevGalleryThumbLinks a.thumbsAnchor").html("<< Previous 9 of " + actg.length + " images");
				$("div.bottomLinks > div.nextGalleryThumbLinks a.thumbsAnchor").removeClass("hide");
				$("div.bottomLinks > div.nextGalleryThumbLinks a.thumbsAnchor").html("Next " + Math.min(9, actg.length - 9) + " of " + actg.length + " images >>");
			}
			else {
				$("div.bottomLinks > div.prevGalleryThumbLinks a.thumbsAnchor").addClass("hide");
				$("div.bottomLinks > div.nextGalleryThumbLinks a.thumbsAnchor").addClass("hide");
			}
			for (var i = 0; i < actg.length; i++) {
				var cicb = function(e) {
					vsgal.ChangeImageSource(e.data.img);
				}
				var ricb = function(e) {
					vsgal.RevertImageSource();
				}
				var licb = function(e) {
					vsgal.LockImageSource(e.data.img);
				}
				var thma = p.anchor("javascript:void(0)", p.image("", actg[i][0], ""));
				$(thma).bind("mouseover", { img: actg[i][1] }, cicb);
				$(thma).bind("click", { img: actg[i][1] }, licb);
				$(thma).bind("mouseout", ricb);
				obj.append(thma);
			}
			vsgal.ChangeImageSource(actg[0][1]);
		}

		var populateSpin = function() {
			vsgal.ActiveGallery = actg;
			$("div.thumbsStripGallery > div.galleryThumbs > div.thumbsPanel").empty();
			$("div.bottomLinks > div.prevGalleryThumbLinks a.thumbsAnchor").addClass("hide");
			$("div.bottomLinks > div.nextGalleryThumbLinks a.thumbsAnchor").addClass("hide");
			var obj = $("div.pvGallery > div.heroImage > div.spinIframeDiv");
			obj.empty()
			obj.css("display", "block");
			obj.append(p.iframe("spin360", "", "no", actg[0][1], 360, 240, "0"));
		}

		var assignActiveGallery = function(arr, str) {
			vsgal.LockedImageSrc = "";
			actg = arr;
			$("div.topLinks > a.dealer").removeClass("selected");
			$("div.topLinks > a.manufacturer").removeClass("selected");
			$("div.topLinks > a.ext360").removeClass("selected");
			$("div.topLinks > a.int360").removeClass("selected");
			$("div.actionIcons > a.galleryMagnify").css("display", "none");

			switch (str) {
				case 'd':
					$("div.topLinks > a.dealer").addClass("selected");
					break;
				case 'm':
					$("div.topLinks > a.manufacturer").addClass("selected");
					break;
				case 'e':
					$("div.topLinks > a.ext360").addClass("selected");
					break;
				case 'i':
					$("div.topLinks > a.int360").addClass("selected");
					break;
				default:
					break;
			}
		}

		switch (s) {
			case 'm':
				assignActiveGallery(Vehix$Galleries$ManufacturerImages, s);
				populateGallery();
				break;
			case 'd':
				assignActiveGallery(Vehix$Galleries$DealerImages, s);
				populateGallery();
				break;
			case 'e':
				assignActiveGallery(Vehix$Galleries$ExteriorSpin, s);
				populateSpin();
				break;
			case 'i':
				assignActiveGallery(Vehix$Galleries$InteriorSpin, s);
				populateSpin();
				break;
		}
	},

	LaunchSlideShow: function() {
		if (this.ActiveGallery != null && this.ActiveGallery.length > 0) {
			var obj = this;
			$(".pvGallery .magnifiedParent").toggleClass("hidden");
			$(".detailsContent .leftContent .panelBody .bodyLeft").addClass("hidden");
			$(".pvGallery .slideShowNavigationParent").children().removeClass("hidden");
			obj.Position = 0;
			obj.SetSlideShowPosition();
			var cb = function() {
				obj.SetSlideShowPosition();
			}
			obj.SlideInterval = setInterval(cb, 2000);
		}
	},

	SetSlideShowPosition: function(e) {
		var dAct = function(pos, actG) {
		$(".pvGallery .fullHeroImage > img").attr("src", actG[pos][1]);
		$("div.magnifiedParent div.titleBar > div.photoCounter").html("Photo " + Number(pos + 1) + " of " + actG.length);
		}
		if (e && e.data) {
			this.Position += e.data.posint;
			this.ClearGallerySlideShow();
			if (this.Position == this.ActiveGallery.length)
				this.Position = 0;
			dAct(this.Position, this.ActiveGallery);
		}
		else {
			if (this.Position == this.ActiveGallery.length)
				this.Position = 0;
			dAct(this.Position, this.ActiveGallery);
			this.Position += 1;
		}
	},

	ChangeImageSource: function(s) {
		this.ClearGallerySlideShow();
		if (s != "") {
			this.CurrentImage = new Image();
			$(this.CurrentImage).bind("load", { gal: this }, function(e) {
				var $o = $("div.pvGallery > div.heroImage img.galleryImage");
				var w = this.width;
				var h = this.height;
				$o.removeAttr("width");
				$o.removeAttr("height");
				e.data.gal.Image = { Width: w, Height: h, Orientation: "Landscape", Magnify: false };
				if (w > h) {
					if (w > 360) {
						$o.width(360);
						e.data.gal.Image.Magnify = true;
					}
				}
				else {
					e.data.gal.Image.Orientation = "Portrait";
					if (h > 280) {
						$o.height(280);
						e.data.gal.Image.Magnify = true;
					}
				}
				if (e.data.gal.Image.Magnify) {
					$("div.actionIcons > a.galleryMagnify").css("display", "inline");
					$("div.actionIcons > a.gallerySlideShow").css("display", "inline");
				}
				else {
					$("div.actionIcons > a.galleryMagnify").css("display", "none");
					$("div.actionIcons > a.gallerySlideShow").css("display", "none");
				}
				$o.attr("src", this.src);
			});
			$(this.CurrentImage).attr("src", s);
		}
	},

	ClearGallerySlideShow: function() {
		var obj = this;
		if (obj.ImageInterval > 0) {
			clearTimeout(obj.ImageInterval);
			obj.ImageInterval = 0;
		}
		if (obj.SlideInterval > 0) {
			clearInterval(obj.SlideInterval);
			obj.SlideInterval = 0;
		}
	},

	RevertImageSource: function(s) {
		var obj = this;
		var cb = function() { obj.ChangeImageSource(obj.LockedImageSrc) }
		this.ImageInterval = setTimeout(cb, 200);
	},

	LockImageSource: function(s) {
		this.ClearGallerySlideShow();
		this.LockedImageSrc = s;
	},

	RenderNextThumbsLink: function() {
		var lngth = this.ActiveGallery.length;
		var num = ($("div.galleryThumbs > div.thumbsPanel").height() - Math.abs(Number($("div.galleryThumbs > div.thumbsPanel").css("margin-top").replace("px", "")))) / 229;
		if (num < 2)
			num = lngth % 9;
		else
			num = 9;
		$("div.bottomLinks > div.nextGalleryThumbLinks a.thumbsAnchor").html("Next " + Math.min(9, num) + " of " + lngth + " images >>");
	},

	NextImageHandler: function(e) {
		$("div.galleryThumbs > div.thumbsPanel").css("margin-top", (Number($("div.galleryThumbs > div.thumbsPanel").css("margin-top").replace("px", "")) - 228) + "px");
		$("div.prevGalleryThumbLinks > a.thumbsAnchor").removeClass("hide");
		if (Math.abs(Number($("div.galleryThumbs > div.thumbsPanel").css("margin-top").replace("px", ""))) > $("div.galleryThumbs > div.thumbsPanel").height() - 229) {
			$(e.target).addClass("hide");
		}
		else {
			this.RenderNextThumbsLink();
		}
	},

	PrevImageHandler: function(e) {
		$("div.nextGalleryThumbLinks > a.thumbsAnchor").removeClass("hide");
		$("div.galleryThumbs > div.thumbsPanel").css("margin-top", (Number($("div.galleryThumbs > div.thumbsPanel").css("margin-top").replace("px", "")) + 228) + "px");
		this.RenderNextThumbsLink();
		if (Number($("div.galleryThumbs > div.thumbsPanel").css("margin-top").replace("px", "")) == 0) {
			$(e.target).addClass("hide");
		}
	}
}
