//force the pages that you request to not be cached
$.ajaxSetup({cache: false});
function sampleText(o, text){
	if (o.val() == "") {
		o.val(text);
		sampleStyle(o, text);
	} else if (o.val() != text) {
		o.addClass("text_clicks");
	}
	o.focus(function(){
		if ($(this).val() == text) {
			$(this).val("").addClass("text_clicks");
		} else {
			$(this).select();		
		}
	}).blur(function(){
		if ($(this).val() == "") {
			$(this).val(text);
			sampleStyle($(this), text);
		}
	});
}
function sampleStyle(o, text) {
	if (logged_in) {
		if (text == "Watch my video on #twitvid") {
			o.addClass("text_clicks");
		} else {
			o.removeClass("text_clicks");	
		}
	} else {
		o.removeClass("text_clicks");
	}	
}
function updateCharsNum(o, maxlen, area) {
	if (!area) {
		area = ".textarea_state";
	} else {
		area = "." + area;
	}
	if (!maxlen) {
		maxlen = 115;
	}
	var message = o.value;
	var message_len = message.length;
	//
	var left = maxlen - message_len;
	$(area).html(left);
	var disabled = false;
	if (message_len > maxlen) {
		$(area).css({"color":"#ff0000"});
		disabled = true;
	} else {
		$(area).css({"color":"#6BADE0"});
	}
	if (swfu) {
		swfu.setButtonDisabled(disabled);	
	}
	/*if (disabled) {
		$(".bt_record_from_webcam").addClass("bt_record_from_webcam_disabled");
		$(".bt_tweet_video").addClass("bt_tweet_video_disabled");
		$(".bt_webcam_capture").addClass("bt_webcam_capture_disabled");
	} else {
		$(".bt_record_from_webcam").removeClass("bt_record_from_webcam_disabled");
		$(".bt_tweet_video").removeClass("bt_tweet_video_disabled");
		$(".bt_webcam_capture").removeClass("bt_webcam_capture_disabled");
	}*/
}

//<img onerror="thumbnailError(this, 'video');" />
function thumbnailError(o, type) {
	if (type == "video") {
		o.src="/UI/default_video_thumbnail.jpg";
	} else if (type == "user") {
		o.src="/UI/images/default_profile_normal.png";
	} else {
		o.src="/UI/default_video_thumbnail.jpg";
	}
}
// onmouseover="showBIO(this,'username');"
function showBIO(o,username) {
	if (!username) {
		return false;	
	}
	var offset = $(o).offset();
	var top = offset.top + $(o).height() + 3;
	var left = offset.left + $(o).width() * 0.5 - 27;
	//
	var uid = username.replace(/(\W)/ig, "_");
	//window.status = uid;
	var tip = $(".tip_"+uid);
	if (tip.length) {
		tip.css({"top":top+"px", "left":left+"px"}).show();
	} else {
		var htm = '<div class="tip tip_'+uid+' clearfix" style="display:none;"><div class="tip_head pngfix"></div><div class="tip_body pngfix"><div class="tooltip"><img src="/UI/images/loading.gif" alt="loading..." /></div></div><div class="tip_foot pngfix"></div></div>';
		$(document.body).prepend(htm);
		var tip = $(".tip_"+uid);
		tip.css({"top":top+"px", "left":left+"px"}).show();
		var url="/index.php?area=ajax&cmd=getBIO&username="+encodeURIComponent(username);
		$.getJSON(url, function(json){
			var htm = '';
			if (json.name) {
				username = json.name;
			}
			htm += '<div class="tip_item"><b>Name:</b> '+username+'</div>';
			if (json.location) {
				htm += '<div class="tip_item"><b>Location:</b> '+json.location+'</div>';
			}
			if (json.description) {
				htm += '<div class="tip_item"><b>Bio:</b> '+json.description+'</div>';
			}
			if (json.screen_name) {
				htm += '<div class="tip_line"></div>';
				htm += '<div class="tip_info">'+addCommas(json.followers_count)+' followers</div>';
				htm += '<div class="tip_info">'+addCommas(json.friends_count)+' following</div>';
			}
			tip.find(".tooltip").html(htm);
		});
	}
	$(o).mouseout(function() {
		$(".tip").hide();	
	});
}

// onmouseover="showVID(this,'guid');"
function showVID(o,guid) {
	if (!guid) {
		return false;	
	}
	var offset = $(o).offset();
	var top = offset.top + $(o).height() - 10;
	var left = offset.left;
	//
	var tip = $(".tip_"+guid);
	if (tip.length) {
		tip.css({"top":top+"px", "left":left+"px"}).show();
	} else {
		var htm = '<div class="tip tip_'+guid+' clearfix" style="display:none;"><div class="tip_head pngfix"></div><div class="tip_body pngfix"><div class="tooltip"><img src="/UI/images/loading.gif" alt="loading..." /></div></div><div class="tip_foot pngfix"></div></div>';
		$(document.body).prepend(htm);
		var tip = $(".tip_"+guid);
		tip.css({"top":top+"px", "left":left+"px"}).show();
		var url="/index.php?area=ajax&cmd=getVID&guid="+guid;
		$.getJSON(url, function(json){
			var htm = '';
			htm += '<div class="tip_item"><b>'+json.username+'</b> '+json.tweet_msg+'</div>';
			htm += '<div class="tip_line"></div>';
			htm += '<div class="clearfix">';
			htm += '<div class="tip_info left">'+addCommas(json.views_num)+' Views</div>';
			htm += '<div class="tip_info right">'+addCommas(json.likes_num)+' Likes</div>';
			htm += '</div>';
			tip.find(".tooltip").html(htm);
		});
	}
	$(o).mouseout(function() {
		$(".tip").hide();	
	});
}

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 ui_tweetText(str) {
	str = " " + str;
	str = str.replace(/(http:\/\/[^\s@]+[^\s@\.]+)/ig, '<a href="$1">$1</a>');
 	str = str.replace(/(\s)#([^\s]*[^\s\.]+)/ig, '$1<a href="http://search.twitter.com/search?q=$2" target="_blank">#$2</a>');
 	str = str.replace(/@(\w+)/ig, '<a href="/videos/$1" onmouseover="showBIO(this,\'$1\');">@$1</a>');
	return str;
}
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}
function checkEmail(email) {
var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
if (!filter.test(email)) {
return false;
}
return true;
}

