/*
######################################################################################
######                                                                        #######
######     POPUP SCRIPTS			                                         #######
######                                                                      #######
##################################################################################
*/

	var popupState	= 'closed';
	var popupWindow = new Object();
	var popupActive	= '';
	var currentTotalHeight	= 0;
	var currentTotalWidth	= 0;
	
/***** GET THE FUNCTIONS STARTED *****/

	var popup;
	var fade;
	var container;
	
	var bgTop;
	var bgMiddle;
	var bgBottom;
	
	var overlay;
	var header;
	var content;
	var footer;
	var shareThis;
	
	popupWindow.Start = function(popupId) {
		$(window).resize(function() { popupWindow.contentPos(); });
		$(window).change(function() { popupWindow.contentPos(); });

	/***** SET GLOBALS *****/
		popup 		= '.popup'
		fade 		= '#fade';
		container 	= '#container';
		
		bgTop		= container + ' .bg_top';
		bgMiddle	= container + ' .bg_middle';
		bgBottom	= container + ' .bg_bottom';
		
		overlay		= container + ' .overlay';
		header		= container + ' .header';
		content		= container + ' .content';
		footer		= container + ' .footer';
	
	/***** REUSABLE FUNCTIONS *****/
		
		var bodyTag = $(document).find('form');
		$(bodyTag).append($(popup));
		
	// Activate the popup
		popupWindow.Open(popupId);
	}

// changing wrapper properties
	popupWindow.setWrapper = function() { 
		$('#wrapper').css({'overflow':'hidden','height':$(window).height() + 'px'});
	}
	popupWindow.unsetWrapper = function() {
		$('#wrapper').css({'overflow':'visible','height':'auto'});
	}

// Scroll
	popupWindow.scrollBarsRemove = function() {
		$(content).jScrollPaneRemove({ 'showArrows':true, 'scrollbarWidth':'9', 'wheelSpeed':'30', 'showArrows ':true });
	}
	popupWindow.scrollBars = function() {
		$(content).jScrollPane({ 'showArrows':true, 'scrollbarWidth':'9', 'wheelSpeed':'30', 'showArrows ':true });
	}

// Close
	popupWindow.Close = function() { popupWindow.Open(); }


/********************************************************************************/
/****** REUSABLE FUNCTIONS *****************************************************/
/******************************************************************************/

/***** VERTICALLY ALIGN CENTRE THE CONTENT *****/

	popupWindow.contentPos = function() {
		
		var areaHeight = $('#body-bg').height() - 57;
		
		$(popup).css({'width': '100%','height': areaHeight + 'px'});
		$(fade).css({'width': '100%','height': areaHeight + 'px'});

	// Set container position
		//var top		= (areaHeight / 2) - ($(container).height() / 2);
		
		var windowTop	= ($(window).scrollTop());
		var header		= $('#header').height();
		
		if(windowTop < header) {
			var top = 185;
		} else {
			var top = windowTop;
		}
		
		var left	= ($(window).width() / 2) - ($(container).width() / 2);
		$(container).css({ 'top': top + 'px', 'left': left + 'px' });
	}

 

/***** OPEN / CLOSE THE POPUP WINDOW *****/

	popupWindow.Open = function(popupId) {
		if(popupState == 'closed') {
	
	/***** FADING *****/
		// Set opacity to 0
			$(fade).css({ opacity: 0 });
			$(container).css({ opacity: 0 });

		// Show the areas
			$(popup).show();
			$('#' + popupId).show();
			
		// Set content positioning
			popupWindow.contentPos();
			popupActive = popupId;
		// Run the animations
			$(fade).animate({
				opacity: 0.80
			}, 500, function() {
				
				
			$('#terms_scroller').jScrollPane({
				'showArrows':true,
				'scrollbarWidth':'24',
				'arrowSize':'23'
			});
				
				$(container).animate({
					opacity: 1
				}, 500, function() {
					popupState = 'open';
					$(fade).click(function() { popupWindow.Open(); });
				});
			});
		} else {
			$(container).animate({ opacity:0 }, 500, function() {

				popupWindow.unsetWrapper();
				
				$(fade).animate({ opacity:0 }, 500, function() {
					popupState = 'closed';
					popupActive = '';
				// Hide all areas
					$(popup).hide();
					$('#' + popupId).hide();
				});
			});
		}
	}
	

