// Creates the hide/show events for the all the lists in the group component

expandListOpen = new Array();
var prevHeight;
var haltDouble = false;
function createExpandList(elm){

	expandListOpen[elm] = true;

	$(elm).getElement('.group-inners-content').id = elm + "-content";

	$(elm).getElement('.group-inner-name').addEvent('click',function(){

		if(!haltDouble)

		{

			haltDouble = true;

			if(expandListOpen[elm])

			{

				$(elm).getElement('.group-inner-name').style.backgroundImage = "url(images/list_collapsed.png)";

				compactElement(elm,21,'auto');

				compactElement(elm + '-content',0,'auto');

				expandListOpen[elm] = false;

			}

			else

			{

				$(elm).getElement('.group-inner-name').style.backgroundImage = "url(images/list_expanded.png)";

				$(elm + '-content').style.height = 'auto';

				$(elm + '-content').style.height = $(elm + '-content').scrollHeight + 'px';

				expandElement(elm,'auto');

				expandListOpen[elm] = true;

			}

			( function() { haltDouble = false; } ).delay(600);

		}

	});

}



// Toggle between camera view on/off

function switchLive(elm){

	if(elm.get('class') == "group-list-live")

	{

		elm.addClass("group-list-not-live");

		elm.removeClass("group-list-live");

	//	elm.getElement('span').innerHTML = "(not live)";

	}

	else if(elm.get('class') == "group-list-not-live")

	{

		elm.addClass("group-list-live");

		elm.removeClass("group-list-not-live");

	//	elm.getElement('span').innerHTML = "(live)";

	}

	else if(elm.get('class') == "group-list-not-live-selected")

	{

		elm.addClass("group-list-live-selected");

		elm.removeClass("group-list-not-live-selected");

	//	elm.getElement('span').innerHTML = "(live)";

	}

	else if(elm.get('class') == "group-list-live-selected")

	{

		elm.addClass("group-list-not-live-selected");

		elm.removeClass("group-list-live-selected");

	//	elm.getElement('span').innerHTML = "(live)";

	}

}



// Create select feature to group list

var currentSelection;

function createGroupSelect(elm){

	elm.addEvent('click',function(){

		if(currentSelection)

		{

			if(currentSelection.get('class') == "group-list-live-selected")

			{

				currentSelection.addClass("group-list-live");

				currentSelection.removeClass("group-list-live-selected");

			}

			if(currentSelection.get('class') == "group-list-not-live-selected")

			{

				currentSelection.addClass("group-list-not-live");

				currentSelection.removeClass("group-list-not-live-selected");

			}

			if(currentSelection.get('class') == "group-go-live-selected")

			{

				currentSelection.addClass("group-go-live");

				currentSelection.removeClass("group-go-live-selected");

			}

			if(currentSelection.get('class') == "group-go-not-live-selected")

			{

				currentSelection.addClass("group-go-not-live");

				currentSelection.removeClass("group-go-not-live-selected");

			}

			currentSelection.style.backgroundColor = "#FFF";

		}

		currentSelection = elm;

		if(currentSelection.get('class') == "group-list-live")

		{

			currentSelection.addClass("group-list-live-selected");

			currentSelection.removeClass("group-list-live");

		}

		if(currentSelection.get('class') == "group-list-not-live")

		{

			currentSelection.addClass("group-list-not-live-selected");

			currentSelection.removeClass("group-list-not-live");

		}

		if(currentSelection.get('class') == "group-go-live")

		{

			currentSelection.addClass("group-go-live-selected");

			currentSelection.removeClass("group-go-live");

		}

		if(currentSelection.get('class') == "group-go-not-live")

		{

			currentSelection.addClass("group-go-not-live-selected");

			currentSelection.removeClass("group-go-not-live");

		}

		currentSelection.style.backgroundColor = "#aedee4";

	});

}



// Add a camera to your list of cameras

