// JavaScript Document
/* modded by lance dolan 7/14/2011 to use $13 variable for JQuery version 1.3 */

if (typeof com == "undefined") { 
	com={}; 
}
if (typeof com.digitaria == "undefined") {
	com.digitaria={}; 
}

com.digitaria.ImgCarousel={
	/**
	 * Initialize the ImgCarousel system.
	 * Set up the next and prev buttons to scroll the carousel.
	 * Set up the thumbnail image links to populate the main content.
	 * Bind everything with .live so the carousel's HTML can be loaded via AJAX if needed.
	 * @returns {void}
	 * @see com.digitaria.ImgCarousel.Scroll (calls)
	 * @author Nick Davison
	 */
	Init: function() {
		// On next prev click, scroll forward by the width of the holder
		$13('#img_pop_carousel_next').live('click', function(e) {
			e.preventDefault();
			com.digitaria.ImgCarousel.Scroll(0-parseInt($13('#img_pop_carousel_content_holder').css('width'),10));
		});
		
		// On the prev click, scroll back by the width of the holder
		$13('#img_pop_carousel_back').live('click', function(e) {
			e.preventDefault();
			com.digitaria.ImgCarousel.Scroll(parseInt($13('#img_pop_carousel_content_holder').css('width'), 10));
		});
		
		// Set up the link clicks
		$13('#img_pop_carousel_content_holder a').live('click', function(e) {
			e.preventDefault();
			innerImgs=$13(this).find('img');
			innerImg=$13(innerImgs[0]);
			$13('#img_pop_header h2').html(innerImg.attr('alt'));
			document.getElementById('img_pop_image').src=innerImg.attr('rev');
			$13('#img_pop_download').show();
			com.digitaria.ImgCarousel.SetAddThis(innerImg.attr('alt'), innerImg.attr('rel'));
		});
	},
	
	SetAddThis: function(title, url) {
		// Remove the existing add this if there is one.
		btns=$13('#img_pop_controls a');
		if (btns.length>1) {
			// Remove the old button
			$13(btns[1]).remove();
		}
			
		// Quit out if the url is invalid
		if ( (typeof url=="undefined") || (url==null) || (url=="") ) return;
		
		//strip down the url so there's only alphanumeric cleanness to send			
		url=url.substring(url.lastIndexOf('/')+1);
		url=url.substring(0, url.indexOf('.'));
		
		// Get the url
		var pageUrl=document.location.toString();
		// If there are already search params, clean them off
		if ((typeof document.location.search!="undefined") && (document.location.search!=null) && (document.location.search!="")) {
			pageUrl=pageUrl.substring(0, pageUrl.indexOf(document.location.search));
		}			
		// Generate the new url (current url)?image=(image file to show)
		pageUrl=pageUrl+"?image="+url;
		
		// Rebuild the add this button with the new properties.
		$13('#img_pop_controls').append('<a onclick="return addthis_sendto();" onmouseout="addthis_close();" onmouseover="return addthis_open(this, \'\', \''+pageUrl+'\', \''+title+'\');" href="http://www.addthis.com/bookmark.php?v=20"><img height="16" width="125" border="0" alt="Share" src="http://s7.addthis.com/static/btn/lg-share-en.gif"/></a>');
	},
	
	/**
	 * Scroll the carousel by a given number of pixels.
	 * If it attempts to scroll out of range, stop it from scrolling beyond the max point.
	 * @returns {void}
	 * @see com.digitaria.ImgCarousel.Init (called by)
	 * @see com.digitaria.ImgCarousel.ShowUrl (called by)
	 * @author Nick Davison
	 */
	Scroll: function(amount) {
		offset=$13('#img_pop_carousel_content_pane').position();
		currentPos=parseInt(offset.left);
		newPos=currentPos+amount;
		
		lastImg=$13('#img_pop_carousel_content_pane img:last');
		imgOffset=lastImg.position();
		maxRight=imgOffset.left+lastImg.width()-parseInt($13('#img_pop_carousel_content_holder').css('width'), 10);
		maxRight=-1*maxRight;
		
		if (newPos<maxRight) newPos=maxRight;
		if (newPos>0) newPos=0;
		
		$13('#img_pop_carousel_content_pane').animate({
			'left': newPos
		});
	},
	
	/**
	 * Find a given url within the lis and, assuming it exists, show it and scroll to center on it.
	 * @returns {void}
	 * @see com.digitaria.ImgCarousel.Scroll (calls)
	 * @author Nick Davison
	 */
	ShowUrl: function(url) {
		pos=-1;
		$13('#img_pop_carousel_content_pane img').each(function(k, v){
			if ($13(v).attr('rel').toString().toLowerCase()==url.toString().toLowerCase()) {
				im=$13(v);
				off=im.position();
				halfCarouselWidth=(parseInt($13('#img_pop_carousel_content_holder').css('width'), 10)/2);
				pos=(off.left+im.width()/2)-halfCarouselWidth;
				// Set the title and src for the main image
				$13('#img_pop_header h2').html(im.attr('alt'));
				document.getElementById('img_pop_image').src=im.attr('rev');
				$13('#img_pop_download').show();
				com.digitaria.ImgCarousel.SetAddThis(im.attr('alt'), im.attr('rel'));
			}
		});
		if (pos!=-1) {
			com.digitaria.ImgCarousel.Scroll(0-pos);
		}
	}
}

// Make sure it's initialized.
$13(document).ready(function() {
	com.digitaria.ImgCarousel.Init();
});
