var globalScopeRef = null;
var isIE = (window.navigator.userAgent.indexOf('MSIE') != -1) ? true : false;
var isOpera = (window.navigator.userAgent.indexOf('Opera') != -1) ? true : false;

function YTMNDLoader(image, audio, bgcolor, tile, audio_offset)
{
  globalScopeRef = this;	
  this._uid = new Date().getTime();
  this._image = null;
  this._image_fn = image;
  this._audio = audio;
  this._audio_offset = (audio_offset!=null) ? audio_offset : 0;
  this._bgColor = bgcolor;
  this._tileImage = tile;
  this._calledAudio = false;

}

YTMNDLoader.prototype.imageIsLoaded = function() 
{
  if (this != globalScopeRef) {
    return YTMNDLoader.prototype.imageIsLoaded.call(globalScopeRef);
  }
  if ((!this._calledAudio && (parseInt(this._image.width) > 0)) || (navigator.appName.indexOf ("Opera") != -1)) {
    this.setImage();
    flash.playAudio();
    this._calledAudio = true;
  } else { 
    setTimeout("YTMNDLoader.prototype.imageIsLoaded()", 150);
  }		

}

YTMNDLoader.prototype.loaderDone = function() 
{
  flash = document.getElementById("mloader");

  if (typeof flash.PercentLoaded == 'undefined') {
    //alert('We have detected you are using Opera. Also, you are a faggot and your web browser is garbage.');

    if (isOpera) {
      setTimeout("YTMNDLoader.prototype.loadAudio()", 4000);
    }
    else {
      setTimeout("YTMNDLoader.prototype.loaderDone()", 150);
    }
		
    return false;
  }


  if (pageloaded != 1) { 
    setTimeout("YTMNDLoader.prototype.loaderDone()", 150);	  
  }
  else {
    flash = document.getElementById("mloader");

    if(document.mloader != null && document.mloader.PercentLoaded() == 100) {
      YTMNDLoader.prototype.loadAudio();
    } else {
      setTimeout("YTMNDLoader.prototype.loaderDone()", 150);
    }	
  }
}

YTMNDLoader.prototype.loadAudio = function() 
{	
  if(this != globalScopeRef) {
    return YTMNDLoader.prototype.loadAudio.call(globalScopeRef);
  }	
  
  flash.loadAudio(this._audio, this._audio_offset);
}

YTMNDLoader.prototype.insertLoader = function () 
{
  var tag = new FlashTag("http://content.ytmnd.com/assets/flash/ytmnd_loader_beta.swf", 250, 250);
  tag.setBgcolor("000000");
  tag.setFlashvars('lcId=' + this._uid);
  tag.setId("mloader");
  tag.write(document);
  setTimeout("YTMNDLoader.prototype.loaderDone()", 100);
}

YTMNDLoader.prototype.loadImage = function () 
{
  this._image = new Image();
  this._image.name = this._image_fn;
  this._image.src = this._image_fn;
  this._image.onload = this.imageIsLoaded;
  //	document.getElementById("loader3").innerHTML = '<div style="background-color: white; width: 300px; height:250px;  z-index: 100000000; position:fixed; top: 100px; left: 39%;">lol ads.</div>';

  if (isOpera) {
    setTimeout("YTMNDLoader.prototype.imageIsLoaded();", 6000);
  }
}

YTMNDLoader.prototype.setImage = function () 
{
	if(this._tileImage) {
		document.body.style.backgroundRepeat  = "repeat";
		document.body.style.backgroundPosition = "0px 0px";
	} else {
		document.body.style.backgroundRepeat  = "no-repeat";
		document.body.style.backgroundPosition = "50% 50%";
	}
//	document.getElementById("loader").style.width = "1px";
//	document.getElementById("loader").style.height = "1px";
//	document.getElementById("loader2").style.position = "absolute";

	if (isOpera) {
		document.getElementById("loader2").style.left = "-2000px";
	}
	else {
		document.getElementById("loader2").style.visibility = "hidden";
	}

//	document.getElementById("loader3").style.display = "none";
	document.getElementById("site_text_table").style.display = "block";

	if(!isIE) {
		var temp_img = document.createElement('img');
		temp_img.setAttribute("src", this._image.src);
	}
	document.body.style.backgroundImage = "url("+this._image.src+")";	
	document.body.style.backgroundColor = '#'+this._bgColor;
}

YTMNDLoader.prepareImage = function() {
	loader.loadImage();
}
function Exception(name, message)
{
    if (name)
        this.name = name;
    if (message)
        this.message = message;
}

Exception.prototype.setName = function(name)
{
    this.name = name;
}

Exception.prototype.getName = function()
{
    return this.name;
}

Exception.prototype.setMessage = function(msg)
{
    this.message = msg;
}

Exception.prototype.getMessage = function()
{
    return this.message;
}

function FlashTag(src, width, height)
{
    this.src       = src;
    this.width     = width;
    this.height    = height;
    this.version   = '7,0,00,0';
    this.id        = null;
    this.bgcolor   = '000000';
    this.flashVars = null;
}

FlashTag.prototype.setVersion = function(v)
{
    this.version = v;
}

FlashTag.prototype.setId = function(id)
{
    this.id = id;
}

FlashTag.prototype.setBgcolor = function(bgc)
{
    this.bgcolor = bgc;
}

FlashTag.prototype.setFlashvars = function(fv)
{
    this.flashVars = fv;
}

FlashTag.prototype.toString = function()
{
    var ie = (navigator.appName.indexOf ("Microsoft") != -1) ? 1 : 0;
    var flashTag = new String();
    if (ie)
    {
        flashTag += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ';
        if (this.id != null)
        {
            flashTag += 'id="'+this.id+'" ';
        }
        flashTag += 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version='+this.version+'" ';
        flashTag += 'width="'+this.width+'" ';
        flashTag += 'height="'+this.height+'" allowScriptAccess="always">';
        flashTag += '<param name="movie" value="'+this.src+'"/>';
        flashTag += '<param name="quality" value="high"/>';
        flashTag += '<param name="allowScriptAccess" value="always"/>';
        flashTag += '<param name="bgcolor" value="#'+this.bgcolor+'"/>';
        flashTag += '<param name="wmode" value="transparent">';
        if (this.flashVars != null)
        {
            flashTag += '<param name="flashvars" value="'+this.flashVars+'"/>';
        }
        flashTag += '</object>';
    }
    else
    {
        flashTag += '<embed src="'+this.src+'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ';
        flashTag += 'quality="high" '; 
        flashTag += 'bgcolor="#'+this.bgcolor+'" ';
        flashTag += 'width="'+this.width+'" ';
        flashTag += 'height="'+this.height+'" ';
        flashTag += 'type="application/x-shockwave-flash" wmode="transparent"';
        if (this.flashVars != null)
        {
            flashTag += 'flashvars="'+this.flashVars+'" ';
        }
        if (this.id != null)
        {
            flashTag += 'name="'+this.id+'" ';
	    flashTag += 'id="'+this.id+'" ';
        }
        flashTag += 'pluginspage="http://www.macromedia.com/go/getflashplayer" allowScriptAccess="always" ';
	flashTag += ' />';
    }
    return flashTag;
}

FlashTag.prototype.write = function(doc)
{
 	//doc.write(this.toString());
	doc.getElementById("loader2").innerHTML = this.toString();

}
