/* Sloganizer, create interactive text fields
 * Author: Tim Isenheim, www.freshlabs.de
 * Version 7 - last update: 03-12-06
 *
 * This work is licensed under the terms of the Attribution-NonCommercial-ShareAlike as published by the Creative Commons License, version 2.5 of the License.
 * This theme is distributed in the hope that it will be useful, but comes as is, without any warranty and/or without any implied warranty.
 * See the Creative Commons License (Attribution-NonCommercial-ShareAlike 2.5)
 * for more details at http://creativecommons.org/licenses/by-nc-sa/2.5/.
 * This copyright must stay intact. Thank you.
 */

//-----------------------------------------------------------------------------
// global variables
	
var sloganizer;						// the parent container and action hook
var slogan; 							// The one and only slogan
var slogan_element = 'a';	// Type of HTML Element the slogan is in
var sloganId = 'slogan';	// the slogan's ID
var sloganbuttons;        // Button Collection

var sloganinput;
var sloganId_input = 'slogan-input';
var sloganID_buttons = 'slogan-buttons';

// input, submit buttons and their IDs
var slBtn_submit, slBtn_cancel, slBtn_reset;
var slBtnID_submit = 'slogan-submit';
var slBtnID_cancel = 'slogan-cancel';
var slBtnID_reset = 'slogan-reset';
var slBtnID_toplist = 'slogan-viewlist';

// various Text variables
var slTxt_start;
var slTxt_init = "New Media Design, Hamburg";

var toplist_shown = false;  // is the Top5 List visible?
var isCustom = false; 	  // is the slogan customized?

//-----------------------------------------------------------------------------
// functions
	
function getSloganText(){
	return slogan;
}

function setSloganText(string){ 
	if(string){
		slTxt_start = string;
		isCustom = true;
	}
	else slTxt_start = slTxt_init;
}

function resetSlogan(){
	sloganinput.value = ""; // clear slogan input
	finishEditing(true);    // end and save
	isCustom = false;       // no more custom
}
	
function createSlogan(){
		/* read possible cookie */
	var cookie = readCookie("freshlabs-slogan");
	var slogantext = cookie ? cookie : getSloganText();
	setSloganText(slogantext);
	sloganizer = document.getElementById('sloganizer');
	sloganizer.innerHTML = '';
	slogan = document.createElement(slogan_element);
	slogan.id = sloganId;
	slogan.href = '#';
	slogan.title = "Click to edit";
	slogan.appendChild(document.createTextNode(slTxt_start));
	//new Effect.Pulsate($(slogan));
	slogan.onclick = function(){
		editSlogan(getSloganText()); 
	}
	
		/* create toplist button */
	slBtn_toplist = document.createElement('a');
	slBtn_toplist.id = slBtnID_toplist;
	slBtn_toplist.href = '#';
	slBtn_toplist.title = "Show Top 5 User slogans";
	var span = document.createElement('span');
	span.innerHTML = 'Top 5';
	slBtn_toplist.appendChild(span);
	
	slBtn_toplist.onclick = function(){
		if(!toplist_shown){
			if(!$('slogans-toplist')){ getTopSlogans(5); }
			else{ showTopList(); }
		}
		else{ hideTopList(); }
	}
	
  	sloganizer.appendChild(slogan);
  	sloganizer.appendChild(slBtn_toplist);
}
	
function editSlogan(text){
	/* create input field */
	sloganbuttons = document.createElement('div');
	sloganbuttons.id = sloganID_buttons;
	sloganinput = document.createElement('input');
	sloganinput.id = sloganId_input;
	sloganinput.type = 'text';
	sloganinput.setAttribute('size','45');
  
	/* make slogan invisible */
	slogan.style.display = "none";
	/* and display input form */
	sloganizer.insertBefore(sloganinput, slBtn_toplist);
	sloganinput.focus();
  
	//sloganizer.appendChild(document.createElement('br'));
	/* create submit button */		
	slBtn_submit = document.createElement('input');
	slBtn_submit.id = slBtnID_submit;
	slBtn_submit.type = 'submit';
	slBtn_submit.value = 'Brand';
	slBtn_submit.onclick = function(){ finishEditing(true); }  
		
	/* create cancel button */
	slBtn_cancel = document.createElement('input');
	slBtn_cancel.id = slBtnID_cancel;
	slBtn_cancel.type = 'submit';
	slBtn_cancel.value = 'Abbrechen';
	slBtn_cancel.onclick = function(){ finishEditing(false); }

	if(isCustom){
	  /* create reset button */
	  	slBtn_reset = document.createElement('input');
		slBtn_reset.id = slBtnID_reset;
		slBtn_reset.type = 'submit';
		slBtn_reset.value = 'Reset';
		slBtn_reset.onclick = function(){ resetSlogan(); }
		
		sloganbuttons.appendChild(slBtn_reset);
		sloganinput.value = text.innerHTML;  // keep custom text
	}
	
	/* deploy on the Document */
	sloganbuttons.appendChild(slBtn_cancel);  // cancel button
	
	// also append submit button			
	sloganbuttons.appendChild(slBtn_submit);
	
	if(!$('slogans-toplist')){
		sloganizer.appendChild(sloganbuttons);
	}else{
		sloganizer.insertBefore(sloganbuttons, slBtn_toplist);
	}
}
	
/* finish the editing, clean up, save slogan(true/false) */
function finishEditing(save){
	var val = sloganinput.value;

	var slogantemp = val.replace(/;/, "");
	slogantemp = slogantemp.replace(/mailto/, "");
	slogantemp = slogantemp.replace(/array/, "");
	slogantemp = slogantemp.replace(/\W/, " ");

	sloganinput.value = slogantemp;  // re-copy trimmed slogan
	
	storeSlogan(slogantemp);
	if(isCustom){
		sloganbuttons.removeChild(slBtn_reset);
	}
	/* if slogan left empty reset to start text */
	if(slogantemp=="" || slogantemp == slTxt_init){
		slogantemp = slTxt_init;
		isCustom = false;
	}
	else { isCustom = true; }
	if(save){
		slogan.innerHTML = slogantemp;
		/* save slogan in cookie */
		createCookie("freshlabs-slogan", slogan.innerHTML, 20);
	}
	sloganizer.removeChild(sloganinput);
	sloganizer.removeChild(sloganbuttons);
	slogan.style.display = "inline";
}


function storeSlogan(slg)
{
	var baseurl = 'http://www.freshlabs.de/wp-content/scripts/sloganizer/sloganizer.php';
	var pars = "slg=" + slg;
	
	var myAjax = new Ajax.Request( baseurl, {method: 'get', parameters: pars, onComplete: showResponse} );
}

function showResponse(originalRequest){	
	setSloganText(originalRequest.responseText);
	
}

function getTopSlogans(howmany){
	var baseurl = 'http://www.freshlabs.de/wp-content/scripts/sloganizer/slogans-toplist.php';
	var pars = "amt=" + howmany;
	
	var myAjax = new Ajax.Request( baseurl, {method: 'get', parameters: pars, onComplete: buildTopList} );
}

function buildTopList(originalRequest){
	var results = document.createElement('div');
	results.id = "slogans-toplist";
	results.title = "Click to close";
	results.style.display = "none";
	sloganizer.appendChild(results);
	results.innerHTML = originalRequest.responseText;
	showTopList();
	results.onclick = hideTopList;
}

function showTopList(){
	new Effect.Appear('slogans-toplist');
	toplist_shown = true;
}

function hideTopList(){
	$('slogans-toplist').style.display = 'none';
	toplist_shown = false;
}

appendOnLoad('createSlogan()');

//-----------------------------------------------------------------------------