// JavaScript Document

$(document).ready( function() {
	
	$("input,textarea").focus( function() { if(this.value==this.defaultValue && !this.readOnly && (this.type==null || this.type=="text" || this.type=="password" || this.type=="textarea")) { if (!$(this).hasClass('ns')) { this.select(); } } } )
	$("input,textarea").click( function() {  if(this.value==this.defaultValue && (this.type==null || this.type=="text" || this.type=="password" || this.type=="textarea")) { if (this.readOnly || $(this).hasClass('nc')) { if (!$(this).hasClass('nc')) { this.select(); } } else { this.value=""; } } } )
	$("input,textarea").blur( function() {  if(jQuery.trim(this.value)=="" && !$(this).hasClass('nc')) { this.value=this.defaultValue; } } )
	
	
	$("input,textarea").keyup(function() {
		var this_limit=250-$(this).attr('value').length;
		$("#counterfor_"+$(this).attr('name')).html(this_limit);
		
		if (this_limit<0) { $("#counterfor_"+$(this).attr('name')).css('color','#FF6666'); }
		else { $("#counterfor_"+$(this).attr('name')).css('color',''); }
		
	} );
	
	
	
} );


function ani(whatid) {
	if (whatid) { $("#"+whatid).html(''); $("#"+whatid).append(img_ani); } 
	img_ani=$('<img />').attr( { src: "/images/content/ani.gif", height: 16, width: 16 } );
	img_ani.addClass("ani");
}
function ani_done(whatid) {
	$("#"+whatid).html('');
	var thisimg=$('<img />').attr( { src: "/images/icons/accept.png", height: 16, width: 16 } );
	thisimg.addClass("ani");
	$("#"+whatid).append(thisimg);
}
var img_ani;
ani(0);


var imgchooser_pos=1;
function ic(newpos, dest, parentid) {
	if (imgchooser_data.length<=1) { return; }
	
	imgchooser_pos+=newpos;
	if (imgchooser_pos>imgchooser_data.length) { imgchooser_pos=1; }
	else if (imgchooser_pos<=0) { imgchooser_pos=imgchooser_data.length; }
	
	$('#imgchooser_target').unbind('load');
	$("#imgchooser_target").load( function() { $("#imgchooser_target").css('top',imgchooser_data[imgchooser_pos-1][1]+'px'); $("#ani_imgchooser").html(''); } );
	ani("ani_imgchooser");
	$("#imgchooser_target").attr('src','/photos/'+escape(dest)+'/'+escape(parentid)+'/'+escape(imgchooser_data[imgchooser_pos-1][0])+'/n.jpg');
	$("#imgchooser_target").parent().attr('href','/'+escape(dest)+'/'+escape(parentid)+'/photos/'+escape(imgchooser_data[imgchooser_pos-1][0]));
	$("#imgchooser_pos").html(imgchooser_pos);
}


function rts(thisid,username) {
	$('#post_wallreplyto').val(thisid);
	$('#post_walltext').html("Reply to "+username+"...");
	$('#post_walltext').focus();
}

function rtng(obj,dest,id) {
	ani("ani_rate");
	if (obj.options[0].value==0) { obj.remove(0); }
	
	$.getJSON("/ajax?action=rate&dest="+escape(dest)+"&id="+escape(id)+"&rt="+escape(parseInt(obj.value)), function(data){ ani_done("ani_rate"); } );
	
}


function flw(obj,dest,id) {
	var this_num=parseFloat(removePunc($("#count_fl").html()));
	if ($(obj).is(':checked')) { $("#count_fl").html(addCommas(this_num+1)); }
	else { $("#count_fl").html(addCommas(this_num-1)); }
	ani("ani_fl");
	
	$.getJSON("/ajax?action=follow&dest="+escape(dest)+"&id="+escape(id)+"&fl="+escape($(obj).is(':checked')), function(data){ $("#ani_fl").html(''); } );
	
}



function tweet(what) { window.open('http://twitter.com/home?status='+encodeURIComponent(what),'sharer','toolbar=0,status=0,width=626,height=436'); return false; }


function facebook_share() { u=location.href; t=document.title; window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436'); return false; }

function facebook_onlogin_ready() {
	try { ani('ani_facebook'); } catch (err) { }
	if (window.location=="http://dubspace.com/?loggedout=1") { window.location="http://dubspace.com/"; return; }
	window.location=window.location;
}
function facebook_onlogout() { window.location="/logout/"; }



function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
function removePunc(thisval) {
	thisval=thisval.replace("+","");
	thisval=thisval.replace(",","");
	return thisval;
}