function addCamera(){

	var newCam = createNewElm('li','group-list-not-live');

	newCam.innerHTML = "<div class=\"group-toggle-box Tips\" rel=\"show/hide video\" onclick=\"switchLive(this.parentNode);\"> </div>"+ $('group-add-camera').value+" <span>(not live)</span>";

	$('my-cameras').getElement('.group-list').appendChild(newCam);

	// give the element an ID since IE6 does not accept newCam as a valid element
	newCam.id = "createSelect";
	createGroupSelect($(newCam.id));
	// remove the given ID so no error with repeat ids when doing the createGroupSelect function
	newCam.id = "";
	
	expandGroup('camera','my-cameras','group-add-camera-container','auto',0,'open');

	createTips('Tips');

}



// Remove items from any list on the group component asking for confirmation to delete and alerts if nothing in that section is selected

function removeListItem(section){

	if(section == "camera")

	{

		if(currentSelection && currentSelection.parentNode.parentNode.id == "my-cameras-content")

		{

			if(confirm("Are you sure you want to delete this camera?"))

			{

				currentSelection.dispose();

				currentSelection = false;

				compactGroup('my-cameras');

			}

		}

		else

			alert("You must select a camera.");

	}

	if(section == "invite")

	{

		if(currentSelection && currentSelection.parentNode.parentNode.id == "group-invited"+currentGroupNum+"-content")

		{

			if(confirm("Are you sure you want to remove this invite?"))

			{

				currentSelection.dispose();

				currentSelection = false;

				if($($('group-invite-container').parentNode.parentNode.id + '-content'))
					compactGroup($('group-invite-container').parentNode.parentNode.id);

			}

		}

		else

			alert("You must select a invite.");

	}

}



// Add to the invite list

function addInvite(){

	var newCam = createNewElm('li',false,false,$('group-invite-user').value);

	$('group-invited'+currentGroupNum).getElement('.group-list').appendChild(newCam);

	// give the element an ID since IE6 does not accept newCam as a valid element
	newCam.id = "createSelect";
	createGroupSelect($(newCam.id));
	// remove the given ID so no error with repeat ids when doing the createGroupSelect function
	newCam.id = "";

	expandGroup('invite',$('group-invite-container').parentNode.parentNode.id,'group-invite-container','auto',0,'open');

}



// Switch between different groups (group 0 is the group list)

var currentGroupNum = 1;
var lastGroupNum = 0;
var currentSwitching = false;
function switchGroups(group){

	if(!morphs['groups-controls'])
		createMorph('groups-controls',1000);
	if(!morphs['groups-all'])
		createMorph('groups-all',1000);

	if(!currentSwitching)
	{
		currentSwitching = true;

		compactAllGroup('all');

		if(!group || group == 0)

		{

			var camClone = $('my-cameras').clone().setStyles({'margin-top':'4px'}).inject('group0', 'top');

			$('group0').style.height = $('group'+currentGroupNum).scrollHeight + 'px';

			$('group-go-back-div').onclick = function () { switchGroups(lastGroupNum); }

			$('group-full-expand').innerHTML = "Group List";

			$('group-top-second1').style.height = 0 + 'px';

			$('group-top-second2').style.height = 'auto';

			$('groups-controls').style.left = $('group-wrapper').offsetWidth + 'px';
			
			$('groups-controls').style.zIndex = 1;

			$('groups-all').style.zIndex = 0;

			morphs['groups-controls'].start({

				'left': 0

			}).chain(function(){

				$('my-cameras').inject('group0', 'top');

				camClone.dispose();

				$('group-profile-container').inject('group0', 'top');

				$('group-invite-container').inject('group-holder', 'top');

				$('group-invites-container').inject('group-holder', 'top');

				$('group'+currentGroupNum).style.height = '0';

				$('group0').style.height = 'auto';
				
				$('group0').style.height = $('group0').scrollHeight + 'px';

				lastGroupNum = currentGroupNum;

				currentGroupNum = 0;

				adjustBar('group');

				currentSwitching = false;

			});

		}

		else

		{

			var camClone = $('my-cameras').clone().setStyles({'margin-top':'4px'}).inject('group'+group, 'top');

			$('group'+group).style.height = 'auto';

			$('group'+group).style.height = $('group'+group).scrollHeight + 'px';

			$('group0').style.height = $('group'+group).scrollHeight + 'px';

			$('group-invites-container').inject('group'+group, 'top');

			$('group-invite-container').inject('group-invited'+group+'-content', 'top');

			var groupName;

			$('group-list-groups').getElements('li').each(function(elm){

				if(elm.getAttribute("rel") == "group"+group)

					groupName = elm.innerHTML.split("</div>");

			});

			$('group-full-expand').innerHTML = groupName[1];

			$('group-top-second2').style.height = 0 + 'px';

			$('group-top-second1').style.height = 'auto';

			$('groups-controls').style.left = 0;

			$('groups-all').style.zIndex = 0;
			
			$('groups-controls').style.zIndex = 1;

			morphs['groups-controls'].start({

				'left': $('group-wrapper').offsetWidth + 'px'

			}).chain(function(){

				$('groups-controls').style.left = '100%';

				$('my-cameras').inject('group'+group, 'top');

				camClone.dispose();

				$('group-profile-container').inject('group'+group, 'top');

				$('group-invites-container').inject('group'+group, 'top');

				$('group'+currentGroupNum).style.height = '0';

				currentGroupNum = group;

				adjustBar('group');

				currentSwitching = false;

			});

		}

	}
}



