function getElementsByName_iefix(tag, name) {
     var elem = document.getElementsByTagName(tag);
     var arr = new Array();
     for(i = 0,iarr = 0; i < elem.length; i++) {
          att = elem[i].getAttribute("name");
          if(att == name) {
               arr[iarr] = elem[i];
               iarr++;
          }
     }
     return arr;
};

var gallery = {
	active_index : 0,
	active_world_gal : 0,
	active_world_img : 0,
	switchphoto : function(ii){
		$('#loader').html('');
		$('#straimg').html('');
		$('#straidesc').html('');
		$('#loader').addClass("loading");
		$('#loader').addClass("plotis13");
		ii = parseInt(ii); //konversija i int'a
		data = photos_array[ii];
		this.active_index = ii;
		var img = new Image();
		img.src = data['src'];
		
		$(img)
	    .load(function () {
	      $(this).hide();
	      $('#loader')
	        .removeClass("loading")
	        .removeClass("plotis13")
	        .html(this);

	      $(this).attr('style','opacity:0;');
		  $(this).animate({opacity: 1.0}, 400 );
				  
		  $('#straidesc').html(data['title']);
	    })
	    
	    .error(function () {
			// klaidos
	    })
	    .attr('src', data['src']).attr('title',data['title']).attr('id','aimg').attr('alt',data['title']);
	
		
		imgs = getElementsByName_iefix('a','galbutton');
		for (i=0; i<imgs.length; i++){
			imgs[i].className = '';
		}
		document.getElementById('galbutton'+data['img']).className= 'active';
		
	},
	nextphoto: function(){
		if (photos_array[this.active_index+1]){
			this.switchphoto(this.active_index+1);
		}else {
			this.switchphoto(0);
		}
	},
	prevphoto: function(){
		if (photos_array[this.active_index-1]){
			this.switchphoto(this.active_index-1);
		}else {
			this.switchphoto(photos_array.length-1);
		}
	},
	
	nextworld: function() {
		this.wswitch(this.active_world_gal, this.active_world_img, 1);
	},
	
	prevworld: function() {
		this.wswitch(this.active_world_gal, this.active_world_img, -1);
	},
	
	initworld: function(gal, img) {
		this.active_world_gal = gal;
		this.active_world_img = img;
	},
	
	wswitch: function(gal_id, img_id, dir){
		$.getJSON('/galleries/world_ajax/'+gal_id+'/'+img_id+'?direction='+dir,null,
			function(data) {
				document.getElementById('aimg').src=data.src;
				document.getElementById('aimg').title=data.description;
				document.getElementById('aimg').alt=data.description;
				document.getElementById('straidesc').innerHTML = data.description;
				document.getElementById('gallery_date').innerHTML = data.date;
				$('#aanchor').attr('title',data.description);
				gallery.initworld(data.gal, data.img);
				if(data.author=='')
					document.getElementById('straimg').innerHTML = data.author;
				else
					document.getElementById('straimg').innerHTML = data.author+" nuotr.";
				/*if(data.prev=='1') {
					//document.getElementById('aprev').href = 'javascript:gallery.wswitch('+data.gal+','+data.img+',-1);';
					$('#aprev').removeClass('hidden');
				} else {
					$('#aprev').addClass('hidden');
				}
				if(data.next=='1') {
					//document.getElementById('anext').href = 'javascript:gallery.wswitch('+data.gal+','+data.img+',1);';
					//$('#aanchor').attr('href', "JavaScript:gallery.nextworld();");
					$('#anext').removeClass('hidden');
				} else {
					//$('#aanchor').attr('href', "JavaScript:void(0);");
					$('#anext').addClass('hidden');
				}*/
			});
		$.ajaxStart(function(){
			$(this).addClass("loading");
			$(this).addClass("plotis13");
		});
		$.ajaxStop(function(){
			$(this).removeClass("loading");
			$(this).removeClass("plotis13");
		});
	}
}