/*
	ModalBox Version 1.0 / 2010 by www.pheist.net
	based 'jQuery Simple Greybox plugin' by Aleksandar Pavic
	
*/


(function($) {
	$.fn.modalBox = function(options) {
		
		/* DEFAULT VARS */
		var defaults = {
			boxleft: 0,
			boxtop: '7%',
			boxwidth: 300,
			boxheight: 100,
			boxbackground: '#fff',
			boxopacity: 85,
			boxpadding: 10,
			border: 1,
			bordercolor: '#bbb',
			shadowblurx: 0,
			shadowblury: 0,
			shadowcolor: '#bbb',
			shadowradius: 7,
			shadowdark: '#dddddd', 
			shadowlight: '#efefef'
		}
		
		/* GET VARS & SET STYLES */
		var opts = $.extend({}, defaults, options);
		return this.each(function() {
			$this = $(this);
			var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
			var width = (o.boxwidth) ? o.boxwidth : defaults.boxwidth;
			if(width == 0) { width = defaults.boxwidth; }
			var height = (o.boxheight) ? o.boxheight : defaults.boxheight;
			var left = (o.boxleft) ? o.boxleft : (document.body.scrollLeft + ($('html,body').width() - width - ((o.shadowradius + o.border + o.boxpadding)*2))/2);
			var top = (o.boxtop) ? o.boxtop : defaults.boxtop;
			var shadowdark = (o.shadowdark) ? o.shadowdark : defaults.shadowdark;
			var shadowlight = (o.shadowlight) ? o.shadowlight : defaults.shadowlight;
			
			
			$(this).css ({
				'left' : left,
				'top' : top,
				'width' : (width + (o.boxpadding*2) + (o.border*4) + o.shadowradius),
				'height' : (height + (o.boxpadding*2) + (o.border*4) + o.shadowradius),
				'z-index' : 9,
				'padding' : o.shadowradius,
				'opacity' : ('.' + o.boxopacity),
				'filter' : ('alpha(opacity=' + o.boxopacity + ')'),
				'-moz-opacity' : ('0.' + o.boxopacity),
				'position' : 'absolute',
				'display' : 'none'
			});
			$('.mb_box').css({
				'width' : width,
				'height' : o.boxheight,
				'overflow' : 'hidden',
				'padding' : o.boxpadding,
				//'border' : (o.border + 'px solid ' + o.boxbackground),
				'border' : (o.border + 'px solid ' + o.bordercolor),
				'background-color' : o.boxbackground,
				'box-shadow' : (o.shadowblurx + ' ' + o.shadowblury + ' ' + o.shadowradius + 'px ' + o.shadowcolor),
				'-o-box-shadow' : (o.shadowblurx + ' ' + o.shadowblury + ' ' + o.shadowradius + 'px ' + o.shadowcolor),
				'-moz-box-shadow' : (o.shadowblurx + ' ' + o.shadowblury + ' ' + o.shadowradius + 'px ' + o.shadowcolor),
				'-webkit-box-shadow' : (o.shadowblurx + ' ' + o.shadowblury + ' ' + o.shadowradius + 'px ' + o.shadowcolor)
			});
			$('.mcloser').css({ 'text-align':'right', 'margin-bottom': '5px' });
			
			
			/* DAMN IE & OPERA - the world won't miss ya */
			$('.mb_shadow_dark').css ({
				'padding' : o.border,
				'width' : (width + (o.boxpadding*2) + (o.border*2)),
				'height' : (height + (o.boxpadding*2) + (o.border*2))
			});
			$('.mb_shadow_light').css ({
				'padding' : o.border*2,
				'width' : (width + (o.boxpadding*2) + (o.border*4)),
				'height' : (height + (o.boxpadding*2) + (o.border*4))
			});
			/* fix incompatible shadows */
			if($.browser.msie){
				$('.mb_box').css('border', (o.border + 'px solid ' + o.bordercolor));
				$('.mb_shadow_dark').css('background-color', shadowdark);
				$('.mb_shadow_light').css('background-color', shadowlight);
			}
			
			/* HEX 2 RGB 
			var h;
			var Rdark = HexToR(shadowdark);
			var Gdark = HexToG(shadowdark);
			var Bdark = HexToB(shadowdark);
			var Rlight = HexToR(shadowlight);
			var Glight = HexToG(shadowlight);
			var Blight = HexToB(shadowlight);
			function HexToR(h) {return parseInt((cutHex(h)).substring(0,2),16)}
			function HexToG(h) {return parseInt((cutHex(h)).substring(2,4),16)}
			function HexToB(h) {return parseInt((cutHex(h)).substring(4,6),16)}
			function cutHex(h) {return (h.charAt(0)=="#") ? h.substring(1,7):h}
			*/
			
			/* fix incompatible shadows using alpha */
			var navAgent = navigator.userAgent;
			if(($.browser.opera) || (navAgent.search('Wyzo') != -1) || (navAgent.search('Flock') != -1) || (navAgent.search('Camino') != -1)){
				$('.mb_box').css('border', (o.border + 'px solid ' + o.bordercolor));
				//$('.mb_shadow_dark').css('background-color', 'rgba('+ Rdark +', '+ Gdark +', '+ Bdark +', 0.4)');
				//$('.mb_shadow_light').css('background-color', 'rgba('+ Rlight +', '+ Glight +', '+ Blight +', 0.2)');
				$('.mb_shadow_dark').css('background-color', 'rgba(150, 150, 150, 0.15)');
				$('.mb_shadow_light').css('background-color', 'rgba(150, 150, 150, 0.075)');
			}
			
			
			/* CLOSE */
			$('.closeModalbox img').css({
				'box-shadow' : 'none',
				'-o-box-shadow' : 'none',
				'-moz-box-shadow' : 'none',
				'-webkit-box-shadow' : 'none'
			});
			$('.mb_box .mcloser').css('cursor', 'move');
			
			/* CLOSE:HOVER */
			$('.closeModalbox').hover(function(){
				if(($.browser.mozilla) || ($.browser.webkit)) { 
					$('.closeModalbox img').css({
						'box-shadow' : ('0 0 4px ' + o.shadowcolor),
						'-o-box-shadow' : ('0 0 4px ' + o.shadowcolor),
						'-moz-box-shadow' : ('0 0 4px ' + o.shadowcolor),
						'-webkit-box-shadow' : ('0 0 4px ' + o.shadowcolor)
					});
				}
			}, function(){
				if(($.browser.mozilla) || ($.browser.webkit)) { 
					$('.closeModalbox img').css({
						'box-shadow' : ('0 0 0 ' + o.boxbackground),
						'-o-box-shadow' : ('0 0 0 ' + o.boxbackground),
						'-moz-box-shadow' : ('0 0 0 ' + o.boxbackground),
						'-webkit-box-shadow' : ('0 0 0 ' + o.boxbackground)
					});
				}
			});
			
			
			/* DRAG */
			$this.bind('drag', function(event) { $(this).css({ top: event.offsetY, left: event.offsetX }); });
			return false;
		});
	};
	
	/* SHOWHIDE */
	$.fn.showBox = function(speed) {
		return this.each( function() { 
			if(($.browser.mozilla) || ($.browser.webkit)) { $(this).fadeIn(speed); } else { $(this).show(); }
			return false; 
		});
	}
	$.fn.hideBox = function(speed) {
		return this.each( function() { 
			if(($.browser.mozilla) || ($.browser.webkit)) { $(this).fadeOut(speed); } else { $(this).hide(); }
			return false; 
		});
	}
	
	
})(jQuery);

