/*
THIS LICENSE HEADER MUST REMAIN INTACT FOR LEGAL USE!
Pagefusion; Pagefusion Core Platform
Description: A powerful, easy-to-use, and highly customizable content management system.
Copyright (C), 2006-2007 Pagefusion.com, All Rights Reserved.
Author: Chris Davis, chris@pagefusion.com
Support portal: www.pagefusion.com

Filename: /components/toggle/toggle.js
Description: (see schema.xml for a detailed description)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/

// toggle
function toggle_target(target) {
	var e = document.getElementById(target);
	var t = e.className;
	if (t.match('toggle_none')) {
		t = t.replace(/toggle_none/gi, 'toggle_row');
	} else {
		t = t.replace(/toggle_row/gi, 'toggle_none');
	} // if
	e.className = t;
} // function

// toggle div
function toggle_object(source, target) {
	var e = document.getElementById(target);
	var t = e.className;
	if (t.match('toggle_none')) {
		t = t.replace(/toggle_none/gi, 'toggle_inline');
	} else {
		t = t.replace(/toggle_inline/gi, 'toggle_none');
	} // if
	e.className = t;
	
	// change toggler source plus/minus
	var f = document.getElementById(source);
	var u = f.className;
	if (u.match('toggle_minus')) {
		u = u.replace(/toggle_minus/gi, 'toggle_plus');
	} else {
		u = u.replace(/toggle_plus/gi, 'toggle_minus');
	} // if
	f.className = u;
} // function

// toggle block
function toggle_block(source, target) {
	var e = document.getElementById(target);
	var t = e.className;
	if (t.match('toggle_none')) {
		t = t.replace(/toggle_none/gi, 'toggle_block');
	} else {
		t = t.replace(/toggle_block/gi, 'toggle_none');
	} // if
	e.className = t;
	
	// change toggler source plus/minus
	var f = document.getElementById(source);
	var u = f.className;
	if (u.match('toggle_minus')) {
		u = u.replace(/toggle_minus/gi, 'toggle_plus');
	} else {
		u = u.replace(/toggle_plus/gi, 'toggle_minus');
	} // if
	f.className = u;
} // function

// toggle table row
function toggle_row(source, target) {
	var e = document.getElementById(target);
	var t = e.className;
	if (t.match('toggle_none')) {
		t = t.replace(/toggle_none/gi, 'toggle_row');
	} else {
		t = t.replace(/toggle_row/gi, 'toggle_none');
	} // if
	e.className = t;
	
	// change toggler source plus/minus
	var f = document.getElementById(source);
	var u = f.className;
	if (u.match('toggle_minus')) {
		u = u.replace(/toggle_minus/gi, 'toggle_plus');
	} else {
		u = u.replace(/toggle_plus/gi, 'toggle_minus');
	} // if
	f.className = u;
} // function

// toggle table row
function toggle_row_close(source, target, source2, target2) {
	var e = document.getElementById(target);
	var t = e.className;
	if (t.match('toggle_none')) {
		t = t.replace(/toggle_none/gi, 'toggle_row');
	} else {
		t = t.replace(/toggle_row/gi, 'toggle_none');
	} // if
	e.className = t;
	
	// change toggler source plus/minus
	var f = document.getElementById(source);
	var u = f.className;
	if (u.match('toggle_minus')) {
		u = u.replace(/toggle_minus/gi, 'toggle_plus');
	} else {
		u = u.replace(/toggle_plus/gi, 'toggle_minus');
	} // if
	f.className = u;
	
	// toggle other source
	var e = document.getElementById(target2);
	var t = e.className;
	if (t.match('toggle_none')) {
		t = t.replace(/toggle_none/gi, 'toggle_row');
	} else {
		t = t.replace(/toggle_row/gi, 'toggle_none');
	} // if
	e.className = t;
	
	// change toggler source plus/minus
	var f = document.getElementById(source2);
	var u = f.className;
	if (u.match('toggle_minus')) {
		u = u.replace(/toggle_minus/gi, 'toggle_plus');
	} else {
		u = u.replace(/toggle_plus/gi, 'toggle_minus');
	} // if
	f.className = u;
} // function

function toggle_group_cookie(cid, id_str) {
	var id_array=id_str.split(",");
	for (var i=0; i<id_array.length; i++) {
		var e = document.getElementById(id_array[i]);
		var t = e.className;
		if (e) {
			if (t.match('toggle_none')) {
				t = t.replace(/toggle_none/gi, 'toggle_row');
				if (Get_Cookie(id_array[i]))  { Delete_Cookie(id_array[i], '/', ''); } // if
				Set_Cookie(id_array[i], 'Y', 0, '/', '', '');
			} else {
				t = t.replace(/toggle_row/gi, 'toggle_none');
				if (Get_Cookie(id_array[i]))  { Delete_Cookie(id_array[i], '/', ''); } // if
				Set_Cookie(id_array[i], 'N', 0, '/', '', '');
			} // if
			e.className = t;
		} // if
	} // for
	
	// change toggler id plus/minus
	var f = document.getElementById(cid);
	var u = f.className;
	if (u.match('toggle_minus')) {
		u = u.replace(/toggle_minus/gi, 'toggle_plus');
	} else {
		u = u.replace(/toggle_plus/gi, 'toggle_minus');
	} // if
	f.className = u;
} // function

function toggle_children_cookie(cid, row_id, id_str, level) {
	
	// change toggler state
	var f = document.getElementById(cid);
	var u = f.className;
	var parent_state;
	
	if (f) {
		if (u.match('toggle_minus')) {
			u = u.replace(/toggle_minus/gi, 'toggle_plus'); // collapsed
			parent_state = "N";
			if (Get_Cookie(cid))  { Delete_Cookie(cid, '/', ''); } // if
			Set_Cookie(cid, 'N', 0, '/', '', '');
		} else {
			u = u.replace(/toggle_plus/gi, 'toggle_minus');  // expanded
			parent_state = "Y";
			if (Get_Cookie(cid))  { Delete_Cookie(cid, '/', ''); } // if
			Set_Cookie(cid, 'Y', 0, '/', '', '');
			
			if (Get_Cookie(row_id))  { Delete_Cookie(row_id, '/', ''); } // if
			Set_Cookie(row_id, 'Y', 0, '/', '', '');
		} // if
		
		f.className = u;
	} // if
	
	var id_array=id_str.split(",");
	for (var i=0; i<id_array.length; i++) {
		var e = document.getElementById(id_array[i]);
		var t = e.className;
		
		if (e) {
			var id_level = id_array[i].substring(0,1);
			var level_plus = level+1;
			
			if (parent_state == "Y") {
				if (id_level == level_plus) {
					t = t.replace(/toggle_none/gi, 'toggle_row');
				} else {
					if (Get_Cookie(id_array[i]) == "Y") {
						t = t.replace(/toggle_none/gi, 'toggle_row');
					} // if
				} // if
			} else {
				t = t.replace(/toggle_row/gi, 'toggle_none');
			} // if
			
			if (id_level == level_plus) {
				if (Get_Cookie(id_array[i]))  { Delete_Cookie(id_array[i], '/', ''); } // if
				if (parent_state == "Y") {
					Set_Cookie(id_array[i], 'Y', 0, '/', '', '');
				} else {
					Set_Cookie(id_array[i], 'N', 0, '/', '', '');
				} // if
			} // if
			
			e.className = t;
		} // if
	} // for
} // function

// toggle table row, keep state using cookie
function toggle_row_cookie(source, target) {

	var e = document.getElementById(target);
	var t = e.className;
	
	if (e) {
		if (t.match('toggle_none')) {
			t = t.replace(/toggle_none/gi, 'toggle_row');
			if (Get_Cookie(target))  { Delete_Cookie(target, '/', ''); } // if
			Set_Cookie(target, 'Y', 0, '/', '', '');
		} else {
			t = t.replace(/toggle_row/gi, 'toggle_none');
			if (Get_Cookie(target))  { Delete_Cookie(target, '/', ''); } // if
			Set_Cookie(target, 'N', 0, '/', '', '');
		} // if
		e.className = t;
	} // if
	
	// change toggler source plus/minus
	var f = document.getElementById(source);
	var u = f.className;
	if (u.match('toggle_minus')) {
		u = u.replace(/toggle_minus/gi, 'toggle_plus');
	} else {
		u = u.replace(/toggle_plus/gi, 'toggle_minus');
	} // if
	f.className = u;
} // function

// toggle class
function toggle_class(target, targetClass) {
	document.getElementById(target).className = targetClass;
} // function

// toggle class, set cookie
function toggle_class_cookie(target, targetClass, cookie_value) {
	document.getElementById(target).className = targetClass;
	if (Get_Cookie(target))  { Delete_Cookie(target, '/', ''); } // if
	Set_Cookie(target, cookie_value, 0, '/', '', '');
} // function

// toggle horizontal grip row
function grip_horizontal(source, target) {
	var e = document.getElementById(target);
	var t = e.className;
	if (t.match('toggle_none')) {
		t = t.replace(/toggle_none/gi, 'toggle_row');
	} else {
		t = t.replace(/toggle_row/gi, 'toggle_none');
	} // if
	e.className = t;
	
	// change toggler
	var f = document.getElementById(source);
	var u = f.className;
	if (u.match('grip horizontal closed')) {
		u = u.replace(/grip horizontal closed/gi, 'grip horizontal opened');
	} else {
		u = u.replace(/grip horizontal opened/gi, 'grip horizontal closed');
	} // if
	f.className = u;
} // function

// toggle vertical grip row
function grip_vertical(source, target) {
	var e = document.getElementById(target);
	var t = e.className;
	if (t.match('toggle_none')) {
		t = t.replace(/toggle_none/gi, 'toggle_row');
	} else {
		t = t.replace(/toggle_row/gi, 'toggle_none');
	} // if
	e.className = t;
	
	// change toggler
	var f = document.getElementById(source);
	var u = f.className;
	if (u.match('grip vertical closed')) {
		u = u.replace(/grip vertical closed/gi, 'grip vertical opened');
	} else {
		u = u.replace(/grip vertical opened/gi, 'grip vertical closed');
	} // if
	f.className = u;
} // function

function closeit(my_id, toggle_class) {
	object=document.getElementById(my_id); object.className=toggle_class;
} // function

function toggle_object_class(object_name, class_name) {
	object=document.getElementById(object_name); object.className=class_name;
} // function
