// AUTHOR: FABIO CIMINELLI
// http://www.onyourglobe.com

// ###### START CONFIGURAZIONE
var pAutore = '';
var totFoto = 30;
var numPerRiga = 4;
var numMaxRighe = 4;
var countPerRiga = 0;
var countRighe = 0;
var arrayFotoGallery = new Array();		
var thumbSize = 2; // thumbnail medium
// ###### END CONFIGURAZIONE

var http = creaXMLHTTP();

function creaXMLHTTP() {
var ro;
if(navigator.appName == "Microsoft Internet Explorer"){
	ro = new ActiveXObject("Microsoft.XMLHTTP");
} else {
	ro = new XMLHttpRequest();
}
return ro;
}


dojo.require("dojo.fx.easing");

function randNum(min,max){
    var m=min;
    var n=max;
    var r=m+Math.round(Math.random()*n); 
	return(r); 
}


function basicFadeinSetup3() {
	for (var i = 0; i < gallery.length; i++){ 
		  var photo = i+'Photo';
		dojo.style(photo, "opacity", "0");
		
		if(i<(numPerRiga * numMaxRighe)) document.getElementById(photo).innerHTML = arrayFotoGallery[i];
	
			var fadeArgs = {
            node: photo,
            duration: 10000,
            easing: dojo.fx.easing.expoOut
        };
			
			dojo.fadeIn(fadeArgs).play();
     } 
 }			

dojo.addOnLoad(basicFadeinSetup3);

function fadeOut() {
	var numCell=randNum(0,(numPerRiga*numMaxRighe)-1);
  var photo = numCell+'Photo';
     // dojo.style(photo, "opacity", "0");
	
	var fadeArgs = {
          node: photo,
          duration: 10000,
          easing: dojo.fx.easing.expoOut
      };
	
      dojo.fadeOut(fadeArgs).play();					
	setTimeout('fadeIn('+numCell+')',25000);
  }

function fadeIn(numCell){
	//var numCell=randNum(0,15);
  var photo = numCell+'Photo';
	
	var fadeArgs = {
          node: photo,
          duration: 10000,
          easing: dojo.fx.easing.expoOut
      };
	
	var numPhotoArray=randNum(0,totFoto);
	if(arrayFotoGallery[numPhotoArray]) document.getElementById(photo).innerHTML = arrayFotoGallery[numPhotoArray];
	
      dojo.fadeIn(fadeArgs).play();					
}

		
function startSlide(){
		var t = setInterval("fadeOut()",4000);			
}
			
			

var gallery;
function photoGallery(panoramio) { 
		var count = panoramio.count;
		if(count>100) totFoto = 100;
		else totFoto = count;
		
    gallery = panoramio.photos; 
		if (gallery.length > 0) {
			 pAutore = gallery[0].owner_name;
		}
}
			 


function writeHtmlGrid(){
	
	var IMAGES_GALLERY  = "<table cellpadding=\"0\"  cellspacing=\"0\" border=\"0\" >";			
	    IMAGES_GALLERY += "<TR><TD>";
			
    	if (gallery.length > 0) {  
    			 
  			 IMAGES_GALLERY_TAB  = "<TABLE cellpadding=\"0\"  cellspacing=\"0\" border=\"0\" >";
  			 IMAGES_GALLERY_TAB += "<TR>";
				 
				 // costruisco la griglia (tabella) 
         for (var i = 0; i < gallery.length; i++) 
         {   
    				if(countRighe>(numMaxRighe-1)) {break; }
  					
    				IMAGES_GALLERY_TAB += "<TD style=\"padding-right:1px;padding-bottom:1px;\" ><div id=\""+i+"Photo\">";
    				IMAGES_GALLERY_TAB += "</div></TD>";
    				
  					countPerRiga += 1;	
  					
    				if(countPerRiga > (numPerRiga-1)){
    						IMAGES_GALLERY_TAB += "</TR><TR>";
    						countPerRiga = 0;			
  							countRighe += 1;		
    				}			  			
         } 				 
  			 IMAGES_GALLERY_TAB += "</TR>";
  			 IMAGES_GALLERY_TAB += "</TABLE>";
				 
      } 

			IMAGES_GALLERY += IMAGES_GALLERY_TAB;
			  	
	    IMAGES_GALLERY += "</TD></TR>";
	    IMAGES_GALLERY += "<TR><TD colspan=\""+numPerRiga+"\" style=\"font-family:verdana,arial;font-size:10px;color:gray;padding-top:3px;\" >";
			IMAGES_GALLERY += "<span><img src=\"http://www.onyourglobe.com/img/panoramio_logo.png\">&nbsp;&nbsp;&nbsp;</span>";
			IMAGES_GALLERY += "<span id=\"panoAutor\" style=\"vertical-align:top;\">by Fabio Ciminelli</span></TD></TR>";
	    IMAGES_GALLERY += "</TABLE>";

  		document.write(IMAGES_GALLERY);	
			document.write("</div>");
}
	
function buidPhotosArray(){				 
		 var strWidth="";
		 if(thumbSize==2) strWidth="width=50px;";

		 // costruisco l'array di photo
		 for (var i = 0; i < gallery.length; i++){   				
				 var idDiv = i+'Photo';
				 var IMAGES_GALLERY_x = "<a href=\""+gallery[i].photo_url+"\" title=\""+gallery[i].photo_title+"\" target=\"photoPanoramio\"><img src=\"" + gallery[i].photo_file_url + "\" border=\"0\" "+strWidth+" ></a>";
				 arrayFotoGallery[i] = IMAGES_GALLERY_x;
       } 
}

function startBadge(idPano, pNumMaxRighe, pNumPerRiga, pSize){

    if(pNumPerRiga) numPerRiga = pNumPerRiga;
    if(pNumMaxRighe) numMaxRighe = pNumMaxRighe;
		if(pSize) thumbSize = pSize;
		//alert(thumbSize +" - "+ pSize);
		
		writeHtmlGrid();
    buidPhotosArray();
    startSlide();
		
		document.getElementById('panoAutor').innerHTML = pAutore;
		
		registraDownload(idPano,pAutore);
}

function registraDownload(idPano,pAutore) {
    http.open('get', 'http://www.onyourglobe.com/badge/insertRecord.asp?idPano=' + idPano + '&user=' + pAutore);
    http.send(null);
}

