var SAVING;
var saving_flag = false;
var whatup_focus = false;
var tonight_focus = false;
function init(lang) {
    if (lang == 'zh') {
        SAVING = "正在保存...";
    } else {
        SAVING = "Saving...";
    }
}

document.onclick = function() {
    if (!whatup_focus && $('whatup_list')) {
        $('whatup_list').setStyle({display:'none'});
    }
    if (!tonight_focus && $('tonight_list')) {
        $('tonight_list').setStyle({display:'none'});
    }
}

function confirm_affiliations(login, user, friend) {
    new Ajax.Request('/'+login+'/friends/confirm_affiliations',{parameters: { user:user,friend:friend }});
    window.location.reload();
}

// used in friends/reqs/id
function fillEndYear(begin, end, year) {
    var thisyear = year;
    var str = "";
    end.innerHTML = "";
    if (begin.options[begin.selectedIndex].text != '') {
        end.setStyle({background:'#fff'});
        end.disabled = false;
        end.options[end.length] = new Option('Now','');
        for (i=thisyear; i>=begin.options[begin.selectedIndex].text; i--) {
            end.options[end.length] = new Option(i,i);
        }
    }else {
        end.setStyle({background:'#ccc'});
        end.disabled = true;
    }
}

function li_focus(obj) {
    obj.className = "bg_grey";
}

function li_blur(obj) {
    obj.className = "";
}

function choose(val, obj) {
    $(obj).value = val;
    $(obj).setStyle({color:'#333'});
    $(obj+'_list').setStyle({display:'none'});
}

function change_whatup_tonight(type, default_value) {
    var val = $(type).value;
    if (!saving_flag && (val != '') && (val != default_value)) {
        saving_flag = true;
        $(type).value = SAVING;
        new Ajax.Updater(type+'_list','/mypage/update_current',{
            parameters: { type: type, value: val, op: 'update' },
            onComplete: function() {
                $(type).value = val;
                saving_flag = false;
            }
        });
    }
}

function show_whatup_tonight(type) {
    $(type+'_list').setStyle({display:'block'});
}

function focus_whatup_tonight(type) {
    if (type == 'whatup') {
        whatup_focus = true;
    } else {
        tonight_focus = true;
    }
}

function blur_whatup_tonight(type) {
    if (type == 'whatup') {
        whatup_focus = false;
    } else {
        tonight_focus = false;
    }    
}

// add by Raecoo 2008-12-04
// use for fast add friend group at System notice page when make friend request
var friend_groups_ieditor = {
	count:1,
	// add a span and input to page panel
	add:function(){
		$('no_groups').hide();
		var id = this.count;
		var group = {};
		var add_tip = LANGUAGE == 'zh' ? '请输入分组名称' : 'Please input group name' ;
		var del_tip	 = LANGUAGE == 'zh' ? '点击删除此分组' : 'Click to remove' ;
		group.id = id;
		group.name = LANGUAGE == 'zh' ? '自定义'+(id) : 'name '+(id);
		var html = "";
		html += '<input type="checkbox" name="group_ids[]" id="gvalue_'+ group.id +'" class="no_border" style="float:left;" value="new_'+group.name+'"/>';
		html += '<span name="gspan_'+group.id+'" id="gspan_'+group.id+'" title="'+ add_tip +'" onclick="friend_groups_ieditor.spanOnClick(this);">';
		html += '<input type="text" name="gtext_'+group.id+'" id="gtext_'+group.id+'" style="border:1px solid #D1D4D1;" maxlength="12" size="8" value="'+group.name+'" ';
		html += ' onkeydown="if(event.keyCode==13){friend_groups_ieditor.confirm(this)};" onblur="friend_groups_ieditor.spanOnBlur(this);"/>';
		html +=	 '</span><a href="javascript:void(0);" style="margin-left:5px;" title="'+ del_tip +'" onclick="friend_groups_ieditor.remove(\'new_group_'+ group.id +'\')"><b>x</b></a>';
		new Insertion.Bottom('groups',"<li id='new_group_"+group.id+"'>"+html+"</li>");
		this.count+=1;
		setTimeout("$('gtext_"+ group.id +"').focus()",0);
		$('scrolldiv').scrollTop;
	},
	// remove a exist group(span and input)
	remove: function(obj_id){
		$(obj_id).remove();
		this.count-=1;
	},
	// update span's innerHTML to the input and edit group name when clicked the span 
	spanOnClick:function(obj){
		var id = obj.id.substr(6);
		var edit_tip =  LANGUAGE == 'zh' ? '点击编辑分组名称' : 'Click to edit group name' ;
		if($('gtext_'+id)) {return;}
		var html = "";
		html += '<input type="text" name="gtext_'+id+'" id="gtext_'+ id+'" title="'+ edit_tip +'" style="border:1px solid #D1D4D1;" maxlength="12" size="8" value="'+obj.innerHTML +'" ';
		html += ' onkeydown="if(event.keyCode==13){friend_groups_ieditor.confirm(this)};" onblur="friend_groups_ieditor.spanOnBlur(this);"/></span>';
		obj.innerHTML = html;
		$('gtext_'+id).focus();
	},
	// update the checkbox's value and switch input to span when the input onblur event
	spanOnBlur:function(obj){
		var id = obj.id.substr(6);
		var edit_tip =  LANGUAGE == 'zh' ? '点击编辑分组名称' : 'Click to edit group name' ;
		if (obj.value==''){ 
			obj.value = LANGUAGE == 'zh' ? 'new_自定义'+(this.count) : 'new_name '+(id); 	
    }else if (lenByte(obj.value) > 12){
      popup_tip = LANGUAGE == 'zh' ? "组名不能超过12个字符(一个中文两个字符)" : "Group names shall not be more than 12 characters"
      alert(popup_tip);
		}else{
			$('gvalue_'+obj.id.substr(6)).value = 'new_'+truncate(obj.value,12);
		}
		$('gspan_'+id).innerHTML = truncate(obj.value,12);
		$('gspan_'+id).title = edit_tip;
	},
	// update the checkbox's value and switch input to span when user press the 'enter'
	confirm:function(obj){
		this.spanOnBlur(obj);
		$('gvalue_'+obj.id.substr(6)).value = 'new_'+truncate(obj.value,12);
		obj.blur();
	},
	// validation the form commit conditions
	validation: function(){
		var ids = "";
		if ($('isknow').value == -1) { $('error_tips').innerHTML = (LANGUAGE=='zh' ? "请完成上面的选项再进行操作" : "Please select one above to continue");return false; }
	    eles = $$("input[name=\"group_ids[]\"]");
		eles.each(function(s) { if(s.checked) { ids+=s.value+"-"; } ;$('check_ids').value=ids;});   
		if (ids=='') { $('error_tips').innerHTML = (LANGUAGE=='zh' ? "请为好友选择一个分组" : "Please select one group for your friend");return false; }
		return true;
	},
	// close the popup-window
	close: function(){
		$('check_ids').value = '';
		$('error_tips').innerHTML = "";
		$('notice_id').value = 0;
		$('status').value = 0;
		$('friend_login').innerHTML = "";
		eles = $$("input[name=\"group_ids[]\"]");
		eles.each(function(s) {
			if(s.checked) {
			  s.checked = false;
			}
		});       
		$('friend_request_popup').hide();
	}
 }
