// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
jQuery.ajaxSetup({  
    'beforeSend': function (xhr) {xhr.setRequestHeader("Accept", "text/javascript")}  
});
function shortenUrl(){
	var url = $('#linkShrink').val();	
	$.ajax({
		type: "GET",
		url: "/links/shorten",
		data: "link="+url,
		success: function(msg){
			api.close();
			var input_val = $('#update').val();
			$('#update').val(input_val + ' ' + msg + ' ');
			$('#linkShrink').val('');
			$("#update").focus(); 
		},
		error: function(e) { alert("Failed to shorten URL, please make sure your URL is formatted correctly!"); 
		} 
	});
}
$.fn.limitSelection = function(limit, options){
		// get the options to use
		var opt = jQuery.extend(
			(limit && limit.constructor == Object ? limit : {
					limit: limit
				, onsuccess: function (limit){ return true; }
				, onfailure: function (limit){ alert("You can only select a maximum a of " + limit + " items."); return false; }
			})
			, options);
		
		var self = this;

		var getCount = function (el){
			if( el.type == "select-multiple" ) return $("option:selected", self).length;
			else if( el.type == "checkbox" ) return self.filter(":checked").length;
			return 0;
		};

		var undoSelect = function (){
			// reduce selection to n items
			setValue(self, getValue(self).slice(0, opt.limit));
			// do callback
			return opt.onfailure.apply(self, [opt.limit]);
		};

		return this.bind(
			(!!self[0] && self[0].type == "select-multiple") ? "change.limitSelection" : "click.limitSelection",
			function (){
				if( getCount(this) > opt.limit ){
					// run callback, it must return false to prevent action
					return (this.type == "select-multiple") ? undoSelect() : opt.onfailure.apply(self, [opt.limit]);
				}
				opt.onsuccess.apply(self, [opt.limit]);
				return true;
			}
		);
};
$(document).ready(function(){
	$(".dim_input").focus(function() {
		$(this).css("color","#000");
		if( this.value == this.defaultValue ) {
			this.value = "";						
		}
	}).blur(function() {
		$(this).css("color","#ababab");
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
	$("#top_login_link").click(function(){
		$("#login").expose({
					maskId: 'mask',
					onLoad: function() {
						this.getExposed().css({backgroundColor: '#c7f8ff'});
						$("#Username:input").focus();
					},
					onClose: function() {
						this.getExposed().css({backgroundColor: null});
					},
					api: true

				}).load();
		
	});
	$("#profile_image").hover(function(){
		$("#profile_image span:first-child").show();
	}, function () {
		$("#profile_image span:first-child").hide();
	});
	$("a.twitterface_nav").click(function(){
		$("a.twitterface_nav").removeClass("active");
		$(this).addClass("active");
	});
});