var _opened = false;
var _lightbox = "#000000";
var _shown = false;
var _busy = false;


function lightBoxWhite() {
  if(_lightbox != '#ffffff') {
    new Effect.Highlight('body',{
      duration: 0.8,
      restorecolor:'#ffffff',startcolor:_lightbox,endcolor:'#ffffff' 
    });
    _lightbox = '#ffffff';
  }
}

function lightBoxBlack() {
  if(_lightbox != '#000000') {
    new Effect.Highlight('body',{
      duration: 0.8,
      restorecolor:'#000000',startcolor:_lightbox,endcolor:'#000000'
    });
    _lightbox = '#000000';
  }
}


// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
Position.getPageSize = function() {
  var xScroll, yScroll;

  if (window.innerHeight && window.scrollMaxY) {  
    xScroll = document.body.scrollWidth;
    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
    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
  pageHeight = Math.max(windowHeight, yScroll);

  // for small pages with total width less then width of the viewport
  pageWidth = Math.max(windowWidth, xScroll);

  return { page: { width: pageWidth, height: pageHeight }, window: { width: windowWidth, height: windowHeight } };
}

function updatePanelPosition() {
  var p = Position.getPageSize();
  $('container').style.marginTop = (p.page.height - 585)/2 + 'px';
}

function updateLightBox() {
  if(!_opened) return;
  var p = Position.getPageSize();
  if(p.window.width*p.window.height>850000) {
    lightBoxBlack()
  } else {
    lightBoxWhite()
  }
}

function makeRequest(url, whatLayer) {
        var httpRequest;

        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/xml');
                // See note below about this line
            }
        } 
        else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } 
                catch (e) {
                           try {
                                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                               } 
                             catch (e) {
                             	// The Browser does not support Ajax...
                             	}
                          }
                                       }

        if (!httpRequest) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        httpRequest.onreadystatechange = function() { displayContent(httpRequest, whatLayer); };
        httpRequest.open('GET', url, true);
        httpRequest.send(null);

    }

    function displayContent(httpRequest, whatLayer) {

        if (httpRequest.readyState == 4) {
            if (httpRequest.status == 200) {
                var response = httpRequest.responseText;
                if(response){
                  // This is where we actually get the content...
                  document.getElementById(whatLayer).innerHTML = response;
                  // OK. We have loaded the data.
                }
            } else {
                alert('There was a problem with the request.');
            }
        }

    }
function openUp(){
	if (_opened) return;
	new Effect.Fade('intro-contents');
	
	new Effect.Scale('container',250, {
		duration:1.2, queue:'end', scaleContent: false, scaleX: true, scaleY: false,
		scaleMode: {originalHeight: 550, originalWidth: 300},

		afterFinish:function() {
			Element.remove('intro-contents');
        	new Effect.Appear('main', { queue:'end'});
		}
    });
	_opened = true;
}


function transition(page){
	if (!_opened) return;
	new Effect.Fade('main', { queue:'end',
		afterFinish:function(){
			
			makeRequest(page+'.php', 'content');
			['home','biography','works','pricing','contact','forsale'].each(function(e) {
				Element.removeClassName(e, 'active');
			});
			Element.addClassName(page, 'active');
			new Effect.Appear('main', { delay:'1.75', queue:'end'});
		}
	});
	
	
}

function showContainer(){
	new Effect.Appear('container');
	
}

function openInfo(page){
	
	
	makeRequest(page+'.php', 'worksWindow');
	['thecardmechanic','shilohtio','eabc'].each(function(e) {
		Element.removeClassName(e, 'active');
	});
	Element.addClassName(page, 'active');
	
	}

function openPricing(page){
		new Effect.Fade('pricingInfo', { queue:'end',
		beforeStart:function(){
			if (_busy){
				Effect.cancel();
				_busy=true;
			}
			
		},
		afterFinish:function(){
			
			makeRequest(page+'.php', 'pricingInfo');

['pcTroubleshooting','networkTroubleshooting','hardwareInstallation','softwareInstallation','osInstallation','up2Date','virusCleaning','securitySweep','websiteDesign','pcPurchasing','pcSecurity','networkDesign'].each(function(e) {
			Element.removeClassName(e, 'active');
		});
		Element.addClassName(page, 'active');
			new Effect.Appear('pricingInfo', { delay:'0.5', queue:'end',
			afterFinish:function(){
				
				_busy=false;
			}
			});
		}
	});
	
}

function openPage(page){
	
	if (!_opened) {
		openUp();
		makeRequest(page+'.php', 'content');
		['home','biography','works','pricing','contact','forsale'].each(function(e) {
			Element.removeClassName(e, 'active');
		});
		
		Element.addClassName(page, 'active');
		new Effect.Highlight('pricingInfo', { startcolor: '#FF0000', endcolor: '#FFFFFF' });
	}
	else{
		transition(page);
		
	}

}
