/*
	javascript module for /views/include/micropost/compose_block ..
	needs durl.js, ui_common.js
*/

selected_tab = TAB_TODAY;

function tab_clicked(e) {
	var target = (e && e.target) || (event && event.srcElement);
	var tab_items = $$('.compose_block .tab_bar .tab_item');
	for(var i=0; i<tab_items.length; i++){
		if(target == tab_items[i]){
			selected_tab = tab_items[i].id;
			tab_items[i].style.fontWeight = "bold";
			tab_items[i].style.color = "#333333";
			tab_items[i].style.backgroundColor = "#ffffff";
			tab_items[i].style.height = "27px";
			tab_items[i].style.lineHeight = "27px";
			tab_items[i].style.borderBottom = "0 none";
		} else {
			tab_items[i].style.fontWeight = "normal";
			tab_items[i].style.color = "#889db7";
			tab_items[i].style.backgroundColor = "#e5eaf0";
			tab_items[i].style.height = "26px";
			tab_items[i].style.lineHeight = "26px";
			tab_items[i].style.borderBottom = "1px solid #c0c5cc";
		}
	}
	clear_select_block();
	close_company_search_popup();
	close_ifund_select_popup();
	switch(selected_tab) {
		case TAB_TALK:
			open_company_search_popup();
		break;
		case TAB_iFUNd:
			open_ifund_select_popup();
		break;
	}
}

function input_area_clicked_not_logged_in() {
	alert('글을 쓰시려면 로그인하세요.');
}

function input_area_enable() {
	$$('.input_block .input_area textarea')[0].style.height = '60px';
	$$('.input_block .write_btn')[0].show();
	$$('.input_block .counter')[0].show();
	$$('.input_block .url_shortening')[0].show();
	$$('.input_block .counter')[0].innerHTML = '';
}

function input_area_disable() {
	$$('.input_block .input_area textarea')[0].style.height = '20px';
	//$$('.input_block .input_area textarea')[0].value = '';
	$$('.input_block .write_btn')[0].hide();
	$$('.input_block .url_shortening')[0].hide();
	$$('.input_block .counter')[0].hide();
}

function open_company_search_popup() {
	if($$('.company_search_popup')[0]){
		$$('.company_search_popup')[0].show();
	}
	if($$('.input_block')[0]){
		$$('.input_block')[0].style.zIndex = '-1';
	}
}

function close_company_search_popup() {
	if($$('.company_search_popup')[0]){
		$$('.company_search_popup')[0].hide();
	}
	if($$('.input_block')[0]){
		$$('.input_block')[0].style.zIndex = '1';
	}
}

function open_ifund_select_popup() {
	if($$('.ifund_select_popup')[0]){
		$$('.ifund_select_popup')[0].show();
	}
	if($$('.input_block')[0]){
		$$('.input_block')[0].style.zIndex = '-1';
	}
}

function close_ifund_select_popup() {
	if($$('.ifund_select_popup')[0]){
		$$('.ifund_select_popup')[0].hide();
	}
	if($$('.input_block')[0]){
		$$('.input_block')[0].style.zIndex = '1';
	}
}


function company_talk_on_select_company(name, code, search_instance)
{
	search_instance.inifocus();
	
	search_instance.cocode_storage.value = code;
	//search_instance.input.value = name + "(" + code + ")";
	search_instance.input.value = '';

	search_instance.drop_box.hide();
	
	var select_block = $$('.select_block')[0];

	var s_begin = document.createElement('span');
	s_begin.className = 'ib26';
	
	var img = document.createElement('img');
	img.src = '/assets/images/static/close.png';
	img.style.cursor = 'pointer';
	img.onclick = function() { 
		clear_select_block(); 
	};

	var s_end = document.createElement('span');
	s_end.className = 'ib26';
	
	select_block.innerHTML = name + "(" + code + ")";
	select_block.appendChild(s_begin);
	select_block.appendChild(img);
	select_block.appendChild(s_end);
	select_block.show();

	close_company_search_popup();
}

function ifund_select_on_select_ifund(name, id)
{
	var select_block = $$('.select_block')[0];
	var ifund_select_ifund_id = $('ifund_select_ifund_id');

	var s_begin = document.createElement('span');
	s_begin.className = 'ib18';

	var img = document.createElement('img');
	img.src = '/assets/images/static/close.png';
	img.style.cursor = 'pointer';
	img.onclick = function() { 
		clear_select_block(); 
	};

	var s_end = document.createElement('span');
	s_end.className = 'ib18';

	ifund_select_ifund_id.value = id;
	select_block.innerHTML = name;
	select_block.appendChild(s_begin);
	select_block.appendChild(img);
	select_block.appendChild(s_end);
	select_block.show();

	close_ifund_select_popup();
}

function clear_select_block()
{
	var select_block = $$('.select_block')[0];
	var selected_ifund_id = $('ifund_select_ifund_id');
	var selected_cocode = $('company_talk_cocode');
	
	select_block.innerHTML = '';
	select_block.hide();
	selected_ifund_id.value = '';
	selected_cocode.value = '';
}

function url_shortening_popup_open()
{
	$$('.input_block .url_shortening .popup')[0].show();
}

function url_shortening_popup_close()
{
	$$('.input_block .url_shortening .popup')[0].hide();
}

function url_shortening_btn_clicked()
{
	var long_url = $$('.input_block .url_shortening .url')[0].value;
	if(long_url){
		if(long_url.substr(0, 7) != 'http://'){
			long_url = 'http://' + long_url;
		}
		url_shortening(long_url, url_shortening_handler);
		$$('.url_shortening .popup .loading')[0].show();
	} else {
		alert('url을 입력하세요.');
	}
}

function url_shortening_handler(data)
{
	var short_url = data.data.url;
	$$('.input_block .input_area textarea')[0].value += short_url;
	$$('.url_shortening .popup .loading')[0].hide();

	url_shortening_popup_close();
}

function url_shortening_popup_btn_mouse_over()
{
	$$('.input_block .url_shortening .popup_btn img')[0].src = '/assets/images/static/btn_link_on.gif';
}

function url_shortening_popup_btn_mouse_out()
{
	$$('.input_block .url_shortening .popup_btn img')[0].src = '/assets/images/static/btn_link.gif';
}

