/*
 * Contentslider - jQuery plugin for sliding elements within a div.
 *
 * Copyright (c) 2008 Adam@bulbouscell
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Revision: $Id: jquery.metadata.js 3620 2007-10-10 20:55:38Z pmclanahan $
 *
 */
(function($){
	$.fn.contentslider = function(options){		
        // build main options before element iteration
		var opts = $.extend({}, $.fn.contentslider.defaults, options);
		var selectLink = function(target){
			$('a[href^='+getLinkPath()+'slideto_]').removeClass(opts.active_link_class);
			$('a[href='+getLinkPath()+'slideto_'+target+']').addClass(opts.active_link_class).blur();
		};
		var getLinkPath = function(){
			if (jQuery.browser.msie){
			return "http://"+location.hostname + "/";
			}else{
				return "";
			}
		};
     	return this.each(function(){
			//set overflow to hidden			
			$(this).css('overflow','hidden');	
			$(this).css('position','relative');			
					
			//wrap the desired div with the container we'll be moving.
			$(this).wrapInner('<div id="contentslider_container" style="position:relative;">').append(' <!-- closes contentslider_container -->');
			//we find the total width of each element inside the div and then float it left.			
			var total_elements_width = 0;			
			$(opts.tag_type+'[id^="slide_"]').each(function(i){
				total_elements_width += $(this).outerWidth();
				$(this).css('float', 'left');
			});
			
			//and now we adjust the wrapper size to fit.
			$('#contentslider_container').css('width',total_elements_width+'px');

			//we check if a hash exists and set the slider to that pos.
			if(location.hash){
				var target = location.hash.slice(1);
				$('#contentslider_container').css('left','-'+$('#slide_'+target).position().left+'px');
				selectLink(target);
			}
			//now when a link is clicked we move it ahead.
			$('a[href^='+getLinkPath()+'slideto_]').click(function(){
				opts.before(); //before callback
				var target = $(this).attr('href').slice(getLinkPath().length+8); //cuts the '/slideto_' off
				if($('#slide_'+target).length != 0){
					$('#contentslider_container').animate({left:'-'+$('#slide_'+target).position().left+'px'},{
						duration:1000, 
						easing: 'easeOutCirc',
						complete: opts.after});
					$.getScript('/js/slate/blank.js');
					selectLink(target);
					location.hash = target;
					return false;
				}
			});			
		});
	};
   // default options - these are used of none others are specified
	$.fn.contentslider.defaults = {
		 tag_type: 'div',
		 after: function(){},
		 before: function(){},
		 active_link_class: 'active',
	     active_link_exception_id: 'top_menu'
	};
})(jQuery);
