$('html').addClass('js');



//------ Main slideshow

$(function(){
	// Declare variables
	var slideshow = $('#slideshow'), 
	container = slideshow.find('div.slides'),
	ul = container.find('ul'),
	li = container.find('li'),
	curr = ul.find('li.selected'),
	controls = slideshow.find('div.clients'),
	pointer = $('#ss-pointer'),
	w = 700, cw = 162, intval = 0, pos = 0, tw, m, num;
	
	
	$(window).unload(function(){
		container.scrollLeft(0);
	});
	
	// Slides
	m = li.length;
	tw = m*w;
	ul.width(tw);
	
	// Start by default
	function stopIntval(){
		clearInterval(intval);
		intval = 0;
	}
	function startIntval(){
		if(intval === 0){
			intval = window.setInterval(slideProcess, 6000);
		}
	}
		
	startIntval();
	
	// Process to do checks. don't break.
	function slideProcess(){
		if(pos < tw){
			doSlide();
		}
	}
	pos=container.scrollLeft();
	
	// num is passed directly to doSlide if there
	// is a paginated slideshow
	function doSlide(num){
		if(!num && num !== 0){
			if(pos == tw-w){
				container.stop(true, false).animate({
					scrollLeft: 0
				}, function(){
					pos=container.scrollLeft(); // update position
				});
				pointer.stop(true, false).animate({
					left: 68
				});
			}else{		
				container.stop(true, false).animate({
					scrollLeft: container.scrollLeft()+w
				}, function(){
					pos=container.scrollLeft(); // update position
				});
				pointer.stop(true, false).animate({
					left: "+=" + (cw+10)
				});
			}
		}else{
			stopIntval();
			container.stop(true, false).animate({
				scrollLeft: (w*num)
			}, function(){
				pos=container.scrollLeft(); // update position 
				startIntval();
			});
			if(num === 0){
				pointer.stop(true, false).animate({
					left: 68
				});
			} else {
				pointer.stop(true, false).animate({
					left: num+1*((cw/2)-13)+(num*(cw+10))
				});
			}
			
		}
		
		
	}

	// Switching based on pagination
	controls.find('li a').bind('click', function(){
		var context = $(this),
		num = parseInt(context.attr('rel'), 10);
		if(!context.parent().hasClass('selected')){
			context.closest('ul').find('li.selected').removeClass('selected');
			context.parent().addClass('selected');
			// No need to go through slide process, we know this won't break
			// ... In theory.
			doSlide(num);
		}
		return false;
	}).bind('mouseover', function(){stopIntval();}).bind('mouseout', function(){startIntval();});
	
	
	
});

//------ Tiny Labels
$(function(){
	var label = $('form label.tiny');
	label.each(function(){
		var lbl=$(this), inp=lbl.next();
		!lbl.hasClass('static') ? lbl.css('display', 'none') : null;
		inp.bind('focus', function(){
			var txt=$(this);
			txt.val() === lbl.text() ? txt.val('') : null;
		});
		inp.bind('blur', function(){
			var txt=$(this); 
			txt.val() === lbl.text() || txt.val() === '' ? txt.val(lbl.text()) : null;
		});
		inp.val() === lbl.text() || inp.val() === '' ? inp.val(lbl.text()) : null;
	});
});








