$(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'
	});

	// form stuff
	var options = { 
		target:        'div#contact_peter_response',   // target element(s) to be updated with server response 
		beforeSubmit:  contactFormPreSubmitPeter,  // pre-submit callback 
		//success:       contactFormPostSubmitPeter  // post-submit callback 
	}

	// bind form using 'ajaxForm' 
	$('#contact_peter').ajaxForm(options);

	function contactFormPreSubmitPeter(formData, htmlForm, options) {
		$('div#contact_peter_response').html('<p>Sending...</p>');
		$('div#contact_peter_response').stop().show();
	}

	function contactFormPostSubmitPeter(formData, htmlForm, options) {
		$('div#contact_peter_response').wait(10000).fadeOut('slow');
	}

	var options = { 
		target:        'div#contact_kelsey_response',   // target element(s) to be updated with server response 
		beforeSubmit:  contactFormPreSubmitKelsey,  // pre-submit callback 
		//success:       contactFormPostSubmitKelsey  // post-submit callback 
	}
	
	// bind form using 'ajaxForm' 
	$('#contact_kelsey').ajaxForm(options);


	function contactFormPreSubmitKelsey(formData, htmlForm, options) {
		$('div#contact_kelsey_response').html('<p>Sending...</p>');
		$('div#contact_kelsey_response').stop().show();
	}
	
	function contactFormPostSubmitKelsey(formData, htmlForm, options) {
		$('div#contact_kelsey_response').wait(10000).fadeOut('slow');
	}



}); // end jquery

