function wc_open(id)
{
	switch(id)
	{
		case 'kw_list': kw_list(); break;
	}
}

kw_edited = false; // <spagetti> this var is manipulated by a function in file media_info.php</spagetti>

function kw_list()
{
	
	//alert("clientWidth:" +  document.documentElement.clientWidth);
	//alert("clientHeight:" + document.documentElement.clientHeight);
	//alert("scrollLeft:" + document.documentElement.scrollLeft);
	//alert("scrollTop:" + document.documentElement.scrollTop);
	
	var win_vert = document.documentElement.scrollTop  + 90; // + 90 puts it below the main navigation of the site
	
	var type=$("edit-profile_media_cat").value;
	
	var win = new Window
	(
		'kw_list', 
		{
			className: "mac_os_x", 
			title: "Keywords used for meda type: " + type,
			top:win_vert, 
			left:100,  
			width:600, 
			height:400, 
			zIndex:150, 
			opacity:1, 
			resizable: true,
			url: "/sites/default/local_lib/php/media_keyword_info.php?type=" + type
		}
	);
	//win.getContent().innerHTML = "Hi there dan!";
	/* Registers a new windows observer. Should be able to respond to onStartResize(), onEndResize(), onStartMove(), onEndMove(), onClose(), onDestroy(), onMaximize(), onMinimize(), onFocus(), onHide(), onShow() functions*/
	//win.addObserver(onClose(){alert('closed!');})
	win.setDestroyOnClose();
	win.show(true);
	
	
	var kw_observer = {
			onStartMove: function(eventName, win) {
				//debug(eventName + " on " + win.getId()) // !!DON'T DELETE!! use this if you need to debug what the windows are doing
			},
			onEndMove: function(eventName, win) {
				//debug(eventName + " on " + win.getId()) // !!DON'T DELETE!! use this if you need to debug what the windows are doing
			},
			onStartResize: function(eventName, win) {
				//debug(eventName + " on " + win.getId()) // !!DON'T DELETE!! use this if you need to debug what the windows are doing
			},
			onEndResize: function(eventName, win) {
				//debug(eventName + " on " + win.getId()) // !!DON'T DELETE!! use this if you need to debug what the windows are doing
			},
			onClose: function(eventName, win) {
				//debug(eventName + " on " + win.getId()) // !!DON'T DELETE!! use this if you need to debug what the windows are doing
				
				// submit the keywordlist if the kw_edited flag is true
				if(kw_edited) // <spagetti> this var is manipulated by a function in file media_info.php</spagetti>
				{
					// submit the form
					// $("user_edit").submit(); // not doing this yet ... is confusing					
				}
				kw_edited = false; // reset the kw_list to false
			},
			onDestroy: function(eventName, win) {
				//debug(eventName + " on " + win.getId()) // !!DON'T DELETE!! use this if you need to debug what the windows are doing
			}
		}
		Windows.addObserver(kw_observer);
	
	
	//showDebug(); // !!DON'T DELETE!! use this if you need to debug what the windows are doing
		
}


