﻿var modalMagic = {
	modalContainer: "#modalContainer",
	iframeContainer: "#modalIframe",
	nonIframeContent: "#nonIframeModalContent",

	initialize: function (containerId, iframeContainerId, nonIframeContentId) {
		modalContainer = containerId;
		iframeContainer = iframeContainerId;
		nonIframeContent = nonIframeContentId;
	},
	showModalForHyperlink: function (sender, width, height) {
		var link = jQuery(sender);
		var url = link.attr("href");

		jQuery(this.iframeContainer).removeAttr("height").removeAttr("width");
		if (height)
			jQuery(this.iframeContainer).attr("height", height + 5);
		if (width)
			jQuery(this.iframeContainer).attr("width", width);

		jQuery(this.iframeContainer).attr("src", url).show();
		lightboxer.show(this.modalContainer);
		return false;
	},
	showModalForHyperlink2: function (sender, width, height) {
		var link = jQuery(sender);
		var url = link.attr("href");

		jQuery(this.nonIframeContent).removeAttr("height").removeAttr("width");
		if (height)
			jQuery(this.nonIframeContent).attr("height", height);
		if (width)
			jQuery(this.nonIframeContent).attr("width", width);

		jQuery(this.nonIframeContent).attr("src", url).show();
		lightboxer.show(this.modalContainer);
		return false;
	},
	closeModal: function () {
		var player = document.getElementById("modalIframe").contentWindow.jwplayer;
		if (player != null) {
			document.getElementById("modalIframe").contentWindow.jwplayer().stop();
		}


		lightboxer.close(this.modalContainer);
		return false;
	},
	resizeModal: function (height, width) {
		jQuery(this.iframeContainer).attr("height", height);
		jQuery(this.iframeContainer).attr("width", width);
	}
}

var lightboxer = {
	show: function (selector, options) {
		stopVideoPlayer();
		var modelEle = jQuery(selector);
		var modalParent = null;
		if (modelEle.parent().hasClass("lightBoxWrapper")) {
			modalParent = modelEle.parent();
		} else {
			modelEle.wrap("<div class='lightBoxWrapper'></div>");
			modalParent = modelEle.parent();
		}
		modelEle.addClass("lightBox");
		modelEle.css("display", "block");

		var lbOptions = {
			centered: true,
			closeClick: true,
			closeEsc: true
		};

		if (typeof (options) != 'undefined' && options != null) {
			if (options.onLoad)
				lbOptions.onLoad = options.onLoad;
			if (options.onClose)
				lbOptions.onClose = options.onClose;
		}

		modalParent.lightbox_me(lbOptions);
		return false;
	},
	close: function (selector) {
		jQuery(selector).removeClass("lightBox");
		var modalEle = this.getModalElement(selector);
		modalEle.trigger("close");
		jQuery("#modalIframe").attr("src", "http://google.com");
		return false;
	},
	reposition: function (selector) {
		var modalEle = this.getModalElement(selector);
		modalEle.trigger('reposition');
	},
	//prevents errors if calling close when modal hasn't been opened yet since we're wrapping it with lightbox class on open
	getModalElement: function (selector) {
		var jSelector = jQuery(selector);
		var modalEle = null;
		if (jSelector.parent().hasClass("lightBoxWrapper"))
			modalEle = jSelector.parent();
		else
			modalEle = jSelector;
		return modalEle;
	}
}
