// open_target - a function to open things
function open_target(target_type_text, target_url, target_params, target_div, ajax_options)
	{
	// target_type_text : method to use to show url
	// target_url : url to show
	// target_params : serialized/json string for parameters
	// target_div : the div to update using self !! as a target type

	// the server should see a request to target_url with two post fields
	// called target_typw_text which contains the json value for target_params and target_params
	// so the back end knows how the front end is using its output
	// target params will be parsed and passed to the widget

	switch(target_type_text)
		{
		case 'self':
			// ajax call to this div [target_div]
			if  ( typeof( ajax_options ) == "undefined" ) { var ajax_options = new Object }
			ajax_options.parameters = new Object;
			ajax_options.parameters.target_type_text = target_type_text;
			ajax_options.parameters.target_params =  Object.toJSON(target_params);
			new Ajax.Updater(target_div, target_url, ajax_options);
		break;
		case 'page':
		case 'blank':
			// wow - err write a form dynamically and then submit it dynamically
                        //document.write not good here because doesn't know where to write (causes original page to die)
		  var form = document.createElement("form");
		  form.setAttribute("id", "open_target_form");
		  form.setAttribute("method", "POST");
		  form.setAttribute("action", target_url);
		  form.setAttribute("style", "display:none;");

                  if (target_type_text=='blank'){
		    form.setAttribute("target", "_BLANK");
		  }
		  var hiddenField = document.createElement("input");
		  hiddenField.setAttribute("type", "hidden");
		  hiddenField.setAttribute("name", "target_type_text");
		  hiddenField.setAttribute("value", target_type_text);
		  form.appendChild(hiddenField);
		  hiddenField = document.createElement("input");
		  hiddenField.setAttribute("type", "hidden");
		  hiddenField.setAttribute("name", "target_params" );
		  hiddenField.setAttribute("value", Object.toJSON(target_params));
		  form.appendChild(hiddenField);
                  
		  document.body.appendChild(form);
		  form.submit();
		  return true;
		break;
		case 'lightbox':
		  Modalbox.show(target_url, { method: "post" , height:600, params: { 'target_type_text': target_type_text, 'target_params': Object.toJSON(target_params) } });
		break;
                case 'broadcast':
		  pi_broadcast(target_params);
		break;
		case 'external':
		  window.open(target_params['link']);
		  break;
		default:
			alert('javascript : target_type.js : open_target :\n you failed to tell me how to get to ' + target_url);
		}
	
	}

// Helper functions to 
function showLoader(loader)
  {
     new Effect.Appear($(loader), {duration: 0.5});
  }

function hideLoader(loader)
  {
     new Effect.Fade($(loader), {duration: 0.5});
  }

function just_widget(w_id, url)
  {
    showLoader('pi_loading_' + w_id);
    open_target('self', '/core/justwidget/' + w_id + '/' + url, null, 'pi_widget_content_' + w_id, {
      onComplete: function() { hideLoader('pi_loading_' + w_id); }
    });
//    open_target('self', '/core/justwidget/' + w_id + '/' + url, null, 'pi_widget_content_' + w_id);
  }

function open_rss(w_id, ds_id)
{
   new Ajax.Request('/core/data/copyof/'+w_id+'/'+ds_id, 
		    {method:"post", onSuccess:open_rss_copy}); 
   return false;
}

function open_rss_copy(transport,param){
  response=transport.responseText;
  var temp = new Array();
  temp = response.split(':');
  w_id = temp[1];
  ds_id = temp[3];
  predicateEl = document.getElementById( 'pi_widget_' + w_id + '_predicate' );
  predicate='EMPTY';  
  if (predicateEl != null){
    predicate = predicateEl.innerHTML;
  }
  var form = document.createElement("form");
  form.setAttribute("target", "_BLANK");
  form.setAttribute("id", "open_rss");
  form.setAttribute("method", "POST");
  form.setAttribute("action", '/core/data/'+ds_id);
  form.setAttribute("style", "display:none;");
  var hiddenField = document.createElement("input");
  hiddenField.setAttribute("type", "hidden");
  hiddenField.setAttribute("name", "datasource_id");
  hiddenField.setAttribute("value", ds_id);
  form.appendChild(hiddenField);
  hiddenField = document.createElement("input");
  hiddenField.setAttribute("type", "hidden");
  hiddenField.setAttribute("name", "predicate" );
  hiddenField.setAttribute("value", predicate);
  form.appendChild(hiddenField);
                  
  document.body.appendChild(form);
  form.submit();
  return false;

}
