/////TAB BASED FUNCTIONS STARTS HERE///////////////////////////////////////////////////////////////////////////
function saveTabInfo(vTabId) {
	var params = {value: QEditor.doc("about_content").body.innerHTML };
	var vTabTitle = $('tbTabName').value;
	showProgress("Saving changes.", true);
	if(vTabTitle != "" && vTabId==0) {
		invoke(null,'channel:editAboutTab', {tab_id:vTabId,title:vTabTitle,content:params.value}, true, function(req) {
			vTabId = req.responseText.split(' ')[1];					
			$('hfchannelTabId').value = vTabId;
			hideProgress();	
			refreshAboutTabs(vTabId);
		});
	}
	else if (vTabId== '-1') {			 
		new Ajax.Request("?event=channel:profile&edit=description&tabid=curActiveTab", {
			parameters:params, 
			method:'post',
			onComplete:function(transport,element){
			
				$("about_content").style.border='1px solid #ccc';
				hideProgress();				          			
				
			}
		});	
	}else {
		params['tab_id'] =  vTabId;	
		params['title'] =  vTabTitle;
		
		new Ajax.Request("?event=channel:update_tab_content", {
			parameters:params, 
			method:'post',
			onComplete:function(transport,element){
			$("about_content").style.border='1px solid #ccc';	
			hideProgress();		
			refreshAboutTabs(vTabId);	          			
			}
		});		
	}

}

function refreshAboutTabs(vTabId){
	if(vTabId=="-1"){

	}
	else if(vTabId=="-2"){
		$('tbInfo').className = 'active';
	}else{
		invoke(null,'channel:channel_about_tab', {isAjax:1}, true, function(req){
			
			$('dvMenuTab').innerHTML = req.responseText;
			$$("ul li a.active").each(function(item) {		
				item.className='';
			});
			
			$('tab' + vTabId).className = 'active';
			$('tab' + vTabId).highlight();
			hideProgress();
			$('tdContent').show();		
			attachTabHandlers();
		});
	}

}

function addTab() {
	$('dvAddTab').show();
	$('tbTabName').value = '';
	QEditor.doc("about_content").body.innerHTML = '';
	$('hfchannelTabId').value = 0;	
	$('btnSaveStaticTabInfo').hide();
	$('tab_info').hide();
	$('tab_content').show();
	$('tbTabName').focus();
}

function showChannelTab(obj,id,viewMode) {

	$$("ul li a.active").each(function(item) {item.className='';});		
	$(obj).className = 'active';
	$('tdContent').show();	
	if(viewMode==0)  $('dvAddTab').hide();
	if(viewMode==0)  $('btnSaveStaticTabInfo').hide();
	
	if(id>1 && viewMode==0)  $('dvAddTab').show();
	if(id==-2) {
		$('tab_info').show();
		$('tab_content').hide();
		hideProgress();
	}else if(id==-3) {
		$('tab_klatch').show();
		$('tab_info').hide();
		$('tab_content').hide();
		hideProgress();
	}else {
		$('hfchannelTabId').value = id;
		invoke(null,'channel:profile_tab_content', {tab_id:id}, true, function(req){	
		
			if(id == -1 && viewMode==0) {
					$('btnSaveStaticTabInfo').show();
					$('tab_info').hide();
					$('tab_klatch').hide();
					$('tab_content').show();
			
				}
			if(viewMode==1){
				
				$('tab_content').innerHTML = req.responseText;
			}
			else{
				QEditor.doc("about_content").body.innerHTML = req.responseText;		
				$('tbTabName').value=$('tab' + id).innerHTML;

			}
			$('tab_info').hide();
			$('tab_klatch').hide();

			hideProgress();
			$('tab_content').show();
		});
	}
	
}		
	
function attachTabHandlers(){
	
	$$("ul.submenu li a.maximized").each(function(item) { addTabHandler(item)});
	$$("ul.submenu li a.minimized").each(function(item) { addTabHandler(item)});
	$$("ul.submenu li a.close_tab").each(function(item) {
			var vtab_id = item.parentNode.getAttribute("tab_id");
			item.observe("click", function(event) {
				Message.confirm("Are you sure to delete this tab?", function(){
				
					invoke(null, "channel:editAboutTab&check=2&tab="+vtab_id, {}, true, function(req){
						loadURL(location.href);
					});
					event.stop();
				});		
			});
		});

}

