// JavaScript Document
var pageLoad = false;
var contact_form_html = "";
function pageLoaded() {
	pageLoad = true;
}
addOnloadEvent(pageLoaded);
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
//
function getPageSize(){
	
	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
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function showOverlay() {
	// prep objects
	var objOverlay = document.getElementById('overlay');
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
}
function hideOverlay() {
	// get objects
	//alert("hiding ... ");
	if (document.getElementById('overlay') && document.getElementById('overlay').style.display != 'none') {
		var objOverlay = document.getElementById('overlay');
		// hide lightbox and overlay
		objOverlay.style.display = 'none';
		if (document.getElementById("client_menu") && document.getElementById("displayMenu")) {
			document.getElementById("client_menu").style.display = 'none';
			var button = document.getElementById("displayMenu");
			button.innerHTML = "Full Client List";
		}
	}
}

function initOverlay() {
	
	var objBody = document.getElementsByTagName("body").item(0);
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objOverlay.onclick = function () {
										//hideOverlay();
										//alert("click");
										//close_adding_layer();
										//ToggleFloatingLayer('FloatingLayer', 0, '');
										//close_div();
										//close_reg();
										//reset_portfolio();
										close_portfolio();
										if (objOverlay.style.display != 'none') objOverlay.style.display = 'none';
										close_portfolio();
										
										return false;
										}
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '5000';
 	objOverlay.style.width = '100%';
	objOverlay.style.opacity = '0.9';
	objOverlay.style.backgroundColor = '#514c49';
	objBody.insertBefore(objOverlay, objBody.firstChild);
}
function show_portfolio(type_div) {
	hide_portfolios();
	if (browser == 'Internet Explorer'){
		hideSelects('hidden');
	}
	
	var obj = document.getElementById("portfolio");
	var parent_obj = document.getElementById("container");
	var coor = findPos(parent_obj);
	//var width_deduct = parseInt(document.getElementById("portfolio_link").offsetWidth);
	//alert(coor[0]+" - "+coor[1]);
	var left_pos = parseInt(coor[0]);
	obj.style.left = left_pos+"px";
	var top = 60;//coor[1];
	obj.style.top = top+"px";
	//if (obj.style.display == "none" || obj.style.display == "") {
		if (document.getElementById(type_div)) document.getElementById(type_div).style.display = "block";
	//	alert("sleepy");
		obj.style.display = "block";
		if (type_div == "portfolio_layer") {if (document.getElementById("account_email")) document.getElementById("account_email").focus();}
		else {if (document.getElementById("fname")) document.getElementById("fname").focus();}
		obj.style.zIndex = '10000';
		var brs=navigator.userAgent.toLowerCase();
		if(brs.indexOf("firefox") != -1){
			obj.style.position = 'fixed';
		}else{
			obj.style.position = 'absolute';
		}
		
	//}
	//else {
	//	if (document.getElementById(type_div)) document.getElementById(type_div).style.display = "none";
	//	obj.style.display = "none";
	//}
	window.onresize = function(){
		var obj = document.getElementById("portfolio");
	var parent_obj = document.getElementById("container");
	var coor = findPos(parent_obj);
	//var width_deduct = parseInt(document.getElementById("portfolio_link").offsetWidth);
	//alert(coor[0]+" - "+coor[1]);
	var left_pos = parseInt(coor[0]);
	//obj.style.left = left_pos+"px";
	//var top = 60;//coor[1];
	//obj.style.top = top+"px";
	}
}
addOnloadEvent(initOverlay);
function addOnloadEvent(fnc){
  if ( typeof window.addEventListener != "undefined" ) {
	window.addEventListener( "load", fnc, false );
  }
  else if ( typeof window.attachEvent != "undefined" ) {
    window.attachEvent( "onload", fnc );
  }
  else {
    if ( window.onload != null ) {
      var oldOnload = window.onload;
      window.onload = function ( e ) {
        oldOnload( e );
        window[fnc]();
      };
    }
    else
      window.onload = fnc;
  }
}
//Browser detect script origionally created by Peter Paul Koch at http://www.quirksmode.org/
var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;
addOnloadEvent(getBrowserInfo);
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 ToggleFloatingLayer(DivID, iState, object) {
	//table_preview here COULD be my_cart
	//set coordinats if exists:
	if (document.getElementById("mini_cart")) {
		//alert("hiya");
		var div_width = document.getElementById("mini_cart").style.width;
		div_width = div_width.substring(0, div_width.length-2);
		var cur_status = document.getElementById(DivID).style.display;
		if (iState == 1 && (cur_status == "none" || cur_status == "")) {
			showOverlay();
			if (document.getElementById("mini_title")) document.getElementById("mini_title").innerHTML='Mini-Cart';
			var y = getElementTop(object);
			var x = getElementLeft(document.getElementById("table_preview"));
			//alert("x="+x);
			y = 3 + y +document.getElementById("table_preview").clientHeight;//object.clientHeight;
			x = x - div_width + document.getElementById("table_preview").clientWidth;
			document.getElementById(DivID).style.top = y+'px';
			document.getElementById(DivID).style.left = x+'px';
			document.getElementById(DivID).style.width = div_width+'px';
			//alert(x+" - "+y+" - "+div_width);
			//alert(object.innerHTML.substring(0,1));
			sendRequest("action=display","mini_cart");
			if (browser == 'Internet Explorer'){
				hideSelects('hidden');
			}
		}
		if (iState == 0) {
			if (document.getElementById('overlay').style.display != 'none') {
				hideOverlay();
				hideSelects('visible');
			}
		}
		//end set coordinats.
		if(document.layers)	   //NN4+
		{
		   document.layers[DivID].display = iState ? "block" : "none";
		}
		else if(document.getElementById)	  //gecko(NN6) + IE 5+
		{
			var obj = document.getElementById(DivID);
			obj.style.display = iState ? "block" : "none";
		}
		else if(document.all)	// IE 4
		{
			document.all[DivID].style.dispaly = iState ? "block" : "none";
		}
	}
	else {
		//alert (document.location);
	}
}
function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}
function hideSelects(visibility){
	selects = document.getElementsByTagName('select');
	for(i = 0; i < selects.length; i++) {
		selects[i].style.visibility = visibility;
	}
}
function portfolio_pop(more,objid) {
		var objBody = document.getElementsByTagName("body").item(0);
	var objOverlay = document.getElementById("overlay");
	objOverlay.onclick = function () {
										//alert("test");
										close_portfolio();
										if (objOverlay.style.display != 'none') objOverlay.style.display = 'none';
										close_portfolio();										
										return false;
										}
	if (pageLoad) {
		if (objid) document.getElementById(objid).style.display = "none";
		if (document.getElementById('overlay') && document.getElementById('overlay').style.display != 'none') {
			close_adding_layer();
			ToggleFloatingLayer('FloatingLayer', 0, '');
			close_div();
			close_reg();
		}
		var obj_link = document.getElementById("a_portfolio_pop");
		if (obj_link.className != "account_link") {
			showOverlay();
			obj_link.className = "account_link";
			//document.getElementById("a_create_account").className = "account_link_back";
			var type_div = "portfolio_layer";//portfolio_layer
			show_portfolio(type_div);
			//refresh_user_account();
		}
		else {
			hideOverlay();
			obj_link.className = "account_link_back";
			hide_portfolios();
		}
		
		sendRequest_portfolio(more,"middle")
	}
}
function hide_portfolios() {
	if (!document.getElementById("account_around")) {
		if (document.getElementById("portfolio")) document.getElementById("portfolio").style.display = "none";
		if (document.getElementById("portfolio_msg")) {document.getElementById("portfolio_msg").innerHTML = "";document.getElementById("portfolio_msg").style.display = "none";}
		if (document.getElementById("create_portfolio")) document.getElementById("create_portfolio").style.display = "none";
		if (document.getElementById("portfolio_layer")) document.getElementById("portfolio_layer").style.display = "none";
		if (contact_form_html != "") document.getElementById(divhandler_popUp.divtag).innerHTML = contact_form_html;
			if (browser == 'Internet Explorer'){
				hideSelects('visible');
			}
	}
}
function reset_portfolio() {
	if (document.getElementById("a_create_account")) document.getElementById("a_create_account").className = "account_link_back";
	if (document.getElementById("a_portfolio_pop")) document.getElementById("a_portfolio_pop").className = "account_link_back";
	hide_portfolios();
}
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
function close_portfolio() {
	try {
		objOverlay = document.getElementById("overlay");
		hide_portfolios();
		reset_portfolio();
		if (objOverlay.style.display != 'none') objOverlay.style.display = 'none';
	}
	catch(e) {
		//oops error	
	}
}
// AJAX!
function newPopup(an, pItem) {
	var width='350';
	var height='290';
	var borderStyle='1px solid #8eb4ec';
	//;
	var href = pItem;
	var boxdiv = document.getElementById(href);
  
	refreshCheck = 0;
  if (boxdiv != null) {
    if (boxdiv.style.visibility=='hidden') {
	  refreshCheck = 1;
      move_box(an, boxdiv);
      boxdiv.style.visibility='visible';
    } else
     // boxdiv.style.visibility='hidden';
	  refreshCheck = 1;
    return false;
  }
if(refreshCheck == 0){
  boxdiv = document.createElement('div');
  boxdiv.setAttribute('id', pItem);
  boxdiv.style.visibility = 'visible';
  boxdiv.style.position = 'absolute';
  boxdiv.style.width = width + 'px';
  //boxdiv.style.height = height + 'px';
  boxdiv.style.border = borderStyle;
  boxdiv.style.overflow = 'visible';
  boxdiv.style.backgroundColor = '#fff';


  document.body.appendChild(boxdiv);
  move_box(an, boxdiv);
  
 	event_p(pItem)
  }

  return false;
}
function event_p(pItem) {
	var more = pItem;
	sendRequest_popup(more,pItem);
}
function handleDivTag_calendarm(divtag){
   var divtag;
   return divtag;
}

