// Davenport Design & Advertising
// www.davenportdesign.net

// All Rights Reserved. Unauthorized copying prohibited.

// core.js

$(document).ready(function(){

	// homepage animation
	$('#rotation').innerfade({
		speed: 1000,
		timeout: 5000,
		type: 'sequence',
		containerheight: '350px'
	});
	
	
	// showcare/project thumb click and change preview
	// get the containers
	var preview = $('#preview div');
	var preview_caption = $('#preview #caption');
	var thumbs = $('#thumbs ul li a');
	
	// select and the first thumb, add border, and put it in the preview box
	var first_thumb = thumbs.eq(0);
	var first_caption = first_thumb.children(".caption").html();
	first_thumb.addClass('current');
	preview.css('background-image', 'url(' + first_thumb.attr("title") + ')');
	preview_caption.html(first_caption);

	// get real width of thumb box just incase ie7 
	thumb_width = 0;
	thumb_width = parseInt(first_thumb.css('width'), 10);
	thumb_width += parseInt(first_thumb.css('padding-left'), 10) + parseInt(first_thumb.css('padding-right'), 10);
	thumb_width += parseInt(first_thumb.css('margin-left'), 10) + parseInt(first_thumb.css('margin-right'), 10);
	thumb_width += parseInt(first_thumb.css('borderLeftWidth'), 10) + parseInt(first_thumb.css('borderRightWidth'), 10);
	thumb_width = ((thumb_width * thumbs.length) + 10);
	$('#thumbs ul').css('width', thumb_width);

	// if the thumbnail is clicked...
	thumbs.click( function() {
		// change the thumb border
		thumbs.removeClass('current');
		$(this).addClass('current');
		// get the caption div inside the thumb
		c = $(this).children(".caption").html();
		// change the preview bg and update the preview caption
		preview_caption.html(c);
		preview.css('background-image', 'url(' + $(this).attr("title") + ')');
	});
	
	
	
	
	// clear and restore input values
	var inputs = $("[class^='input']");
	jQuery.each(inputs, function() { $(this).attr('title', $(this).val()); });
	inputs.focus( function() {
		if ($(this).get(0).tagName == 'SELECT') return;
		if ($(this).attr('title') == $(this).val()) $(this).val('');
	});
	inputs.blur(function() {
		if ($(this).get(0).tagName == 'SELECT') return;
		if ($(this).val() == '' || $(this).val() == ' ') $(this).val($(this).attr('title'));
	});

});

// showcare/project thumb slider
window.onload = function () {
	var container = $('#thumbs');
	var ul = $('ul', container);

	var itemsWidth = thumb_width - container.outerWidth();
	$('#controller #bar', container).slider({
		min: 0,
		max: itemsWidth,
		animate: true,
		handle: '#handle',
		stop: function (event, ui) {
			ul.animate({'left' : ui.value * -1}, 500);
		},
		slide: function (event, ui) {
			ul.css('left', ui.value * -1);
		}
	});
	
	$('#moveright').mousedown(function() {
		pos = parseInt(ul.css('left'));
		if (Math.abs(pos) < itemsWidth) ul.animate({'left': (pos - 133) + 'px'}, 200);
		return;
	});
	
	$('#moveleft').mousedown(function() {
		pos = parseInt(ul.css('left'));
		if (pos < 0) ul.animate({'left': (pos + 133) + 'px'}, 200);
		return;
	});

};