function show_terms_of_use()
{
	
	//alert("clientWidth:" +  document.documentElement.clientWidth);
	//alert("clientHeight:" + document.documentElement.clientHeight);
	//alert("scrollLeft:" + document.documentElement.scrollLeft);
	//alert("scrollTop:" + document.documentElement.scrollTop);
	
	var win_vert = document.documentElement.scrollTop  + 90; // + 90 puts it below the main navigation of the site
	
/*	var type=$("edit-profile_media_cat").value;*/
	
	var win = new Window
	(
		'tos', 
		{
			className: "mac_os_x", 
			title: 'Commonpath Terms of Use Agreement',
			top:win_vert, 
			left:100,  
			width:800, 
			height:600, 
			zIndex:150, 
			opacity:1, 
			resizable: true,
			url: "/sites/default/content/cp_terms_of_use.php"
		}
	);
	//win.getContent().innerHTML = "Hi there dan!";
	/* Registers a new windows observer. Should be able to respond to onStartResize(), onEndResize(), onStartMove(), onEndMove(), onClose(), onDestroy(), onMaximize(), onMinimize(), onFocus(), onHide(), onShow() functions*/
	//win.addObserver(onClose(){alert('closed!');})
	win.setDestroyOnClose();
	win.show(true);
	
	
	var tos_observer = {
			onStartMove: function(eventName, win) {
				//debug(eventName + " on " + win.getId()) // !!DON'T DELETE!! use this if you need to debug what the windows are doing
			},
			onEndMove: function(eventName, win) {
				//debug(eventName + " on " + win.getId()) // !!DON'T DELETE!! use this if you need to debug what the windows are doing
			},
			onStartResize: function(eventName, win) {
				//debug(eventName + " on " + win.getId()) // !!DON'T DELETE!! use this if you need to debug what the windows are doing
			},
			onEndResize: function(eventName, win) {
				//debug(eventName + " on " + win.getId()) // !!DON'T DELETE!! use this if you need to debug what the windows are doing
			},
			onClose: function(eventName, win) {
				//debug(eventName + " on " + win.getId()) // !!DON'T DELETE!! use this if you need to debug what the windows are doing
				
				// submit the keywordlist if the kw_edited flag is true
				if(kw_edited) // <spagetti> this var is manipulated by a function in file media_info.php</spagetti>
				{
					// submit the form
					// $("user_edit").submit(); // not doing this yet ... is confusing					
				}
				kw_edited = false; // reset the kw_list to false
			},
			onDestroy: function(eventName, win) {
				//debug(eventName + " on " + win.getId()) // !!DON'T DELETE!! use this if you need to debug what the windows are doing
			}
		}
		Windows.addObserver(kw_observer);
	
	
	//showDebug(); // !!DON'T DELETE!! use this if you need to debug what the windows are doing
		
}





/*
function get_content()
{

	var handlerFunc = function(t) 
	{
	    alert(t.responseText);
	}

	var errFunc = function(t) 
	{
	    alert('Error ' + t.status + ' -- ' + t.statusText);
	}

	//For an XML response:
//   var handlerFunc = function(t) 
//   {
//       var xmlDoc = t.responseXML.documentElement;
//       //Handle data.
//   	alert(xmlDoc);
//   }

	new Ajax.Request('sites/default/local_lib/php/media_info.php', {parameters:'thisvar=true&thatvar=Howdy', onSuccess:handlerFunc, onFailure:errFunc});	
}
*/

function fill_artist_finder(arg)
{
	if(arg != "") { send_arg = "?" + arg } else send_arg = "";
		
	new Ajax.Updater(
		'artist_finder', 
		'/sites/default/local_lib/php/artist_finder.php' + send_arg, 
		{
			onComplete:function(){ new Effect.Highlight('artist_finder'); },
			asynchronous:true, 
			evalScripts:true
		}
	);
 	// this will evaluate any scripts in <script></script> blocks. Also it will hightlight mydiv on complete.
/*	// this might work as a post ... hmmmm
	new Ajax.Updater(
		'div_to_be_updated', 
		'/sites/default/local_lib/php/artist_finder.php',
		{
			asynchronous:true, 
			parameters:Form.serialize(this)
		}
	); */

}

function my_clear(el,val) { if(el.value == val) el.value = ""; }


// ctl is a postfix to the element passed in: if el is "foo" and ctl is "_somthing" then the foo_somthing control to fade out at the same time that the el is unfolding
function unfold(el,ctl)
{
	
	//alert("will unfold element " + el +" and hide element with id: " + ctl);
	
	Effect.BlindDown(el); // open the requested element

	// hide the opener control
	if(ctl!=null)
	{
		new Effect.Opacity(el + ctl, {duration:0.9, from:1.0, to:0.0}); // WOW ... have to say new Eff...!  who knew!
		Effect.BlindUp(el + ctl); 
	} 	
}

