/**
 * @version Virtuo PhotoGallery - version 0.2
 * @copyright SystemCore Ltd. 2008. All rights reserved.
 */

function PhotoGallery(){
};

PhotoGallery.prototype.cmdShowPicture = function(target){
	
	var body = $('body');
	
	var container = document.createElement('div');
	container.id = 'photo_gallery_container';
	
	var loading = document.createElement('div');
	loading.id = 'photo_gallery_loading';
	
	body.appendChild(container);
	container.appendChild(loading);
	
	classPhotoGallery.current_target = target.getElementsByTagName('a')[0];
   	classPhotoGallery.pictures = $('photo_container').getElementsByTagName('a');
	
	for(var i=0; i<classPhotoGallery.pictures.length; i++) {
		if(classPhotoGallery.current_target == classPhotoGallery.pictures[i]) {
			classPhotoGallery.current_index = i;
			classPhotoGallery.picture = classPhotoGallery.pictures[i].href;
		    classPhotoGallery.description = classPhotoGallery.pictures[i].title;
		    
			var img = document.createElement('img');
			img.id = 'photo_gallery_img';
			img.src = classPhotoGallery.picture;
			
		    classPhotoGallery.isShowing = true;
		    classPhotoGallery.image_object = new Image();
			classPhotoGallery.image_object.src = classPhotoGallery.picture;
			classPhotoGallery.timer = setInterval(classPhotoGallery.cmdCheckStatus,500);
			break;
		}		
	}
};

PhotoGallery.prototype.cmdCheckStatus = function(){
	
	if(classPhotoGallery.image_object.complete == true) {
		clearInterval(classPhotoGallery.timer);
		
		var width = classPhotoGallery.image_object.width + 30;
		var height = classPhotoGallery.image_object.height + 70;
		
    	var body = $('body');
		var container = $('photo_gallery_container');
		var loading = $('photo_gallery_loading');
		
		container.removeChild(loading);
		
		var frame = document.createElement('div');
		frame.id = 'photo_gallery_frame';
		
		var img = document.createElement('img');
		img.id = 'photo_gallery_img';
		
		var close_button = document.createElement('div');
		close_button.id = 'photo_gallery_close_button';
		close_button.onclick = function(){ classPhotoGallery.cmdCloseGallery(); }; //mocskos IE miatt kell így!!!
		
		var footer = document.createElement('div');
		footer.id = 'photo_gallery_footer';
		footer.style.cssText = 'width:' + Number(width - 30) + 'px;';
		
		var prev_button = document.createElement('div');
		prev_button.id = 'photo_gallery_prev_button';
		
		var next_button = document.createElement('div');
		next_button.id = 'photo_gallery_next_button';
		
		var desc_container = document.createElement('div');
		desc_container.id = 'photo_gallery_desc';
		desc_container.style.cssText = 'width:' + Number(width - 76) + 'px;';
		img.src = classPhotoGallery.picture;
		
		container.appendChild(frame);
		
		frame.appendChild(close_button);
		
		frame.style.cssText = 'width:' + width + 'px; height:' + height + 'px;';
		frame.appendChild(img);
		
		frame.appendChild(footer);
		footer.appendChild(prev_button);
		footer.appendChild(desc_container);
		footer.appendChild(next_button);	
		
		prev_button.onclick = function(){classPhotoGallery.cmdPrev();};
		next_button.onclick = function(){classPhotoGallery.cmdNext();};
		
		
		desc_container.innerHTML = classPhotoGallery.description;
	}
};

PhotoGallery.prototype.cmdPrev = function() {
	if(classPhotoGallery.current_index > 0) {
		classPhotoGallery.current_index--;
	} else {
		classPhotoGallery.current_index = classPhotoGallery.pictures.length-1;
	}
	var target = classPhotoGallery.pictures[classPhotoGallery.current_index].parentNode.parentNode.parentNode;
	
	classPhotoGallery.cmdCloseGallery();
	classPhotoGallery.cmdShowPicture(target);
};

PhotoGallery.prototype.cmdNext = function() {
	if( classPhotoGallery.current_index < classPhotoGallery.pictures.length-1) {
		classPhotoGallery.current_index++;
	} else {
		classPhotoGallery.current_index = 0;
	}
	var target = classPhotoGallery.pictures[classPhotoGallery.current_index].parentNode.parentNode.parentNode;
	
	classPhotoGallery.cmdCloseGallery();
	classPhotoGallery.cmdShowPicture(target);	
};

PhotoGallery.prototype.cmdCloseGallery = function(){
	var body = $('body');
	var container = $('photo_gallery_container');
	body.removeChild(container);
};

var classPhotoGallery = new PhotoGallery();
