initFollowPrompts();

/**
 * jQuery.HTML5 placeholder - Placeholder plugin for input fields
 * Written by Ludo Helder (ludo DOT helder AT gmail DOT com)
 * Licensed under the WTFPL (http://sam.zoy.org/wtfpl/).
 * Date: 2010/11/18
 *
 * @author Ludo Helder
 * @version 1.0.1
 *
 **/
$(function(){
  var d = "placeholder" in document.createElement("input");
  if (!d){ 
	  $("input[placeholder]").each(function(){
		  $(this).val(element.attr("placeholder")).addClass('placeholder');
	  }).bind('focus',function(){
		  if ($(this).val() == element.attr('placeholder')){
			  $(this).val('').removeClass('placeholder');
		  }
	  }).bind('blur',function(){
		  if ($(this).val() == ''){
			  $(this).val(element.attr("placeholder")).addClass('placeholder');
		  }
	  });
  }
});

/* Drop-down menus on hover */
$('#nav li').hover(
	function() { $(this).find('ul.tabs').show(); },
	function() { $(this).find('ul.tabs').hide(); }
);

/*
 * jQuery extensions
 */

// Shows the current element(s) and hides the element(s) passed or specified by selector.
jQuery.fn.showAndHide = function(elemToHide) {
	$(elemToHide).hide();
	return this.show();
}

// Returns model id parsed from element id w/ format "prefix-<id>"
jQuery.fn.modelId = function() {
	var id = $(this).attr('id');
	if (id == null) return null; // Just in case; browsers tested return empty string for missing id.

	var idParts = id.split(/[-_]/g); // Split on hyphens and underscores
	return (idParts.length > 1) ? idParts[idParts.length-1] : null;
}

/*
 * Custom libs
 */

Notify = {
	success: function(message) {
		Notify.notify('success', message);
	}, 
	error: function(message) {
		Notify.notify('error', message);
	},
	clear: function() {
		$('.notice').hide()
	},
	notify: function(typeOfNoticeClass, message) {
		$('.notice').hide();
		$('#ajax-message').text(message);
		$('.notice').
			removeClass().
			addClass('notice').
			addClass(typeOfNoticeClass).
			show();
	}
}

$.fn.clank = function(message) {
	if (message == null) return;
	
	var $form = $(this).closest('form');
	$form.find('.errorExplanation').remove();
	$form.find('fieldset').removeClass('fieldWithErrors');
	
	message = '<div class="errorExplanation" id="errorExplanation"><h2>Clank!</h2><ul><li>' + message + '</li></ul></div>'
	$form.prepend(message);
	$form.find('fieldset').addClass('fieldWithErrors');
}

ShowAndHideControl = function(options) {
	var elemToShowAndHide = $(options['target']);
	var showTriggers = $(options['showControl']);
	var hideTriggers = $(options['hideControl']);
	
	showTriggers.
		click(function() {
			elemToShowAndHide.showAndHide(showTriggers);
			return false;
		});

	hideTriggers.
		click(function() {
			elemToShowAndHide.hide();
			showTriggers.show();
			return false;
		});
}

/*
 * Converts a number to its value in thousands (K), e.g. 12,000 => 12
 */
function toK(num) {
	return num / 1000;
}

function formatK(num) {
	return toK(num) + 'K';
}

(function($){
	$.fn.fadeable = function(options){

		var settings = {
			target: this,
			duration: 'fast'
		};
		$.extend(settings, options)

		$(this).live('hover', function(event) {
			switch(event.type){
				case 'mouseenter':
					$(this).find(settings.target).stop().fadeTo(settings.duration, 1);
					break;
				case 'mouseleave':
					$(this).find(settings.target).stop().fadeTo(settings.duration, 0);
					break;
			}
		});
	}
})(jQuery);

// show screenshot info on hover
$('ol.dribbbles li div.dribbble-img').fadeable({target: 'a.dribbble-over'});

// show prev/next arrows on hover
$('ol.prevnext li a').fadeable({target: 'strong'});

// show zoom icon on thumbnail hover
$('ol.activity li div.act-shot').fadeable({target: 'strong'});
$('div.the-rebound div.dribbble-img').fadeable({target: 'a.dribbble-over'});

// show zoom icon on multi thumb hover
$('ol.multi-grid li a').fadeable({target: 'strong'});

function attachPlayerTooltipsToGroupShots() {
	$('ol.multi-grid li a.zoom').each(function() {
		var link = $(this);
		link.tipsy({
			gravity: 'n',
			html: true,
			title: function() { return link.closest('li').find('div.tipsy-player').html(); }
		});
	});
}

function showOverlay(selector) {
	var $overlay = $(selector);
	$overlay.css('height', document.body.clientHeight+'px');
	$overlay.find('.lightbox').css('top', $(window).scrollTop() + 60 + 'px');
	$(selector).fadeIn();
	return false;
}

$('div.overlay').each(function() {
	var $overlay = $(this);
	$overlay.find('a.close').live('click', function() {
		$overlay.hide();
		return false;
	});
});

// show/hide pixel useage help
$('#pixels-help-a').click(function() {
	$('#pixels-help').slideToggle("normal");
	return false;
})

// show/hide search in adaptive small screen mode
$('#t-search').click(function() {
	$('#dashboard').slideToggle("normal");
	return false;
})

$('[rel=tipsy]').tipsy({fade: true, gravity: 's'});

/*
 * Attaches to behavior to any follow/unfollow link on the page.
 */
function initFollowPrompts() {
	$('.follow-prompt a').live('click', function() {
		var $link = $(this);
		var followPrompt = $link.closest('.follow-prompt');
		var originalHtml = followPrompt.html();
		
		$link.closest('.follow-prompt form').ajaxSubmit({
			beforeSend: function() {
				$link.addClass('processing');
				$link.find('span').text('Wait...');
			},
			success: function(responseHtml) {
				// Update to refect follow status
				$link.closest('.follow-prompt').html(responseHtml);
			},
			error: function(request) {
				followPrompt.html(originalHtml);
				alert(request.responseText);
			}
		});
		return false;
	});
}