// ctl is a postfix to the element passed in: if el is "foo" and ctl is "_somthing" then the foo_somthing control to fade in at the same time that the el is folding
function fold(el,ctl)
{
	//alert("will fold element: " + el +" and show element with id: " + ctl);
	
	Effect.BlindUp(el); // close the requested element
	
	// open the opener control
	if(ctl!=null)
	{
		Effect.BlindDown(el + ctl); 
		new Effect.Opacity(el + ctl, {duration:0.9, from:0.0, to:1.0}); // WOW ... have to say new Eff...!  who knew!
	}
}

function swap_unfold(el)
{
	
	//alert("will unfold element " + el +" and hide element with id: " + ctl);
	
	Effect.BlindDown(el); // open the content element

	// hide the teaser
	new Effect.Opacity(el + '_teaser', {duration:0.9, from:1.0, to:0.0}); // WOW ... have to say new Eff...!  who knew!
	Effect.BlindUp(el + '_teaser'); 
}

// ctl is a postfix to the element passed in: if el is "foo" and ctl is "_somthing" then the foo_somthing control to fade in at the same time that the el is folding
function swap_fold(el)
{
	//alert("will fold element: " + el +" and show element with id: " + ctl);
	
	Effect.BlindUp(el); // close the requested element
	
	Effect.BlindDown(el + '_teaser'); 
	new Effect.Opacity(el + '_teaser', {duration:0.9, from:0.0, to:1.0}); // WOW ... have to say new Eff...!  who knew!

}


function detach(id)
{
	var msg = 'Are you sure you want to delete: ' + $( id + '_dsc').innerHTML;
	
	if(confirm(msg))
	{
		//alert('detaching link: ' + real(id));
		
		/*
		var opt = {
		    // Use POST
		    method: 'post',
		    // Send this lovely data
		    postBody: 'thisvar=true&thatvar=Howdy&theothervar=2112',
		    // Handle successful response
		    onSuccess: function(t) {
		        alert(t.responseText);
		    },
		    // Handle 404
		    on404: function(t) {
		        alert('Error 404: location "' + t.statusText + '" was not found.');
		    },
		    // Handle other errors
		    onFailure: function(t) {
		        alert('Error ' + t.status + ' -- ' + t.statusText);
		    }
		}

		new Ajax.Request('/foo/bar', opt);
		*/
		var opt = {
		    // Use POST
		    method: 'post',
		    // Send this lovely data
		    postBody: 'lid=' + real(id),
		    // Handle successful response
		    onSuccess: function(t) {
			
				json = eval(t.responseText);
				
				//alert("return is: " + json.value + ', ' + json.message);
			
				if(json.value)
				{
					detach_element(id);
				}
				else
				{
					alert("ERROR: deletion failed. The message returned from the server was: " + json.message)
				}
			
				// debugging
				//$('jsout').innerHTML = t.responseText	
		    },
		    // Handle 404
		    on404: function(t) {
		        alert('Error 404: location "' + t.statusText + '" was not found.');
		    },
		    // Handle other errors
		    onFailure: function(t) {
		        alert('Error ' + t.status + ' -- ' + t.statusText);
		    }
		}
		
		new Ajax.Request('/sites/default/modules/add_curated_piece/mod_detach_link.php', opt);
		
	}
}

function detach_element(id)
{
	// dont have to remove it as it dosent exist anymore on the sever and will be gon at next page load
	// fade the element out
	//function mycallback(id) // this works (is called) but throws a wierd error
	//{
		Effect.BlindUp(id);
	//}
	
	//new Effect.Opacity(id, {duration:0.9, from:1.0, to:0.0, afterFinish: mycallback });
}

function real(id) // get a real id from a fake id ... cause html is stupid! not allowing an id to start with a number <valley_girl>... I mean C'mon, like get real!</valley_girl>
{
	return id.substr(1,id.length);
}

