/**
 * @author KoreLogic
 */
var mouseCorrection = 0;
var imageHolder = new Array();
var commentsHolder = new Array();
var fileHolder = new Array();
var imgID = 0	;
var intPosLeft = 0;
var intPosTop = 0;
var maxHeight = 0;
var imageCounter = 0;
var currentFrame = 1;
var infoActive = 0;
var currentImageID = 0;
var wideImage = "";


function initScrollLayers() {
 			var slider2 = new Slider('d_2_bar', 'd_2_button', '', '', {
				a: 'ver',
				onComplete: function(anh) {
					//$('text').setHTML('Set : Pos ' + anh[0] + ' px / ' + 
					//anh[1] + ' px = ' + anh[0]/anh[1]*100 + ' %');
				},
				onDrag: function(anh) {
					//$('text').setHTML($('text').scrollHeight)
					$('text').scrollTop = ($('text').scrollHeight/anh[1])*anh[0];
					
					//$('text').setHTML('Drag : Pos ' + anh[0] + ' px / ' + 
					//anh[1] + ' px = ' + anh[0]/anh[1]*100 + ' %');
				}
			});
}

var koreTools = {
	timer: 0,
	start: function(){
		$('content').addEvent('mouseover', koreTools.expandContent);
		$('image').addEvent('mouseover', koreTools.contractContent);
		$('imageWide').addEvent('mouseover', koreTools.contractContent);
		$('image').addEvent('mouseover', koreTools.showInfo);
		$('image').addEvent('mouseout', koreTools.hideInfo);
		

		

	},
	expandContent : function(){
	    clearTimeout(this.timeout);	
	    if($('content').getStyle('width') == '300px'){
			//$('grid').setStyles({
			//	background: '#111 url('+ wideImage +') no-repeat left top'
			//})
			var slideIn = new fx.Style('content' ,'width',  {duration: 300});
			var slideIn2 = new fx.Style('content' ,'left',  {duration: 300});
			var imagewide = new fx.Style('imageWide' ,'opacity',  {duration: 300});
	    	this.timeout = setTimeout(function(){slideIn.custom(300, 600);slideIn2.custom(600, 300);imagewide.custom(0, 1);},400);
	    }
	},
	contractContent : function(){
	    clearTimeout(this.timeout);	
	    if($('content').getStyle('width') == '600px'){
			var slideIn = new fx.Style('content' ,'width',  {duration: 300});
			var slideIn2 = new fx.Style('content' ,'left',  {duration: 300});
			var imagewide = new fx.Style('imageWide' ,'opacity',  {duration: 300});
	    	this.timeout = setTimeout(function(){slideIn.custom(600, 300);slideIn2.custom(300, 600);imagewide.custom(1, 0);},400);
        }
	},
	showInfo : function(){
			if(commentsHolder[currentImageID] != ""){
				$('infoComments').innerHTML = 'Comments: <br />'+commentsHolder[currentImageID];
			}
			if(fileHolder[currentImageID] != ""){
				$('infoName').innerHTML = fileHolder[currentImageID];
			}else{
				$('infoName').innerHTML = 'Anon';
			}
		    //clearTimeout(this.timeout);	
		    if($('infoHolder').style.visibility == 'hidden'){
			//alert('hi')
			var infoHolder2 = new fx.Style('infoHolder', 'opacity', {duration:300});
            infoHolder2.custom(0, .85);
		    }
	},
	hideInfo : function(){
		if($('infoHolder').style.visibility == 'hidden'){
			clearTimeout(this.timeout);
		}else{
			var infoHolder = new fx.Style('infoHolder', 'opacity', {duration:300});
            this.timeout = setTimeout(function(){infoHolder.custom(.85, 0);},300);
        }
	},
	hoverImage : function(){
	    clearTimeout(this.timeout);	
	    if($('hover'+this.id).style.visibility == 'hidden'){
			var hover = new fx.Style($('hover'+this.id), 'opacity', {duration:200});
	    	this.timeout = setTimeout(function(){hover.custom(0, 1);},300);
	    }
	},
	hoverOutImage : function(){
		if($('hover'+this.id).style.visibility == 'hidden'){
			clearTimeout(this.timeout);
		}else{
			var hover = new fx.Style($('hover'+this.id), 'opacity', {duration:200});
	    	this.timeout = setTimeout(function(){hover.custom(1, 0);},100);
        }
	},
	
	getMouse: function(evt){
    	var evt = evt || window.evt
    	return {x: evt.clientX+(!document.all ? Window.getScrollLeft() : 0), y: evt.clientY+(!document.all ? Window.getScrollTop() : 0)}
	},

	setupGrid: function(){
		var totalImages = imageHolder.length;
		var ranNum = 0;
		var imageRan = new Array();

		for (var i = 0; i < totalImages; i++){
			imageRan.push(i);
		}
		for (var i = 0; i < 25; i++)
		{
			ranNum = Math.floor(Math.random()*imageRan.length);
			var photo = new Element('div').addClass('photoHolder').setOpacity(0).addEvent('click', koreTools.showImage).addEvent('mouseover', koreTools.hoverImage).addEvent('mouseout', koreTools.hoverOutImage).setStyles({
				left: intPosLeft+'px',
				top: intPosTop+'px'//,
				//background: 'url(/admin/ImageGen.aspx?image='+imageHolder[imageRan[ranNum]]+'&height=62) no-repeat -1px -1px'
			}).setProperties({
			    id: 'photo_'+i,
				title: imageRan[ranNum]
			}).injectInside($('grid'));
			var photohover = new Element('div').addClass('hoverHolder').setOpacity(0).setStyles({
				background: 'url(/gfx/hover.gif) no-repeat center center'
			}).setProperties({
			    id: 'hoverphoto_'+i
			}).injectInside($('photo_'+i));
			var photohover = new Element('div').addClass('hoverHolder').setOpacity(0).setStyles({
				background: 'url(/gfx/selected.gif) no-repeat center center'
			}).setProperties({
			    id: 'selectedphoto_'+i
			}).injectInside($('photo_'+i));
			var photohover = new Element('img').setStyles({
				margin: '-1px 0 0 -1px'
			}).setProperties({
			    src: '/admin/ImageGen.aspx?image='+imageHolder[imageRan[ranNum]]+'&height=64'
			}).injectInside($('photo_'+i));			
			if( i == 0){
				$('imageView1Img').setProperties({
					src: '/admin/ImageGen.aspx?image='+imageHolder[imageRan[ranNum]]+'&height=312'
				});
				$('selectedphoto_'+i).setOpacity(1)
			}
			
			intPosLeft+=60;
			imageRan.splice(ranNum, 1);
			imageCounter++;
			if(intPosLeft >= 300){
				intPosTop += 60;
				intPosLeft = 0;
			}
			
		}
		
		koreTools.showGrid();
	},
	
	showImage: function(){
		
		currentImageID = this.title;
		var imageView1 = new fx.Style('imageView1', 'opacity', {duration:500});
		var imageView2 = new fx.Style('imageView2', 'opacity', {duration:500});
		$('selected'+this.id).setOpacity(1)
		if (currentFrame == 1){
			$('imageView1Img').setProperties({
				src: '/admin/ImageGen.aspx?image='+imageHolder[this.title]+'&height=312'
			});
			imageView1.custom(0, 1);imageView2.custom(1, 0);
			currentFrame = 2;
		}else{
			$('imageView2Img').setProperties({
				src: '/admin/ImageGen.aspx?image='+imageHolder[this.title]+'&height=302'
			});
			currentFrame = 1;
			imageView1.custom(1, 0);imageView2.custom(0, 1);
		}
	},
	
	showGrid: function(){
		var imageRan = new Array();
		var ranNum = 0;
		for (var i = 0; i < 25; i++)
			imageRan.push(i);	
		
		$S('#grid .photoHolder').each(function(){
			ranNum = Math.floor(Math.random()*imageRan.length);
			this.timer += 50;
			var slideIn = new Fx.Style($('photo_'+imageRan[ranNum]), 'opacity');
			slideIn.myCustom = function(){
				slideIn.custom('0', '1');
			};
			slideIn.myCustom.delay(this.timer);
			imageRan.splice(ranNum, 1);
		}, this);
	},
	slideMenu: function(){
		$S('#tabs li').each(function(el){
			this.timer += 150;
			var slideIn = new fx.Style(el ,'margin-top',  {duration: 300});
			slideIn.myCustom = function(){
				slideIn.custom(30, 0)
			};
			slideIn.myCustom.delay(this.timer);
		}, this);
	}
};


