

//	CGeneric Javascript class
function CGeneric(oName)
{
	//	Name of this instantiation
	this.oName=oName;
	
	//	Generic functions
	this.GetViewportDimensions=CGeneric_GetViewportDimensions;
	this.FindPosition=CGeneric_FindPosition;
	
	//	Ajax Functions
	this.Ajax=new CGeneric_Ajax();
	this.Ajax.root=this;
	
	//	Frame functions
	this.Frames=new CGeneric_Frames();
	this.Frames.root=this;
	
	//	Nav functions
	this.nav=new CGeneric_Nav();
	this.nav.root=this;
}

//	Grab the height/width of the browser's viewport
function CGeneric_GetViewportDimensions()
{
	//
	var viewportwidth;
	var viewportheight;

	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if(typeof window.innerWidth != 'undefined'){
		viewportwidth = window.innerWidth;
		viewportheight = window.innerHeight;
	}

	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else if(typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0){
		viewportwidth = document.documentElement.clientWidth;
		viewportheight = document.documentElement.clientHeight;
	}
	
	// older versions of IE
	else{
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
		viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}
	
	//
	var toReturn=new Object();
	toReturn['width']=viewportwidth;
	toReturn['height']=viewportheight;
	
	return toReturn;
}

//	Grab absolute position of an element
function CGeneric_FindPosition(o)
{
	//	Calc Position
	var oTemp=o;
	var curleft = curtop = 0;
	if (oTemp.offsetParent) {
		curleft = oTemp.offsetLeft
		curtop = oTemp.offsetTop
		while(oTemp = oTemp.offsetParent)
		{
			curleft += oTemp.offsetLeft
			curtop += oTemp.offsetTop
		}
	}
	
	//	Calc Width/Height
	var width=height=0;
	if(typeof o.clip !== "undefined"){
		width=o.clip.width;
		height=o.clip.height;
	}
	else{
		if(o.style.pixelWidth){
			width=o.style.pixelWidth;
			height=o.style.pixelWidth;
		}
		else{
			width=o.offsetWidth;
			height=o.offsetHeight;
		}
	}
	
	//	Return in a nice object
	var toReturn=new Object();
	toReturn['left']=curleft;
	toReturn['top']=curtop;
	//
	toReturn['width']=width;
	toReturn['height']=height;
	//
	toReturn['right']=toReturn['left']+toReturn['width'];
	toReturn['bottom']=toReturn['top']+toReturn['height'];
	
	return toReturn;
}

//	CGeneric Ajax Object
function CGeneric_Ajax()
{
	//
	this.CreateXMLHttpRequestObject=CGeneric_Ajax_CreateXMLHttpRequestObject;
	this.Callback=CGeneric_Ajax_Callback;
	this.SimpleResponse=CGeneric_Ajax_SimpleResponse;
	this.PostFormWithAjax=CGeneric_Ajax_PostFormWithAjax;
	
	//
	this.root=null;
}

//	Create an ajax object we can use
function CGeneric_Ajax_CreateXMLHttpRequestObject()
{
	var req;
	
	// branch for native XMLHttpRequest object
	if(window.XMLHttpRequest){
		req = new XMLHttpRequest();
	}
	else if(window.ActiveXObject){
		req = new ActiveXObject( "Microsoft.XMLHTTP" );
	}
	else{
		alert("Error: Your browser doesn't seem to support our type of AJAX object. Please notify the admins.");
		return false;
	}
	
	return req;
}

//
function CGeneric_Ajax_Callback(http,callbackName)
{
	if(http.readyState==4){
		if(http.status == 200 ){
			eval(callbackName+"(http.responseText);");
		}
	}
	
	return true;
}

//
function CGeneric_Ajax_SimpleResponse(response)
{
	alert(response);
}

//
function CGeneric_Ajax_PostFormWithAjax(f,callbackName)
{
	//	Try to create an ajax object
	var http=this.CreateXMLHttpRequestObject();
	if(!http){
		return false;
	}
	
	//	Grab all form elements and turn them into a serialized string
	var formData='';
	if(f){
		for(var a=0;a<f.elements.length;a++){
			var element=f.elements[a];
			if(element.name!=''){
				formData+="&"+element.name+"="+escape(element.value);
			}
		}
		formData=formData.substr(1);
		
		//	Does the form have any action? If not, use the ajax interface
		if(!f.action){
			f.action=this.interfaceURL;
		}
	}
	
	//	Is there a callback name set? If not,
	//	use the built-in one
	if(!callbackName){
		callbackName=this.root.oName+'.Ajax.SimpleResponse';
	}
	
	//
	http.open("POST",f.action,true);
	
	//	Use a trick to keep us inside our object
	toEval="http.onreadystatechange=function(){"+this.root.oName+".Ajax.Callback(http,'"+callbackName+"');}";
	eval(toEval);
	
	//	Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", formData.length);
	http.setRequestHeader("Connection", "close");
	
	http.send(formData);
	
	return true;
}

