
window.addEvent('domready', setupFlickr);
var galleryID = "";
var imageID = "";
var thumbID = "";
var imageInfo = new Object();
var thumbInfo = new Object();
var infoRequest = null;
var thumbRequest = null;
var sizesRequest = null;
var contextRequest = null;
var searchArgs = "photo_id,api_key,user_id,tags,tag_mode,text,min_upload_date,max_upload_date,max_taken_date,license,sort,privacy_filter,bbox,accuracy,machine_tags,machine_tag_mode,group_id,extras,per_page,page";
function setupFlickr(){
	
	//find all the galleries
	var galleries = $$('.flickrgallery');
	for(var i=0;i < galleries.length;i++){
		var gallery = galleries[i];
		gallery.setAttribute('id', 'gallery_'+i);
		galleryID = 'gallery_'+i;
		var user_id = galleries[i].getAttribute('user_id');
		var tags = galleries[i].getAttribute('tags');
		
        var queryString = "";
		var galleryAttributes = gallery.attributes;
		for(var j=0;j < galleryAttributes.length;j++){
		  var attrName = galleryAttributes.item(j).nodeName;
		  if(searchArgs.search(attrName) > 0){
		    var attrValue = galleryAttributes.item(j).nodeValue;
		    queryString = queryString + '&' + attrName + '=' + attrValue;    
		  }
		    
		}
		var requestString = '/flickr.php?method=flickr.photos.search&format=json&api_key=ade5a37cea17a02f850128425ca8f6bf'+queryString;
		//alert(queryString);
		var jSonRequest = new Request.JSON({url:requestString ,async: true, onComplete: setGallery}).send();
	}
	
	//find all the images
	var images = $$('.flickrImage');
	for(var i=0;i < images.length;i++){
		var image = images[i];
		doImage(image, i);
		
	}
	
	//find all the thumbs
	var thumbs = $$('.flickrThumb');
	for(var i=0;i < thumbs.length;i++){
		var thumb = thumbs[i];
		doThumb(thumb, thumb.get('photo_id'));
		
	}
}

function doThumb(thumb, id)
{
	
	thumbID = thumb.getAttribute('id');
	if(thumbID == null)
	{
		thumb.setAttribute('id', 'thumb_'+id);
		thumbID = thumb.getAttribute('id');
	}
	
	
    var queryString = "";
	var thumbAttributes = thumb.attributes;
	for(var j=0;j < thumbAttributes.length;j++){
	  var attrName = thumbAttributes.item(j).nodeName;
	  
	  if(searchArgs.search(attrName) >= 0){
	    var attrValue = thumbAttributes.item(j).nodeValue;
	    queryString = queryString + '&' + attrName + '=' + attrValue;    
	  }
	    
	}
	thumbInfo = new Object();
	thumbInfo.currentSize = $(thumbID).getAttribute('size') || 'Medium';
	var requestString = '/flickr.php?method=flickr.photos.getInfo&format=json&api_key=ade5a37cea17a02f850128425ca8f6bf'+queryString;
	var jSonRequest = new Request.JSON({url:requestString ,async: true, onComplete: setThumb}).send();
}

function doImage(image, id)
{
	
	imageID = image.getAttribute('id');
	if(imageID == null)
	{
		image.setAttribute('id', 'image_'+id);
		imageID = image.getAttribute('id');
	}
	
	
    var queryString = "";
	var imageAttributes = image.attributes;
	for(var j=0;j < imageAttributes.length;j++){
	  var attrName = imageAttributes.item(j).nodeName;
	  
	  if(searchArgs.search(attrName) >= 0){
	    var attrValue = imageAttributes.item(j).nodeValue;
	    queryString = queryString + '&' + attrName + '=' + attrValue;    
	  }
	    
	}
	imageInfo = new Object();
	imageInfo.currentSize = $(imageID).getAttribute('size') || 'Medium';
	var requestString = 'http://www.flickr.com/services/rest/?method=flickr.photos.getInfo&format=json&jsoncallback=setImageInfo&api_key=ade5a37cea17a02f850128425ca8f6bf'+queryString;
	infoRequest = new JSONscriptRequest(requestString);
	infoRequest.buildScriptTag();
	infoRequest.addScriptTag();
}

function setThumb(rsp, text){
	var source = '';
	var img = new Element('img', {'width':235,'src':'http://farm' + rsp.photo.farm + '.static.flickr.com/' + rsp.photo.server + '/' + rsp.photo.id + '_' + rsp.photo.secret + '.jpg'});
	$('thumb_'+rsp.photo.id).adopt(img);
	/*infoRequest.removeScriptTag();
	//$(imageID).setHTML('got image info');
	imageInfo.info = rsp;
	var photo_id = rsp.photo.id;
	var requestString = 'http://www.flickr.com/services/rest/?method=flickr.photos.getContext&format=json&jsoncallback=setContextInfo&api_key=ade5a37cea17a02f850128425ca8f6bf&photo_id='+photo_id;
	contextRequest = new JSONscriptRequest(requestString);
	contextRequest.buildScriptTag();
	contextRequest.addScriptTag();
	*/
}

