
// start CustomModalVideoPlayer
// CustomModalVideoPlayer is branched from ModalVideoPlayer to accommodate switching between YouTube and Vimeo
var CustomModalVideoPlayer = Class.create ({
	initialize: function(flash,links,options,flashOptions)
	{
		//this.elBody = $(document.body);
		this.flash = flash;
		this.flashID = this.flash.identify();
		this.flashParent = this.flash.up();
		this.flash.remove();
		this.links = links;
		this.length = this.links.size();
		this.options = Object.extend({
			leftOffset: 0,
			topOffset: 0,
			minTopSpacing: 10
		}, options || {});
		this.flashOptions = Object.extend({
			stageWidth: '640',
			stageHeight: '510',
			videoWidth: '640',
			videoHeight: '480',
			bgcolor: '#ffffff',
			version: '9.0.0',
			swfPath: 'videoplayer.swf'
		}, flashOptions || {});
		this.lnkClose = $('lnk_close_videoplayer');
		this.modalOverlay = $('modaloverlay');
		if (!isIE6) {this.modalOverlay.setOpacity(0.0);}
		this.modalVideoPlayer = $('modalvideoplayer');
		this.width = this.modalVideoPlayer.getWidth();
		this.height = this.modalVideoPlayer.getHeight();
		//this.modalVideoPlayer.remove();
		//this.modalOverlay.insert({after: this.modalVideoPlayer});
		this.leftPos = ( ( ( document.viewport.getWidth() - this.width ) / 2 ) + this.options.leftOffset ) + 'px';
		this.topPos = ( ( ( document.viewport.getHeight() - this.height ) / 2 ) + this.options.topOffset );
		if (this.topPos <= this.options.minTopSpacing) {this.topPos = this.options.minTopSpacing;} // make sure there is always min spacing at top
		this.topPos = this.topPos + 'px';
		this.modalVideoPlayer.setStyle({left: this.leftPos, top: this.topPos});
		// set event handlers
		var bindClickOpen = this.__ClickOpen.bindAsEventListener(this);
		var bindClickClose = this.__ClickClose.bindAsEventListener(this);
		this.links.invoke('observe', 'click', bindClickOpen);
		this.modalOverlay.observe('click', bindClickClose);
		this.lnkClose.observe('click', bindClickClose);
		this.active = false;
	},
    __ClickOpen: function(e)
    {
		e.stop();
		var el = e.findElement('a');
		this.active = true;
		var lnkRel = el.rel.split(';');
		var videoType = lnkRel[0];
		var videoID = lnkRel[1];
		this.OpenPopover(videoType,videoID);
    },
    __ClickClose: function(e)
    {
		e.stop();
        if (this.active)
        {
			this.active = false;
			this.ClosePopover();
		}
    },
    OpenPopover: function(videoType,videoID)
    {
		if (isIE6) {
			this.modalOverlay.addClassName('active').setStyle({height: document.body.getHeight()});
			var arrPageScrollOffsets = document.viewport.getScrollOffsets(); //returns array [horiz,vert]
			this.topPos = ((document.viewport.getHeight() - this.height) / 2 ) + arrPageScrollOffsets[1]; // ( (viewport height - player height) / 2 ) + vert offset
			this.topPos = this.topPos + 'px';
			this.modalVideoPlayer.setStyle({top: this.topPos}).addClassName('active');
			this.flashParent.insert(this.flash);
			this.FlashInit(videoType,videoID);
		} else {
			this.modalOverlay.addClassName('active').appear({
				duration: 0.3, from: 0.0, to: 0.7,
				afterFinish: function() {
					this.modalVideoPlayer.addClassName('active');
					this.flashParent.insert(this.flash);
					this.FlashInit(videoType,videoID);
				}.bind(this)
			});
		}
    },
    ClosePopover: function()
    {
		$(this.flashID).remove();
		if (isIE6) {
			this.modalOverlay.removeClassName('active');
		} else {
			this.modalOverlay.removeClassName('active').setOpacity(0.0);
		}
		this.modalVideoPlayer.removeClassName('active');
    },
    FlashInit: function(videoType,videoID)
    {
		if (videoType == 'youtube')
		{
			this.PlayYouTube(videoID);
		}
		else if (videoType == 'vimeo')
		{
			this.PlayVimeo(videoID);
		}
		else {
			this.ClosePopover();
		}
    },
    PlayYouTube: function(videoID)
    {
	    var flashvars = {
			autoPlayBool: 'true',
			loopBool: 'false',
			playerPath: 'http://www.youtube.com/apiplayer?version=3',
			videoWidth: this.flashOptions.videoWidth,
			videoHeight: this.flashOptions.videoHeight,
			videoID: videoID
		};
	    var params = {
			allowFullScreen: 'true',
			allowScriptAccess: 'always',
			autoplay: 'true',
			loop: 'false',
			menu: 'true',
			quality: 'high',
	    	salign: 't',
	    	scale: 'exactfit',
	    	wmode: 'opaque'
	    };
		var attributes = {
			bgcolor: this.flashOptions.bgcolor,
	    	id: this.flashID
		};
		swfobject.embedSWF(this.flashOptions.swfPath, this.flashID, this.flashOptions.stageWidth, this.flashOptions.stageHeight, this.flashOptions.version, false, flashvars, params, attributes);
    },
    PlayVimeo: function(videoID)
    {
	    var flashvars = {
			clip_id: videoID,
			autoplay: 1,
			fullscreen: 1,
			js_api: 1,
			show_byline: 0,
			show_portrait: 0,
			show_title: 0,
			js_onLoad: 'vimeo_player_loaded',
			js_swf_id: 'moogaloop'
		};
	    var params = {
			allowFullScreen: 'true',
			allowScriptAccess: 'always',
			autoplay: 'true',
	    	wmode: 'opaque'
	    };
		var attributes = {
			bgcolor: this.flashOptions.bgcolor,
	    	id: this.flashID
		};
		swfobject.embedSWF('http://vimeo.com/moogaloop.swf', this.flashID, this.flashOptions.stageWidth, this.flashOptions.stageHeight, this.flashOptions.version, false, flashvars, params, attributes);
    }
});
// end CustomModalVideoPlayer

