/*
Created By: Chris Campbell
Website: http://particletree.com
Date: 2/1/2006

Inspired by the lightboxAAA implementation found at http://www.huddletogether.com/projects/lightboxAAA/
*/

/*-------------------------------GLOBAL VARIABLES------------------------------------*/

var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;

/*-----------------------------------------------------------------------------------------------*/

//Browser detect script origionally created by Peter Paul Koch at http://www.quirksmode.org/
var closeButtonAAA = 'close.gif';

function getBrowserInfo() {
	if (checkIt('konqueror')) {
		browser = "Konqueror";
		OS = "Linux";
	}
	else if (checkIt('safari')) browser 	= "Safari"
	else if (checkIt('omniweb')) browser 	= "OmniWeb"
	else if (checkIt('opera')) browser 		= "Opera"
	else if (checkIt('webtv')) browser 		= "WebTV";
	else if (checkIt('icab')) browser 		= "iCab"
	else if (checkIt('msie')) browser 		= "Internet Explorer"
	else if (!checkIt('compatible')) {
		browser = "Netscape Navigator"
		version = detect.charAt(8);
	}
	else browser = "An unknown browser";

	if (!version) version = detect.charAt(place + thestring.length);

	if (!OS) {
		if (checkIt('linux')) OS 		= "Linux";
		else if (checkIt('x11')) OS 	= "Unix";
		else if (checkIt('mac')) OS 	= "Mac"
		else if (checkIt('win')) OS 	= "Windows"
		else OS 								= "an unknown operating system";
	}
}

function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

/*-----------------------------------------------------------------------------------------------*/

Event.observe(window, 'load', initialize, false);
Event.observe(window, 'load', getBrowserInfo, false);
Event.observe(window, 'unload', Event.unloadCache, false);

var lightboxAAA = Class.create();

