// global vars
var value = 0;
var div_for_darken_ie = 'darklight_container'; // used for IE 6 only
var div_for_darken_all = 'lightsoff-background'; // used for all browsers

// getting IE version
function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

// apply darken on page (for IE<7.0 only)
function setOpacity(height_w)
{
   value += 3.8;
   jQuery('#' + div_for_darken_ie).css({ width: '100%' });
   jQuery('#' + div_for_darken_ie).css({ height: height_w + 'px' });
   jQuery('#' + div_for_darken_ie).css({ display: 'block' });
   jQuery('#' + div_for_darken_ie).css({ opacity: value/10 });
   myTimeout = setTimeout("setOpacity("+height_w+")", 1);


   if ((value/5) >= .8) {
      clearTimeout(myTimeout);

      jQuery('#' + div_for_darken_ie).css({ opacity: 0.8 });
      jQuery('#' + div_for_darken_ie).html('');
      // getting banner code from #banner1 div
      var banner_code = jQuery('#banner1').html();
      // trimming spaces between tags
      banner_code = banner_code.replace(new RegExp(">\\s+<","g"),"><");
      // creating extra layer on darken div
      jQuery('#' + div_for_darken_ie).append('<div id="banner_code" style="position: absolute; top: '+jQuery('#banner1 iframe').offset().top+'px; left: '+jQuery('#banner1 iframe').offset().left+'px;">'+banner_code+'</div>');
      // hide banner1
      $("#banner1").css("display", "none");
      // setting handler for dissappearing darkness
      jQuery('#' + div_for_darken_ie).click(function()
       {
		  removeOpacity();
		  return false;
	   });
	  // setting handler for clicking on banner
	  jQuery('#banner_code').click(function()
       {
		   if (jQuery('#banner1 a').attr('target') == '_blank')
		   {
       	      window.open(jQuery('#banner1 a').attr('href'));
		   }
		   else
		   {
		   	  window.location.replace(jQuery('#banner1 a').attr('href'));
		   }
       	   return false;
	   });
   }
}

// remove darken from page (for IE<7.0 only)
function removeOpacity()
{
   value -= 2.5;
   myTimeout2 = setTimeout('removeOpacity()', 1);
   jQuery('#' + div_for_darken_ie).css({ opacity: value/10 });

   if ((value/5) <= 0) {
   	  jQuery('#' + div_for_darken_ie).css({ display: 'none' });
      clearTimeout(myTimeout2);
      jQuery('#' + div_for_darken_ie).css({ opacity: 1 });
      // show banner1
      $("#banner1").css("display", "block");
   }
}

// getting page and window sizes
function getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth;
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = xScroll;
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
}

function doResize()
{
	var page_size = getPageSize();
	if (navigator.userAgent.indexOf("MSIE")!=-1 && getInternetExplorerVersion()<7.0)
        {
        	// alternative lightsout for IE<7.0
        	if(jQuery('#' + div_for_darken_ie).css('opacity') == 0.8)
        		{
        	       setOpacity(page_size[1]);
        		}
        } // end of script for IE<7.0
        // main script - for all browsers and >IE6
        else
        {
        	if (jQuery('#' + div_for_darken_all).css('opacity') == 0.8)
        	{
        	  jQuery('#' + div_for_darken_all)
	           .css({
		               backgroundColor: "#000",
		               opacity:			0.8,
	                   height:          page_size[1],
				       width:			page_size[2]
	                }).show();
        	}
        }

}

function LightsOut()
{
	var page_size = getPageSize();
	// IE<7.0 browser fixes
    if (navigator.userAgent.indexOf("MSIE")!=-1 && getInternetExplorerVersion()<7.0)
        {
        	// alternative lightsout for IE<7.0
        	if(jQuery('#' + div_for_darken_ie).css('opacity') == 1)
        	{
        		setOpacity(page_size[1]);
        	}
        	else
        	{
        		if(jQuery('#' + div_for_darken_ie).css('opacity') == 0.8)
        		{
        		  removeOpacity();
        		}
        	}
        } // end of script for IE<7.0
        // main script - for all browsers and >IE6
        else
        {
        	// don't apply z-index changing for IE 7
        	if ((navigator.userAgent.indexOf("MSIE")!=-1 && getInternetExplorerVersion()>7.9) || (navigator.userAgent.indexOf("MSIE") == -1))
        	{
        	   jQuery(".page_container").css("z-index","auto");
        	}
        	jQuery('#' + div_for_darken_all)
			.fadeOut(function()
			{
				jQuery('#' + div_for_darken_all).remove();
				jQuery('#' + div_for_darken_all).css({ opacity: 1 });
			});
			jQuery('#videoplayer, embed, object, object type')
			.css({ 'visibility' : 'visible' });
			jQuery('#banner1')
			.after('<div id="'+ div_for_darken_all + '"></div>');

			jQuery('#' + div_for_darken_all)
			.css({
				backgroundColor:  "#000",
				opacity:		  0.8,
				height:			  page_size[1],
				width:			  page_size[2],
				position:         "fixed",
				margin:           0,
				padding:          0,
				zIndex:           20,
				left:             0,
				top:              0
			}).show();

			// assigning large z-index for 100% showing banner
			jQuery('#banner1').css({"zIndex":"101", "position":"absolute"});

			jQuery('#' + div_for_darken_all).click(function()
			{
				jQuery('#' + div_for_darken_all)
				.fadeOut(function()
				{
					jQuery('#' + div_for_darken_all).remove();
				});
				return false;
			});
			return false;

	}  // end of main script - for all browsers and >IE6
}

// remove lightsout after finishing playback
function LightsOutRemove()
{
  // IE<7.0 browser fixes
  if (navigator.userAgent.indexOf("MSIE")!=-1 && getInternetExplorerVersion()<7.0)
    {
       removeOpacity();
    } // end of script for IE<7.0
    // main script - for all browsers and >IE6
  else
    {
    	jQuery('#' + div_for_darken_all)
		.fadeOut(function()
		  {
			jQuery('#' + div_for_darken_all).remove();
		  });
	}  // end of main script - for all browsers and >IE6
}
