function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
 return [ myWidth, myHeight ];
}

//#########################################################################

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement &&
      ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}	



/*
	addEvent
		usage: Ability to attach multiple events onto objects
		example: addEvent(window, 'onload', initMenu);
*/
function addEvent(oTarget, sType, fpDest) {
  var oOldEvent = oTarget[sType];
  if (typeof oOldEvent != "function") {
    oTarget[sType] = fpDest;
  } else {
    oTarget[sType] = function(e) {
      oOldEvent(e);
      fpDest(e);
    }
  }
}

addEvent(window,"onload",IEpatternRoll);
domainM = "snaplocktinceilings.com";
function IEpatternRoll(){
	if(!document.all ) return;
	var divs = document.getElementsByTagName("DIV");
	for(var i=0;i<divs.length;i++){
		var div = divs[i];
		if( div.className == "pattern" ){
			div.onmouseover = function(){ this.className =  this.className + " hover"; }
			div.onmouseout = function(){ this.className = this.className.replace(" hover", "");}
		}
	}
}


MoviePlayer = function(){
	
	var mov = this;

	this.isVisible = false;
	this.isBuilt = false;
	
	this.attachPlayer = function(){
		if( !document.getElementsByTagName ) return;
		
		
		var domain = document.domain;
		if( !domain.match(domainM) ) return;
		
		var aTags = document.getElementsByTagName('A');
		for(var i=0;i<aTags.length;i++){
			if(aTags[i].getAttribute('rel') == 'video'){
				aTags[i].onclick = function(){
					if(mov.isVisible) return false;
					if(!mov.isBuilt)  mov.buildPlayer();
					
					mov.render(this.href, this.title);
					
					return false;
				}
			}
		}
	}
	
	
	this.buildPlayer = function(){
		
		var objBody = document.getElementsByTagName("BODY").item(0);

		var objDivContainer = document.createElement("DIV");
		objDivContainer.setAttribute("id","moviePlayer");
		objDivContainer.style.display = 'none';
		objBody.appendChild(objDivContainer);
		
		var objDivTitle = document.createElement("DIV");
		objDivContainer.appendChild(objDivTitle);
		
		var objSpanTitle = document.createElement("SPAN");
		objSpanTitle.setAttribute('id',"moviePlayerTitle");
		objSpanTitle.setAttribute('class', 'title');
		objDivTitle.appendChild(objSpanTitle);
		
		var objSpanClose = document.createElement("SPAN");
		objSpanClose.setAttribute('class','close');
		objDivTitle.appendChild(objSpanClose);
		
		var objAnchorClose = document.createElement("A");
		objAnchorClose.setAttribute("href", "#");
		objAnchorClose.onclick = function() { mov.hideMovie(); return false; }
		objSpanClose.appendChild(objAnchorClose);
		
		var objTextClose = document.createTextNode("X");
		objAnchorClose.appendChild(objTextClose);
		
		var objEmbed = document.createElement("EMBED");
		objEmbed.setAttribute("id","moviePlayerEmbed");
		objEmbed.setAttribute("align","middle");
		objEmbed.setAttribute("type","application/x-shockwave-flash");
		objEmbed.setAttribute("src","");
		objEmbed.setAttribute("quality","best");
		objEmbed.setAttribute("bgcolor", "#ffffff");
		objEmbed.setAttribute("scale", "noScale");
		objEmbed.setAttribute("salign","TL");
		objEmbed.setAttribute("FlashVars","playerMode=embedded");
		objDivContainer.appendChild(objEmbed);
		
		
		var garyStupidNoteContainer = document.createElement("DIV");
		var garyStupidNote = document.createElement("SPAN");
		garyStupidNote.setAttribute("class","bottomPlayer");
		garyStupidNoteContainer.appendChild(garyStupidNote);
		
		var garyStupidText = document.createTextNode("Please click play button to view video.");
		garyStupidNote.appendChild(garyStupidText);
		
		objDivContainer.appendChild(garyStupidNoteContainer);
		

		this.isBuilt = true;
		
	}
	
	this.render = function(link,title){
		this.isVisible = true;
		
		var objDivContainer = document.getElementById('moviePlayer');
		
		var objMovieTitle = document.getElementById('moviePlayerTitle');
		objMovieTitle.innerHTML = title;
		
		var objEmbed = document.getElementById('moviePlayerEmbed');
		objEmbed.src = link;
		
		var WinXY = getWindowSize();
		var ScrollXY = getScrollXY();
		
		var left = ( ScrollXY[0] + (( WinXY[0] / 2 ) - 200 ) ) < 0 ? 0 : ( ScrollXY[0] + (( WinXY[0] / 2 ) - 200 ) );
		var top =  ( ScrollXY[1] + (( WinXY[1] / 2 ) - 190 ) ) < 0 ? 0 : ( ScrollXY[1] + (( WinXY[1] / 2 ) - 190 ) );
		
		
		objDivContainer.style.top = top+"px";
		objDivContainer.style.left = left+"px";
		objDivContainer.style.display = 'block';
		
	}
	
	this.hideMovie = function(){
		this.isVisible = false;
		var objDivContainer = document.getElementById('moviePlayer');
		objDivContainer.style.display = 'none';
	}
	
}

var mp = new MoviePlayer();
addEvent(window, 'onload', mp.attachPlayer);