// start ModalVideoPlayer
var ModalVideoPlayer = Class.create ({
	initialize: function(flash,links,options,flashOptions)
	{
		//this.elBody = $(document.body);
		this.flash = flash;
		this.flashID = this.flash.identify();
		this.flashParent = this.flash.up();
		this.flash.remove();
		this.links = links;
		this.length = this.links.size();
		this.options = Object.extend({
			leftOffset: 0,
			topOffset: 0,
			minTopSpacing: 10
		}, options || {});
		this.flashOptions = Object.extend({
			stageWidth: '640',
			stageHeight: '510',
			videoWidth: '640',
			videoHeight: '480',
			bgcolor: '#ffffff',
			version: '9.0.0',
			swfPath: 'videoplayer.swf',
			videoPath: 'video.flv',
			imgPath: false
		}, flashOptions || {});
		this.lnkClose = $('lnk_close_videoplayer');
		this.modalOverlay = $('modaloverlay');
		if (!isIE6) {this.modalOverlay.setOpacity(0.0);}
		this.modalVideoPlayer = $('modalvideoplayer');
		this.width = this.modalVideoPlayer.getWidth();
		this.height = this.modalVideoPlayer.getHeight();
		//this.modalVideoPlayer.remove();
		//this.modalOverlay.insert({after: this.modalVideoPlayer});
		this.leftPos = ( ( ( document.viewport.getWidth() - this.width ) / 2 ) + this.options.leftOffset ) + 'px';
		this.topPos = ( ( ( document.viewport.getHeight() - this.height ) / 2 ) + this.options.topOffset );
		if (this.topPos <= this.options.minTopSpacing) {this.topPos = this.options.minTopSpacing;} // make sure there is always min spacing at top
		this.topPos = this.topPos + 'px';
		this.modalVideoPlayer.setStyle({left: this.leftPos, top: this.topPos});
		// set event handlers
		var bindClickOpen = this.__ClickOpen.bindAsEventListener(this);
		var bindClickClose = this.__ClickClose.bindAsEventListener(this);
		this.links.invoke('observe', 'click', bindClickOpen);
		this.modalOverlay.observe('click', bindClickClose);
		this.lnkClose.observe('click', bindClickClose);
		this.active = false;
	},
    __ClickOpen: function(e)
    {
		e.stop();
		var el = e.findElement('a');
		this.active = true;
		this.OpenPopover();
    },
    __ClickClose: function(e)
    {
		e.stop();
        if (this.active)
        {
			this.active = false;
			this.ClosePopover();
		}
    },
    OpenPopover: function()
    {
		if (isIE6) {
			this.modalOverlay.addClassName('active').setStyle({height: document.body.getHeight()});
			var arrPageScrollOffsets = document.viewport.getScrollOffsets(); //returns array [horiz,vert]
			this.topPos = ((document.viewport.getHeight() - this.height) / 2 ) + arrPageScrollOffsets[1]; // ( (viewport height - player height) / 2 ) + vert offset
			this.topPos = this.topPos + 'px';
			this.modalVideoPlayer.setStyle({top: this.topPos}).addClassName('active');
			this.flashParent.insert(this.flash);
			this.WriteSwf();
		} else {
			this.modalOverlay.addClassName('active').appear({
				duration: 0.3, from: 0.0, to: 0.7,
				afterFinish: function() {
					this.modalVideoPlayer.addClassName('active');
					this.flashParent.insert(this.flash);
					this.WriteSwf();
				}.bind(this)
			});
		}
    },
    ClosePopover: function()
    {
		//var videoplayer = $(this.flashID);
		//videoplayer.stopVideo();
		$(this.flashID).remove();
		if (isIE6) {
			this.modalOverlay.removeClassName('active');
		} else {
			this.modalOverlay.removeClassName('active').setOpacity(0.0);
		}
		this.modalVideoPlayer.removeClassName('active');
    },
    WriteSwf: function()
    {
		// set flash values
	    var flashvars = {
			videoWidth: this.flashOptions.videoWidth,
			videoHeight: this.flashOptions.videoHeight,
			videoPath: this.flashOptions.videoPath,
			loopBool: 'false',
			autoPlayBool: 'false',
			imgPath: this.flashOptions.imgPath,
			hideControlsBool: 'false',
			controlPadding: '1',
			//controlSetup: 'PlayPause,ProgressBar,TimeDisplay,FlyoutVolumeControl,FullScreen',
			showActualSize: 'true'
		};
	    var params = {
			allowFullScreen: 'true',
			allowScriptAccess: 'sameDomain',
			autoplay: 'true',
			loop: 'false',
			menu: 'true',
			quality: 'high',
	    	salign: 't',
	    	scale: 'exactfit',
	    	wmode: 'opaque'
	    };
		var attributes = {
			bgcolor: this.flashOptions.bgcolor,
	    	id: this.flashID
		};
		swfobject.embedSWF(this.flashOptions.swfPath, this.flashID, this.flashOptions.stageWidth, this.flashOptions.stageHeight, this.flashOptions.version, false, flashvars, params, attributes);
    }
});
// end ModalVideoPlayer

