function boxOffHover(box, id) {
	if (box != oldbutton) {
		if (wishlist[id] && wishlist[id] == 1) {
			box.className = 'items';
		} else {
			box.className = 'item';
		}
	}
}

var oldsource = '';

var oldbutton = '';
var oldid = '';

function boxOnHover(box, id) {
	if (box != oldbutton) {
		if (wishlist[id] && wishlist[id] == 1) {
			box.className = 'itemso';
		} else {
			box.className = 'itemo';
		}
	}
}

var wishlist = [];

if(!Array.indexOf){
	Array.prototype.indexOf = function(obj) {
		for (var i=0; i<this.length; i++) {
			if (this[i] == obj) {
				return i;
			}
		}

		return -1;
	}
}

function addWish(id, request, hideitem) {
	if (wishlist[id] && wishlist[id] == 1) {
		wishlist[id] = 0;

		if (hideitem) {
			document.getElementById('outer' + id).style.display = 'none';

			var i = wishlist.indexOf(1);

			if (i != -1) {
				document.getElementById('item' + i).onclick();
			} else {
				document.getElementById('floatdiv').style.display = 'none';
				document.getElementById('middlebar').style.display = 'none';
				curBox = null;
			}
		}

		newimg = 'smallstar.gif';
		newtext = "Add to Event Wishlist";
	} else {
		wishlist[id] = 1;
		newimg = 'smallstarb.gif';
		newtext = "Remove from Wishlist";
		document.getElementById('add' + id).style.display = '';
	}

	document.getElementById('addi' + id).src = 'http://www.eventsplus.com/images/layout/' + newimg;

	if (document.all){ 
		document.getElementById('addt' + id).innerText = newtext;
	} else{
		document.getElementById('addt' + id).textContent = newtext;
	}

	if (request) {
		ajaxRequest('http://www.eventsplus.com/indirect/wishajax.php?a=add&id=' + id + '&add=' + wishlist[id] + '&time=' + (new Date()).getTime());
	}
}

function handleResponse(r)
{
	switch (r.firstChild.firstChild.nodeValue)
	{
	case 'add':
		ajaxRequest('http://www.eventsplus.com/indirect/wishajax.php?a=count&time=' + (new Date()).getTime());
		break;

	case 'count':
		var c = r.getElementsByTagName('result')[0].firstChild.nodeValue;

		if (document.all){
			document.getElementById('wishcount').innerText = c + ' item' + (c != 1 ? 's' : '');
		} else{
			document.getElementById('wishcount').textContent = c + ' item' + (c != 1 ? 's' : '');
		}
		break;
	}
}

