var toggle = 0;
var isSelect = 0;
var tabActive = 0;
var content = "";
var remainPopup = "";

var ObjectCollection = Class.create();
ObjectCollection.prototype = {
	
	initialize : function (){
		this.objects = new Array();
		this.length = 0;
	},
	
	addContent : function (_content){
		this.objects[_content.id] = _content;
		this.length++;
	},
	
	getContent : function (_contentId){
		return this.objects[_contentId];
	}
}

var ObjContents = Class.create();
ObjContents.prototype  = {
	
	initialize : function (_id){
		this.id = _id;
		this.objects = new Array();
		this.length = 0;
	},
	
	addContent : function(_content){
		this.objects[this.length] = _content;
		this.length++;
	},
	
	active : function (){
		$(this.id).className = "tabactive";	
	},
	
	inactive : function (){
		$(this.id).className = "tab";	
	},
	
	show : function (){
		//TODO: change as css approach
		$(this.id).style.display = 'block';	
	},
	
	hide : function (){
		//TODO: change as css approach
		$(this.id).style.display = "none";	
	},
	
	setName : function(_name){
		this.name = _name;
	},
	
	assembleTab : function (){
		if (this.name==null)
			return;
			
			
	}
}

var MyDate = Class.create();
MyDate.prototype = {
	
	initialize : function (_date){
		this.date = _date;
	},
	
	getStringFormatDate : function (){
		var now = this.date;
		var month = now.getMonth()+1;
		var day = now.getDate();
		var year = now.getFullYear();
		var hours = now.getHours();
		var minutes = now.getMinutes();
		var seconds = now.getSeconds();
		return month +"/"+day+"/"+year+" "+hours+":"+minutes+":"+seconds;
	}
}

//var divContents = "{'tabs' : [{'id' : 'tab1','childs' : [{'id' : 'welcome','name' : 'Welcome'},{'id' : 'content5'},{'id' : 'content9'} ]},{'id' : 'tab2','childs' : [{'id' : 'content6'},{'id' : 'contentOfMemberInfo'},{'id' : 'groupPopUp'},{'id' : 'groupPopUp'},{'id' : 'directoryList'}]},{'id' : 'tab3','childs' : [{'id' : 'content7'},{'id' : 'bulletinLog'}]},{'id' : 'tab4','childs' : [{'id' : 'content8'},{'id' : 'messageLog'}]}]}".evalJSON();
var divContents = "{'tabs' : [{'id' : 'tab1','childs' : [{'id' : 'welcome','name' : 'Welcome'},{'id' : 'content9','name' : 'Edit Me'},{'id' : 'content6','name' : 'Group'},{'id' : 'contentOfMemberInfo','name' : 'Member'},{'id' : 'groupPopUp','name':'Create Group'},{'id' : 'directoryList','name':'Directory'},{'id' : 'createBulletin','name':'Create Bulletin'}]},{'id' : 'tab2','childs' : [{'id' : 'content5'}]},{'id' : 'tab3','childs' : [{'id' : 'content7'}]},{'id' : 'tab4','childs' : [{'id' : 'content8'}]}]}".evalJSON();
var _objCollect = new ObjectCollection();
for (var i = 1; i<5; i++){
	var obj = new ObjContents('tab'+(i));
	var _tabs = divContents.tabs[i-1];
	if (_tabs){
		var childs = _tabs.childs;
		for (var x=0;x<childs.length; x++){
			var _id = childs[x].id;
			var myContents = new ObjContents(_id); 
			if (childs[x].name!=null)
				myContents.setName(childs[x].name);
			obj.addContent(myContents);
		}
	}
	_objCollect.addContent(obj);
}


// JavaScript Document

function showTabHandler(_tabNumber,contentId){
	updateTabStatus(_tabNumber,contentId);
	showTab(_tabNumber,contentId);
}

function getURL( name ){

  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return null;
  
  return results[1];
}

function showFirstTabHandler(_tabNumber){
	var contentId = "directoryList";
	for (var i = 0; i<_objCollect.length+1 ;i++){
		if (i == _tabNumber){
			var objs = _objCollect.getContent('tab'+(i));
			if (objs)
				objs.active();
			
			for (var x = 0; x<objs.length; x++){
				var _content = objs.objects[x];
				if (_content){
					
					var firstTabContent = $('firstTab').innerHTML;
					
					if (getURL("isCreate")!=null){
						firstTabContent = "Create Group"
					}
					
					//check if isMember is not null
					//if (firstTabContent == "Member")
						//firstTabContent = "Group";
				
					if (_content.name == firstTabContent){
						_content.show();
						content = _content.id;
						if (_content.name == "Directory"){
							showDirectory();
						}
						continue;
					}
					_content.hide();
				}
			}
		}else{
			var objs = _objCollect.getContent('tab'+(i));
			if (objs){
				objs.inactive();	
				
				for (var x = 0; x<objs.length; x++){
					var _content = objs.objects[x];
					if (_content){
						_content.hide();
					}
				}
			}
		}
	} 
	tabActive = _tabNumber;
	updateTabStatus(_tabNumber,content);
	Chat.stopTimer();
}

