var $j = jQuery.noConflict();

$j(document).ready(function(){
	// Default values for comments
	$j("#commentform #author").defaultValue("Name");
	$j("#commentform #email").defaultValue("Email (Optional)");
	$j("#commentform #url").defaultValue("URL (Optional)");
	$j("#commentform #comment").defaultValue("Comments");
	// Sliders
	$j("#slider").easySlider({
		auto: true, 
		continuous: true
	});
});


$j.fn.defaultValue = function(text){
    return this.each(function(){
		//Make sure we're dealing with text-based form fields
		if(this.type != 'text' && this.type != 'password' && this.type != 'textarea')
			return;
		
		//Store field reference
		var fld_current=this;
		
		//Set value initially if none are specified
        if(this.value=='') {
			this.value=text;
		} else {
			//Other value exists - ignore
			return;
		}
		
		//Remove values on focus
		$j(this).focus(function() {
			if(this.value==text || this.value=='')
				this.value='';
		});
		
		//Place values back on blur
		$j(this).blur(function() {
			if(this.value==text || this.value=='')
				this.value=text;
		});
		
		//Capture parent form submission
		//Remove field values that are still default
		$j(this).parents("form").each(function() {
			//Bind parent form submit
			$j(this).submit(function() {
				if(fld_current.value==text) {
					fld_current.value='';
				}
			});
		});
    });
};







/*
 * 	Easy Slider 1.7 - jQuery plugin
 *	http://cssglobe.com/post/4004/easy-slider-15-the-easiest-jquery-plugin-for-sliding
 */

$j.fn.easySlider = function(options){
	// default configuration properties
	var defaults = {			
		prevId: 'prevBtn',
		prevText: '',
		nextId: 'nextBtn',	
		nextText: '',
		controlsShow: true,
		controlsBefore: '',
		controlsAfter: '',	
		controlsFade: true,
		firstId: 'firstBtn',
		firstText: 'First',
		firstShow: false,
		lastId: 'lastBtn',	
		lastText: 'Last',
		lastShow: false,				
		vertical: false,
		speed: 800,
		auto: false,
		pause: 5000,
		continuous: false, 
		numeric: false,
		numericId: 'controls'
	}; 
	var options = $j.extend(defaults, options);
	this.each(function(){
		var obj = $j(this);		
		var s = $j("li", obj).length;
		var w = $j("li", obj).width();
		var h = $j("li", obj).height();
		var clickable = true;
		obj.width(w);
		obj.height(h);
		obj.css("overflow","hidden");
		var ts = s-1;
		var t = 0;
		$j("ul", obj).css('width',s*w);
		// Continuous
		if(options.continuous)
		{
			$j("ul", obj).prepend($j("ul li:last-child", obj).clone().css("margin-left","-"+ w +"px"));
			$j("ul", obj).append($j("ul li:nth-child(2)", obj).clone());
			$j("ul", obj).css('width',(s+1)*w);
		};				
		// Slide vertically or horizontally?
		if( !options.vertical ) $j("li", obj).css('float','left');
		// Show controls
		if(options.controlsShow)
		{
			var html = options.controlsBefore;				
			// Numeric navigation
			if(options.numeric)
			{
				html += '<ol id="'+ options.numericId +'"></ol>';
			}
			// Next/prev navigation
			else
			{
				if(options.firstShow) html += '<span id="'+ options.firstId +'"><a href=\"javascript:void(0);\">'+ options.firstText +'</a></span>';
				html += ' <span id="'+ options.prevId +'"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></span>';
				html += ' <span id="'+ options.nextId +'"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></span>';
				if(options.lastShow) html += ' <span id="'+ options.lastId +'"><a href=\"javascript:void(0);\">'+ options.lastText +'</a></span>';				
			};
			html += options.controlsAfter;						
			$j(obj).after(html);										
		};
		// Numberic navigation
		if(options.numeric)
		{	
			// Add the numbers
			for(var i=0; i<s; i++)
			{						
				$j(document.createElement("li"))
					.attr('id',options.numericId + (i+1))
					.html('<a rel='+ i +' href=\"javascript:void(0);\">'+ (i+1) +'</a>')
					.appendTo($j("#"+ options.numericId))
					.click(function(){							
						animate($j("a",$j(this)).attr('rel'),true);
					}); 												
			};							
		}
		// Next/prev navigation
		else
		{
			// Next/prev actions
			$j("a","#"+options.nextId).click(function(){		
				animate("next",true);
			});
			$j("a","#"+options.prevId).click(function(){		
				animate("prev",true);				
			});	
			$j("a","#"+options.firstId).click(function(){		
				animate("first",true);
			});				
			$j("a","#"+options.lastId).click(function(){		
				animate("last",true);				
			});				
		};
		// Current view
		function setCurrent(i)
		{
			i = parseInt(i) + 1;
			$j("li", "#" + options.numericId).removeClass("current");
			$j("li#" + options.numericId + i).addClass("current");
		};
		// Adjust
		function adjust(){
			if(t>ts) t=0;		
			if(t<0) t=ts;	
			if(!options.vertical) {
				$j("ul",obj).css("margin-left",(t*w*-1));
			}
			else
			{
				$j("ul",obj).css("margin-left",(t*h*-1));
			}
			clickable = true;
			if(options.numeric) setCurrent(t);
		};
		// Animate the slide
		function animate(dir,clicked){
			if(clickable)
			{
				clickable = false;
				var ot = t;				
				switch(dir){
					case "next":
						t = (ot>=ts) ? (options.continuous ? t+1 : ts) : t+1;						
						break; 
					case "prev":
						t = (t<=0) ? (options.continuous ? t-1 : 0) : t-1;
						break; 
					case "first":
						t = 0;
						break; 
					case "last":
						t = ts;
						break; 
					default:
						t = dir;
						break; 
				};	
				var diff = Math.abs(ot-t);
				var speed = diff*options.speed;						
				if(!options.vertical)
				{
					p = (t*w*-1);
					$j("ul",obj).animate(
						{ marginLeft: p }, 
						{ queue:false, duration:speed, complete:adjust }
					);				
				}
				else
				{
					p = (t*h*-1);
					$j("ul",obj).animate(
						{ marginTop: p }, 
						{ queue:false, duration:speed, complete:adjust }
					);
				};
				if( !options.continuous && options.controlsFade )
				{					
					if(t==ts)
					{
						$j("a","#"+options.nextId).hide();
						$j("a","#"+options.lastId).hide();
					}
					else
					{
						$j("a","#"+options.nextId).show();
						$j("a","#"+options.lastId).show();					
					};
					if(t==0)
					{
						$j("a","#"+options.prevId).hide();
						$j("a","#"+options.firstId).hide();
					}
					else
					{
						$j("a","#"+options.prevId).show();
						$j("a","#"+options.firstId).show();
					};
				};				

				if(clicked) clearTimeout(timeout);
				if(options.auto && dir=="next" && !clicked)
				{
					timeout = setTimeout(function(){
						animate("next",false);
						},
						diff*options.speed+options.pause);
				};

			};
		};
		// init
		var timeout;
		if(options.auto){
			timeout = setTimeout(function(){
				animate("next",false);
			},options.pause);
		};		
		// Add the numbered navigation
		if(options.numeric) setCurrent(0);
		// Don't show the next/prev
		if( !options.continuous && options.controlsFade )
		{					
			$j("a","#"+options.prevId).hide();
			$j("a","#"+options.firstId).hide();				
		};				
	});
};