// open_target - a function to open things
function open_target(target_type_text, target_url, target_params, target_div, ajax_options, clear_before_update)
	{
	// 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

	
	if((typeof(clear_before_update) != 'undefined') && clear_before_update)
	{
//		$(target_div).innerHTML = '<div class="pi_widget_body"><p class="ajax_load"></p></div>';
	}
	
	switch(target_type_text)
		{
		case 'self':
			if((tdiv = $(target_div).up('.pi_widget').select('.pi_widget_title  .icons .refresh')).length)
				tdiv.first().addClassName('loading');
			// 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);
			ajax_options.onSuccess = function(tdiv){return function(req){
				tdiv.length && tdiv.first().removeClassName('loading');				
			}}(tdiv);
			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});
     $jqcspi('#'+loader).fadeIn(500);
  }

function hideLoader(loader)
  {
     //new Effect.Fade($(loader), {duration: 0.5});
     $jqcspi('#'+loader).fadeOut(500);
  }

function just_widget(w_id, url, params)
  {
 //   showLoader('pi_loading_' + w_id);
    open_target('self', '/core/justwidget/' + w_id + '/' + url, params, 'pi_widget_content_' + w_id, {
      onComplete: function(w_id){return function() {
    	  hideLoader('pi_loading_' + w_id);
    	  if(typeof window['on_refresh_'+w_id] != 'undefined') window['on_refresh_'+w_id](); }
    }(w_id)});
//    open_target('self', '/core/justwidget/' + w_id + '/' + url, null, 'pi_widget_content_' + w_id);
  }

function just_widget_new(w_id, url)
{
	// Add extra class to the refresh icon
	$('pi_refresh_'+w_id).addClassName('loading');
	open_target('self', '/core/justwidget/' + w_id + '/' + url, null, 'pi_widget_content_' + w_id, {
		onComplete: function() { $('pi_refresh_'+w_id).removeClassName('loading'); }
	});
}

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;

}

