/**
 *	fires when the dom is ready
 *
 */
$(document).ready(function() {		
	
	tabs();
	cufonReplace();	
	infieldHandler();
	dropdownMenu();	
	$('.twitter').initTwitterBlock();
	
});

/**
 * tab functions
 *
 */
function tabs() {	
	
	$("ul.tabs").tabs("> .tab_content", {  
        rotate: true 
    }).slideshow({
		autoplay: true,
		interval: 4000
		}
	); 
	
};	

/** 
 * "target_blank" links
 *
 */
function targets() {
	$(".flickr_badge_image a").attr("target", "_blank");
}

/**
 *	replace targeted fonts using cufon
 *
 */
function cufonReplace() {
	Cufon.replace('#header li a', {
		fontFamily: 'GothamBold',
		hover: true		
	});
}	

/**
 *	dropdown Menu
 *
 */
function dropdownMenu() {

	$("#header ul.subnav").parent().append("<span></span>");
	$("#header ul.navigation li a").mouseover(function() { 

		$(this).parent().find("ul.subnav").slideDown('fast').show();
		$(this).parent().hover(function() {
		}, function(){	
			$(this).parent().find("ul.subnav").slideUp('medium'); 
		});
		
		}).hover(function() { 
			$(this).addClass("subhover");
		}, function(){
			$(this).removeClass("subhover");
	});
}

/**
 *	Allows for input and textarea elements to have infield labels
 *
 */
function infieldHandler() {

	if ( !$('#searchform').length )
		return false;
	
	$('#searchform .infield').each( function(index, e) {

					
		// don't show value if something is filled in
		if ( $(e).val() != '' ) {
			getInfieldTarget(this).css('opacity', 0);
		}
		
		// what happens on focus
		$(e).focus( function() {
			getInfieldTarget(this).addClass('infield_focus');
		// what happens on blur
		}).blur( function() {
			// restore if empty
			if ( $(this).val() == '') {
				getInfieldTarget(this).animate({
					opacity: 1
				}, 450);
			}
			
			getInfieldTarget(this).removeClass('infield_focus');
		});
		
		// check value on keypress
		$(e).keypress( function() {
			if ( $(this).value != '')
				getInfieldTarget(this).css('opacity', 0);
		});
		
		// ie fix for transparency
		$(e).prev('.infield_label').click( function() {
			$(this).closest('p').find('.infield').focus();
		});
	});
}

function getInfieldTarget(e) {
	// common parent of each form elements
	var parentTag = 'p';
	
	return $(e).closest(parentTag).find('.infield_label span');
}
	
/**
 *	Get twitter feeds
 *
 */
$.fn.initTwitterBlock = function() {

	var hashtag = $('span.twitterhash',this).text();
	var username = $('span.twitteruser',this).text();
	$(this).empty();	
	
	if ( (hashtag!='') && (username=='') ) {
		$(this).liveTwitter(hashtag, { limit: 3, rate: 5000 });
	}	
	if (username!='') {
		$(this).liveTwitter(username, {limit: 3, rate: 5000, mode: 'user_timeline', showAuthor: true});
	}
};

	

	
	
	
	
	
