var inUse = false;

var swap_id = null;
var swap_ordernum = null;
var swap_type = null;

var url = '/cms/cms/remote.php';
var afterTransfer = 'window.location.href = window.location.href';

function pickoption(it, subid, type){
	switch (it.value){
		case 'insertGallery':
			connect('gallery', 'type=1&subid=' + subid);
			break;	
		case 'delsub':
			var decision = window.confirm('You are deleting this entire subblock.  Are you sure?');
			if (decision) connect('delsub', 'subid=' + subid);
			break;
		case 'edittitle':
			popup('/cms/cms/edit.php?id=' + subid + '&action=editsubtitle', 'editor', 400, 75);
			it.selectedIndex = 0;
			break;
		case 'edittext':
			popup('/cms/cms/edit.php?subid=' + subid + '&action=editsub&mode=text', 'editor', editor_width, editor_height);
			break;
		case 'edithtml':
			popup('/cms/cms/edit.php?subid=' + subid + '&action=editsub&mode=html', 'editor', editor_width, editor_height);
			break;
		case 'delblock':
			var decision = window.confirm('You are deleting this entire block.  Are you sure?');
			if (decision) connect('delblock', 'blockid=' + subid);
			break;
		case 'newsub':
			it.style.background = '#BDFFC1';
			connect('newsub', 'blockid=' + subid);
			break;
		case 'editblocktitle':
			popup('/cms/cms/edit.php?id=' + subid + '&action=editblocktitle', 'editor', 400, 75);
			break;
	}

	it.selectedIndex = 0;
}


function gallerySelect(it, subid, type){
	switch (it.value){
		case 'gallery1':
			it.style.background = '#BDFFC1';
			connect('gallery', 'type=1&subid=' + subid);
			break;	
		case 'gallery2':
			it.style.background = '#BDFFC1';
			connect('gallery', 'type=2&subid=' + subid);
			break;
		case 'gallery3':
			it.style.background = '#BDFFC1';
			connect('gallery', 'type=3&subid=' + subid);
			break;
		case 'gallery4':
			it.style.background = '#BDFFC1';
			connect('gallery', 'type=4&subid=' + subid);
			break;
		case 'delgallery':
			var decision = window.confirm('You are deleting the gallery in this subblock.  Are you sure?');
			if (decision) {
				it.style.background = '#BDFFC1';
				connect('delgallery', 'subid=' + subid);
			}
			break;
		case 'editgallery':
			if (type == 0){
				alert("Please select a gallery type first.");
				return;
			}
			popup('/cms/cms/editgallery.php?subid=' + subid, 'editor', editor_width, editor_height);
			break;
	}

	it.selectedIndex = 0;
}


function logout(it){
	it.style.background = "url('/cms/cms/images/greengradient.jpg')";
	connect('logout', '');
}

function editlinks(pageid, location, scope){
	popup('/cms/cms/editlinks.php?location=' + location + '&pageid=' + pageid + '&scope=' + scope, 'editor', 640, 480);
}


function newblock(it, pageid, location){
	it.style.background = "url('/cms/cms/images/greengradient.jpg')";
	connect('newblock', 'pageid=' + pageid + '&location=' + location);
}


function reorder(type, id, ordernum, it){
	if (inUse){
		if (swap_type == type){		// this is the second press of a swap button

			if (id == swap_id){	// cancel swap if you press the same button twice
				swap_id = null;
				swap_ordernum = null;
				swap_type = null;
				it.style.background = "url('/cms/cms/images/bluegradient.jpg')";
				inUse = false;
				return;
			}

			else {	// actually perform the reorder...
				it.style.background = "url('/cms/cms/images/greengradient.jpg')";
				connect('reorder', 'reorder=' + type + '|' + swap_id + '|' + swap_ordernum + '|' + id + '|' + ordernum);			
			}
		}

		else return;	// swap block and swap subblock are getting mixed up...return;
	}

	else {	// this is the first press of s swap button
		inUse = true;
		it.style.background = "url('/cms/cms/images/greengradient.jpg')";
		swap_id = id;
		swap_ordernum = ordernum;
		swap_type = type;
		return;
	}
	

//	 return;
//	it.style.background = "url('/cms/cms/images/greengradient.jpg')";

/*
	if (id == firstsubid){
		firstsubid = null;
		firstsubordernum = null;		
		it.style.background = "url('/cms/cms/images/bluegradient.jpg')";
		return;
	}

	if (firstsubid == null){
		firstsubid = id;
		firstsubordernum = ordernum;
		return;
	}
*/

}


// PAGE FUNCTIONS ********************************************************

function edit_page(pageid){
	popup('/cms/cms/mod_editpage.php?edit=true&pageid=' + pageid, 'editpage', 500, 500);
}

function new_page(pageid){
	popup('/cms/cms/mod_editpage.php?new=true&pageid=' + pageid, 'editpage', 500, 500);
}

function del_page(it, pageid){
	var decision = window.confirm('Really delete this entire page?');
	if (decision){
		afterTransfer = 'del_page2()';
		connect('delPage', 'pageid=' + pageid);
		it.style.background = "url('/cms/cms/images/greengradient.jpg')";
	}
}

function del_page2(){
	window.location.href = request.responseText;
}




