$(function() {
	
	// Form field text replacement
	function switchText() {
		if($(this).val() == $(this).attr('title')) {
			$(this).val('');
		}
		else if($.trim($(this).val()) == '') {
			$(this).val($(this).attr('title'));
		}
	}
	
	$('.field input, .field textarea, #manage-wrap .field input, #manage-wrap .field textarea').each(function() {
		if($.trim($(this).val()) == '') {
			$(this).val($(this).attr('title'));
		}
	}).focus(switchText).blur(switchText);

	$.fn.wait = function(time, type) {
		time = time || 2000;
		type = type || "fx";
		return this.queue(type, function() {
			var self = this;
			setTimeout(function() {
				$(self).dequeue();
			}, time);
		});
	};
  
	// Fade the Login Bar
	if($('#loginBar').hasClass('fadeOut')) {
		$("#loginBar").wait().fadeOut('slow');      
	}
	
	function mycarousel_initCallback(carousel) {
		$('.buttonContainer').html('<a id="mycarousel-prev" href="#">&#8592;</a><a id="mycarousel-next" href="#">&#8594;</a>');
		$('a#mycarousel-next').bind('click', function() {
			carousel.next();
			return false;
		});

		$('a#mycarousel-prev').bind('click', function() {
			carousel.prev();
			return false;
		});

		$('#portfolioBox a#description').live('click', function() {
			if ($(this).hasClass('active')) {
				return false;
	  	}
			showDescription();
			return false;
		});
	};

	$('#portfolioBox a#thumbnails').live('click', function() {
		if ($(this).hasClass('active')) {
			return false;
		}
		$('#tabContainer a').removeClass('active');
		$('#portfolioBox a#thumbnails').addClass('active');
		
		$('embed#vimeo_player').each(function() {
			video_id = $(this).attr('src');
		});
		//console.log($('embed#vimeo_player'), video_id, video_id.indexOf('&'));
		video_id = video_id.substr(39, video_id.indexOf('&') - 39);
		portfolioLoader();
		$('#portfolioBox .boxContent').load('video_list.php?video_id=' + video_id, function() {
			$('#mycarousel').jcarousel({
				initCallback: mycarousel_initCallback,
				// This tells jCarousel NOT to autobuild prev/next buttons
				buttonNextHTML: null,
				buttonPrevHTML: null, 
				visible: 4,
				wrap: 'last'
			});
		});
		return false;
	});
	
	function showDescription() {
		$('#tabContainer a').removeClass('active');
		$('#portfolioBox a#description').addClass('active');
		video_id = $('ul#mycarousel li a.active').attr('id').substr(10);
		$('.buttonContainer a').unbind('click');
		$('.buttonContainer').html('');
		$(window).unbind('resize');
		portfolioLoader();
		$('#portfolioBox .boxContent').load('video_info.php', 'video_id=' + video_id);
		return false;
	}
	
	function portfolioLoader() {
		$('#portfolioBox .boxContent').html('<br /><br /><div style="text-align:center"><br /><br /><img src="images/loadingAnimation.gif" width="208" height="13" /><br /><br /></div>');
	}
   	
	$('#mycarousel').jcarousel({
		initCallback: mycarousel_initCallback,
		// This tells jCarousel NOT to autobuild prev/next buttons
		buttonNextHTML: null,
		buttonPrevHTML: null, 
		visible: 4,
		wrap: 'last'
	});

}); // end jquery