// start ModalInterstitial
var ModalInterstitial = Class.create ({
    initialize: function(links,options)
    {
		this.elBody = $(document.body);
		this.links = links;
		this.length = this.links.size();
		this.options = Object.extend({
			leftOffset: 0,
			topOffset: 0,
			minTopSpacing: 10
		}, options || {});
		this.lnkCancel = $('btn_cancel_interstitial');
		this.lnkProceed = $('btn_proceed_interstitial');
		this.modalOverlay = $('modaloverlay');
		if (!isIE6) {this.modalOverlay.setOpacity(0.0);}
		this.modalInterstitial = $('modalinterstitial');
		this.width = this.modalInterstitial.getWidth();
		this.height = this.modalInterstitial.getHeight();
		//this.modalInterstitial.remove();
		//this.modalOverlay.insert({after: this.modalInterstitial});
		this.leftPos = ( ( ( document.viewport.getWidth() - this.width ) / 2 ) + this.options.leftOffset ) + 'px';
		this.topPos = ( ( ( document.viewport.getHeight() - this.height ) / 2 ) + this.options.topOffset );
		if (this.topPos <= this.options.minTopSpacing) {this.topPos = this.options.minTopSpacing;} // make sure there is always min spacing at top
		this.topPos = this.topPos + 'px';
		this.modalInterstitial.setStyle({left: this.leftPos, top: this.topPos});
        var bindClickOpen = this.__ClickOpen.bindAsEventListener(this);
        var bindClickClose = this.__ClickClose.bindAsEventListener(this);
        var bindClickContinue = this.__ClickContinue.bindAsEventListener(this);
        this.links.invoke('observe', 'click', bindClickOpen);
        this.modalOverlay.observe('click', bindClickClose);
        this.lnkCancel.observe('click', bindClickClose);
        this.lnkProceed.observe('click', bindClickContinue);
        this.active = false;
    },
    __ClickOpen: function(e)
    {
        e.stop();
        var element = e.findElement('a');
        this.active = true;
		this.links.each(function(el, i)
		{
			if (el == element)
			{
				this.OpenPopover(i);
				throw $break;
			}
		}, this);
    },
    __ClickClose: function(e)
    {
        e.stop();
        if (this.active)
        {
			this.active = false;
			this.ClosePopover();
		}
    },
    __ClickContinue: function(e)
    {
        this.active = false;
        this.ClosePopover();
    },
    OpenPopover: function(i)
    {
        this.lnkProceed.setAttribute('href', this.links[i].href);
		if (isIE6) {
			this.modalOverlay.addClassName('active').setStyle({height: document.body.getHeight()});
			var arrPageScrollOffsets = document.viewport.getScrollOffsets(); //returns array [horiz,vert]
			this.topPos = ((document.viewport.getHeight() - this.height) / 2 ) + arrPageScrollOffsets[1]; // ( (viewport height - player height) / 2 ) + vert offset
			this.topPos = this.topPos + 'px';
			this.modalInterstitial.setStyle({top: this.topPos}).addClassName('active');
		} else {
			this.modalOverlay.addClassName('active').appear({
				duration: 0.3, from: 0.0, to: 0.8,
				afterFinish: function() {
					this.modalInterstitial.addClassName('active');
					this.elBody.addClassName('modal-overlay-active');
				}.bind(this)
			});
		}
    },
    ClosePopover: function()
    {
		if (isIE6) {
			this.modalOverlay.removeClassName('active');
		} else {
			this.modalOverlay.removeClassName('active').setOpacity(0.0);
		}
        this.modalInterstitial.removeClassName('active');
    }
});
// end ModalInterstitial

