$(document).ready(function(){

	truncate = function (str, limit) {
		var bits, i;
		if ('string' !== typeof str) {
			return '';
		}
		bits = str.split('');
		if (bits.length > limit) {
			for (i = bits.length - 1; i > -1; --i) {
				if (i > limit) {
					bits.length = i;
				}
				else if (' ' === bits[i]) {
					bits.length = i;
					break;
				}
			}
			bits.push('...<div style="text-align: right; color: #900; text-decoration: underline; font-weight: normal;">Click for more</div>');
		}
		return bits.join('');
	};

	showFullDesc = function (target) {
		var fullDesc = target.prev('div');
		target.hide();
		fullDesc.show(200);
	}

	showIntro = function (target) {
		var introDesc = target.next('div');
		target.hide(100);
		introDesc.show();
	}

	$('.nav').hover(
		function(){
			var id = $(this).attr('id');
			this.src = 'media/images/menu/'+id+'_on.jpg';
		},
		function(){
			var id = $(this).attr('id');
			this.src = 'media/images/menu/'+id+'_off.jpg';
		}
	);

	$('img#works-map').click(function(){
		var content = '<div><iframe width="450" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=the+works,+93950&aq=&sll=43.394961,-111.921133&sspn=0.363726,0.617294&ie=UTF8&hq=the+works,&hnear=Pacific+Grove,+California+93950&ll=36.627,-121.92&spn=0.015017,0.029268&output=embed"></iframe></div>';
		$(content).dialog({
			title: 'Interactive Map',
			modal: false,
			height: 460,
			width: 485
		});
	});

	$('div.works-book-intro').each(function(){
		var txt = '';
		if ($(this).prev('div').height() > 100) {
			txt = truncate($(this).prev('div').html(), 300);
		}
		else {
			txt = $(this).prev('div').html();
			$(this).css('cursor', 'default');
		}
		$(this).html(txt);
	});

	$('div.works-book-intro').click(function(){
		if ($(this).prev('div').height() > 100) {
			showFullDesc($(this));
		}
	});

	$('div.works-book-full-description').click(function(){
		showIntro($(this));
	});

	$('input#order-submit').click(function(){
		// Validate the fields
		var err = false;
		$('#works-book-order-form input').each(function(){
			if ($(this).val() == ''){
				$(this).css('background', '#F00');
				err = true;
			}
			else {
				$(this).css('background', '#FFF');
			}
		});
		if (err == true) {
			var msg = '<div>Some required fields were left empty.<br/><br/>'+
				'Please fill out the fields in red and submit again.</div>';
			$(msg).dialog({modal:true, width:500, resizable:false})
			return false;
		}

		// Get the values
		var params = {
			fname:		$('#customer-fname').val(),
			lname:		$('#customer-lname').val(),
			phone:		$('#customer-phone').val(),
			email:		$('#customer-email').val(),
			title:		$('#book-title').val(),
			author:		$('#book-author').val(),
			isbn:		$('#book-isbn').val(),
			comments:	$('#book-comments').val()
		};

		// Submit the data
		$.post(
			'process/order',
			params,
			function(data){
				var title, msg;
				if (data.error != null){
					msg = '<div>There was an error while submitting your order. '+
						'Please try again.<br/><br/>If the problem continues, you '+
						'may send your order directly to: '+
						'<a href="mailto:orders123@theworkspg.com">orders123@theworkspg.com</a></div>';
					title = 'Error processing your order';
				}
				else {
					msg = '<div>Thank you for your order! We will contact you to '+
						'confirm and process your order as soon as possible!</div>';
					title = 'Order received';
				}
				$(msg).dialog({title:title, modal:true, width:500, resizable:false});
			},
			'json'
		);
	});
	
	if ($('.food-for-thought-text') != null) {
		Cufon.replace('.food-for-thought-text');
	}

});