// creates a new group

var totalGroups = 0;

function createNewGroup(){

	totalGroups++;

	var newGroupLI = createNewElm('li','group-go-live');
	
	newGroupLI.setAttribute('rel','group'+totalGroups);

	newGroupLI.innerHTML = "<div class=\"group-toggle-box groups-li\" onclick=\"switchGroups("+totalGroups+");\"> </div>"+$('group-add-group').value+"(0/0)";

	$('group-list-groups').appendChild(newGroupLI);

	var newGroup = createNewElm('div','group-sections','group'+totalGroups);

	var newGroupInvite = createNewElm('div','group-inners rounded','group-invited'+totalGroups);

	newGroupInvite.innerHTML = "<div class=\"group-inners-top\"><div class=\"group-inner-controls\"><div class=\"group-blank\"> </div><div class=\"group-add\" onclick=\"expandGroup('invite','group-invited"+totalGroups+"','group-invite-container','auto',0,'close');\"> </div><div class=\"group-remove\" onclick=\"removeListItem('invite');\"> </div></div><div class=\"group-inner-name\">Invited</div></div><div class=\"group-inners-content\"><ul class=\"group-list\"></ul></div>";

	newGroup.appendChild(newGroupInvite);

	var newGroupOffline = createNewElm('div','group-inners rounded','group-offline'+totalGroups);

	newGroupOffline.innerHTML = "<div class=\"group-inners-top\"><div class=\"group-inner-controls\"> </div><div class=\"group-inner-name\">Offline</div></div><div class=\"group-inners-content\"><ul class=\"group-list\"></ul></div>"

	newGroup.appendChild(newGroupOffline);

	$('groups-all').appendChild(newGroup);

	

	// Nifty("div#"+newGroupInvite.id, "transparent");

	// Nifty("div#"+newGroupOffline.id, "transparent");

	createExpandList(newGroupInvite.id);

	createExpandList(newGroupOffline.id);

	// give the element an ID since IE6 does not accept newGroupLI as a valid element
	newGroupLI.id = "createSelect";
	createGroupSelect($(newGroupLI.id));
	// remove the given ID so no error with repeat ids when doing the createGroupSelect function
	newGroupLI.id = "";

	expandGroup('group',false,'group-add-group-container','auto',0,'open');

}

// removes a group

function removeGroup(){

	if(currentSelection && currentSelection.parentNode.id == "group-list-groups")

	{

		if(confirm("Are you sure you want to remove this group?"))

		{

			currentSelection.dispose();

			$(currentSelection.getAttribute('rel')).dispose();

			currentSelection = false;

		}

	}

	else

		alert("You must select a group.");

}

// makes the enter key act like pressing the add/send buttons when adding groups, cameras, or invites