function showTab(_tabNumber,contentId){

	tabActive = _tabNumber;
	content = contentId;

	if (toggle == 1) {
		dropDownActive();
	}

	//welcome sreen
	if (_tabNumber == 0){
		new ObjContents(contentId).show();
		return;	
	}

	//if (Chat.periodicalEvent!=null)
	//	Chat.periodicalEvent.stop();
	//if (timerRunning)
		Chat.stopTimer();
	
	
	for (var i = 0; i<_objCollect.length+1 ;i++){
		if (i == _tabNumber){
			var objs = _objCollect.getContent('tab'+(i));
			if (objs)
				objs.active();
			
			if (_tabNumber == 4){
				Chat.doChatStart();
			}else{
				Chat.stopTimer();
			}
			
			for (var x = 0; x<objs.length; x++){
				var _content = objs.objects[x];
				if (_content){
					if (_content.id == contentId){
						if (_tabNumber==1){
							if (_content.name !=null){
								$('firstTab').innerHTML = _content.name;
								if (getURL("isCreate")!=null){
									if (getURL("isCreate")=="false"){
										$('firstTab').innerHTML = "Edit Group";
									}
								}
							}
						}
						_content.show();
						//if (_content.id == "directoryList"){
						//	showDirectory();
						//}
						
						continue;
					}
					_content.hide();
					//if (_content.name!=null){
					//	if (_content.name == $('firstTab').innerHTML){
					//		_content.show();
							//if (_content.id == "directoryList"){
							//	showDirectory();
							//}
					//		continue;
					//	}
					//}
				}
			}
		}else{
			var objs = _objCollect.getContent('tab'+(i));
			if (objs){
				objs.inactive();	
				
				for (var x = 0; x<objs.length; x++){
					var _content = objs.objects[x];
					if (_content){
						_content.hide();
					}
				}
			}
		}
	} 
}

function setElementsIds(_ids, _class, isDiv){
	if (!document.getElementById(_ids))
		return;
		
	if (isDiv){	
		document.getElementById(_ids).style.display = _class;
		if (_ids == 'content8' && _class == 'block'){
			//Chat.doChatStart();
		}
		return;
	}

	document.getElementById(_ids).className = _class;	
	
}

function clickFunction(listId,path,isGroup,groupId) {
	var id=listId.id;
	if (isGroup){
		window.location=path+"?groupId="+ id+"&tab="+tabActive+"&content="+content;
		//window.location=path+"?groupId="+ id+"&tab=1&content=content6";
	}else{
		window.location=path+"?groupId="+ groupId+"&memberId="+ id+"&tab=1";
	}
}

function clickDirectoryFunction(_id,path,_tab,_content){
	window.location=path+"?groupId="+ _id+"&tab="+_tab+"&content="+_content;
}

function clickMemberFunction(id,path,isGroup,groupId){
	window.location=path+"?groupId="+ groupId+"&memberId="+ id+"&tab=1";
}

function groupLocation(path, isCreate) {
		window.location=path+"?isCreate="+isCreate+"&tab=1";
}

function showRegistration(){
	document.getElementById('introduction_text').style.display = "none";
	document.getElementById('registration_form').style.display = "block";
}

function showDefault(){
	document.getElementById('introduction_text').style.display = "block";
	document.getElementById('registration_form').style.display = "none";
}

function showInfo(){
	document.getElementById('introduction_text').style.display = "none";
	document.getElementById('registration_form').style.display = "none";
	
}

function clearFields(){
	if ($('groupNameText')==null)
		return;
	$('groupNameText').value = "";
	$('descriptionText').value = "";
}

function hideGroupName(){
	document.getElementById('groupName').style.display = "none";
}

function showGroupName(){
	document.getElementById('groupName').style.display = "block";
	clearFields();
}

function cancelInvitation(){
	document.getElementById('emailAdd').value = "";
}

function showSelectData() {
	document.getElementById('selectDataList').style.display = "block";
}

function setStickerValue(_val) {
	document.getElementById("selectSticker").value = _val;
	document.getElementById("dropText").innerHTML = "Icon " + _val;
	dropDownInactive();
}

function dropOnBlur() {
	if (isSelect != 1) {
		if ($('ulSelectBox')){
			document.getElementById('ulSelectBox').style.display = "none";
			toggle = 0;
		}
	}
	isSelect = 0;
}

function dropDownActive() {
	isSelect = 1;
		if (toggle == 1) {
			document.getElementById('ulSelectBox').style.display = "none";
			toggle = 0;
		} else {
			document.getElementById('ulSelectBox').style.display = "block";
			toggle++;
		}
}

function clickLog(path,isMsg){
	var _tab = 3;
	if (isMsg){
		_tab = 4;
	}
	window.location = path+"?isMsg="+isMsg+"&tab="+_tab;
}

function updateTabStatus(_tabIndex,_content){
	if (_tabIndex == 1){
		if ($('firstTab').innerHTML.toLowerCase().match("group")){
			_content = "content6";
		}
	}
	new Ajax.Request(tabHandler, { method: 'get',parameters: 'tabIndex='+_tabIndex+'&tabContent='+_content, onComplete: null});
}

function onLoginSubmit(){
	var _date = new MyDate(new Date());
	$('clientTime').value = _date.getStringFormatDate();
	return true;
}


function setCookie(){
	document.cookie = 'popup='+escape(remainPopup)+';path=/';
	//alert(remainPopup)
}

function showMe(){
	//alert(getCookies("popup"));
}

function getCookie ( cookie_name ){
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}

function getCookies(name) {
    var theCookies = document.cookie.split(/[; ]+/);
    for (var i = 0 ; i < theCookies.length; i++) {
        var aName = theCookies[i].substring(0,theCookies[i].indexOf('='));
        if (aName == name) {
            return unescape(theCookies[i]);
        }
    }
}

function getParentURL() {
	//new Ajax.Request(getParentPath, { method: 'get', parameters: 'url='+parent.location.href,
	//				onComplete: function(e){
	//				}
	//			});
}

function validateBulletinFields(){
	if(isEmpty($('bulletin_title').value)){
		messageInfoDisplay('errorMsg',"Title is required.","block");	
		$('bulletin_title').focus();
		return false;
	}else if(isEmpty($('bulletin_message').value)){
		messageInfoDisplay('errorMsg',"Content is required.","block");	
		$('bulletin_message').focus();
		return false;
	}
	
	return true;
}


