function getIETop(whichelement) {
    var toppos = whichelement.offsetTop;
    var parentelement = whichelement.offsetParent;
    while (parentelement != null) {
        toppos += parentelement.offsetTop;
        parentelement = parentelement.offsetParent;
    }
    return toppos;
}

function getIELeft(whichelement) {
    var leftpos = whichelement.offsetLeft;
    var parentelement = whichelement.offsetParent;
    while (parentelement != null) {
        leftpos += parentelement.offsetLeft;
        parentelement = parentelement.offsetParent;
    }
    return leftpos;
}

var bannerImg = new Array();
var bannerText = new Array();
var bannerUrl = new Array();

/*var myCSS = new Asset.css('/styles/jsenabled.css');   //rewritten below to fix SLI redirection problem */

document.write('<link type="text/css" rel="stylesheet" href="/styles/jsenabled.css">');

/*if ((document.location).toString().indexOf('party-supply.centurynovelty') != -1) {
	if (!Browser.Engine.trident) {
		document.write('<base href="http://www.centurynovelty.com/">');
	} else {
		var thebase = document.getElementsByTagName("base");
		thebase[0].href = "http://www.centurynovelty.com/"; 
	}
}*/

window.addEvent('domready', function() {
	/* --- Dropdown menus ------------------------ */

	var dropTimer;
	var activeDrop = null;
	var activeAnch = null;
	
	var turnOffActiveFunc = function() {
		if (activeDrop != null) {
			activeDrop.set('tween', { duration: 250 });
			activeDrop.tween('opacity', 0);
			turnOnSelects.run();
			activeDrop = null;
		}
		
		if (activeAnch != null) {
			activeAnch.setStyle('background-position', 'left 0px');
			activeAnch = null;
		}
	}
	
	var turnOffSelects = function() {
		if (Browser.Engine.trident4) {
			$$('select').each(function(el) {
				el.setStyle('visibility', 'hidden');
			});
		}
	}
	
	var turnOnSelects = function() {
		if (Browser.Engine.trident4) {
			$$('select').each(function(el) {
				el.setStyle('visibility', 'visible');
			});
		}
	}
	
	$$('#mainnavcontainer a').addEvent('mouseover', function() {
		clearTimeout(dropTimer);
		if (activeDrop != null) {
			activeDrop.setStyle('display', 'none');
			activeDrop = null;
		}
		if (activeAnch != null) {
			activeAnch.setStyle('background-position', 'left 0px');
			activeAnch = null;
		}
		var parLi = this.getParent('li');
		var dropid = parLi.id.replace(/mainnav_/i, 'dropnav_');
		
		if ($(dropid)) {
			$(dropid).setStyle('left', parLi.getPosition().x - 2);
			$(dropid).setStyle('top', $('mainnavcontainer').getPosition().y + 35);
			$(dropid).setStyle('display', 'block');
			$(dropid).setStyle('opacity', 0);
			$(dropid).set('tween', { duration: 125 });
			$(dropid).tween('opacity', 1);
			turnOffSelects.run();

			activeDrop = $(dropid);
			activeAnch = this;
			activeAnch.setStyle('background-position', 'left -100px');
		} else {
			this.setStyle('background-position', 'left -100px');
			this.addEvent('mouseout', function() {
				this.setStyle('background-position', 'left 0px');
			});
		}
	});
	
	$$('#mainnavcontainer a').addEvent('mouseout', function() {
		clearTimeout(dropTimer);
		dropTimer = turnOffActiveFunc.delay(500);
	});
	
	$$('div.dropnav').addEvent('mouseover', function() {
		clearTimeout(dropTimer);
	});
	
	$$('div.dropnav').addEvent('mouseout', function() {
		clearTimeout(dropTimer);
		dropTimer = turnOffActiveFunc.delay(500);
	});


	/* --- Search form ------------------------ */
	
	if ($('sli_search_1')) {
		$('search_form').addEvent('submit', function() {
			if ($('sli_search_1').value == 'Enter a search term' || $('sli_search_1').value == '') {
				return false;
			} else {
				return true;
			}
		});
		
		if ($('sli_search_1').value == '') {
			$('sli_search_1').value = 'Enter a search term';
		}
			
		$('sli_search_1').addEvent('focus', function() {
			if (this.value == 'Enter a search term') {
				this.value = '';
			}
		});
		
		$('sli_search_1').addEvent('blur', function() {
			if (this.value == '') {
				this.value = 'Enter a search term';
			}
		});
	}
	
	/* --- Enlarge photo overlay ------------------------ */
	
	var enlargeOverlayVisible = false;

	var detailReq = new Request.HTML({
		onSuccess: function(html) {
			$('enlargephotooverlay').empty();
			$('enlargephotooverlay').setStyle('opacity', 0);
			$('enlargephotooverlay').adopt(html);
			turnOffSelects.run();
			
			$('enlargephotooverlay').setStyle('left', $('enlargephotooverlay').getPosition().x - ($('enlargephotooverlay').getCoordinates().width / 2));
			$('enlargephotooverlay').setStyle('top', $('enlargephotooverlay').getPosition().y - ($('enlargephotooverlay').getCoordinates().height / 2));
			
			if ($('enlargephotooverlay').getPosition().y < 0) {
				$('enlargephotooverlay').setStyle('top', 0);
			}
			
			$('enlargephotooverlay').set('tween', { delay: 100 });
			$('enlargephotooverlay').tween('opacity', 1);
			
			$('enlargephotooverlay_close').onclick = function() {
				$('enlargephotooverlay').fade(0);
				turnOnSelects.run();
				enlargeOverlayVisible = false;
			}
			enlargeOverlayVisible = true;
		},
		onFailure: function() {
			$('enlargephotooverlay').set('text', 'The request failed.');
			enlargeOverlayVisible = false;
		}
	});
	
	if ($$('tr.items_img a').length > 0) {
		var enlDiv = new Element('div', {
			'id': 'enlargephotobtn',
			'events': {
				'click': function() {
				
				}
			}
		});
		
		enlDiv.set('html', '<img src="/images/common/largerview.png" />');
		
		enlDiv.inject(document.body);
		
		
	}
	
	var enltimer = null;
	var enlholder = null;
	
	if ($('enlargephotooverlay')) {
		$('enlargephotooverlay').addEvent('mouseover', function() {
			turnOffEnlButton.run();	
		});
	}
	
	var turnOffEnlButton = function() {
		if ($('enlargephotobtn')) {
			$('enlargephotobtn').setStyle('display', 'none');
			enlholder = null;
		}
	}
	
	if ($('enlargephotobtn')) {
		$('enlargephotobtn').addEvent('mouseover', function() {
			$clear(enltimer);
		});
	}
	
	$$('tr.items_img a').addEvent('mouseover', function() {
		$clear(enltimer);
		if (!Browser.Engine.trident) {
			$('enlargephotobtn').setStyle('left', this.getPosition().x + (this.getCoordinates().width / 2) - 38);
			$('enlargephotobtn').setStyle('top', this.getPosition().y + this.getCoordinates().height - 26);
		} else {
			$('enlargephotobtn').setStyle('left', getIELeft(this) + (this.getCoordinates().width / 2) - 38);
			$('enlargephotobtn').setStyle('top', getIETop(this) + this.getCoordinates().height - 26);
		}
		$('enlargephotobtn').setStyle('display', 'block');
		
		enlholder = this;
		
		$('enlargephotobtn').addEvent('click', function() {
			detailReq.cancel();
			
			var relstr = enlholder.getAttribute('rel');
			var itemstr = relstr.substr(0, relstr.indexOf(':'));
			var catstr = relstr.substr(relstr.indexOf(':') + 1);

			if (!Browser.Engine.trident) {
				$('enlargephotooverlay').setStyle('left', enlholder.getPosition().x + (enlholder.getCoordinates().width / 2));
				$('enlargephotooverlay').setStyle('top', enlholder.getPosition().y + (enlholder.getCoordinates().height / 2));
			} else {
				$('enlargephotooverlay').setStyle('left', getIELeft(enlholder) + (enlholder.getCoordinates().width / 2));
				$('enlargephotooverlay').setStyle('top', getIETop(enlholder) + (enlholder.getCoordinates().height / 2));
			}
	
			$('enlargephotooverlay').set('html','');
			$('enlargephotooverlay').setStyle('opacity', 0);


			detailReq.send({url: 'http://party-supply.centurynovelty.com/sli_enlargePhotoOverlay.php?item=' + itemstr + '&category=' + catstr});
		});
	});

	$$('tr.items_img a').addEvent('mouseout', function() {
		enltimer = turnOffEnlButton.delay(100);
	});

	
});
