var Popout = function()
{
	var me = this;
	this.popList = [];
	this.cloned = null;
	this._to = null; this._in = null;
	this.make = function(el)
	{
		$(el).parent().mouseout(Popout.clear);
		$(el).children("li").each(function(){
			me.popList.push($(this));
			$(this).mouseenter(Popout.OnMouseEnter);
			$(this).mouseleave(Popout.OnMouseLeave);
		});
		Popout.currentPop = -1;
		me._to = setTimeout(me.popOut,1000);
	}
	
	this.popOut = function(){
		try{
			if(++Popout.currentPop >= me.popList.length){ Popout.currentPop=0; }
			if(me.cloned){ me.cloned.remove(); }
			me.popList[Popout.currentPop].trigger('mouseenter');
			if(me.cloned){ me.cloned.trigger('click'); }
		}catch(e){ ; }
		if(me._to){ clearTimeout(me._to); }
		me._to = setTimeout(me.popOut, 6000);
	}
	
	this.clear = function(a){
		if(me.cloned && $(a.target).hasClass("gallery")){ $(me.cloned).remove(); } }
	this.OnMouseEnter = function()
	{
		var omeID = parseInt($(this).attr('id').substring(3));
		if(me.cloned){ $(me.cloned).remove(); }
		me.cloned = $("<div class=\"gallery-item-clone\"></div>");
		me.cloned.css({
			'position': 'absolute',
			'left': $(this).offset().left+'px',
			'top': $(this).offset().top+'px',
			'width': $(this).outerWidth()+'px',
			'height': $(this).outerHeight()+'px',
			'background-position': 'center center',
			'background-image': $(this).css('background-image')
		});
		
		var nx = $(this).offset().left-10+'px';
		var ny = $(this).offset().top-10+'px';
		var nw = $(this).width()+10+'px';
		var nh = $(this).height()+10+'px';
		
		me.cloned.animate({
			'left': nx, 'top': ny,
			'width': nw, 'height': nh
		},{duration: 100, easing: 'easeOutBack'});
		$("body").append(me.cloned);
		
		me.cloned.click(function(){
			Popout.currentPop = omeID;
			var me = $(this);
			var img = new Image();
			img.width=670;
			img.onload = function()
			{
				$('.main-photo img').remove();
				var newimg = $("<img src=\""+this.src+"\" />");
				newimg.css({opacity:0});
				$('.main-photo').append(newimg);
				delete newimg;
				$('.main-photo').removeClass('loading');
				$('.main-photo img').animate({opacity: 1});
				delete this;
			}
			
			$('.main-photo img').animate({
				opacity: 0
			},{complete: function(){
				$('.main-photo').addClass('loading');
				var pat = /^url\(\"?([A-Za-z0-9-_\/\.\:]+)(\/crop.*)$/i.exec(me.css('background-image'));
				if(pat && pat[1]){
					img.src=pat[1]+"/670";
				}
			}});
		});
		
	}
	
	this.OnMouseLeave = function(a,e,i)
	{
		if($(a.relatedTarget).hasClass("gallery-item-clone")){ ; }else{
			if(me.cloned){ $(me.cloned).remove(); }
		}
	}
}
Popout = new Popout();
Popout.currentPop = 0;
