var gigel = {
};

gigel.init = function( target )
{
	if ( target == null )
		TTS.setupAjax();
	
	target = target == null ? $("body") : target;
	
	fixInputWidths( target );
};

$(function() {
	
	function vote( element, postId, step ) { 
		$.ajax( {
			data: { "action": "post.rating", "postId": postId, "step": step },
			success: function( result ) { 
				if (result.result == true )
				{
					element.find("span").unbind("click");
					element.removeClass("enabled");
					element.addClass("rated-"+step);
					var value = element.find("span.value");
					value.html( parseInt(value.html()) + step );
				}
			}, 
			complete: function() {}
		});
	};
	
	$(".rating").each( function() {
		var me = $(this);
		var postId = me.param("postId");
		var step = me.param("step");
		me.addClass("rated-"+step);
		if ( step == null )
		{
			me.addClass("enabled");
			$(".ratingUp", this).click( function() {  vote( me, postId, 1 ); } );
			$(".ratingDown", this).click( function() { vote( me,postId, -1 ); } );
		}
	});
	
	function padLeft(s) { var ret =  s + ''; while ( ret.length < 2 ) ret = "0" + ret; return ret; }
	$( function() { 
		$(".date").each( function() {
			var date = new Date( Number($(this).attr("title")) );
			var sDate = date.getFullYear() + "." + padLeft(date.getDate()) + "." + padLeft(date.getMonth()+1) + " " + padLeft(date.getHours()) + ":" + padLeft(date.getMinutes());
			$(this).html( sDate );
		});
	});
	
	$(".post .starter .postImg").each( function() {
		$(this).css( { cursor: "pointer" } );
		$(this).wrap( $("<a/>").attr("href", $(this).parents(".post").find("h3 a").attr("href") ) );		
	});
	
	fixLinks();
});

function loginFormInit(popup) 
{
	gigel.init( $(this) );
	$(this).find("form").submit( function () {
		popup.submit( $(this) );
		return false;
	});
};

function fixInputWidths( target )
{
	target.find(".full input.text, .full input.password, .full textarea").each( function() { 
		$(this).css( "width", $(this).parent().innerWidth() + $(this).width() - $(this).outerWidth() );
	});
};

function fixLinks()
{
	var domains = new RegExp( "gigel.org", "i");
	$( "a.link" ).each( function() {
		if ( this.hostname.search(domains) == -1 )
		{
			this.target = "_blank";
			this.href = this.href.replace( "%3Cwbr/%3E", "" );
		}
	});
};

function cookieHideElement(elementId)
{
	$.cookie( elementId, 1, {expires: 300, path: '/'});
	$("#" + elementId).animate( { height: 'toggle', opacity: 'toggle' }, "normal");
}


gigel.postFormInit = function()
{
	var form = $(this);
	
	gigel.init(form);
	
	function lenghtCounter() {
        var left = 500 - $(this).val().length;
        if (left < 0) {
            $('#counter').text(left);
            $('#counter').addClass("excess");
        } else {
            $('#counter').text(left);
            $('#counter').removeClass();
        }
    }
	
	form.find( "textarea#content" ).autoResize(); //.keyup( lenghtCounter );
	form.submit( function() {
		$(this).find("button").attr("disabled", "disabled");
	});
};

gigel.postDelete = function( postId )
{
	//if ( confirm('sigur?') )
		$.ajax( { 
			data: { "action": "post.delete", "postId": postId },
			complete: function() { $("#post-" + postId).remove(); }
		} );
};

function eml()
{
	var a = "gig"; a += "el@gi"; a += "gel.org";
	document.write("<a href='mailto:" + a + "'>" + a + "</a>");
};

