var Dom = YAHOO.util.Dom,
	YEvent = YAHOO.util.Event;

IRIS.namespace('eaauctions');
YEvent.addListener(window, 'load', function() {
	setTimeout(function() { IRIS.eaauctions.Preloader.init(); }, 0);
	var images = document.getElementsByTagName('IMG'), i;
	for( i = 0; i < images.length; i++ ) {
		images[i].onmousedown = function(e) {
			if( navigator.appName == 'Microsoft Internet Explorer' ) {
				if( event.button == 2 ) {
					alert("No right click please");
					return false;
				}
			}
			else if( e.button == 2 ) {
				return false;
			}
		};
	}
	if( !IRIS.widget.ImageSwitcher ) {
		return;
	}
	IRIS.widget.ImageSwitcher.init('#topNav > ul > li > a');
});
document.onselectstart = function(e) {
	return false;
};

IRIS.eaauctions = {
	getFormattedTime: function( timeInSeconds, extendedTime ) {
		var mod, primaryPeriod, primaryTime, secondaryTime,
			periods = [86339, 3599, 59], maxPeriods = [23, 59], labels = [' day', ' hour', ' min', ' sec']
			rv = '';
		
		for( var i = 0; i < periods.length; i++ ) {
			if( timeInSeconds > periods[i] ) {
				periods[i]++;
				break;
			}
		}
		
		if( extendedTime && (i < (labels.length - 1))) {
			secondaryTime = timeInSeconds % periods[i];
			primaryTime = (timeInSeconds - secondaryTime) / periods[i];
			if( maxPeriods[i] && Math.round(secondaryTime / (periods[i+1]+1)) > maxPeriods[i] ) {
				secondaryTime = 0;
				primaryTime++;
			}
			if( periods[i+1] ) secondaryTime = Math.round(secondaryTime / (periods[i+1]+1));
			rv = primaryTime + labels[i] + (primaryTime > 1 ? 's ' : ' ') + (secondaryTime > 0 ? secondaryTime + labels[i+1] + (secondaryTime > 1 ? 's ' : ' ') : '');
		}
		else {
			if( periods[i] )
				rv = Math.floor(timeInSeconds / periods[i]) + labels[i] + (primaryTime > 1 ? 's ' : ' ');
			else
				if( timeInSeconds < 0 )
					rv = '0 sec';
				else
					rv = timeInSeconds + labels[i] + (timeInSeconds > 1 ? 's ' : ' ');
		}

		return rv;
	}
};

IRIS.eaauctions.ItemList = {
	lastRotation: 0,
	easing: null,
	interval: 3,
	duration: 1,
	rotateFeatured: function( cfg ) {
		if( !this.easing && YAHOO.util.Easing ) {
			this.easing = YAHOO.util.Easing.easeBoth;
			//* debug */ IRIS.eaauctions.ItemList.interval = 3;
		}
		var now = new Date().getTime(),
			nextRotation = (this.lastRotation ? this.lastRotation : now)
					+ (this.interval * 1000);
		setTimeout(function() {
				IRIS.eaauctions.ItemList.updateFeatured(cfg);
			}, nextRotation - now
		);
		this.lastRotation = nextRotation;
	},
	updateFeatured: function( cfg ) {
		var uri = cfg.uri
				+ (cfg.auctionId ? '&auctionId=' + cfg.auctionId : '')
				+ (cfg.offset ? '&offset=' + cfg.offset : '');
		YAHOO.util.Connect.asyncRequest('GET', uri, {
			success: function( response ) {
				var oldEl = Dom.get(cfg.el).parentNode,
					newEl = document.createElement('DIV');
				newEl.style.marginLeft = '660px';
				newEl.innerHTML = response.responseText;
				Dom.getChildren(newEl)[0].style.minHeight = Dom.getChildren(oldEl)[0].offsetHeight + 'px';
				oldEl.parentNode.appendChild(newEl);
				IRIS.util.Loader.handleScripts(newEl);
				// IRIS.util.Loader.notifyAllLoaded(newEl, 'IMG', function() {
					var oldOut = new YAHOO.util.Anim(oldEl, {
								marginLeft: {to: -660}
							}, this.duration, this.easing
						),
						newIn = new YAHOO.util.Anim(newEl, {
								marginLeft: {to: 0}
							}, this.duration, this.easing
						);
					newIn.onComplete.subscribe(function() {
						oldEl.parentNode.removeChild(oldEl);
					});
					if( Browser.getInstance().is('ie7') ) {
						oldEl.style.display = 'none';
						newIn.animate();
					}
					else {
						oldOut.animate();
						if( Browser.getInstance().is('ie8') ) {
							setTimeout(function() { newIn.animate(); }, 100);
						}
						else {
							newIn.animate();
						}
					}
				// }, this);
			},
			failure: function() {},
			scope: this
		});
	}
};

IRIS.eaauctions.Preloader = {
	images: ['banners.jpg'],
	init: function() {
		for( var i = 0; i < this.images.length; i++ ) {
			var image = new Image();
			image.src = '/images/' + this.images[i];
		}
	}
};

IRIS.eaauctions.Payment = {
	showConfirmPanel: function( el ) {
		var ContainerUtils = IRIS.widget.ContainerUtils;
		ContainerUtils.showDialog('confirmPayment', null, el.innerHTML, 
				[{ text: "Ok, I Agree", handler: function() { ContainerUtils.dialogCloseHandler.call(this); } },
				 { text: "Cancel", handler: function() { window.history.go(-1) } }],
				null, {width: '500px'}
		);
	}
};
