/*
 * jQuery toplayer
 * By: PRODUKTION 203 AB
 * Version 1.2
 * Last Modified: 2009-10-20
 * 
 * Copyright 2009 PRODUKTION 203
 * 
 */
 
(function($) {
	$.prompt = function(message, options) {
		
		options = $.extend({},$.prompt.defaults,options);
		
		var $body	= $(document.body);
		var $window	= $(window);
		
		if(options.bg) {
			var background = '<div id="toplayer-'+ options.prefix +'-bg"></div>';
		}
		var style = '';
		if(options.width != 0) {
			style += 'width: ' + options.width + 'px;';	
		}
		// If its ie7 we have to set position fixed because changing position through javascript isnt working
		if(checkIE(7)) {
			style += 'position: fixed;';	
		}
		
		//Toplayer htmlbox
		var boxhtml = '<table cellspacing="0" cellpadding="0"' + (style.length == 0 ? "" : ' style="' + style + '"') + ' class="toplayer" id="toplayer-' + options.prefix + '">';
		boxhtml += '<tr><td class="top_left"></td><td class="top_center">';
		if(options.topArrow && !checkIE(6)) {
			boxhtml += '<img src="/img/site/toplayer-submenu-arrow.png" alt=""' + (options.ArrowLeft > 0 ? ' style="margin-left: ' + options.ArrowLeft + 'px;"' : '') +' />';	
		}
		boxhtml += '</td><td class="top_right"></td></tr>';
		boxhtml += '<tr>';
		boxhtml += '<td class="left"></td>';
		boxhtml += '<td class="content">';
		if(options.title.length > 0) {
			boxhtml += '<h1 id="toplayer-title-' + options.prefix + '">' + options.title + '</h1>';
		}
		if(options.closer == 1) {
			boxhtml += '<a href="javascript:void(0);" id="toplayer-close-' + options.prefix + '"><img src="' + (checkIE(6) ? '/img/site/spacer.png' : '/img/site/toplayer-close.png') + '" id="layer-close-' + options.prefix + '" alt="St&auml;ng" title="St&auml;ng" class="right" /></a>';
		}
		boxhtml += '<div class="layercontent" id="layer-content-' + options.prefix + '">' + message + '</div>';
		boxhtml += '</td><td class="right"></td></tr><tr><td class="bottom_left"></td><td class="bottom_center"></td><td class="bottom_right"></td></tr></table>';
		
		if(options.bg) {
			var $bglayer = $(background).appendTo($body);
		}
		var $toplayer = $(boxhtml).appendTo($body);
		
		// Set transaprent style for bg layer if we needed one
		if(options.bg) {
			$bglayer.css({
				height: ($body.height() > $window.height() ? $body.height() + 1 : $window.height() + 1) + 'px',
				opacity: options.opacity,
				width: '100%',
				backgroundColor: '#000000',
				position: 'absolute',
				zIndex: 999,
				top: 0,
				left: 0
			});
		}
		
		
		// Position the toplayer correctly
		// This is if toplayer is shorter than window height
		// Then position it centered and fixed
		if($toplayer.height() < $window.height()) {
			
				if(options.forceAbsolute) {
					
					$toplayer.css({ 
						top: options.top + 'px', 
						left: options.left + 'px'
					});
					
				}else{
				
					$toplayer.css({ 
						position: 'fixed', 
						top: (($window.height()-$toplayer.height())/2) + 'px', 
						left: (($window.width()-$toplayer.width())/2) + 'px'
					});
				}
		
		// Else use absolute positioning so we can scroll with it
		}else{
			
			var scrolloffset = $window.scrollTop();
			
			if((scrolloffset + $toplayer.height()) > $body.height()) {
				
				var newtop = $body.height() - $toplayer.height();
			}else{
				var newtop = scrolloffset;	
			}
			
			$toplayer.css({ 
						  
				top: newtop + 'px', 
				left: (($window.width()-$toplayer.width())/2) + 'px'
			});
			
			
		}
		
		// If we want a close button bind that button so its clickable
		if(options.closer == 1) {
			
			$toplayer.find('#toplayer-close-' + options.prefix).click(function() {
				$('#toplayer-' + options.prefix).remove();
				if(options.bg) {
					$('#toplayer-'+ options.prefix +'-bg').remove();
				}
			});
		}
		
	};
	
	$.prompt.defaults = {
		prefix:'toplayer',
		title: '',
		bg: true,
		forceAbsolute: false,
		topArrow: false,
		ArrowLeft: 0,
		ArrowTop: 0,
		top: 0,
		left: 0,
		opacity: 0.55,
		closer: 1,
		width: 0
	};
	
	
})(jQuery);