function addTabHandler(item){
		
		var type = item.parentNode.getAttribute("tabtype");
		item.setStyle({cursor: "pointer"});
		item.observe("click", function(event) {
			invoke(null, "channel:editAboutTab&check=1&type="+(parseInt(type)), {}, true, function(req){
				loadURL(location.href);
			});
			event.stop();
		});
}
//TAB BASED FUNCTIONS ENDS HERE////////////////////////////////////////////////////////////
function send_message(user_id){
	if($('message_text').value=='') Message.alert("Message  cannot be empty.",'info');
	if($('message_to').value=='') Message.alert("Message To cannot be empty.",'info');
	if($('message_to').value!="" && $('message_text').value!="")
	{
		showProgress("Sending message", true);
		invoke(null,'user:send_new_message', {message:$('message_text').value,fk_user_id:user_id,friend_name:$('message_to').value}, true, function(req){
		hideProgress();
		Control.Modal.close();
	});
	}
}

function initAddressList(){
	new Ajax.Autocompleter("message_to", "address_list", "?event=channel:friendsSuggest", {
		paramName: "value",
		minChars: 1,
		tokens: [';'],
		updateElement : setSelection
	});

}

var toInfo=Array();
function setSelection(li) {
	toInfo.push(li.id);
	$("message_to").value="";
	for(j=0;j < toInfo.length;j++)	{
		if(j==0) 
			$("message_to").value= toInfo[j] + ";";
		else
			$("message_to").value=$("message_to").value + toInfo[j] + ";";
	}
}

function showGallery(iframe) {
		new Control.Modal("?event=story:showGallery&iframe="+iframe.id, {width:520, height:320}).open();
		return false;
}
  
function showStoryList(iframe) {
  	new Control.Modal("?event=story:showStories&iframe="+iframe.id, {width:520, height:320}).open();
  	return false;
}
	  
function insertStoryLink(href, content, iframe) {
	var n=QEditor.doc(iframe).createElement("A");
	n.setAttribute("href", href);
	n.appendChild(QEditor.doc(iframe).createTextNode(content));
	QEditor.surroundSelection(document.getElementById(iframe), n);
}	
	

function insertItem(href, content, actualContent,iframe,iMode) {
	var range = document.getElementById(iframe).contentWindow.getSelection().getRangeAt(0);
	if(iMode==false) {				
		var n=QEditor.doc(iframe).createElement("A");
		n.setAttribute("href", href);
		if (range.toString() == "") {
			n.appendChild(QEditor.doc(iframe).createTextNode(content));
			range.insertNode(n);
		} else { 
			range.surroundContents(n);
		}
	} else {
		QEditor.doc(iframe).body.innerHTML = QEditor.doc(iframe).body.innerHTML + actualContent;
	}

}
function attachStylesheet() {
	var ifrmLen;
	if(document.frames)
		ifrmLen = document.frames.length;
	else
		ifrmLen = window.frames.length;			
	for(var j = 0; j < ifrmLen; j++) {
	var cssfileName = ['templates/newsstream/css/newsstream.css','templates/main/css/main.css','templates/story/css/story.css','templates/channel/css/channel.css'];
		for(var i = 0;i < cssfileName.length;i++) {
			var ss = document.createElement("link");
			ss.type = "text/css";
			ss.rel = "stylesheet";
			ss.href = cssfileName[i];				
	
			if(document.all)
				document.frames[j].document.createStyleSheet(ss.href);
			else
				window.frames[j].document.getElementsByTagName("head")[0].appendChild(ss);
		}
	}
}
	

function showOwnerPage(vUserId){

loadURL("?user=" + vUserId);


}

function activateDefaultTabs(vMode){
	if(vMode==1){
			if (tabState_1 == 0 && tabState_2 == 0 && firstTabId != 0){
				showChannelTab($('tab' + firstTabId),firstTabId,tabViewMode);
			} else if (tabState_1 == 1){
				showChannelTab($('tbAbout'),-1,tabViewMode);		
			} else if (tabState_2 == 1){			
				showChannelTab($('tbInfo'),-2,tabViewMode);		
			} else if (tabState_3 == 1){			
				showChannelTab($('tbKlatch'),-3,tabViewMode);		
			}
	}
	else if(vMode==2){
	
			if (emptyTab == 1){ tabState_1 = 0; $('tabAbout').hide();}
	
			if (tabState_1 == 0 && tabState_2 == 0 && firstTabId == 0){
				$('setup_status').show();
			} else if (tabState_1 == 0 && tabState_2 == 0 && firstTabId != 0) {
				showChannelTab($('tab' + firstTabId),firstTabId,tabViewMode);
			} else if (tabState_1 == 1){			
				showChannelTab($('tbAbout'),-1,tabViewMode);
			} else if (tabState_2 == 1){			
				showChannelTab($('tbInfo'),-2,tabViewMode);		
			} else if (tabState_3 == 1){			
				showChannelTab($('tbKlatch'),-3,tabViewMode);		
			}
	
	}
	else if(vMode==3){
			if(tabState_3==1) showChannelTab($('tbKlatch'),-3,tabViewMode);	
	}

}

