/* MMEDIT: Pulled scripts from moofnet for what we need. */
function _js(lib){
	document.writeln("<script type=\"text/javascript\" src=\"" + lib + "\"><\/script>");
}
function _css(sheet, conditional){
	if(conditional){
		document.writeln("<!--[if " + conditional + "]>");
	}
	document.writeln("<link rel=\"StyleSheet\" type=\"text/css\" href=\"" + sheet + "\" />");
	if(conditional){
		document.writeln("<![endif]-->");
	}
}
//include core
_js("/pixiray/object.js");
_js("/pixiray/string.js");
_css("/pixiray/pixiray.css");

PixiRay = {
	Messages: [
		"Hello!  Welcome to the Rinn website, where we are committed to making quality products with the needs of dental professionals and their patients in mind.",
		"Welcome to the New Products preview.  Sneak a peek at these amazing new products from Rinn!"
	],
	
	Elements: {
		Container: false,
		Output: false,
		Message: false,
		Visual: false
	},
	Full: {
		X: 0,
		Y: 200,
		Width: 225,
		Height: 300
	},
	Small: {
		X: 0,
		Y: 0,
		Width: 0,
		Height: 100
	},
	Increment: {
		X: 0,
		Y: 0,
		Width: 0,
		Height: 0
	},
	FullOffset: {
		X: -100,
		Y: 0
	},
	SmallOffset: {
		X: 110,
		Y: 0
	},
	
	MessageIndex: -1,     //current message index to display.
	Timer: null,          //transition step timer
	State: 0,             //current state
	Direction: 0,         //current direction (if in transition)
	Steps: 50,            //number of steps to take in transition
	Interval: 40,         //timer interval for transition redraw
	CookieTimeout: 720,     //cookie flag timeout in hours 
	AutoCloseTimer: null, //timer for autoclose
	AutoCloseDelay: 30,   //autoclose delay (in seconds)
	
	Constants: {
		//State constants
		Full: 1,
		Small: -1,
		InTransition: 0,
		
		//Direction constants
		Opening: 1,
		NoDirection: 0,
		Closing: -1
	},
	
	Transition: {
		StepToFull: function(){
			var pr_x = $(PixiRay.Elements.Container).getX();
			var pr_y = $(PixiRay.Elements.Container).getY();
			pr_x += PixiRay.Increment.X;
			pr_y += PixiRay.Increment.Y;
			if(PixiRay.Increment.X > 0){
				if(pr_x > PixiRay.Full.X){
					pr_x = PixiRay.Full.X;
				}
			}else{
				if(pr_x < PixiRay.Full.X){
					pr_x = PixiRay.Full.X;
				}
			}
			if(PixiRay.Increment.Y > 0){
				if(pr_y > PixiRay.Full.Y){
					pr_y = PixiRay.Full.Y;
				}
			}else{
				if(pr_y < PixiRay.Full.Y){
					pr_y = PixiRay.Full.Y;
				}
			}
			$(PixiRay.Elements.Container).setPosition(pr_x, pr_y);
			if(PixiRay.Timer==null){
				PixiRay.Timer = setInterval("PixiRay.Transition.StepToFull()",PixiRay.Interval);
			}else if(pr_x==PixiRay.Full.X && pr_y==PixiRay.Full.Y){
				$(PixiRay.Elements.Visual).setDimensions(PixiRay.Full.Width, PixiRay.Full.Height);
				clearInterval(PixiRay.Timer);
				PixiRay.Timer = null;
				
				$(PixiRay.Elements.Output).show('inline-block');
				PixiRay.Elements.Message.innerHTML = PixiRay.Messages[PixiRay.MessageIndex];				
				PixiRay.State = PixiRay.Constants.Full;
				PixiRay.Direction = PixiRay.Constants.NoDirection;
				PixiRay.AutoCloseTimer = setTimeout("PixiRay.Close()",PixiRay.AutoCloseDelay * 1000);
			}else{
				var pr_w = $(PixiRay.Elements.Visual).getWidth();
				var pr_h = $(PixiRay.Elements.Visual).getHeight();
				pr_w += PixiRay.Increment.Width;
				pr_h += PixiRay.Increment.Height;
				if(pr_w > PixiRay.Full.Width || pr_h > PixiRay.Full.Height){
					pr_w = PixiRay.Full.Width;
					pr_h = PixiRay.Full.Height;
				}
				$(PixiRay.Elements.Visual).setDimensions(pr_w, pr_h);
			}
		},
		StepToSmall: function(){
			var pr_x = $(PixiRay.Elements.Container).getX();
			var pr_y = $(PixiRay.Elements.Container).getY();
			pr_x -= PixiRay.Increment.X;
			pr_y -= PixiRay.Increment.Y;			
			if(PixiRay.Increment.X > 0){
				if(pr_x < PixiRay.Small.X){
					pr_x = PixiRay.Small.X;
				}
			}else{
				if(pr_x > PixiRay.Small.X){
					pr_x = PixiRay.Small.X;
				}
			}
			if(PixiRay.Increment.Y > 0){
				if(pr_y < PixiRay.Small.Y){
					pr_y = PixiRay.Small.Y;
				}
			}else{
				if(pr_y > PixiRay.Small.Y){
					pr_y = PixiRay.Small.Y;
				}
			}
			$(PixiRay.Elements.Container).setPosition(pr_x, pr_y);
			if(PixiRay.Timer==null){
				PixiRay.Timer = setInterval("PixiRay.Transition.StepToSmall()",PixiRay.Interval);
			}else if(pr_x==PixiRay.Small.X && pr_y==PixiRay.Small.Y){
				$(PixiRay.Elements.Visual).setDimensions(PixiRay.Small.Width, PixiRay.Small.Height);
				clearInterval(PixiRay.Timer);
				PixiRay.Timer = null;
				
				PixiRay.State = PixiRay.Constants.Small;
				PixiRay.Direction = PixiRay.Constants.NoDirection;
			}else{
				var pr_w = $(PixiRay.Elements.Visual).getWidth();
				var pr_h = $(PixiRay.Elements.Visual).getHeight();
				pr_w -= PixiRay.Increment.Width;
				pr_h -= PixiRay.Increment.Height;
				if(pr_w < PixiRay.Small.Width || pr_h < PixiRay.Small.Height){
					pr_w = PixiRay.Small.Width;
					pr_h = PixiRay.Small.Height;
				}
				$(PixiRay.Elements.Visual).setDimensions(pr_w, pr_h);
			}
		}
	},
	
	//Functions
	Initialize: {
		Create: function(){
			//Create PixiRay
			PixiRay.Elements.Container = $(document.body).createNode("div","PixiRayContainer");
			PixiRay.Elements.Visual = PixiRay.Initialize.BuildSwfObject();
			PixiRay.Elements.Output = $(document.body).createNode("table","PixiRayOutput",null,{cellspacing:0, cellpadding:0,cellSpacing:0, cellPadding:0});
			
			var tbody; //to fix an IE bug.  I do love IE bugs.  They make my job more interesting.
			var row;
			var cell;
			
			tbody = $(document.body).createNode("tbody");

			//top row
			row = $(tbody).createNode("tr");
			cell = $(row).createNode("td","PixiRayOutputTop",null,{colspan:2,colSpan:2});	//colSpan is an IE bugfix.  Again.
			$(cell).createNode("a","PixiRayOutputClose",null,{href:"javascript:PixiRay.Close();"},null,"close").addTo(cell);
			$(cell).addTo(row);
			$(row).addTo(tbody);
			//output row
			row = $(tbody).createNode("tr");
			cell = $(row).createNode("td","PixiRayOutputLeft");
			$(cell).createNode("img",null,null,{src:"/pixiray/output/left.gif",alt:""}).addTo(cell);
			$(cell).addTo(row);
			PixiRay.Elements.Message = $(row).createNode("td","PixiRayOutputContent");
			$(PixiRay.Elements.Message).addTo(row);
			$(row).addTo(tbody);
			//bottom row
			row = $(tbody).createNode("tr");
			cell = $(row).createNode("td","PixiRayOutputBottom",null,{colspan:2,colSpan:2},null," ");
			$(cell).addTo(row);
			$(row).addTo(tbody);
			
			$(tbody).addTo(PixiRay.Elements.Output);
			$(PixiRay.Elements.Visual).addTo(PixiRay.Elements.Container);
			$(PixiRay.Elements.Output).addTo(PixiRay.Elements.Container);
		},
		Detect: function(){
//MMEDIT:  Updating for new layout
//			var output = $(document.body).firstElement();
			var output = $('Header').firstElement();
			//find my full positions
			if(PixiRay.Full.X==0){
				PixiRay.Full.X = Math.round(($(output).getWidth() - PixiRay.Full.Width)/2);
			}
			PixiRay.Full.X += $(output).getX() + PixiRay.FullOffset.X;
			PixiRay.Full.Y += $(output).getY() + PixiRay.FullOffset.Y;
			//and the small position and width
			PixiRay.Small.X = $(output).getX() + PixiRay.SmallOffset.X;
			PixiRay.Small.Y = $(output).getY() + PixiRay.SmallOffset.Y;
			PixiRay.Small.Width = Math.round(PixiRay.Full.Width * (PixiRay.Small.Height/PixiRay.Full.Height));
			//and incrementing values
			PixiRay.Increment.X = Math.round((PixiRay.Full.X-PixiRay.Small.X) / PixiRay.Steps);
			PixiRay.Increment.Y = Math.round((PixiRay.Full.Y-PixiRay.Small.Y) / PixiRay.Steps);
			PixiRay.Increment.Width = Math.round((PixiRay.Full.Width-PixiRay.Small.Width) / PixiRay.Steps);
			PixiRay.Increment.Height = Math.round(PixiRay.Increment.Width * (PixiRay.Full.Height / PixiRay.Full.Width));
		},
		Position: function(){
			$(PixiRay.Elements.Container).addTo(document.body);
			$(PixiRay.Elements.Container).setPosition(PixiRay.Small.X, PixiRay.Small.Y);
			$(PixiRay.Elements.Visual).setDimensions(PixiRay.Small.Width, PixiRay.Small.Height);
			$(PixiRay.Elements.Output).hide();
			
			PixiRay.State = PixiRay.Constants.Small;
			PixiRay.Direction = PixiRay.Constants.NoDirection;
		},
		BuildSwfObject: function(){			
			var div = $(document.body).createNode("div","PixiRayVisual");
			div.innerHTML = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" width=\"100%\" height=\"100%\" id=\"PixiRaySwf\" align=\"middle\">" +
				"	<param name=\"allowScriptAccess\" value=\"sameDomain\" />" +
				"	<param name=\"allowFullScreen\" value=\"false\" />" +
				"	<param name=\"movie\" value=\"/pixiray/pixiray.swf\" />" +
				"	<param name=\"quality\" value=\"high\" />" +
				"	<param name=\"wmode\" value=\"transparent\" />" +
				"	<embed src=\"/pixiray/pixiray.swf\" wmode=\"transparent\" quality=\"high\" width=\"100%\" height=\"100%\" name=\"FlashPromos\" align=\"middle\" allowScriptAccess=\"sameDomain\" allowFullScreen=\"false\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.adobe.com/go/getflashplayer\" />" +
				"</object>";
			return div;
		}
	},
	
	Resize: function(){
		PixiRay.Initialize.Detect();
		if(PixiRay.State != PixiRay.Constants.InTransition){
			if(PixiRay.State==PixiRay.Constants.Small){
				$(PixiRay.Elements.Container).setPosition(PixiRay.Small.X, PixiRay.Small.Y);
			}else{
				$(PixiRay.Elements.Container).setPosition(PixiRay.Full.X, PixiRay.Full.Y);
			}
		}
	},
	
	Init: function(MsgIndex,Cookie){
		PixiRay.Initialize.Detect();
		PixiRay.Initialize.Create();
		PixiRay.Initialize.Position();
		if(MsgIndex!=undefined){
			if(Cookie!=undefined){
				setTimeout("PixiRay.CheckInitSay('" + Cookie + "'," + MsgIndex + ")",1);
			}else{
				PixiRay.Say(MsgIndex);
			}
		}
	},
	Say: function(Index){
		if(Index < 0 || Index >= PixiRay.Messages.length){
			alert("Bad Message Index");
			return;
		}
		PixiRay.MessageIndex = Index;
		if(PixiRay.State==PixiRay.Constants.Full){
			PixiRay.Elements.Message.innerHTML = PixiRay.Messages[PixiRay.MessageIndex];
		}else if(PixiRay.State==PixiRay.Constants.Small || PixiRay.Direction==PixiRay.Constants.Closing){
			if(PixiRay.Direction==PixiRay.Constants.Closing){
				clearInterval(PixiRay.Timer);
				PixiRay.Timer = null;
			}
			PixiRay.State = PixiRay.Constants.InTransition;
			PixiRay.Direction = PixiRay.Constants.Opening;
			PixiRay.Transition.StepToFull();
		}
	},
	Close: function(){
		if(PixiRay.State==PixiRay.Constants.Small){
			return;
		}
		if(PixiRay.State==PixiRay.Constants.Full || PixiRay.Direction==PixiRay.Constants.Opening){
			if(PixiRay.Direction==PixiRay.Constants.Opening){
				clearInterval(PixiRay.Timer);
				PixiRay.Timer = null;
			}else{
				$(PixiRay.Elements.Output).hide();
				if(PixiRay.AutoCloseTimer != null){
					clearTimeout(PixiRay.AutoCloseTimer);
					PixiRay.AutoCloseTimer = null;
				}
			}
			PixiRay.State = PixiRay.Constants.InTransition;
			PixiRay.Direction = PixiRay.Constants.Closing;
			PixiRay.Transition.StepToSmall();
		}
	},
	CheckInitSay: function(Cookie, Index){
		var expires = new Date();
		expires.setHours(expires.getHours()+PixiRay.CookieTimeout);
		var c_idx = document.cookie.indexOf(Cookie + "=");
		document.cookie = Cookie+"="+true+";expires="+expires.toGMTString();
		if(c_idx > -1){
			return;
		}
		PixiRay.Say(Index);
	}
};