function findPos(obj) {
	var curtop = 0;
	//var curleft = curtop = 0;

	if (obj.offsetParent) {
		do {
			//curleft += obj.offsetLeft;
			curtop  += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}

	return curtop;
	//return [curleft, curtop];
}

function findPosY(obj) {
	var curtop = 0;

	if (obj.offsetParent) {
		while(1) {
			curtop += obj.offsetTop;

			if(!obj.offsetParent){
				break;
			}

			obj = obj.offsetParent;
		}

	} else if(obj.y) {
		curtop += obj.y;
	}

	return curtop;
}

function boxOnClick(box, id, source, alt, pics, second_pic) {
	if (document.getElementById('outer' + id).style.display == 'none') {
		return;
	}

	if (source == '') {
		alt = "No image available.";
	}

	showImage();

	if (wishlist[oldid] && wishlist[oldid] == 1) {
		oldbutton.className = 'items';
	} else {
		oldbutton.className = 'item';
	}

	if (wishlist[id] && wishlist[id] == 1) {
		box.className = 'itemsh';
	} else {
		box.className = 'itemh';
	}

	if (oldid != '') {
		if (wishlist[oldid] && wishlist[oldid] == 1) {
			document.getElementById('add' + oldid).style.display = '';
		} else {
			document.getElementById('add' + oldid).style.display = 'none';
		}
	}

	oldbutton = box;
	oldid = id;


	if (source != '') {
		crossfade(document.getElementById('floatimg'), source, 0.18, alt);
	} else {
		document.getElementById('floatimg').src = 'http://www.eventsplus.com/images/layout/placeholder.gif';
	}

	document.getElementById('add' + id).style.display = '';

	if (pics > 1) {
		document.getElementById('floatimglink').style.cursor = 'pointer';
		document.getElementById('floatimglink').onclick = function(){ tb_show(alt, second_pic, 'gallery-item' + id); };
		document.getElementById('floatimg').alt = 'Click to see additional pictures for this item';

		document.getElementById('picdiv').style.display = '';
		cap = (pics - 1) + ' more picture' + (pics > 2 ? 's' : '') + ' available';
	} else {
		document.getElementById('floatimglink').style.cursor = 'default';
		document.getElementById('floatimglink').onclick = '';


		document.getElementById('picdiv').style.display = 'none';
		cap = '';
	}

	if (document.all){
		document.getElementById('floatcaption').innerHTML = alt;
		document.getElementById('pictext').innerHTML = cap;
	} else{
		document.getElementById('floatcaption').textContent = alt;
		document.getElementById('pictext').textContent = cap;
	}

	curBox = box;

	var boxpos = findPosY(box);

	//strange behavior, offsets different from IE8
	if ((navigator.userAgent.indexOf('MSIE 6') != -1)
	||  (navigator.userAgent.indexOf('MSIE 7') != -1)) {
		boxpos -= 11;
	}

	//document.getElementById('floatdiv').style.top = boxpos + box.offsetHeight/2 - 187 + 'px';
	//floatingMenu.targetY = boxpos + box.offsetHeight/2 - 187;

	moveMiddle(box, boxpos);

	document.getElementById('piclink').onclick = function(){ tb_show(alt, second_pic, 'gallery-item' + id); };
}

function redText(c)
{
	document.getElementById('pictext').style.color = c;
}

var imgXwidth  = 522;

// Distance from right side
var imgXoffset = imgXwidth + 30;

var floatspeed = .2;
var floatingMenuId = 'floatdiv';

var hasElement = typeof(document.documentElement) == 'object'
	&& typeof(document.documentElement.clientWidth) == 'number'
	&& (document.documentElement.clientWidth > 0);

// The image will not float higher than this on the page
var minfloat = originalminfloat = 211;
var maxfloat = 1000;

var curBox = null;

function resizeContent()
{
	var pageWidth =
	hasElement
	? document.documentElement.clientWidth
	: document.body.clientWidth;

	var contentbox =
        document.getElementById
        ? document.getElementById('contentbox')
        : document.all
          ? document.all['contentbox']
          : document.layers['contentbox'];

	if (pageWidth < 925) {
		pageWidth = 925;

		//floatingMenu.targetX = pageWidth - imgXoffset;
		document.getElementById('floatdiv').style.left = pageWidth - imgXoffset + 'px';
		document.getElementById('headerbar').style.width = pageWidth + 'px';
	} else {
		document.getElementById('floatdiv').style.left = pageWidth - imgXoffset + 'px';
		document.getElementById('headerbar').style.width = '100%';
	}

	contentbox.style.width = (pageWidth - imgXoffset - 15) + 'px'; //# is margin between contentbox and floatingimg

	if (curBox) {
		var boxpos = findPosY(curBox);

		//strange behavior, offsets different from IE8
		if ((navigator.userAgent.indexOf('MSIE 6') != -1)
		||  (navigator.userAgent.indexOf('MSIE 7') != -1)) {
			boxpos -= 11;
		}

		moveMiddle(curBox, boxpos);
	}
}

function moveMiddle(box, boxpos)
{
	var middlebar = document.getElementById('middlebar');

	middlebar.style.height = box.offsetHeight - 6 + 'px';
	middlebar.style.width = '33px';
	middlebar.style.top = boxpos + 'px';
	middlebar.style.left = box.offsetWidth + 15 + 'px';

	minfloat = Math.max(originalminfloat, boxpos + box.offsetHeight - document.getElementById('floatdiv').offsetHeight + 15);
	maxfloat = boxpos - 15;
}

function showImage()
{
	document.getElementById('floatdiv').style.display = '';
}

function hideImage()
{
	document.getElementById('floatdiv').style.display = 'none';
}

function preloadImages(image_url)
{
	if (document.images) {
		preloaded_images = new Array();

		var i = 0;
		for (i=0; i < image_url.length; i++) {
			preloaded_images[i] = new Image();
			preloaded_images[i].src = image_url[i];
		}
	}
}
