/* --------------------------------------------------------------
   GMLightBox.js 2008-08-10 gambio
   Gambio OHG
   http://www.gambio.de
   Copyright (c) 2008 Gambio OHG
   --------------------------------------------------------------
   Released under the GNU General Public License
   --------------------------------------------------------------
*/

function GMLightBox()
{
	var current_box_id = '';
	
	//dimScreen()
	//by Brandon Goldman
	jQuery.extend({
	  //dims the screen
	  dimScreen: function(speed, opacity, callback, nc_height) {
      if(jQuery('#__dimScreen').size() > 0) return;
      
      if(typeof speed == 'function') {
        callback = speed;
        speed = null;
      }

      if(typeof opacity == 'function') {
        callback = opacity;
        opacity = null;
      }

      if(speed < 1) {
        var placeholder = opacity;
        opacity = speed;
        speed = placeholder;
      }
      
      if(opacity >= 1) {
        var placeholder = speed;
        speed = opacity;
        opacity = placeholder;
      }

      speed = (speed > 0) ? speed : 500;
      opacity = (opacity > 0) ? opacity : 0.5;
      
      //NC_MB_MOD
		if(typeof nc_height == 'undefined'){
					if(typeof nc_height == 'undefined') var nc_height = $(document).attr('height');	//firefox
					if(typeof nc_height == 'undefined') var nc_height = $('body').attr('offsetHeight'); //IE
					if(typeof nc_height == 'undefined') var nc_height = $(document).height(); //Opera
		  }
		var gm_height = $(document).height() + 150;

      return jQuery('<div></div>').attr({
              id: 					'__dimScreen',
              fade_opacity: opacity,
              speed: 				speed
          }).css({
			background: '#000000',
			//height: 		gm_height + 'px',
          	left: 			'0px',
          	opacity: 		'0',
          	position: 	'absolute',
          	top: 				'0px',
          	width: 			'100%',
          	zIndex: 		'999'
          }).appendTo(document.body).fadeTo(speed, opacity, callback);
      
          
	  },
	  
	  //stops current dimming of the screen
	  dimScreenStop: function(callback) {
      var x = jQuery('#__dimScreen');
      var opacity = x.attr('fade_opacity');
      var speed = x.attr('speed');
      x.fadeOut(speed, function() {
        x.remove();
        if(typeof callback == 'function') callback();
      });
	  }
	});
	
	this.load_box = function(box_id, fade_background_speed, fade_in, mb_height)
	{
		if(typeof v == 'undefined') var fade_background_speed = 100;
		if(typeof fade_in == 'undefined') var fade_in = true;
		
		current_box_id = box_id;

		$(current_box_id).css(
		{
			zIndex: 		'1000',
			display:		'none'
		});
		
		$.dimScreen(fade_background_speed, 0.7, function() 
		{
			if(fb)console.log('dim done:' + current_box_id);
			if(fade_in) $(current_box_id).fadeIn();
			else $(current_box_id).show();
		}, mb_height);
	}
	
	this.close_box = function()
	{
		// BOF MOD by PT
		$('#menubox_gm_scroller').css({
			display: 'block'
		});
		// EOF MOD by PT

		$.dimScreenStop();
		$(current_box_id).fadeOut("normal", function(){
			if (navigator.appVersion.match(/MSIE [0-6]\./)) {
				$('.lightbox_visibility_hidden').css(
				{
					visibility: 	'visible'
				});
			}		
		});
		current_box_id = '';
	}
	
	this.centered_left = function(element_width) {
		var x = (screen.width / 2) - (element_width / 2);
		if(fb)console.log('centered width:' + x);
		return Math.round(x);
	}
	this.centered_top = function(element_height) {
		var y = (screen.height / 2) - (element_height / 2);
		if(fb)console.log('centered height:' + y);
		return Math.round(y);
	}
	
	

	this.test = function() {
		$('.wrap_shop').append('<div id="test_box" onClick="gmLightBox.close_box()"></div>');
		$('#test_box').css(
		{
			position: 	'absolute',
			left: 			this.centered_left(500) + 'px',
			top: 				'150px',
			width: 			'500px',
			height: 		'0px',
			background: 'white'
		});
		this.load_box('#test_box');
	}
	
}
