var flickr = {
	
	myFlickrFeed: new Ajax("/en/experience/feeds/flickr.xml", {method: 'get'}).request(),
	flickrPhoto : [],
	photoCount: 0,
	maxPhotos: 28,
	photoArray: new Array(),
	

	read : function() {
		// This is going to read the photos Array
		var flickrXMLDoc = this.myFlickrFeed.response.xml;
		var photos = flickrXMLDoc.getElementsByTagName('photo');
		var currNode;

		/*LOOP THRU YOU TUBE FEED AND MAKE ARRAY*/
		for (i=0; photos.length > i; i++){
			var nodeCount = photos[i].childNodes.length;
			this.flickrPhoto["photo" + i] = new Array(); //new array for each photo
			
			for (n=0; nodeCount > n; n++) {//photo nodes
				if (photos[i].childNodes[n].nodeType == 1) {
					//populate array with <photo> child nodes
					if(photos[i].childNodes[n].nodeName == "tags"){
						if(photos[i].childNodes[n].hasChildNodes()) {							
							var tags = photos[i].childNodes[n].childNodes;
							var cur_tags = new Array();
							
							for(x=0; tags.length>x; x++) { //loop thru <photo><tags>
								if(tags[x].nodeType == 1) {	
									cur_tags.include(tags[x].firstChild.nodeValue)
								}
							}
							
							this.flickrPhoto["photo" + i][photos[i].childNodes[n].nodeName] = cur_tags;
						}
                    }else{
						if(photos[i].childNodes[n].nodeName == "title"){
							if(photos[i].childNodes[n].hasChildNodes()) {
								currNode = photos[i].childNodes[n].firstChild.nodeValue;
							} else {
								currNode = "Untitled";
							}
							this.flickrPhoto["photo" + i][photos[i].childNodes[n].nodeName] = currNode;
						} else {
							this.flickrPhoto["photo" + i][photos[i].childNodes[n].nodeName] = photos[i].childNodes[n].firstChild.nodeValue;
						}
					}
				}
			}
		}
	},
	
	find : function() {
		// This will build out thumbs from XML array
		var thumbLayout = '';
        
		for (photo in this.flickrPhoto) {
			if (typeof(this.flickrPhoto[photo]) == 'object') {
				for(tag in this.flickrPhoto[photo]['tags']) {
					if(typeof(this.flickrPhoto[photo]['tags'][tag]) == 'string') {
						if(global.filtersArray.contains(this.flickrPhoto[photo]['tags'][tag])) {
							this.photoArray.include(this.flickrPhoto[photo]);
						}
					}
				}
			}
		}
		
		//Start Random Generation of Tagged images
		var randomNumberArray = new Array();
		var argItem = '';
		
		for (i=0; this.photoArray.length > i;i++){
		   
		    var randomnumber = Math.floor(Math.random()*this.photoArray.length);
		    
		    if(!randomNumberArray.contains(randomnumber)){
		    
		        randomNumberArray.include(randomnumber);
		        
		        argItem = this.photoArray[randomnumber];
		        
		        
		        var randomMSNumber = Math.floor(Math.random() * 3500) + 1000;
                setTimeout("flickr.animate('image" + this.photoCount+"')",randomMSNumber);

                thumbLayout += flickr.build(flickr.photoArray[randomnumber]);
		        
				this.photoCount++;
		     }else{
		        i--;
		     }
		     
		     //if(this.photoCount == 28){ i = this.photoArray.length}
			 if(this.photoCount == this.maxPhotos){ i = this.photoArray.length}
		}   
		
		thumbLayout += '<div class="autoclear"><br /></div>';
		
		$('flickr_target').setHTML(thumbLayout);
		
		global.makeTips({x:-252,y:-110},'.flickr_link','flickr-tool');
		
	},

	build : function(argItem) {
		var itemLayout = '';
		var imageTitle = '';
		var dataAdded = new Date(parseInt(argItem['dateadded']) * 1000);
		var catchQuote = '&#34;';
	    var catchSingleQuote = '&#39;';
	    var filterTitle1 = argItem['title'].replace(/"/g, catchQuote);
	    var filterTitle2 = filterTitle1.replace(/'/g, catchSingleQuote);
	    
	    argItem['title'] = filterTitle2;
	    
		var months = new Array(12);
        months[0] = "January";
        months[1] = "Febuary";
        months[2] = "March";
        months[3] = "April";
        months[4] = "May";
        months[5] = "June";
        months[6] = "July";
        months[7] = "August";
        months[8] = "Septemeber";
        months[9] = "October";
        months[10] = "November";
        months[11] = "December"; 


		var imageDate = "<br /><strong>Uploaded To Flickr On</strong>: <b>" + months[dataAdded.getMonth()] + " " + dataAdded.getDate() + ", " + dataAdded.getFullYear() + "</b>";
	
		if(argItem['title']){
		    imageTitle += '<strong>'+ argItem['title'] +'</strong><br />';
		}else{
		    imageTitle += '<strong>Untitled </strong><br />';
		}
		
		if(argItem['ownername']){
		    imageTitle += '<b> by '+ argItem['ownername']+ '</b><br />';
		}
		
		imageTitle += imageDate + '<br />';
		
		itemLayout += '<div id="photo' + this.photoCount + '" class="photo_thumb_link"><a class="flickr_link" title="'+imageTitle+'"href="/en/experience/bounce.html?item=http%3A%2F%2Fflickr.com%2Fphotos%2F' + argItem['owner'] + '%2F' + argItem['id'] + '" name="&lid=photo' + this.photoCount + '&lpos=jeep_community_flickr" target="_blank"><img id="image' + this.photoCount + '" src="http://farm' + argItem['farm'] + '.static.flickr.com/' + argItem['server'] + '/' + argItem['id'] + '_' + argItem['secret'] + '_s.jpg" /></a></div>';
        
		return itemLayout;
	},
	
	animate : function(argItem) {
	    
	    var theStyleEffectObj1 = new Fx.Styles(argItem, {duration: 350, wait:false, transition:Fx.Transitions.Quad.easeIn, fps:31});
	    theStyleEffectObj1.start({
            //'opacity': [0,.75]
			'opacity': [0,100]
        });
        var theStyleEffectObj2 = new Fx.Styles(argItem, {duration: 350, wait:false, transition:Fx.Transitions.Quad.easeIn, fps:31});
        
        theStyleEffectObj2.start({
            'height': [20,80],
            'width': [20,80],
            'margin-top': [30,1],
            'margin-left': [30,1]
        });
 
	}
	
	
}