// JavaScript Document
var content_id = 152;
		
function open_share() {
		more = "action=form";
		document.getElementById("share_page").innerHTML = "Loading...";
		//OPEN FORM	
		ajax_json_call('resources/js/ajax_php/shareAJAX.php', 'POST', more)
		
}	
function send_share(f) {		
		document.getElementById("share_page").innerHTML = "Loading...";
		//SEND SHARED PAGE
		var more = "action=send_email";
		more += "&your_name="+encodeURI(f.your_name.value);
		more += "&your_email="+encodeURI(f.your_email.value);
		more += "&friend_name="+encodeURI(f.friend_name.value);
		more += "&friend_email="+encodeURI(f.friend_email.value);
		more += "&comments="+encodeURI(f.comments.value);
		more += "&content_id="+content_id;
		//more += "&comment="+encodeURI(obj.comment.value);
		more += "&page_url="+location.href;
		ajax_json_call('resources/js/ajax_php/shareAJAX.php', 'POST', more)
 }
function share_with_friend(e) {
	if (page_loaded && document.getElementById("share_page").style.display != "block") {
	  var tempX = 0;
	  var tempY = 0;
	  var offset = 5;	
	  if (document.all) {
		tempX = event.clientX + document.documentElement.scrollLeft;//document.body.scrollLeft;
		tempY = event.clientY + document.documentElement.scrollTop;//document.body.scrollTop;
	  } 
	  else {
		tempX = e.pageX;
		tempY = e.pageY;
	  }

	// step 4
	if (tempX < 0){tempX = 0}
	if (tempY < 0){tempY = 0}
  	tempY = tempY/2 - document.getElementById("share_page").style.height/2;
	tempX = (tempX / 2) - document.getElementById("share_page").style.width/2;
	
	document.getElementById("share_page").style.left = tempX+"px";
	document.getElementById("share_page").style.top = tempY+"px";
	//alert(tempX+" "+tempY);
	document.getElementById("share_page").style.display = "block";
	
	//lightbox action
	showOverlay();
	if (browser == 'Internet Explorer'){
		hideSelects('hidden');
	}
	
	//OPEN FORM;
	open_share();
	}
 }
	
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 close_div() {
	if (document.getElementById("share_page")) {
		if (document.getElementById("event_iframe")) document.getElementById("event_iframe").style.display = "none";
		//lightbox close:
		if (document.getElementById('overlay').style.display != 'none') {
			hideOverlay();
			hideSelects('visible');
		}
		//
		document.getElementById("share_page").style.display = "none";
	}
 }

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;
  }
}

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';
	}
	
}
function page_onload() {
	page_loaded = true;	
}

//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 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;
	}
}
//lightbox prevention onuser click everywhere on a page

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;
}
addOnloadEvent(initOverlay);
addOnloadEvent(page_onload);
function initOverlay() {
	var objBody = document.getElementsByTagName("body").item(0);
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objOverlay.onclick = function () {
										close_div();
										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%';
	objBody.insertBefore(objOverlay, objBody.firstChild);
}

//STANDARD AJAX HANDLERS
function ajax_json_call(callUrl, callType, callVars, onCompleteFunc) {
	try {
		$.ajax({
			type: callType,
			url: callUrl,
			data: callVars,
			dataType: "json",
			success: function(data) {
				if (data) {
					process_success_ajax_json_call(data);
				} else {
					//error
				}
		 	},
			complete: function() {
				if (eval("typeof " + onCompleteFunc + " == 'function'")) {
					eval(onCompleteFunc+'()');
				}
			}
		});
	} catch (e) {
		//nothing for now
	}
}

function process_success_ajax_json_call(data) {
	if (data) {
		var alert_content="";
		$.each(data, function(id, dataHolder) {
			if ($("#"+id) && id!="alert_msg") {
				if (dataHolder.jAction.indexOf('insert')>-1) {
					$(dataHolder.jContentReturn)[dataHolder.jAction]('#'+dataHolder.jHelpId);
				} else {
					$("#"+id)[dataHolder.jAction](dataHolder.jContentReturn);
				}
			}
			if (id=="alert_msg") alert_content += content+"\n";
		});
		if (alert_content != "") alert(alert_content);
	}
	else {
		//process error here...
	}
}
