/**
*
* Node common.js 
* @ver 2.0 
* @author denes.szabo@internode.hu
* @since 2007.01.13
* @lastmod 2007.10.19 - Den
*
*/
var fontSizeCookieName = 'preferences_fontsize';

/**
 * Displays an confirmation box beforme to submit a "DROP/DELETE/ALTER" query.
 * This function is called while clicking links
 *
 * @param   object   the link
 * @param   object   the sql query to submit
 *
 * @return  boolean  whether to run the query or not
 */
function confirmLink(theLink, theMsg)
{
	// Confirmation is not required in the configuration file
	// or browser is Opera (crappy js implementation)
	if ( theMsg == '' || typeof(window.opera) != 'undefined') {
		return true;
	}

	var is_confirmed = confirm(theMsg);
	if (is_confirmed) {
		theLink.href += '/is_confirmed/1';
		window.location=theLink.href;
		return true;
	}
	return false;
	//return is_confirmed;
} // end of the 'confirmLink()' function

/* Cookie API  v1.0.1
 * documentation: http://www.dithered.com/javascript/cookies/index.html
 * license: http://creativecommons.org/licenses/by/1.0/
 * code (mostly) by Chris Nott (chris[at]dithered[dot]com)
 */
function setCookie( name, value, expires, path, domain, secure )
{
	path="/";
	var curCookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
	document.cookie = curCookie;
}
function getCookie( name )
{
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf( "; " + prefix );
	if ( begin == -1 )
	{
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	}
	else
	{
		begin += 2;
	}
	var end = document.cookie.indexOf( ";", begin );
	if ( end == -1 )
	{
		end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie( name, path, domain )
{
	path="/";
	var value = getCookie( name );
	if ( value != null )
	{
		document.cookie = name + "=" +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
	return value;
}

function whatPlus(code) {
	var replaces = 0;
	var emailarr;
	var alltags = $(".email");
	for (var i=0; i < alltags.length; i++) {
		while (alltags[i].innerHTML.indexOf(code) != -1 && replaces < 10000) {
			alltags[i].innerHTML = alltags[i].innerHTML.replace(code, String.fromCharCode(64));
			emailarr = alltags[i].innerHTML.split("|");
			if (! emailarr[1])
			{
				alltags[i].innerHTML = "<a href=\"mai"  + "lto:" + alltags[i].innerHTML + "\">" + alltags[i].innerHTML + "<\/a>";
			}
			else
			{
				alltags[i].innerHTML = "<a href=\"ma" + "ilto:" + emailarr[1] + "\">" + emailarr[0] + "<\/a>";
			}
			replaces++;
		}
	}
}

/*

Stylesheet works by Listapart
http://www.alistapart.com/articles/relafont

*/

function fontsizeup() {
  active = getActiveStyleSheet();
  switch (active) {
    case 'xx-small' : 
      setActiveStyleSheet('x-small');
      break;
    case 'x-small' : 
      setActiveStyleSheet('small');
      break;
    case 'small' : 
      setActiveStyleSheet('medium');
      break;
    case 'medium' : 
      setActiveStyleSheet('large');
      break;
    case 'large' :
      break;
    default :
      setActiveStyleSheet('medium');
      break;
  }
  
}

function fontsizedown() {
  active = getActiveStyleSheet();
  switch (active) {
    case 'large' : 
      setActiveStyleSheet('medium');
      break;
    case 'medium' : 
      setActiveStyleSheet('small');
      break;
    case 'small' : 
      setActiveStyleSheet('x-small');
      break;
    case 'x-small' : 
      setActiveStyleSheet('xx-small');
      break;
    case 'xx-small' : 
       break;
    default :
      setActiveStyleSheet('xx-small');
      break;
  }
  
}

function showPrintView() {
	$("#fontSizeUp").hide();
	$("#fontSizeDown").hide();
	setActiveStyleSheet("print");
	$("#printView").click(function() {
  		disablePrintView();
  	});
}

function disablePrintView() {
	var cookie = getCookie(fontSizeCookieName);
	var title = cookie ? cookie : 'x-small';
	setActiveStyleSheet(title);
	
	$("#fontSizeUp").show();
	$("#fontSizeDown").show();
	
	$("#printView").click(function() {
  		showPrintView();
  	});
}

function setActiveStyleSheet(title) {
	//console.log("set style to: " + title);
	var i, a, main;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
			a.disabled = true;
			//console.log("active style: " + a.getAttribute("title"));
			
			if (a.getAttribute("title") == title) {
				a.disabled = false;
				//console.log("set to: " + title);
				if (title != "print") {
					//console.log('saved to cookie...');
					setCookie(fontSizeCookieName, title);
				}
			}
		}
	}
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  return ('x-small');
}

function initFontResize() {
	var cookie = getCookie(fontSizeCookieName);
	var title = cookie ? cookie : 'x-small';
	setActiveStyleSheet(title);
	drawFontResizeButtons();
}

function drawFontResizeButtons() {
        $("#fontSizeSwitcher").each(function() {
        	$(this).append('<a href="#" id="fontSizeDown">A-</a>');
        	$("#fontSizeDown").click(function() {
        		fontsizedown();
        		return false;
        	});
        	
        	$(this).append('<a href="#" id="fontSizeUp">A+</a>');
        	$("#fontSizeUp").click(function() {
        		fontsizeup();
        		return false;
        	});
        	
        	$(this).append('<a href="#" id="printView">P</a>');
        	$("#printView").click(function() {
        		showPrintView();
        		return false;
        	});
        });
}