function createRequestObject_popup() {
	var req;
	if(window.XMLHttpRequest){
		req = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("There was a problem creating the XMLHttpRequest object");
		
	}
		//alert("Test");
	return req;
}
	
var http_popUp = createRequestObject_popup();
var divhandler_popUp = new handleDivTag_calendarm(null);

var id = "";
function sendRequest_portfolio(more,divtag) {
	//alert(divtag);
	if (document.getElementById(divtag)) document.getElementById(divtag).innerHTML = "<img src='/filebin/images/loading.gif'> Loading...";
	//document.getElementById("middle").innerHTML = '<img src="/filebin/images/sending.gif" border="0" />';
	http_popUp.open("get", "http://www.orbitmedia.com/pages/popup/194.php?id="+more);
	id = more;
	
	http_popUp.onreadystatechange = handleResponse_popup;
	divhandler_popUp.divtag = divtag;
	//if (document.getElementById(divhandler_popUp.divtag).innerHTML == "logout") window.location.reload();
	http_popUp.send(null);
	//alert("Test or something");
}
function handleResponse_popup() {   

	if(http_popUp.readyState == 4 && http_popUp.status == 200){
		var response = http_popUp.responseText;
		if(response) {
			contact_form_html = document.getElementById(divhandler_popUp.divtag).innerHTML;
				
			document.getElementById(divhandler_popUp.divtag).innerHTML = response;
			//google_track_thankyou();
			//alert(document.getElementById("flash2").innerHTML);
		    var url = "flashXML.php?id="+id;
			
    		var xml = new JKL.ParseXML( url );
    		var data = xml.parse();
			if (data) {    			
				flash(data["items"]["item"]["fla"],data["items"]["item"]["fW"],data["items"]["item"]["fH"]);
			}
		}
		//else document.getElementById(divhandler_popUp.divtag).innerHTML = "Loading...";
	}
}

function flash(fla,fW,fH) {
	//sendRequest_flash();
	// <![CDATA[
	var fo2 = new SWFObject(fla, "fotester2",fW, fH, "6", "#FFFFFF");
	fo2.addParam("wmode", "window");
	fo2.addParam("allowScriptAccess", "always");       
	fo2.write("flash2");
	// ]]>
}
