﻿var AssurantHealth = window.AssurantHealth || {};

/*----------------------------------------------------------
 * Written by R/GA: Sharda Roodal
 *
 * Enables overlay functionality via Ajax calls or hidden divs.
 * The content that gets loaded into overlays are based on 
 * the link's href, the img src, or the special source attribute.  
 * 
 * It will also create tabs and custom scrollbars as needed.
---------------------------------------------------------*/

AssurantHealth.overlayTriggers = [];
AssurantHealth.overlayTriggerSetup = function(trigger) {
	var that = this;
	this.trigger = trigger;
	
	AssurantHealth.overlayTriggers.push(this);
	
	this.determinePath = function(trigger) {
		switch (trigger.tagName.toLowerCase()) {
			case('img'):
				this.origPath = trigger.src;
				this.altText = $(trigger).attr('alt');
				return this.source = trigger.getAttribute('source') || trigger.src;
				break;
			case('a'):
				this.origPath = $(trigger).attr('href');
				return this.source = this.origPath;
				break;
			case('div'):
				this.origPath = trigger.getAttribute('source');
				return this.source = trigger.getAttribute('source');
				break;
			default:
				this.origPath = '';
				return this.source = this.origPath;
		}
	}
		
	this.triggerAction = function() {
		this.overlay = new AssurantHealth.overlayBuilder(this);
	}

	this.determinePath(trigger);
	$(trigger).click(function(e) {
		e.preventDefault();
		that.triggerAction();
	});
	
	/*Deeplink functionality
	 * This looks for the URL's hash value and if it matches the current 
	 * overlay trigger's source, build that overlay
	*/
	if (location.hash.length > 0) {
		var source = this.source.split(location.host)[1] || this.source;
		if (location.hash.indexOf(source) !== -1) this.triggerAction();
	}
}

