if (!ywUtil.GetGlobal( 'deferMarquee' )) { //defer if using data from gallery
	ywUtil.ScheduleHandler( ywMarquee );
}
function ywMarquee( gallery ) { //randomized thumbnail marquee
	var marquee = arguments.callee; //can't use 'this' here
	if (marquee.divId) {
		var marqueeEl = document.getElementById( marquee.divId );
		if (!marqueeEl) {
			return;
		}
		marquee.element = marqueeEl;
	}
	else {
		return;
	}
	marquee.href = marquee.getHref();
	marquee.src = marquee.getSrc();
	
	if (!ywUtil.GetGlobal( 'deferMarquee' )) { //if not in gallery page
		marquee.gallery = null;
		marquee.sendRequest();
	}
	else {
		marquee.gallery = gallery;
		marquee.makeMarquee( marquee.getThumbNails() );
	}
}
ywMarquee.setDivId = function( id ) {
	this.divId = id;
};
ywMarquee.getHref = function() {
	var tag = this.element.getElementsByTagName('a');
	if (tag ) {
		var parse = tag[0].href; //extract anchor
		tag[0].removeAttribute( 'href' );
		return parse;
	}
	else {
		return null;
	}
};
ywMarquee.getSrc = function() {
	var tag = this.element.getElementsByTagName('img');
	if (tag ) {
		tag[0].removeAttribute( 'src' );
		return ywUtil.Pathname() + ywUtil.GetGlobal( 'imagesrc' );
	}
	else {
		return null;
	}
};
ywMarquee.getThumbNails = function() {
	var thumb = [];
	var path = 'http://' + window.location.host + '/' + this.src + '/';
	var tab = this.gallery.gallery;
	for (var i = 0; i < tab.length; i++) {
		thumb[i] = path + tab[i].image.replace( '.', '-thumb.' );
	}
	return thumb;
};
ywMarquee.sendRequest = function() {
	if (!this.src) {
		return;
	}

	var post = 'dir=' + ywUtil.Encode( this.src ) +
		'&ext[]=' + ywUtil.Encode( '-thumb.gif' ) + 
		'&ext[]=' + ywUtil.Encode( '-thumb.jpg' ) + 
		'&ext[]=' + ywUtil.Encode( '-thumb.png' ); 
	var obj = this;
	var callback = function( responseText ) {
		obj.parseResponse( responseText );
	};

	ywUtil.SendRequest( 'controllers/thumbnail.php', post, callback );
};
ywMarquee.parseResponse = function( responseText ) {
	var response = eval( '(' + responseText + ')' );
	this.makeMarquee( response.data );
};
ywMarquee.makeMarquee = function( thumb ) {
	thumb.sort( function( a, b ) { return( Math.random() - 0.5 ); } );

	var htmlStr = '';
	for (var i = 0; i < Math.min( thumb.length, 9 ); i++) {
		htmlStr += this.display( thumb[i] );
	}
	this.element.innerHTML = htmlStr;

	ywScroller( 100, 2, this.element );
};
ywMarquee.display = function( nail ) {
	var html = this.element.innerHTML;	
	html = html.replace( /<img/i, '<img src="' + nail + '"' );
	
	if (html.search( /Category/i ) != -1) {
		var match = nail.match( /\/([^\/]+)\/[^\/]+\-thumb\.(gif|jpg|png)/i );
		var category = ywUtil.DecodeCategory( match[1] );
		
		html = html.replace( /category/i, category );
		if (this.href) {
			var temp = '<a href="' + this.href + '?c=' + match[1] + '"';
			if (this.gallery)
				temp += " onclick=\"ywMarquee.gallery.changeCategory('"
					+ match[1] + "'); return false;\"";
			html = html.replace( /<a/i, temp );
		}
	}
	else if (this.href) {
		html = html.replace( /<a/i, '<a href="' + this.href[1] + '"' );
	}

	return html;
};

//define scroller class
function ywScroller( pos, speed, element ) {
	var scroller = arguments.callee;
	scroller.pos = pos;
	scroller.speed = speed;
	scroller.element = element;
	
//use closures to register some callbacks & start scrolling :)
	element.onmouseover=function() { scroller.stop(); };
	element.onmouseout=function() { scroller.resume(); };
	scroller.timerId = 
		window.setTimeout( function() { ywScroller.start(); }, 18 );
}
ywScroller.stop = function() {
	window.clearTimeout( this.timerId );
	this.timerId = null;
};
ywScroller.resume = function() {
	if (!this.timerId) {
		this.timerId =
			window.setTimeout( function() { ywScroller.start(); }, 18 );
	}
};
ywScroller.start = function() {
	this.pos -= this.speed;
	if (this.pos < -this.element.offsetHeight) {
		this.pos = 200;
	}
	this.element.style.top = this.pos + "px";
	this.timerId =
		window.setTimeout( function() { ywScroller.start(); }, 18 );
};