var expanderClick = function() {
	if($(this).html() == "Show more...") {
		$(this).html('Show less...').addClass('expanded');
	}
	else {
		$(this).html('Show more...').removeClass('expanded');
	}
	$(this).parent().children('div.expander').slideToggle();
	return false;
}
$(document).ready(function() {
	$('a[@title]').Tooltip({
		track: true,
		delay: 0,
		showURL: false
	});

	// Portfolio
	$('#pagePortfolio ul.portfolio li:odd').addClass('odd');
	if(!this_is_ie6) {
		$('#pagePortfolio ul.portfolio a.thumb').hover(function() {
			$(this).fadeTo('slow', 1);
		},function() {
			$(this).fadeTo('slow', .4);
		});
	}	
	if(!this_is_ie6) {
		$('#pagePortfolio ul.portfolio a.thumb').fadeTo('fast', .4);
	} else {
		$('#pagePortfolio ul.portfolio a.thumb').css('opacity','.5');
	}
	$('#pagePortfolio ul.portfolio a.thumb').click(function() {
		var site = $(this).attr('id');
		pageTracker._trackPageview('/portfolio/'+site+'.htm');
		$('#pagePortfolio ul.portfolio').slideUp('medium');
		$('#pagePortfolio ul.portfolio').after('<a href="#" id="showPortfolio">&laquo; Return to listing</a>');
		$('#showPortfolio').click(function() {
			$(this).remove();
			$('#pagePortfolio ul.portfolio').slideDown('medium');
			$('#pagePortfolio div.portfolio').remove();
			return false;
		});
		$('#pagePortfolio').append('<div class="portfolio" id="full_'+site+'"></div>');
		$('#pagePortfolio #full_'+site).addClass('loading').load('/portfolio.php', {
			site: site
		}, function() {
			$(this).removeClass('loading');
			$('#pagePortfolio #full_'+site+' a.mid img').clone().appendTo('#full_'+site+' a.mid').hide().addClass('second').attr('src','/media/portfolio/'+site+'_alt.png');
			$('#pagePortfolio #full_'+site+' a.mid img:first-child').addClass('first');
			$('#pagePortfolio #full_'+site+' a.mid').bind("click", function(){
				return !window.open(this.href);
			});
			if(!this_is_ie6) {
				$('#pagePortfolio #full_'+site+' a.mid').hover(function() {
					$(this).children('img.first').fadeOut('slow');
					$(this).children('img.second').fadeIn('slow');
				},function() {
					$(this).children('img.second').fadeOut('slow');
					$(this).children('img.first').fadeIn('slow');
				});
			}
			$('#pagePortfolio div.portfolio a[@title]').Tooltip({
				track: true,
				delay: 0,
				showURL: false
			});
		});
		if( $(this).parent().attr('class')=="odd") {
			$('#full_'+site).addClass('alternate');
		}
		return false;
	});

	// Random
	$('#pageRandom div.expander').removeClass('noscript').before('<a class="expander" href="#">Show more...</a>');
	$('#pageRandom div.container a.expander').removeClass('expanded').html('Show more...').bind('click', expanderClick);
	$('#pageRandom div.corner form').submit(function() {
		$.post("/corner/db-articles.php",{
			keywords: $('#keywords').val(),
			sql_search: '1'
			},function(data) {
				$('#cornerArticleList').empty().html(data);
				return false;
			});
		return false;
	}).append('<a href="#" class="button">Search</a>');
	$('#pageRandom div.corner form a.button').click(function() {
		$('#pageRandom div.corner form').trigger('submit');
		return false;
	});
  	$('#pageRandom div.quotes').prepend('<ul class="quotes"></ul>');
	getQuotes();

	// Contact
	$('#pageContact a.button').click(function() {
		$(this).removeClass('hover');
		$('#contactForm').submit();
		return false;
	});
	$('#last_name , #first_name').focus(function() {
		$(this).val('').removeClass('empty');
	});
	$('#mediumSelect , #intentionSelect').change(function() {
		var id = $(this).attr('id').replace('Select','');
		if($(this).children('option:selected').val()=="other") {
			$('#'+id+'Other').show();
		}
		else {
			$('#'+id+'Other').hide();
		}
		$('#'+id).val($(this).children('option:selected').val());
	});
	$('#mediumOther , #intentionOther').change(function() {
		var id = $(this).attr('id').replace('Other','');
		$('#'+id).val($(this).val());
	});
	$('#contactForm').submit(function() {
		$('#contactForm .message').remove();
		$('#contactForm').prepend('<p class="message"></p>');
		$('#contactForm p.message').hide();
		error = "";
		if($('#contactForm #message').val() == "") {
			error = "Please enter a message";
		}
		if($('#contactForm #email').val() == "") {
			error = "Please enter your e-mail address";
		}
		if($('#contactForm #last_name').val() == "" || $('#contactForm #last_name').val() == "Last") {
			error = "Please enter your last name";
		}
		if($('#contactForm #first_name').val() == "" || $('#contactForm #first_name').val() == "First") {
			error = "Please enter your first name";
		}
		if(error != "") {
			$('#contactForm .message').addClass('error').html(error).slideDown('fast');
			return false;
		}
		else {
			$.post("/send.php", {
				first_name: $('#first_name').val(),
				last_name: $('#last_name').val(),
				email: $('#email').val(),
				screenName: $('#screenName').val(),
				medium: $('#medium').val(),
				intention: $('#intention').val(),
				message: $('#message').val()
			},function(data) {
				$('#contactForm p.error').slideUp();
				$('#contactForm .message').empty().html(data).slideDown();
				return false;
			});
		}
		return false;
	});

	// IE6 Bar
	if(this_is_ie6) {
		var message = '<span></span>'+
					  '<p>You are using antiquated software. It is strongly recommended that you immediately <strong>pimp your browser!</strong></p>'+
					  '<a href="#" class="close">Close</a>';
		$('body').append('<div id="ie6_notice_bar" />');
		$('#ie6_notice_bar').hide().html(message).hover(function() {
			$(this).addClass('hover');
		},function() {
			$(this).removeClass('hover');
		}).click(function() {
			window.open('http://www.browserpimp.com');
		})
		$('#ie6_notice_bar a.close').click(function() {
			$(this).parent().remove();
		});
		var t1=setTimeout("$('#ie6_notice_bar').slideDown()", 1500);
		var t2=setTimeout("$('#ie6_notice_bar').slideUp()", 15000);
	}
	
	// Hide things
	$('.jsHide').hide();
	
	// External links
	$("a[@href^=http]").not("[@href*='quiller.org']").bind("click", function(){
		return !window.open(this.href);
	});
});
