/* * MAIN FUNCTIONS * */
function openTerms()
{
	alert('Terms');
}

function openPrivacy()
{
	alert('Privacy');
}

function toggleDiv(id,linkobj)
{
	obj=document.getElementById(id);
	if (obj.style.display=='none')
	{
		obj.style.display='block';
	} else {
		obj.style.display='none';
	}
}


function getXYpos(elem) {
   if (!elem) {
      return {"x":0,"y":0};
   }
   var xy={"x":elem.offsetLeft,"y":elem.offsetTop}
   var par=getXYpos(elem.offsetParent);
   for (var key in par) {
      xy[key]+=par[key];
   }
   return xy;
}

function toggleSearch()
{
	objButton=document.getElementById('bsearch')
	objButton.className='selected';
	info=getXYpos(objButton);
	spaceLeft=info['x'];
	obj=document.getElementById('searchdiv');
	obj.style.display='block'; 
	obj.style.left=(spaceLeft-180)+'px';
	
	//All menu normal:
	menuArray=[ 'bhome','bmagazine','bsubscribe','bstore','bservice','bblog' ];
	for ( var i in menuArray )
	{
	try {
			document.getElementById(menuArray[i]).className='';
		} catch (e) {} 		
	} 
}

function getPageHeightWithScroll()
{
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		return (window.innerHeight + window.scrollMaxY);
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		return document.body.scrollHeight;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		return document.body.offsetHeight;
	}
}

function showPreview(url)
{

	obj=document.getElementById('previewpopup');obj.style.display='block';
	//Resize:
	var _docHeight = getPageHeightWithScroll();
	obj.style.height=_docHeight+'px';

	obj2=document.getElementById('flashpreview');obj2.style.display='block';
	y=document.body.scrollTop;
	obj2.style.position='absolute';
	obj2.style.top=y+'px';
	
	// Preview iframe:
	pane=document.getElementById('previewframe');
	pane.src=url;
	pane.focus();
}

function saveNewsletter()
{
	name=document.getElementById('newsletterName').value;
	email=document.getElementById('newsletterEmail').value;
	
	// Check email:
	AtPos=email.lastIndexOf('@');
	DotPos=email.lastIndexOf('.');

	if (!(DotPos>0) || !(AtPos>0) || AtPos==1 || DotPos==email.length || AtPos > DotPos)
	{
		alert('Invalid email address');
	} else {
		saveObject(Array('newsletter',name,email)); 
		toggleDiv('signupNewsletter',this); 
	}
	return false;
}

/* Colorbox jquery */
	$(document).ready(function(){
		//Examples of how to assign the ColorBox event to elements
		$("a[rel='example1']").colorbox({width: "700", height: "466"});
		$("a[rel='example2']").colorbox({transition:"fade"});
		$("a[rel='example3']").colorbox({transition:"none", width:"75%", height:"75%"});
		$("a[rel='example4']").colorbox({slideshow:true});
		$(".example5").colorbox();
		$(".example6").colorbox({iframe:true, innerWidth:425, innerHeight:344});
		$(".example7").colorbox({width:"80%", height:"80%", iframe:true});
		$(".example8").colorbox({width:"50%", inline:true, href:"#inline_example1"});
		$(".example9").colorbox({
			onOpen:function(){ alert('onOpen: colorbox is about to open'); },
			onLoad:function(){ alert('onLoad: colorbox has started to load the targeted content'); },
			onComplete:function(){ alert('onComplete: colorbox has displayed the loaded content'); },
			onCleanup:function(){ alert('onCleanup: colorbox has begun the close process'); },
			onClosed:function(){ alert('onClosed: colorbox has completely closed'); }
		});
		
	});

/* ** MYFRAME AJAX *************************************************** */

// Global Request Object
var req;

// GET Url to My Frame to save object
function loadXMLDoc(url) 
{
	req = false;
	if(window.XMLHttpRequest && !(window.ActiveXObject)) {
		try {
			req = new XMLHttpRequest();
			req.overrideMimeType("text/html; charset=ISO-8859-1");
		} catch(e) {
			req = false;
		}
	// branch for IE/Windows ActiveX version
	} else if(window.ActiveXObject) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				req = false;
			}
		}
	}
	if(req) {
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send("");
	}
}

// Process id req.request is ready/done
function processReqChange() 
{
	// only if req shows "loaded"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			response=req.responseText;
			if (response=="OK")
			{
				// alert("Saved "+response);
			} else {
				responseArray=response.split("@@@@");
				alert("Error: "+responseArray[1]+" [99a]" );
				eval(responseArray[0] +"Toggle()");
			}
		} else {
			alert("There was a problem savinng your data:\n" +
				req.statusText);
		}
	}
}

	
// Save Form object to encoded URL
function saveObject(args)
{
	delimiter="@@@@";
	urlargs=args.join(delimiter);
	url="/index.php?module=myelephant&action=saveObject&data="+urlargs;
	loadXMLDoc(url);
}	