Toran=new Object();
Toran.single = [];
Toran.init_urlInput_text = function(o){
    if(!o.value || o.value == o.title){
        $(o).addClass("greyinput");
        o.value = o.title;
    }
    $(o).focus(function(){
        $(o).removeClass("greyinput");
        if(o.value == o.title) o.value = "";
    });
    $(o).blur(function(){
        if(!o.value){
            $(o).addClass("greyinput");
            o.value = o.title;
        }
    });
}
Toran.widgethtml='<div id="widget" class="dn"><dl><dt>自定义别名</dt><dd>别名只能包含字母、数字和下划线，并且最多30个字符。</dd>	<dt>密码保护</dt><dd>你可以设置一个密码来创建私人条目，这样别人就必须要知道密码才能查看内容。</dd><dt>标题</dt><dd>使用一个标题，或者写一些简单的关键词能够方便你以后查找和分享。</dd></dl></div>';
Toran.totwitterhtml='<span id="totwitter"><label style="font-weight:normal;" for="update2"><input type="checkbox" name="update2" id="update2" value="twitter" />同步到Twitter</label></span>';
String.prototype.trim= function(){
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
$(document).ready(function(){
	if($('#status span.email').length>0) Toran.userlogin=true; else Toran.userlogin=false;
	initHTML();
	initInput(location.hash);
	var $tablink = $('div.tabs a');
	$tablink.click(function(){
		if(initInput($(this).attr('href')) === true){
			$('div.tabs a.on').removeClass('on');
			$(this).addClass('on');
		}
	});
	$tablink.each(function(){
		if($(this).attr('href') == location.hash)
			$(this).click();
	});
	$('img.help-icon').hover(function(){$('#widget').show();},function(){$('#widget').hide();});

	$('span.edi').live('click', function(){
		$(this).addClass('dn');
		$(this).parent('div.descri').append("<div class='desarea'><textarea name='description' rows='1' cols='1'></textarea><span class='cancel'><a href='javascript:void 0;' onclick='javascript:removeDesare(this);'>取消说明</a></span></div>");
		$('div.desarea textarea').focus();
	});

	$('input.submitbutton').click(function(){$('form').submit();});
	$("form").submit(function(){
		//return true;
		for(var i=0;i<$('div.content').length;i++){
			var divc=$('div.content').eq(i);
			if(divc.css('display') != 'block')
				continue;
			var allnull = true;
			divc.find('.notnull').each(function(){
				if($(this).hasClass('input_field')){
					if($(this).val().trim() != '' && $(this).val().trim() != 'http://')
						allnull = false;
				}else{
					if($(this).val().trim() != '')
						allnull = false;
				}

			});
			if(allnull == true){
				divc.find('.errnotnull').show();
				return false;
			}
		}
		if($('#nickname').val().trim().length > 0 && $('#nickname').val().match(/^[_0-9a-zA-Z]+$/) === null){
			$('p.alias').show(); return false;
		}
		if($('#submittype').attr('value') == 'video'){
			for(var i=0; i<$('input.videourl').length; i++){
				var val = $('input.videourl').eq(i).val();
				if(val.trim() == '')
					continue;
				if(checkVideoUrl(val) == false){
					$('#videodiv').find('span.errnotnull').show();
					return false;
				}
			}
		}
		if($('#update2').attr("checked")==true && $('#submittype').attr('value') != 'text'){
			var $des=$('textarea[name=description]:visible');
			if($des.length==0 || $des.val()==''){
				if(!confirm("添加到说明中的文字将显示在同步的twitter中\n你确定不写点什么？")){
					if($des.length==0) $('span.edi a:visible').click();
					return false;
				}
			}
		}
		$('#hd').append('<div id="message-area"><span id="loading">Loading...</span></div>');
	});
	$('input.input_field').each(function(){
		Toran.init_urlInput_text(this);
	});
	$('#update2').live('click',function(){
		if(Toran.userlogin==false){
			alert('登陆后才能同步喔！');
			return false;
		}else if($('meta[name=microblog]').attr('content') != 'twitter'){
			alert('你还没有绑定twitter账户呢，请在“我的账户”中绑定.'); return false;
		}
	});
});
function removeDesare(t){
	$(t).parents('div.descri').find('span.edi').removeClass('dn'); $(t).parents('div.desarea').remove();
}
function initHTML(){
	$('#troption').append(Toran.widgethtml);
}
function initInput(t){
	if(typeof t === 'undefined' || t == '') t=$('div.tabs a.on').attr('href');
	$('form div.content').hide();
	$('div.content').find('input,textarea,select').attr('disabled','disabled');
	$('#submittype').removeAttr('disabled');
	$('form').removeAttr('enctype');
	$('#totwitter').remove();$('#submit'+t.substr(1)).after(Toran.totwitterhtml);
	switch(t){
		case '#text':  $('#textsave').show(); $('#textsave').find('input,textarea,select').removeAttr('disabled');$("#texta").focus();
			$('#submittype').attr('value', 'text'); 
			return true;
		case '#url': $('#urlshorten').show();$('#urlshorten').find('input,textarea,select').removeAttr('disabled');$("#url").focus(); 
			$('#submittype').attr('value', 'url');
			return true;
		case '#music': $('#musicdiv').show();$('#musicdiv').find('input,textarea,select').removeAttr('disabled');	$("#musicurl").focus();
			$('#submittype').attr('value', 'music'); 
			return true;
		case '#image': $('#imagediv').show();	 $('#imagediv').find('input,textarea,select').removeAttr('disabled');$("#imgurl").focus(); 
			$('#submittype').attr('value', 'image');
			return true;
		case '#video': $('#videodiv').show();$('#videodiv').find('input,textarea,select').removeAttr('disabled');	$("#videourl").focus(); 
			$('#submittype').attr('value', 'video'); 
			return true;
		case '#code': $('#codediv').show();$('#codediv').find('input,textarea,select').removeAttr('disabled');	$("#codetext").focus(); 
			$('#submittype').attr('value', 'code'); 
			return true;
		case '#file': $('#filediv').show(); $('form').attr('enctype','multipart/form-data').attr( "encoding", "multipart/form-data");
			$('#submittype').attr('value', 'file'); $('#filediv').find('input,textarea,select').removeAttr('disabled');
			return true;
	}
	return false;
}
function checkVideoUrl(url){
	var pattern1=/^(http:\/\/)?v\.youku\.com\/v_show\/id_[0-9a-z=]+=?=?\.html/i;
	var pattern2=/^(http:\/\/)?www\.tudou\.com\/programs\/view\/[-0-9a-z]+/i;
	if(pattern1.test(url) || pattern2.test(url))
		return true;
	else
		return false;
}