function createEnterKeyEvents(){

	$('group-add-camera').addEvent('keyup',function(e){

		if(e.key == "enter")

			addCamera();

	});

	$('group-invite-user').addEvent('keyup',function(e){

		if(e.key == "enter")

			addInvite();

	});

	$('group-add-group').addEvent('keyup',function(e){

		if(e.key == "enter")

			createNewGroup();

	});

}

function compactGroup(sect)
{
	(function(){
		$(sect + '-content').style.height = $(sect + '-content').scrollHeight + 'px';
		$(sect).style.height = $(sect).scrollHeight + 'px';
		$('group'+currentGroupNum).style.height = $('group'+currentGroupNum).scrollHeight + 'px'; 
	}).delay(500);
}

function compactAllGroup(selectElm){
	if(selectElm != 'profile')
	{
		compactElement('group-profile-container',0,'auto');
	}
	if(selectElm != 'advanced')
	{
		compactElement('group-advanced-camera-container',0,'auto');
		compactGroup('my-cameras');
	}
	if(selectElm != 'group')
	{
		compactElement('group-add-group-container',0,'auto');
	}
	if(selectElm != 'camera')
	{
		compactElement('group-add-camera-container',0,'auto');
		compactGroup('my-cameras');
	}
	if(selectElm != 'invite')
	{
		compactElement('group-invite-container',0,'auto');
		if($($('group-invite-container').parentNode.parentNode.id + '-content'))
			compactGroup($('group-invite-container').parentNode.parentNode.id);
	}
	if(selectElm != 'invites')
	{
		compactElement('group-invites-container',0,'auto');
	}
}

function expandGroup(selectElm,sect,elm,eHgt,cHgt,start,auto)
{ 
	if(sect)
	{
		$(sect).style.height = 'auto';
		$(sect + '-content').style.height = 'auto';
	}
	$('group'+currentGroupNum).style.height = 'auto';
	compactAllGroup(selectElm);
	toggleElement(elm,eHgt,cHgt,start,auto);
	if(sect)
		compactGroup(sect);
}

function expandAdvControl(section)
{
	$('my-cameras').style.height = 'auto';
	$('my-cameras-content').style.height = 'auto';
	$('group'+currentGroupNum).style.height = 'auto';
	$('group-advanced-camera-container').style.height = 'auto';
	if(section == "camera")
	{
		toggleElement('group-camera-change',247,58,'close');
		compactElement('group-mic-settings',58);
	}
	if(section == 'mic')
	{
		toggleElement('group-mic-settings',221,58,'close');
		compactElement('group-camera-change',58);
	}
	(function(){ $('group-advanced-camera-container').style.height = $('group-advanced-camera-container').scrollHeight + 'px'; }).delay(500);
	compactGroup('my-cameras');
}


// Fires the functions when the page finishes loading

window.addEvent('domready', function(){

	createDrag('group');
	setComponentLimit('group', 26);

	createScroll('group');

	changeResize('group','leftmod');

	createSlider('group-vid-quality','horizontal',10);

	createSlider('group-mic-volume','horizontal',20);

	$('group1').style.height = 'auto';

	$('group1').style.height = $('group1').scrollHeight + 'px';

	$('group-top-second1').style.height = 'auto';

	totalGroups = $('group0').getElements('li').length;

	createEnterKeyEvents();

	createScroll('group-invites');

	$$('.group-inners').each(function(inners){
	
		createExpandList(inners.id);

	});

	$$('.group-list').each(function(lists){
	
		lists.getElements('li').each(function(listItems){

			createGroupSelect(listItems);

		});

	});

	expandGroup('profile',false,'group-profile-container','auto',0,'open');
	expandGroup('invites',false,'group-invites-container','auto',0,'open');
	expandGroup('advanced','my-cameras','group-advanced-camera-container','auto',0,'open');
	expandGroup('group',false,'group-add-group-container','auto',0,'open');
	expandGroup('invite','group-invited1','group-invite-container','auto',0,'open');
	expandGroup('camera','my-cameras','group-add-camera-container','auto',0,'open');
	compactAllGroup();
	
});