// AJAX
function sendrequest(url,data,outputhandler) {
var httprequest = false;

if (window.XMLHttpRequest) {
	httprequest = new XMLHttpRequest();
	if (httprequest.overrideMimeType) {
		httprequest.overrideMimeType('text/xml');
	}
} else if (window.ActiveXObject) {
// If regular request method failed, try IE's method...
	try {
		httprequest = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			httprequest = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
		
		}			
	}
}
if (!httprequest) { return false; }
httprequest.onreadystatechange = function() { processrequest(httprequest,outputhandler); };
httprequest.open('POST', url, true);
httprequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
httprequest.send(data);
}
	
function processrequest(httprequest,outputhandler) {
if (httprequest.readyState == 4) {
	if (httprequest.status == 200) {
		if (outputhandler) { output = httprequest.responseText; output = output.replace('<xml>',''); output = output.replace('</xml>',''); eval(outputhandler); }
	} else {
		return false;
	}
}
}

//------------------------

function getElementsByClassName(classname, node) {
	if(!node) node = document.getElementsByTagName("body")[0];
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b');
	var els = node.getElementsByTagName("*");
	for(var i=0,j=els.length; i<j; i++)
	if(re.test(els[i].className))a.push(els[i]);
	return a;
}

//------------------------
function setCursor(cursor) {
	document.body.style.cursor = cursor;
}
//------------------------
function hasClass(ele,cls) {
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
function addClass(ele,cls) {
	if (!this.hasClass(ele,cls)) ele.className += " "+cls;
}
function removeClass(ele,cls) {
	if (hasClass(ele,cls)) {
		var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		ele.className=ele.className.replace(reg,' ');
	}
}
//------------------------

function textfieldbg(elementid,toggle){
	if (toggle ==1) { document.getElementById(elementid).style.backgroundPosition = "0px -38px"; } 
	else { document.getElementById(elementid).style.backgroundPosition = "0px 0px";  }
}

function textfieldcontent(elementid,currenttext,changedtext) {
	if (document.getElementById(elementid).value == currenttext) { document.getElementById(elementid).value = changedtext; }
}

function textfieldcontentrouteplanner(elementid, currenttext, changedtext){
	if (document.getElementById(elementid).value == currenttext) { document.getElementById(elementid).value = changedtext; }
}

function buttonbg(elementid,toggle){
	if(toggle ==1) { document.getElementById(elementid).style.backgroundImage = "url('images/buttonrollover.gif')"; }
	else { document.getElementById(elementid).style.backgroundImage = "url('images/button.gif')"; }
}

function clearWebbits() {
	forms = document.getElementById('webbits').getElementsByTagName('form');
	i = 0; numforms = forms.length;
	while (i < numforms) { forms[i].style.display='none'; i++; }
}
function showWebbit(webbit) {
	clearWebbits();
	document.getElementById('webbit_'+webbit).style.display='block';
	sendrequest('index.php','act=savewebbit&webbit='+webbit,null);
	return false;
}
function slideWebbit(webbit2) {
	var targetContent = $('#webbit_'+webbit2, document.getElementById('webbits'));
	targetContent.slideDown(300);
}
function addEmail() {
	emailaddr = document.getElementById('mailbox').value;
	sendrequest('index.php','act=joinmail&email='+emailaddr,'mailResponse();');
	return false;
}
function mailResponse() {
	var hideForm = 0;
	var responseText = '';
	if (output == '0') { 
		responseText = 'Your email address appears to be invalid. Please ensure that you enter a valid email@domain.com.';
	} else if (output == '1') {
		responseText = 'Thank You! Your email has been added to the mailing list.';
		var hideForm = 1;
	} else if (output == '2') {
		responseText = 'You already appear to be on the Haverhill Business mailing list.';
		var hideForm = 1;
	}
	document.getElementById('mailmsg').innerHTML = responseText;
	if (hideForm == 1) { document.getElementById('mailinglistform').style.display='none'; }
}
function init(){
	document.getElementById("googlesubmit").onmouseover = function() { textfieldbg('googlebg',1); }
	document.getElementById("googlesubmit").onmouseout = function() { textfieldbg('googlebg',0); }	
	document.getElementById("businesssubmit").onmouseover = function() { textfieldbg('businessbg',1); }
	document.getElementById("businesssubmit").onmouseout = function() { textfieldbg('businessbg',0); }
	document.getElementById("wikisubmit").onmouseover = function() { textfieldbg('wikibg',1); }
	document.getElementById("wikisubmit").onmouseout = function() { textfieldbg('wikibg',0); }
	document.getElementById("dictionarysubmit").onmouseover = function() { textfieldbg('dictionarybg',1); }
	document.getElementById("dictionarysubmit").onmouseout = function() { textfieldbg('dictionarybg',0); }
	document.getElementById("ypsubmit").onmouseover = function() { textfieldbg('ypbg',1); }
	document.getElementById("ypsubmit").onmouseout = function() { textfieldbg('ypbg',0); }
	
	document.getElementById("googletext").onfocus = function() { textfieldcontent("googletext","Search Google",""); }
	document.getElementById("googletext").onblur = function() { textfieldcontent("googletext","","Search Google"); }	
	if (document.getElementById("mailbox")) {
		document.getElementById("mailbox").onfocus = function() { textfieldcontent("mailbox","Enter E-Mail Here",""); }
		document.getElementById("mailbox").onblur = function() { textfieldcontent("mailbox","","Enter E-Mail Here"); }
	}
	document.getElementById("businesstext").onfocus = function() { textfieldcontent("businesstext","Search Business",""); }
	document.getElementById("businesstext").onblur = function() { textfieldcontent("businesstext","","Search Business"); }
	document.getElementById("wikipediatext").onfocus = function() { textfieldcontent("wikipediatext","Search Wikipedia",""); }
	document.getElementById("wikipediatext").onblur = function() { textfieldcontent("wikipediatext","","Search Wikipedia"); }
	document.getElementById("dictionarytext").onfocus = function() { textfieldcontent("dictionarytext","Define Word",""); }
	document.getElementById("dictionarytext").onblur = function() { textfieldcontent("dictionarytext","","Define Word"); }
	document.getElementById("yellowpagestext").onfocus = function() { textfieldcontent("yellowpagestext","Search Yellow Pages",""); }
	document.getElementById("yellowpagestext").onblur = function() { textfieldcontent("yellowpagestext","","Search Yellow Pages"); }
	
	document.getElementById("routeplannertotext").onfocus = function() { textfieldcontentrouteplanner("routeplannertotext","Postcode, Street Name, UK",""); }
	document.getElementById("routeplannertotext").onblur = function() { textfieldcontentrouteplanner("routeplannertotext","","Postcode, Street Name, UK"); }
	document.getElementById("routeplannerfromtext").onfocus = function() { textfieldcontentrouteplanner("routeplannerfromtext","Haverhill, Suffolk, UK",""); }
	document.getElementById("routeplannerfromtext").onblur = function() { textfieldcontentrouteplanner("routeplannerfromtext","","Haverhill, Suffolk, UK"); }
	
	document.getElementById("routeplannerbuttonbg").onmouseover = function() { buttonbg('routeplannerbuttonbg',1); }
	document.getElementById("routeplannerbuttonbg").onmouseout = function() { buttonbg('routeplannerbuttonbg',0); }
	
	//if (document.getElementById('mailinglistform')) { document.getElementById('mailinglistform').onsubmit = addEmail(); }
	
	//showWebbit('wikipedia');
	
	// Start with the google search selected and ready to go?
	document.getElementById('googletext').select();
}

window.onload=init;

$(document).ready(
	function () {
		$('a.closeEl').bind('click', toggleContent);
		$('div.column').Sortable(
			{
				accept: 'moveable',
				helperclass: 'sortHelper',
				activeclass : 	'sortableactive',
				hoverclass : 	'sortablehover',
				handle: 'h3',
				tolerance: 'pointer',
				onChange : function(ser)
				{
				},
				onStart : function()
				{
					$.iAutoscroller.start(this, document.getElementsByTagName('body'));
				},
				onStop : function()
				{
					$.iAutoscroller.stop();
					savePanelPositions();
				}
			}
		);
	}
);

var toggleContent = function(e) {
	var targetContent = $('div.middle', this.parentNode.parentNode);
	if (targetContent.css('display') == 'none') {
		targetContent.slideDown(300);
		$(this).html('<img src="/images/arrowdown.gif" alt="-">');
		openClosePanel(this.parentNode.parentNode.id,1);
	} else {
		targetContent.slideUp(300);
		$(this).html('<img src="/images/arrowside.gif" alt="+">');
		openClosePanel(this.parentNode.parentNode.id,0);
	}
	return false;
};

function serialize(s) {
	serial = $.SortSerialize(s);
	return serial.hash;
};

function savePanelPositions() {
	serialhash = serialize(['column1', 'column2', 'column3']);
	sendrequest('index.php','act=updpanels&'+serialhash,null);
}
function openClosePanel(panel,open) {

	if (panel) {
	
		if (open) { open = '1'; } else { open = '0'; }
		sendrequest('index.php','act=updpanelopen&panel='+panel+'&openclose='+open,null); 
	}
}

function addBookMark() {
title = 'Haverhill Business'; url = 'http://www.haverhillbusiness.co.uk';
if (window.sidebar)
	window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){
	var elem = document.createElement('a');
	elem.setAttribute('href',url);
	elem.setAttribute('title',title);
	elem.setAttribute('rel','sidebar');
	elem.click();
} 
else if(document.all)
	window.external.AddFavorite(url, title);
	
	return false;
}