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,
	image_loader_class : 'loading',
	switchphoto : function(ii){
		$('#loader').html('');
		$('#straimg').html('');
		$('#straidesc').html('');
 		$('#watermark').html('');

        $('#loader').addClass(this.image_loader_class);
		ii = parseInt(ii); //konversija i int'a
		data = photos_array[ii];
		this.active_index = ii;
		var img = new Image();
		img.src = data['src'];
		
		if(data.watermark!=""){
			var watermark = new Image();
			watermark.src = '/'+data['watermark'];
		} else {
			var watermark = "";
		}	
		$('#watermark').hide();
		$(img)
		    .load(function () {
		      $(this).hide();
		      $('#loader')
		        .removeClass(gallery.image_loader_class)
		        .html(this);
		      $('#watermark').html(watermark);
		      $('#watermark').fadeIn();
		     // $(this).fadeIn();

		      $(this).attr('style','display:block; opacity:0;');
			  $(this).animate({opacity: 1.0}, 400 );
					  
			  $('#straidesc').html(data['title']);
			  if(data['author']=='')
				  $('#straimg').html(data['author']);
			  else
				  $('#straimg').html(data['author']+" nuotr.");
		    })
		    
		    .error(function () {
				// klaidos
		    })
		    .attr('src', data['src']).attr('title',data['title']).attr('class','border5 pad10').attr('id','aimg').attr('alt',data['title']);
		
		imgs = getElementsByName_iefix('a','galbutton');
		for (i=0; i<imgs.length; i++){
			imgs[i].className = 'btn_image_pasive fl pad27';
		}
		document.getElementById('galbutton'+data['img']).className= 'btn_image_active fl pad27';
		
	},
	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, show) {
		this.active_world_gal = gal;
		this.active_world_img = img;
		if(show)
			this.wswitch(gal, img, 0);
	},
	wswitch: function(gal_id, img_id, dir){
		$('#loader').html('');
		
		$('#straidesc').html('');
		$('#straimg').html('');
		$('#watermark').html('');
        $('#loader').addClass(this.image_loader_class);
		
		$.getJSON('/galleries/world_ajax/'+gal_id+'/'+img_id+'?direction='+dir,null,
			function(data) {
				var img = new Image();
				img.src = data.src;
				if(data.watermark!=""){
					var watermark = new Image();
					watermark.src = '/'+data.watermark;
				} else {
					var watermark = "";
				}
				$('#watermark').hide();
				$(img)
				    .load(function () {
				      $(this).hide();
				      $('#loader')
				        .removeClass(gallery.image_loader_class)
				        .html(this);
						
				      $('#watermark').html(watermark);
				      $('#watermark').fadeIn();
				      //$(this).fadeIn(function(){$(this).attr('style','display:block;');});
					//var randomnumber=1 + Math.floor(Math.random()*4);
					//$('#loader').attr('style', 'background-image:url(/public/layouts/images/preloader-' + randomnumber  + '.png');
					    
				      $(this).attr('style','display:block; opacity:0;');
					  $(this).animate({opacity: 1.0}, 400 );					  
				      
					  $('#straidesc').html(data.description);
					  $('#gallery_date').html(data.date);
					  $('#aanchor').attr('title',data.description);
					  if(data.author=='')
					  	$('#straimg').html(data.author);
					  else
						$('#straimg').html(data.author+" nuotr.");
					
					$('#loader').attr('onclick', 'return false;');
				    })
				    
				    .error(function () {
						// klaidos
				    })
				    .attr('src', data.src).attr('title',data.description).attr('class','border5 pad10').attr('id','aimg').attr('alt',data.description);
				
				gallery.initworld(data.gal, data.img, false);

				pageTracker._trackPageview('/galleries/world/'+gal_id+'/'+img_id+'/ajxed.html');
				    
				
				
				/*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');
				}*/
			});
	}
}