AssurantHealth.overlayBuilder = function(trigger) {
	//settings
	this.source = trigger;
	this.sourceTag = trigger.trigger.tagName.toLowerCase();
	this.bodyActiveClass = 'overlay-active';
	this.bodyLoadingClass = 'overlay-loading';
	this.closeClass = 'overlay-close';
	this.containerClass = 'overlay-container';
	this.contentClass = 'overlay-content';
	this.innerClass = 'overlay-inner';
	this.headingClass = 'overlay-heading';
	this.originalContentClass = 'overlay-origContent';
	this.shareClass = 'overlay-shareContent';
	this.shareContainerClass = 'overlay-shareContainer';
	this.shieldClass = 'overlay-shield';
	this.titleClass = 'overlay-title';
	this.utilityClass = 'overlay-utility';
	this.animation = false;
		
	this.build = function() {
		var that = this;
		this.hasTabs = false;
		this.contentShield = $('<div class="'+ this.shieldClass +'" style="height: '+ document.body.scrollHeight +'px"></div>');
		this.container = $('<div class="'+ this.containerClass +'"></div>');
		this.inner = $('<div class="'+ this.innerClass +'"></div>').appendTo(this.container);
		this.heading = $('<div class="'+ this.headingClass +'"></div>').appendTo(this.inner);
		this.title = $('<h3 class="'+ this.titleClass +'"></h3>').appendTo(this.heading);
		this.utility = $('<div class="' + this.utilityClass+ '"><ul class="horiz arrow">'
			+ '<li><a class="'+ this.shareClass +'" href="javascript:void(0);">Share</a></li>'
			+ '<li><a class="print" href="javascript:print();">Print</a></li>'
			+ '<li class="last"><a class="'+ this.closeClass +'" href="javascript:void(0);">[x]</a></li>'
			+ '</ul></div><div class="clear"></div>').appendTo(this.heading);
		this.content = $('<div class="'+ this.contentClass +'"></div>').appendTo(this.inner);
		this.contentDest = $('<div class="overlay-contentDest"></div>').appendTo(this.content);
		
		//Set up some click events
		$('a.' + this.shareClass, this.utility).click(function() {that.share()});
		$('a.' + this.closeClass, this.utility).click(function() {that.trash()});
		this.contentShield.click(function() {that.trash()});
		
		//Bind the Esc key to the trash function
		$(window).bind('keypress', function(e) {
			if (e.which == 0) that.trash();
		});
		
		this.getContent(this.orig.source);
	}
	
	this.share = function() {
		var that = this;
		this.trashShare();
		this.shareBox = $('<div class="'+ this.shareContainerClass +'"></div>').appendTo(this.inner);
		this.shareBox.html('<p>Paste link in email or IM</p><input type="text" value="'+ location.href +'">');
		var shareCloser = $('<a href="javascript:void(0);" class="'+ this.closeClass +'">[x]</a>').appendTo(this.shareBox);
		this.shareBox.find('input').focus().select();
		shareCloser.click(function() {that.shareBox.remove();});
	},
	
	this.trashShare = function() {
		if(this.shareBox) this.shareBox.remove();
	},
	
	this.getContent = function(source) {
		var that = this;
		$(document.body).addClass(this.bodyLoadingClass);
		
		//We can't create tabs before overlay content has been set inside the overlay
		//so we call this function after the content comes in
		//same goes for scrollbars
		var tabify = function(originalContent) {
			if (originalContent.hasClass('tabs')) {
				that.hasTabs = true;
				that.heading.after(originalContent.find('.ui-tabs-nav'));
				originalContent.removeClass('tabs');
				that.inner.addClass('tabs');
				that.inner.tabs();
				
				var tabs = that.container.find('.ui-tabs-nav').children();
				tabs.css({width: Math.floor(100/tabs.length) + '%'});
				
				that.panelContainers = that.container.find('.ui-tabs-panel');
				that.panels = that.container.find('.section .pane');
				
				tabs.each(function(index,tab) {
					if ($(tab).find('a').attr('href').split('#')[1] == that.panelContainers[index].id) {
						$(that.panels[index]).before($('<h2 class="tabbed overlay-title">' + $(tab).find('a').text() + '</h2>'));
						if (index == 0) $(that.panelContainers[index]).addClass('first');
					}
				});
				
				that.panels.jScrollPane();
				
			} else {
				that.content.jScrollPane();
			}
		}
		
		//if this source is a hidden div # on the page, clone and show it
		if (source.replace(location.href, '').indexOf('#') == 0) {
			source = source.replace(location.href, '');
			this.hiddenOverlay = true;
			$(source).clone().removeAttr('id').removeClass('overlay-inactive').appendTo(this.contentDest);
			this.title.html($('.title', this.contentDest).remove().html());
			location.hash = source;
		} else if (this.sourceTag == 'img') {
			this.contentDest.html('<img src="' + source + '" alt="' + this.source.altText + '" />');
			this.title.html(this.source.altText);
		} else {
			this.contentDest.load(source + ' #main', function() {
				that.title.html($('.title', that.contentDest).remove().html());
				$('#main', that.contentDest).removeAttr('id');
				tabify(that.content.find('.' + that.originalContentClass));
			});
			location.hash = source;
		}
		
		this.show();
	}
	
	this.show =  function() {
		this.contentShield.appendTo(document.body);
		this.container.appendTo(document.body);
		$(document.body).removeClass(this.bodyLoadingClass);

		//Figure out some dimensions so we can position the overlay
		var viewportHeight = $(window).height();
		var overlayHeight = this.container.height();
		var centerY = ((viewportHeight - overlayHeight) / 2) <= 65 ? 65 : (viewportHeight - overlayHeight) / 2;
		
		this.container.css({top: Math.floor(centerY) + $(window).scrollTop()});

		//If animations are enabled, fade in the shield, otherwise just set the opacity
		if (this.animation) {
			$(this.contentShield).animate({opacity: '0'}, 0);
			$(this.contentShield).animate({opacity: '0.5'}, 1000);
		} else {
			$(this.contentShield).animate({opacity: '0.6'}, 0);
		}
		
		if (this.hiddenOverlay) this.content.jScrollPane();
		$(document.body).addClass(this.bodyActiveClass);
	}
	
	this.trash = function() {
		var that = this;
		var finisher = function() {
			that.contentShield.remove();
			that.container.remove();
			$(document.body).removeClass(that.bodyActiveClass);
			location.hash = '#';
		}

		if (this.animation) {
			$(this.contentShield).animate({opacity: '0'}, 500, function() {finisher();});
		} else {
			finisher();
		}

		//Clean up our bound events
		$(window).unbind('keypress');
		
	}
	
	//INITIALIZE
		this.orig = trigger;
		this.build();
	//INITIALIZE

};

$(function(){
	$('.overlay').each(function(index, el) {
		new AssurantHealth.overlayTriggerSetup(el);
	});
});