var rim = rim || {};
/*
 * PageModal creates a generic modal instance that is used
 * to handle the registration and display of all modal windows in a page.
 *
 */
rim.PageModal = function () {
	rim.PageModal = this;
	this.DialogID = 'pageModal';
	this.FlashID = 'flashInModal';
	this.FlashContainerID = 'flashPlaceholder';
	this.ModalLinkSelector = '.launchModal';
	this.OverlayClassName = 'pageModalOverlay';
	this.OverlaySelector = '.' + this.OverlayClassName;
	this.CloseLink = '.close';
	this.ModalTemplate = '<div><div class="content" role="main"></div><div class="end"></div></div>';
	this.PageModals = {};
	this.PageModals.Ids = [];
	this.DefaultUrl = rim.lang.rootPath+'devices/modalContent.jsp';
	this.CurrentOnShow = null;
	this.CurrentOnCenter = null;
	this.CurrentTarget = null;
	this.ModalIdAttr = 'modalid';

	this.Self = new Rf.Modal(null,{OverlayClassName: this.OverlayClassName, DialogID: this.DialogID ,AllowCache : true, ZIndex: '10000', OverlayOpacity: '0.8', Template: this.ModalTemplate});

	this.Init();
};

rim.PageModal.prototype = {
	Init: function () {
		//set up global listeners
		$(this.OverlaySelector).live('click', $.proxy(function(e){
			e.preventDefault();
			this.Self.Close();
		}, this));

		$('#' + this.DialogID + ' ' + this.CloseLink).live('click', $.proxy(function(e){
			e.preventDefault();
			this.Self.Close();
		}, this));

		this.Self.Bind(Rf.Modal.Events.Show, $.proxy(this.OnShow, this));
		this.Self.Bind(Rf.Modal.Events.Centered, $.proxy(this.OnCenter, this));

		$(this.ModalLinkSelector).live('click', $.proxy(function(e){
			e.preventDefault();
			var clickTarget = $(e.target),
				target = clickTarget.is(this.ModalLinkSelector)? clickTarget: clickTarget.closest(this.ModalLinkSelector),
				id = target.attr(this.ModalIdAttr),
				modalData = this.PageModals[id]||null;
			if (!modalData) return;
			this.Self.Url = modalData.url;
			this.CurrentTarget = target;
			this.CurrentOnShow = modalData.show || null;
			this.CurrentOverlay = modalData.overlay;
			this.CurrentOnCenter = modalData.center || null;
			this.Self.Open();
		}, this));
	},
	OnShow: function (e) {
		//global on show functionality
		if (this.CurrentOnShow) this.CurrentOnShow.call(this);
		if (this.CurrentOverlay) this.Self._Overlay.addClass('modal-overlay');
		if ($('.ie').length > 0 && !this.CurrentOverlay) this.Self._Overlay.addClass('ieOverlayFix');
	},
	OnCenter: function (e){
		//global on center functionality
		if (this.CurrentOnCenter) this.CurrentOnCenter.call(this);
		//otherwise, modal centers on page
	},
	Register: function (id, url, show, center, overlay) {
		this.PageModals[id] = {'url': url, 'show':show, 'center':center, 'id':id, 'overlay':overlay || false};
		this.PageModals.Ids.push(id);
	},
	InitFlash: function(config) {
		var flashPath = window.location.protocol + '//' + window.location.host + rim.ContextUrl + '/flash/',
			pathToFlash = flashPath + config.swfTemplateFile,
			flashVars = {
				file: flashPath + config.assetPath,
				skin: '/assests_refresh/example_skin.xml',
				autostart: true
			},
			flashParams = {
				allowscriptaccess: 'always',
				wmode: 'transparent',
				salign: 't',
				align: 't',
				height: config.height + 'px',
				width: config.width + 'px',
				version: '9'
			},
			flashAttributes = {
				id: rim.PageModal.FlashID
			};

		var	vidModal = this.Self._Dialog.find('#videoModal'),
			isVidModal = vidModal.length > 0;
		if (isVidModal) vidModal.css({height:config.height, width:config.width});

		swfobject.embedSWF(
			pathToFlash,
			rim.PageModal.FlashContainerID,
			config.width,
			config.height,
			flashParams.version,
			false,
			flashVars,
			flashParams,
			flashAttributes,
			function(e) {
				if (e.success) {
					// Flash supported
				}
				else {
					$('#' + rim.PageModal.FlashContainerID).html(rim.lang.noflash);
				}
			});

	},
    InitImage: function(config) {
        var imagePath = window.location.protocol + '//' + window.location.host + rim.ContextUrl + '/',
            pathToImage = imagePath + config.imageFile;

        var	vidModal = this.Self._Dialog.find('#imageModal'),
            isVidModal = vidModal.length > 0;
        if (isVidModal) vidModal.css({height:config.height, width:config.width});

        var imageContainer = vidModal.find('.videoContainer .swf');
            imageContainer.html('<img src="' + pathToImage + '" height="' + config.height + '" width="' + config.width + '" />');
    },

	InitImageVert: function(config) {
		        var imagePath = window.location.protocol + '//' + window.location.host + rim.ContextUrl + '/',
		            pathToImage = imagePath + config.imageFile;

		        var	vidModal = this.Self._Dialog.find('#imageModalVert'),
		            isVidModal = vidModal.length > 0;
		        if (isVidModal) vidModal.css({height:config.height, width:config.width});

		        var imageContainer = vidModal.find('.videoContainer .swf');
		            imageContainer.html('<img src="' + pathToImage + '" height="' + config.height + '" width="' + config.width + '" />');
		    }
};

rim.CenterModalOnLink = function () {
		var linkPosition = this.CurrentTarget.offset();
		var css = {},
			dialog = this.Self._Dialog,
			dialogWidth = dialog.width();
		css.left = linkPosition.left + 'px';
		css.top = linkPosition.top + 'px';
		if ($('#compareModal').length) {
		var scrollerLeft = 	$('ul.scroller').position().left;
		var windowWidth = 	$(window).width();
		var modalRight = 	linkPosition.left + $($('#compareModal div')[0]).width();

		if ((modalRight + scrollerLeft) - 250 > windowWidth) {
			newLeft = linkPosition.left - (((modalRight + scrollerLeft) - 250) - windowWidth);
			css.left = newLeft + 'px';
		} else if (linkPosition.left < -scrollerLeft + 150) {
			newLeft = linkPosition.left + ((-scrollerLeft + 150) - linkPosition.left);
			css.left = newLeft + 'px';
		}
		}
		dialog.css(css).fadeIn('slow');
};

rim.OnloadEvents.Register(function () {
	new rim.PageModal();
});
