$(document).ready(function() {
	if ($('ul#other_panels').length > 0) panel_rollovers();
	if ($('.thumbnails').length > 0) panel_content();
	if ($('a.tooltip').length > 0) tooltips();
	if ($('#news').length > 0) news_slideshow();
	if ($('#map_form').length > 0) map_form_validation();
	
	client_check();
});

function map_form_validation() {
	$('#form_submit').click(function() {
		// dark #950210
		// light #D59A9F
		var name = $('#your_name').val();
		var email = $('#your_email').val();
		var title = $('#your_title').val();
		var url = $('#your_url').val();
		var description = $('#your_description').val();
		var exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var error = 0;
		
		if (email == '' || email == ' ' || !exp.test(email)) { input_background('#your_email'); error++; }
		if (name == '' || name == ' ') { input_background('#your_name'); error++; }
		if (title == '' || title == ' ') { input_background('#your_title'); error++; }
		if (url == '' || url == ' ') { input_background('#your_url'); error++; }
		if (description == '' || description == ' ') { input_background('#your_description'); error++; }
		
		reset_background('#your_name');
		reset_background('#your_email');
		reset_background('#your_url');
		reset_background('#your_title');
		reset_background('#your_description');
		
		if (error > 0) {
			return false;
		} else {
			return true;
		}
	});
}

function reset_background(id) {
	if (id == '#your_description') {
		var image = 'background-gradient-form.jpg';
	} else {
		var image = 'background-gradient-input.jpg';
	}
	
	$(id).focus(function() {
		if ($(this).val() != '') {
			$(this).css('background','#FFFFFF url(/transformation/lib/img/'+image+') repeat-x bottom left');
		}
	});
	$(id).blur(function() {
		if ($(this).val() != '') {
			$(this).css('background','#FFFFFF url(/transformation/lib/img/'+image+') repeat-x bottom left');
		}
	});
}

function input_background(id) {
	$(id).css('background-image','none');
	$(id).animate({
		backgroundColor:'#D59A9F'
	},{
		duration:400,
		easing:'swing',
		complete:function() {
			$(this).animate({backgroundColor:'#ecd1d4'},{duration:800,easing:'swing'});
		}
	});
}


function client_check() {
	// we are disabling typekit for safari users on windows and firefox 3.0 users
	if (($.client.os == 'Windows' && $.client.browser == 'Firefox') || ($.client.browser == 'Safari') || ($.client.browser == 'Chrome') || ($.client.browser == 'Firefox' && $.client.version == '3.0')) {
		$('.tk-pill-gothic-300mg').each(function() {
			$(this).removeClass('tk-pill-gothic-300mg');
		});
		$('head').append('<link rel="stylesheet" type="text/css" media="all" href="/transformation/lib/css/no-typekit.css" />');
	}
}

function tooltips() {
	$('a.tooltip').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		fade: 250
	});
}

function panel_rollovers() {
	$('ul#other_panels li').each(function() {
		$(this).mouseover(function() { $(this).css('background-color','#E5D3B4'); });
		$(this).mouseout(function() { $(this).css('background-color','#EEE3D0'); });
	});
	$('ul#other_panels li.past').each(function() {
		$(this).mouseover(function() { $(this).css('background-color','#F8F4EC'); });
		$(this).mouseout(function() { $(this).css('background-color','#FCF9F5'); });
	});
}

function panel_content() {
	$('.thumbnails a').each(function() {
		$(this).click(function() {
			var index =  $('.thumbnails a').index(this);

			// find corresponding index of tables, turn all others off, turn corresponding on
			$('div.thumbnail_content').attr('class','thumbnail_content hide');
			$('div.thumbnail_content').eq(index).attr('class','thumbnail_content show');

			// turn all tabs off, turn me on
			$('.thumbnails a').attr('class','');
			$(this).attr('class','active');

			return false;

		});
	});
}

function news_slideshow() {	
	$('.news_item').each(function() {
		$(this).fadeOut(0);
		if ($(this).hasClass('active')) $(this).fadeIn(0);
	});
	
	// start slideshow
	var my_int = window.setInterval('auto_news_slideshow()',5000);
}

function fade(index) {
	$('.news_item').attr('class','news_item');
	$('.news_item').eq(index).fadeIn(250);
	$('.news_item').eq(index).attr('class','news_item active')
}

function auto_news_slideshow(timeint) {
	var total = ($('.news_item').length)-1;

	$('#news div.active').each(function() {
		$(this).fadeOut(250);
		var index = $('.news_item').index(this);
		if (index >= total) {
			fade(0);
		} else {
			fade(index+1);
		}
	});
}