


function getImage(parent) {
  var el = parent.firstChild;
  
  while (el) {  // walk through till as long as there's an element
    if (el.nodeName.toUpperCase() == "IMG") { // found an image
      // flickr uses "_s" suffix for small, and "_m" for big images respectively
      return el.src.replace(/_s\.jpg$/, "_m.jpg");
    }
    el = el.nextSibling;
  }
  
  return "";
}

var View = {
	images: 0,
	title: 0,
	ajMech: 0,
	overlay: 0,
	selectedIndex: 0,
	totalImages: 0,
	mangledTitle: 0,
	
	carousel: 0,
	
	init_complete: 0,
	
	init: function(title,initial_imgNum) {
		this.mangledTitle = title;
		
		gallery.root = GalleryRoot;
		this.thumb_root = GalleryRoot + gallery.hid + "/";

		getElement("gallery_title_anchor").href = "/Photos/" + title;
		getElement("gallery_caption").innerHTML = gallery.detail;

		//this.carousel = new YAHOO.widget.Carousel("car_imagelist");
		//this.carousel.CONFIG.MAX_PAGER_BUTTONS = 20;
		/*
		var s = "";
		for(i=0;i<gallery.total;i++) {
			var photo = gallery.images[i];
			var thumb = photo.img.replace(/\.jpg/,"_thumb_100x100.jpg");
			var img = FilledElement("img",this.thumb_root + thumb);
			img.border = "0";
			img.id = "thumb_" + i;
			img.alt = photo.caption;
			img.title = photo.caption;
			img.src = this.thumb_root + thumb;
			this.carousel.addItem(FilledElement("li",img));
		}
		
		
		this.carousel.on("itemSelected", function (index) {
			View.set_photo(index,true);
		});
		
		this.carousel.render(); // get ready for rendering the widget
		this.carousel.show();   // display the widget
		this.carousel.set("numVisible",5);
		this.carousel.CONFIG.MAX_PAGER_BUTTONS = 20;
		var lastone = gallery.length - 1;
		this.carousel.scrollTo(lastone,true);
		*/
		
		
		gallery.images.sort(
			function(a,b) {
				if(a.index < b.index) {
					return -1;
				}
				if(a.index > b.index) {
					return 1;
				}
				return 0;
			}
		);
		
		this.selectedIndex = initial_imgNum;

		this.set_photo(this.selectedIndex);
		
		this.init_complete = 1;
		
	},
	
	
	lastSelectedIndex: 0,
	
	resizeURI: 0,
	
	set_photo: function(i,fromCarousel) {
		if(this.animIval_ID) { return; }
		if(i >= gallery.total || i < 0) { return; }
		if(this.selectedIndex != i) {
			this.selectedIndex = i;
		}

		var photoSrc = gallery.root + gallery.hid + "/" + gallery.images[i].img;
		
		getElement("thePhoto").src = photoSrc;
		
		getElement("thePhoto").alt = gallery.images[i].cap;
		getElement("thePhoto").title = gallery.images[i].cap;
		getElement("photo_caption").innerHTML = gallery.images[i].cap.replace(/\\/g,"");
		if (gallery.images[i].byline) {
			getElement("photo_byline").innerHTML = "Photo credit: " + gallery.images[i].byline;
		} else {
			getElement("photo_byline").innerHTML = "";
		}

		
		
		this.build_nav(i,(i+1));
		
		var dir = 1;
		if(this.lastSelectedIndex > this.selectedIndex) {
			dir = -1;
		}
		
		this.lastSelectedIndex = this.selectedIndex;
		
		var pd = {
			"title":	photoSrc,
			"channel":	"wube",
			"prop12":	"Photo Gallery - " + gallery.title
		};
		renewOmni(pd);
		
		if(!fromCarousel) {
			//View.carousel.scrollTo(i,true);
		}
		
		View.reGAD();
		
		
	},
	
	

	
	build_nav: function(firstIndex,lastIndex) {
		

		var lastPossibleStarter = gallery.total - this.thumb_count;
		var nextStarter = (lastIndex >= lastPossibleStarter) ? lastPossibleStarter : lastIndex;
		var prevStarter = firstIndex - 1;
		if(prevStarter < 0) {
			prevStarter = 0;
		}

		
		var label;
		var first;
		var prev;
		var next;
		var last;
		
		label = FilledElement("span","Photo " + (firstIndex + 1) + " of " + (gallery.total));
		
		if (firstIndex != 0) {
			first = FilledElement("a", "First");
			first.href = "javascript:View.set_photo(0)";
		} else {
			first = FilledElement("span","First","greyed");
		}
		
		if (firstIndex != 0) {
			prev = FilledElement("a", "Prev");
			prev.href = "javascript:View.set_photo(" + prevStarter + ")";
		} else {
			prev = FilledElement("span","Prev","greyed");
		}
		
		if (this.selectedIndex < gallery.total - 1) {
			next = FilledElement("a", "Next");
			next.href = "javascript:View.set_photo(" + nextStarter + ")";
		} else {
			next = FilledElement("span","Next","greyed");
		}
		
		
		if (this.selectedIndex < gallery.total - 1) {
			last = FilledElement("a", "Last");
			last.href = "javascript:View.set_photo(" + (gallery.total - 1) + ")";
		} else {
			last = FilledElement("span","Last","greyed");
		}
		
		
		var nav = FilledElement("ul",
			[
				FilledElement("li",label),
				FilledElement("li",first),
				FilledElement("li",prev),
				FilledElement("li",next),
				FilledElement("li",last,"right")
			]
		);
		getElement("photo_countNav").innerHTML = "";
		getElement("photo_countNav").appendChild(nav);
		getElement("photo_countNav_b").innerHTML = getElement("photo_countNav").innerHTML;
	},
	
	
	GADdivs: [
		"gads_mrec","gads_leaderboard","gads_mrec_bottom","gads_foursquare"
	],
	reGAD: function(){
		if(!this.init_complete) { return; }
		for (var i = 0; i < this.GADdivs.length; i++) {
			var d = document.getElementById(this.GADdivs[i]);
			d = d.contentWindow;
			d.location = d.location;
		}
	}
	
}