lightboxAAA.prototype = {

	yPos : 0,
	xPos : 0,

	initialize: function(ctrl) {
	//alert("init");
		this.content = ctrl.href;
		Event.observe(ctrl, 'click', this.activate.bindAsEventListener(this), false);
		ctrl.onclick = function(){return false;};
	},

	// Turn everything on - mainly the IE fixes
	activate: function(){
	//alert("activate");
		if (browser == 'Internet Explorer'){
			this.getScroll();
			this.prepareIE('100%', 'hidden');
			this.setScroll(0,0);
			this.hideSelects('hidden');
		}
		this.displaylightboxAAA("block");
	},

	// Ie requires height to 100% and overflow hidden or else you can scroll down past the lightboxAAA
	prepareIE: function(height, overflow){
	//alert("prepareIE");
		bod = document.getElementsByTagName('body')[0];
		bod.style.height = height;
		bod.style.overflow = overflow;

		htm = document.getElementsByTagName('html')[0];
		htm.style.height = height;
		htm.style.overflow = overflow;
	},

	// In IE, select elements hover on top of the lightboxAAA
	hideSelects: function(visibility){
	//alert("hideselets");
		selects = document.getElementsByTagName('select');
		for(i = 0; i < selects.length; i++) {
			selects[i].style.visibility = visibility;
		}
	},

	// Taken from lightboxAAA implementation found at http://www.huddletogether.com/projects/lightboxAAA/
	getScroll: function(){
	//alert("getscroll");
		if (self.pageYOffset) {
			this.yPos = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			this.yPos = document.documentElement.scrollTop;
		} else if (document.body) {
			this.yPos = document.body.scrollTop;
		}
	},

	setScroll: function(x, y){
		window.scrollTo(x, y);
	},

	displaylightboxAAA: function(display){
	//alert("displaylightbox");
		$('overlayAAA').style.display = display;
		$('lightboxAAA').style.display = display;
		if(display != 'none') this.loadInfo();
	},

	// Begin Ajax request based off of the href of the clicked linked
	loadInfo: function() {
	//alert("loadinfo");
		var myAjax = new Ajax.Request(
        this.content,
        {method: 'post', parameters: "", onComplete: this.processInfo.bindAsEventListener(this)}
		);
	},

	// Display Ajax response
	processInfo: function(response){
	//alert("processinfo");
		info = "<div id='lbContentAAA'>" + response.responseText + "</div>";
		new Insertion.Before($('lbLoadMessageAAA'), info)
		$('lightboxAAA').className = "done";
		//	 formObject = document.aaaMobile;
			//alert(object23);
		//	if(formObject!=undefined)
	// {
	//		document.aaaMobile.phone1.value=document.testForm.to1.value;
	//		document.aaaMobile.phone2.value=document.testForm.to2.value;
	//	        document.aaaMobile.phone3.value=document.testForm.to3.value;
      //   }
		// alert(document.aaaMobile.phone1.value);
		// alert(document.testForm.to1.value+"procesinfo");
		this.actions();
	},

	// Search through new links within the lightboxAAA, and attach click event
	actions: function(){
	//alert("inside actions function");
		lbActions = document.getElementsByClassName('lbAction');

		for(i = 0; i < lbActions.length; i++) {
			Event.observe(lbActions[i], 'click', this[lbActions[i].rel].bindAsEventListener(this), false);
			lbActions[i].onclick = function(){return false;};
		}

		closeLb = document.getElementsByClassName('closeAction');
	    for(i = 0; i < closeLb.length; i++) {
			Event.observe(closeLb[i], 'click', this[closeLb[i].rel].bindAsEventListener(this), false);
			//closeLb[i].onclick = function(){return false;};
		}
		
		mouseMove = document.getElementsByClassName('Move');
	    for(i = 0; i < mouseMove.length; i++) {
			//  alert(mouseMove[i].name);
			Event.observe(mouseMove[i], 'keyup', this.testing.bindAsEventListener(this), false);
			//mouseMove[i].onkeyup = function(){return false;};
			//alert("hello....2");
		      
		}
		
	
	},

	// Example of creating your own functionality once lightboxAAA is initiated
	insert: function(e){
	//alert("insert");
	                   var test1Value = document.aaaMobile.phone1.value;
	                   if(test1Value=="")
	                   {
	                     test1Value = " ";
	                   }
	             	   var test2Value = document.aaaMobile.phone2.value;
		           var test3Value = document.aaaMobile.phone3.value;
				   var subject = document.aaaMobile.subject.value;
				//  var body = document.aaaMobile.body.value;
				   var modelNumber = document.aaaMobile.model.value;
				   var modelNumber1 = document.aaaMobile.modelNumber.value;
			 	   link = Event.element(e).parentNode;
				   var finalAction = link.href + "subject="+subject+"&toField1="+test1Value+"&toField2="+test2Value+"&toField3="+test3Value+"&model="+modelNumber+"&modelNumber="+modelNumber1 ;
			         //  alert(finalAction);
				   Element.remove($('lbContentAAA'));
			 	 	   var myAjax = new Ajax.Request(
				 			  finalAction,
				 			  {method: 'post', parameters: "", onComplete: this.processInfo.bindAsEventListener(this)}
				   );
	},

	// Example of creating your own functionality once lightboxAAA is initiated
	deactivate: function(){
	
//	 formObject = document.aaaMobile;
	//alert(formObject);
//	if(formObject!=undefined)
//	{
//		      	document.testForm.to1.value=document.aaaMobile.phone1.value;
//		        document.testForm.to2.value=document.aaaMobile.phone2.value;
//		        document.testForm.to3.value=document.aaaMobile.phone3.value;
//	}else{
//	                document.testForm.to1.value="";
//	                document.testForm.to2.value="";
//		        document.testForm.to3.value="";
//	}

		     //   alert(document.aaaMobile.phone1.value);
		     //   alert(document.aaaMobile.phone2.value);
		     //   alert(document.aaaMobile.phone3.value);
           //     alert(document.testForm.to1.value+"deactivate");
           //     alert(document.testForm.to2.value+"deactivate");
           //     alert(document.testForm.to3.value+"deactivate");
		Element.remove($('lbContentAAA'));

		if (browser == "Internet Explorer"){
			this.setScroll(0,this.yPos);
			this.prepareIE("auto", "auto");
			this.hideSelects("visible");
		}

		this.displaylightboxAAA("none");

		},
		
		testing: function(textBoxName){
					box = Event.element(textBoxName);
					boxName = box.name;
					valueText = box.value;
					boxLength = valueText.length;
		    if(boxName=="phone1"&&boxLength==3)
		    {
		  document.aaaMobile.phone2.focus();
		    }
		       if(boxName=="phone2"&&boxLength==3)
		    		    {
		    		  document.aaaMobile.phone3.focus();
		    }
		      // if(boxName=="phone3"&&boxLength==4)
		    //		    {
		    //		  document.submitForm.focus();
		    //}
	       }	
		

}

