dojo.require('dojo.io.cne_io');
dojo.require('dojo.lfx.cne_extras');
dojo.require('dojo.event.*');

function submitTags( the_form, base_url, instance ) {

  var form = document.getElementById( the_form );
  // for debug, swithc mime to 'text/plain'
  args = {
      url: MP3_BASE_URL + '/index.php?action=add_tags&',
      form: the_form,
	  mime: 'text/json',
      onSuccess: function(t,d) { processTagResults(d, instance) }
  }
  dojo.io.basicXHRTransport(args);

  // Adding dw tagging -coonce (9/17/2006)

  switch(dojo.byId(the_form).ref_type_id.value){
  	// User is tagging a story
  	case "1101":
  		var dw_url = 'http://dw.com.com/redir?ltype=&siteid=45&edid=3&asId=&astId=12&ptId=&ontid=9349&useract=109&destURL=http://img.gamespot.com/gamespot/b.gif';
		break;
  	// User is tagging an artist
  	case "1":
  		var dw_url = 'http://dw.com.com/redir?ltype=&siteid=45&edid=3&asId=&astId=12&ptId=&ontid=9349&useract=106&destURL=http://img.gamespot.com/gamespot/b.gif';
		break;
  	// User is tagging an album
  	case "2":
  		var dw_url = 'http://dw.com.com/redir?ltype=&siteid=45&edid=3&asId=&astId=12&ptId=&ontid=9349&useract=108&destURL=http://img.gamespot.com/gamespot/b.gif';
		break;
	//User tagging a list
    case "6001":
        var dw_url = '';
        break;		
  }
  var clrgif = document.createElement('img');
  clrgif.setAttribute('src', dw_url);
  document.body.appendChild(clrgif);
  return true;
}



function showAddTagForm(instance) {

    if (!instance) instance = 'single';

    var add_tags = dojo.byId('add_tags_container_'+instance);
    var success = dojo.byId('add_success_'+instance);

    add_tags.style.display = 'block';
    success.style.display = 'none';

    var tagging_module = dojo.byId('tagging_module_'+instance);
    var btn_add_tag = dojo.byId('btn_add_tag_'+instance);

    if (dojo.style.isShowing(tagging_module)) {
        dojo.lfx.fadeWipeOut(tagging_module, 200, dojo.lfx.easeInOut).play();
    }
    else {
        dojo.style.setOpacity(add_tags, 1);
        dojo.lfx.fadeWipeIn(tagging_module, 200, dojo.lfx.easeInOut,
            function() {
                dojo.byId('tag_body_' + instance).focus();
            }
        ).play();
    }

}


function processTagResults( results, instance ) {

    // for debug
    //alert(results); /*
    if (!instance) instance = 'single';

    var tagging_module = dojo.byId('tagging_module_'+instance);
    var add_tags = dojo.byId('add_tags_container_'+instance);
    var success = dojo.byId('add_success_'+instance);

	// fade wipe the textarea and add button
    dojo.lfx.fadeWipeOut(add_tags, 200, dojo.lfx.easeInOut,
		// this displays our success/failure and closes tagging module
		function() {
			success.innerHTML = results.response;
			dojo.lfx.fadeWipeIn(success, 200, dojo.lfx.easeInOut,
				function() {
					setTimeout(
						function() {
							dojo.lfx.fadeWipeOut(tagging_module, 200, dojo.lfx.easeInOut).play();
							add_tags.style.display = 'block';
							success.style.display = 'none';
						},
						3000
					);
				}
			).play();
		}
	).play();/**/
}
