//load ie javascript
if(sc.browser.agent == 'ie' && (sc.browser.version >= 5 && sc.browser.version < 7)){
	sc.javascript.load('../js/ie.js');
	sc.styles.load('../css/ie.css');
}

//global variables
var body = $('body');

//instantiate the gallery object
var gallery = {
	gid : 1,
	thumbnails : [],
	links :{
		all : $('a'),
		galleries : [],
		thumbs :[]
	},
	
	
	loadFullSize : function(){
		this.fullSize.src = this.src;
		
	},
	
	markEmpty : function(obj){
		obj = $(obj);
		obj.style.backgroundColor='#c2cd43';
		obj.style.color='white';
	},
	
	selectGal : function(obj){
		obj = $(obj);
		obj.style.backgroundColor='red';
		obj.style.color='white';
	},
	
	loadImage : function(){
		gallery.fullSize.src = this.src.replace("th_", "");
		gallery.fullSize.style.width='auto';
		gallery.mid = this.mid;
		sc.css(gallery.fullSize, 'opacity', 0);
		gallery.fullSize.loading = new sc.effects.fade('fullSize');
		gallery.fullSize.loading.duration =5;
		gallery.fullSize.onload = function(){
			if(this.offsetWidth > 640){
				this.style.width="640px";
			}
			gallery.fullSize.loading.start(0, 100);
			$('captionContainer').style.width = this.offsetWidth+'px';
		}
		
		if(gallery.clickForFullSize ===1){
			gallery.fullSize.title = 'Click to open full size image in new window';
			gallery.fullSize.style.cursor='pointer';
			gallery.fullSize.onclick = function(){
				sc.browser.win('../media/gal_'+gallery.gid+'/orig_'+this.src.basename());
			}
		}
		
		$('caption').innerHTML = this.title;
	},
	
	getThumbs : function(){
		new ajax({
			debug :0,
			value : this.value,
			format :'js',
			method : 'post',
			url : '../data/process.php',
			data : 'getThumbs='+gallery.gid,
			handler : function(r){
				
				eval(r);
				gallery.displayThumbs();
				if(typeof galleryAuthor != 'undefined'){
					galleryAuthor.makeThumbsDeletable();
				}
				
			}
		}).fetch();	
	},
	
	displayThumbs : function(){
		$('thumbs', 'img').forEach(function(v,k,a){
			
				remove(v);
			
		});	
			
		this.thumbs.forEach(function(v,k,a){
		
			
			var im = ce('img');
			
			gallery.thumbsContainer.appendChild(im);
			im.style.width="120px";
			im.style.height="auto";
			im.mid = v.mid;
				im.title = v.caption || gallery.blankCaption;
			im.onclick = gallery.loadImage;
			
			if(k == 0){
				im.onload = gallery.loadImage;
				
			}
			
			im.src = '../media/gal_'+gallery.gid+'/th_'+v.src;
			gallery.thumbnails.push(im);
			
			im = null;
			
		});
	},
	
	
	init : function(){
		
		this.galleriesToLinks();
		this.displayThumbs();
		
		
	},
	
	clearThumbnails : function(){
		$('thumbs', 'li').forEach(function(v,k,a){
			if(v.firstChild.nodeName.match(/a/i)){
				remove(v);
			}
			
		});	
	},
	
	clearViewer : function(){
		$('caption').innerHTML = '';
		$('fullSize').src='../surebertCom/spacer.gif';
	},
	
	clearAll : function(){
		this.clearThumbnails();
		this.clearViewer();
	},
	
	selectCurrent : function(){
		
		this.links.galleries.forEach(function(v,k,a){
		
			if(v.total_media ==0 ){
				gallery.markEmpty(v);
			}
			
			if(v.gid == gallery.gid){
				gallery.select(v);
			}
		});
	},
	select : function(a){
		a.style.color='green';
		a.style.backgroundColor='yellow';
	},
	
	deselect : function(a){
		a.style.backgroundColor='';
		a.style.color ='';
		if(a.total_media ==0 ){
			gallery.markEmpty(a);
		}
	},
	
	
	galleriesToLinks : function(){
		
		this.galleries.forEach(function(v,k,a){
			
			var li = ce('li');
			var a = ce('a');
			a.gid = v.gid;
			a.home = v.home;
			a.total_media = v.total_media;
			
			if(v.total_media ==0 ){
				gallery.markEmpty(a);
			}
			
			
			
			if(v.gid == gallery.gid){
				gallery.select(a);
				gallery.total_media = v.total_media || 0;
			}
			
			a.href="../app/index.php?gid="+v.gid;
			a.innerHTML = v.title;
			
			/*
			//add later to mark homepage
			if(v.home ==1){
				a.innerHTML +='&nbsp;';
				im = ce('img');
				im.src = '../img/ico_home_mini.png';
				sc.browser.pngFix(im);
				a.appendChild(im);
				im = null;
			}*/
			
			a.title = v.total_media+' files';
			li.appendChild(a);
			$('galleries').appendChild(li);
			
			gallery.links.galleries.push(a);


			a, li = null;
			
		});
	},
	
	warn : function(str, h){
		sc.warning.height=h || 120;
		sc.warning.warn('<img src="../img/ico_help_large.png" style="float:left;margin-right:10px;width:100px;height:102px;" />'+str);
	},
	
	noGalleriesFound : function(){
		body.style.backgroundColor='orange';
		body.style.backgroundImage ='none';
		var d = ce('div');
		d.style.padding='20px';
		d.style.height='800px';
		d.style.width='100%';
		d.style.backgroundColor='orange';
		d.style.position='absolute';
		d.style.top='30px';
		d.style.left='0px';
		d.style.zIndex='999';
		d.style.color='white';
		d.style.textAlign='center';
		d.style.fontSize='20px';
		
		d.innerHTML = 'There are no galleries, please come again later!';
		$('body').appendChild(d);
		p1 = ce('p');
		p1.style.fontSize='400px';
		p1.innerHTML = 'G';
		d.appendChild(p1);
		d,p1=null;
		
	}
	
};

//define the echo home default
gallery.gid = $_GET['gid'] || gallery.gid;
//set the targets for links that should open in a new window
gallery.links.all.forEach(function(v,k,a){
	if(v.className == 'blank'){
		v.target = '_blank';
	}
});


sc.events.add(window, 'load', function(){
	if(gallery.galleries.length ==0){
	
			gallery.noGalleriesFound();
		
	}
});


gallery.slideShow = {
	image : 1,
	status : 0,
	
	timer : new sc.timer(5.0, function(){
		if(gallery.slideShow.image <gallery.thumbnails.length){
			gallery.slideShow.next();
		} else {
			gallery.slideShow.image=0;
		}
	}),
	
	next : function(){
		gallery.loadImage.apply(gallery.thumbnails[gallery.slideShow.image]);
		gallery.slideShow.image++;	
	},
	
	start : function(){
		this.timer.begin();
	},
	
	stop : function(){
		this.timer.end();
	},
	
	toggle : function(){
		if(this.status ==0){
			this.start();
			this.status =1;
			this.next();
			this.button.src = '../img/ico_pause.png';
		} else {
			this.stop();
			this.button.src = '../img/ico_play.png';
			this.status=0;
		}
	},
	
	init : function(){
		this.button = sc.dom.ce('img');
		this.button.style.padding='10px';
		
		$('viewer').appendChild(this.button);
		this.button.src = '../img/ico_play.png';
		this.button.onclick = function(){
			gallery.slideShow.toggle();
		}
	}
};

gallery.slideShow.init();