//	Frames functions object
function CGeneric_Frames()
{
	//	Variables
	this.bMusicPlayerEnabled=true;
	
	//
	this.BustOutOfExternalFrames=CGeneric_Frames_BustOutOfExternalFrames;
	this.BustOutOfAnyFrames=CGeneric_Frames_BustOutOfAnyFrames;
	this.EnsureMusicPlayer=CGeneric_Frames_EnsureMusicPlayer;
	this.FrameTitleToWindow=CGeneric_Frames_FrameTitleToWindow;
	
	//
	this.AreWeInFrames=CGeneric_Frames_AreWeInFrames;
	this.AreWeInExternalFrames=CGeneric_Frames_AreWeInExternalFrames;
	
	//
	this.root=null;
}

//
function CGeneric_Frames_BustOutOfExternalFrames()
{
	//
	if(!this.AreWeInFrames()){
		return false;
	}
	
	//	Internal Frames?
	if(!this.AreWeInExternalFrames()){
		return false;
	}
	
	//	Bust out!
	top.location=window.location;
	
	//	Just in case for some weird reason,
	//	we run into timing problems between busting
	//	out of an external frame and trying to add
	//	the music player, just disable the music
	//	player at this point
	this.bMusicPlayerEnabled=false;
}

//
function CGeneric_Frames_BustOutOfAnyFrames()
{
	//	If this function is being called,
	//	its safe to assume we should NOT
	//	initialize the music player frames
	this.bMusicPlayerEnabled=false;
	
	//
	if(!this.AreWeInFrames()){
		return false;
	}
	
	//	Bust out!
	top.location=window.location;
}

//
function CGeneric_Frames_EnsureMusicPlayer()
{
	//
	if(!this.bMusicPlayerEnabled){
		return false;
	}
	
	//	Make sure we're not in any external frames
	this.BustOutOfExternalFrames();
	
	//	If we're in frames, bounce
	if(this.AreWeInFrames()){
		return false;
	}
	
	//	Bounce!
	window.location='http://www.MikePeralta.com/index.php?Action=MusicPlayerFrames&URL='+escape(window.location);
	
	return true;
}

//
function CGeneric_Frames_FrameTitleToWindow(titleID)
{
	//
	if(!this.AreWeInFrames()){
		return false;
	}
	
	//
	if(!document.title){
		return false;
	}
	
	//
	var oTitle=parent.document.getElementById(titleID);
	if(!oTitle){
		return false;
	}
	
	//
	try{
		oTitle.innerHTML=document.title;
	}
	catch(e){
		window.title=document.title;
	}
	
	return true;
}

//
function CGeneric_Frames_AreWeInFrames()
{
	return self!=top;
}

//
function CGeneric_Frames_AreWeInExternalFrames()
{
	//	Must be in frames, in the first place
	if(!this.AreWeInFrames()){
		return false;
	}
	
	//
	var location=false;
	try{
		location=parent.location.toString();
	}
	catch(e){
		return true;
	}
	
	return false;
}

//	Nav functions object
function CGeneric_Nav()
{
	//
	this.TopNav_Mouseoverout=CGeneric_Nav_TopNav_Mouseoverout;
	
	//	Root
	this.root=null;
}

//	
function CGeneric_Nav_TopNav_Mouseoverout(mainLink_id,subLink_id,bOnOff)
{
	//	Grab objects
	var oMainLink=document.getElementById(mainLink_id);
	if(!oMainLink){
		alert("Error: Unable to activate navigation bar sub menu - couldn't grab mainLink");
		return false;
	}
	var oSubLink=document.getElementById(subLink_id);
	if(!oSubLink){
		alert("Error: Unable to activate navigation bar sub menu - couldn't grab subLink");
		return false;
	}
	
	//	Grab viewport's dimensions
	var viewportDimensions=this.root.GetViewportDimensions();
	
	//	Show then position
	if(bOnOff){
		
		//	Where is the main link?
		var mainPos=this.root.FindPosition(oMainLink);
		var subPos=this.root.FindPosition(oSubLink);
		
		//	Should we place the sub link aligned to the left or right?
		if(mainPos['left']<viewportDimensions['width']/2){
			oSubLink.style.left=mainPos['left'];
		}
		else{
			oSubLink.style.left=mainPos['left']-mainPos['width'];
		}
		
		//	Turn on the sub block
		oSubLink.style.display='block';
	}
	else{
		oSubLink.style.display='none';
	}
	
	return true;
}