function setImageInfo(rsp){
	infoRequest.removeScriptTag();
	//$(imageID).setHTML('got image info');
	imageInfo.info = rsp;
	var photo_id = rsp.photo.id;
	var requestString = 'http://www.flickr.com/services/rest/?method=flickr.photos.getContext&format=json&jsoncallback=setContextInfo&api_key=ade5a37cea17a02f850128425ca8f6bf&photo_id='+photo_id;
	contextRequest = new JSONscriptRequest(requestString);
	contextRequest.buildScriptTag();
	contextRequest.addScriptTag();
}


function setContextInfo(rsp){
	contextRequest.removeScriptTag();
	//$(imageID).setHTML('got context info');
	imageInfo.context = rsp;
	var photo_id = $(imageID).getAttribute('photo_id');
	var requestString = 'http://www.flickr.com/services/rest/?method=flickr.photos.getSizes&format=json&jsoncallback=setImageSizes&api_key=ade5a37cea17a02f850128425ca8f6bf&photo_id='+photo_id;
	sizesRequest = new JSONscriptRequest(requestString);
	sizesRequest.buildScriptTag();
	sizesRequest.addScriptTag();
}

function setImageSizes(rsp){
	sizesRequest.removeScriptTag();
	//$(imageID).setHTML('got sizes info');
	imageInfo.sizes = rsp.sizes;
	renderImage(imageInfo);
}

function renderImage(imageData){
	//new Ajax('/image_template', {method: 'get', onSuccess: processTemplate}).request();
	
	var result = $('image_template').get('text');
	
	//function processTemplate(result){
		//var infoResult = result.process(imageData, {throwExceptions:true});
		var infoResult = TrimPath.processDOMTemplate("image_template", imageData);
		$(imageID).set('html', infoResult);
	//}
}



function setGallery(rsp){
	$(galleryID).empty();
	for(var i=0;i < rsp.photos.photo.length;i++){
		var imgSrc = 'http://farm1.static.flickr.com/'+rsp.photos.photo[i].server+'/'+rsp.photos.photo[i].id + '_' + rsp.photos.photo[i].secret + '_s.jpg';
		
		var navigation = $(galleryID).getAttribute('navigation');
		
		var img = new Element('img');
		img.setProperty('src', imgSrc);
		var link = new Element('a');
		if(navigation == '' || navigation == null){
			link.setProperty('href', '/image/'+rsp.photos.photo[i].id);
		}else{
			var mImgSrc = 'http://farm1.static.flickr.com/'+rsp.photos.photo[i].server+'/'+rsp.photos.photo[i].id + '_' + rsp.photos.photo[i].secret + '.jpg';
			link.setProperty('href', 'javascript:$("'+navigation+'").setAttribute("photo_id","'+ rsp.photos.photo[i].id +'");doImage($("'+ navigation +'"),"'+navigation+'");');
			//link.setProperty('href', 'javascript:');
		}
		link.setProperty('class', 'flickrLink');
		
		link.adopt(img);
		//alert(gallery.id)
		$(galleryID).adopt(link);
	}
	if(rsp.photos.photo.length == 0)
	{
		$(galleryID).set('html','no images');
	}
	//Lightbox.init();
}

function showNotes(event){
		var inside = false;
		
		$$('.noteBox').each(function(el){
			var coords = el.getCoordinates();
			var thisInside = (event.pageX >= coords.left && 
				event.pageX <= coords.right &&
				event.pageY >= coords.top &&
				event.pageY <= coords.bottom);
			
			if( thisInside){
				inside = true;	
			}			
		});
		if(! inside){
			$$('.noteBox').each(function(el){
			var coords = el.getCoordinates();
				
				el.effect('opacity').set(1);
				//el.show();
			});
		}
		
		
	}
	
	function hideNotes(event){
		var inside = false;
		
		$$('.noteBox').each(function(el){
			var coords = el.getCoordinates();
			var thisInside = (event.pageX >= coords.left && 
				event.pageX <= coords.right &&
				event.pageY >= coords.top &&
				event.pageY <= coords.bottom);
			
			if( thisInside){
				inside = true;	
			}			
		});
		if(! inside){
			$$('.noteBox').each(function(el){
				var coords = el.getCoordinates();
				el.effect('opacity').set(0);
				
			});
		}
	}
	
	
	window.addEvent('domready', function(){
		//var myTips = new Tips($$('.noteBox'));
	});