/*-----------------------------------------------------------------------------------------------*/

// Onload, make all links that need to trigger a lightboxAAA active
function initialize(){
	lbox = document.getElementsByClassName('lbOn');
	if(lbox.length > 0){	
		addlightboxAAAMarkup();
		//lbox = document.getElementsByClassName('lbOn');
	for(i = 0; i < lbox.length; i++) {
		valid = new lightboxAAA(lbox[i]);
		}

	}

}

// Add in markup necessary to make this work. Basically two divs:
// overlayAAA holds the shadow
// lightboxAAA is the centered square that the content is put into.
function addlightboxAAAMarkup() {
	bod 				= document.getElementsByTagName('body')[0];
	overlayAAA 			= document.createElement('div');
	overlayAAA.id		= 'overlayAAA';
	
	bod.insertBefore(overlayAAA, bod.firstChild);

	lb					= document.createElement('div');
	lb.id				= 'lightboxAAA';
	lb.className 	= 'loading';

	lb.innerHTML	= '<div id="lbLoadMessageAAA">' +
						  '<p>Loading</p>' +
						  '</div>';
	bod.insertBefore(lb, overlayAAA.nextSibling);

	// create link
		var objLink = document.createElement("a");
		objLink.setAttribute('href','#');
		objLink.setAttribute('id','closeButtonLink');
		//objLink.setAttribute('class','closeAction');
		objLink.className = 'closeAction';
		objLink.setAttribute('rel','deactivate');
		objLink.setAttribute('title','Click to close');
		//objLink.onclick = function () {this.deactivate();return false;}
		lb.appendChild(objLink);

		// preload and create close button image
		var imgPreloadCloseButton = new Image();

		// if close button image found,
		imgPreloadCloseButton.onload=function(){

			var objCloseButton = document.createElement("img");
			objCloseButton.src = closeButtonAAA;
			objCloseButton.setAttribute('id','closeButtonAAA');
			objCloseButton.style.position = 'absolute';
			objCloseButton.style.zIndex = '200';
			objLink.appendChild(objCloseButton);

			return false;
		}

		imgPreloadCloseButton.src = closeButton;

    // create details div, a container for the caption and keyboard message
	var objLightboxDetails = document.createElement("div");
	objLightboxDetails.setAttribute('id','lightboxDetailsAAA');
	lb.appendChild(objLightboxDetails);

	// create caption
	var objCaption = document.createElement("div");
	objCaption.setAttribute('id','lightboxCaptionAAA');
	objCaption.style.display = 'none';
	objLightboxDetails.appendChild(objCaption);

	// create keyboard message
	var objKeyboardMsg = document.createElement("div");
	objKeyboardMsg.setAttribute('id','keyboardMsgAAA');
	objKeyboardMsg.innerHTML = 'press <a href="#" id="bottomMessage" class="closeAction" rel="deactivate"><kbd>x</kbd></a> to close';
	objLightboxDetails.appendChild(objKeyboardMsg);


	//bod.appendChild(overlayAAA);
	//bod.appendChild(lb);


}

