//Global js functions

// ========================================
// id and classname builder for jQuery
// add '.' and '#', and escapes illegal chars
// ========================================

function jqId(id) { return '#' + jqEscape(id);}
function jqClass(id) {	return '.' + jqEscape(id);	}
function jqEscape(id) { return id.replace(/(:|\.)/g,'\\$1'); }

// --------------
// SORTABLE ON TABLE ROWS
// --------------
var sortedTr;
var sortStop = function(e){
	// --------------
	// What should happen at stop? ... Define local stopSortableLocal()
	// --------------
	if (sortedTr&&window.stopSortableLocal) stopSortableLocal(sortedTr);
	sortedTr = null;
	}
var fixHelper = function(e, tr)
// Return a helper with preserved width of cells
  {
    sortedTr = tr
    var originals = tr.children();
    var helper = tr.clone();
    helper.attr('class','sortablehelper');
    helper.css('min-height','20px');
    helper.children().each(function(index)
    {
      // Set helper cell sizes to match the original sizes
      jQuery(this).width(originals.eq(index).width());
      jQuery(this).attr({'class':originals.eq(index).attr('class')+'_classremoved'});
    });
    return helper;
  }

jQuery(document).ready( function() { 
	//alert('xx');
	jQuery("tbody#sortsortsort").sortable({
	helper: fixHelper, stop: sortStop
}).disableSelection();
});

var formHash;
function scrollToFormHash() { 
	// Define the variable formHash in the document header (after loading globals.js)
	// add scrollToFormHash() as onLoad function, and the page scrolls to an <anchor name="name"></anchor> where name is formHash
	// 
	jQuery('anchor').each( function() { 
		pos = jQuery(this).parent();
		if (pos.attr('id')==formHash) {
			xy = pos.position();
			jQuery(window).scrollTop(xy.top);
			//jQuery(window).animate({scrollTop:xy.top},2000,'ease');
			}
		});
	}

// ====================
// Params handling
// ====================

params2Url = {
	// Convert {'aa':89, 'bb': [12,34] } kind of paramset to simple url form string
	// Used instead of 
	convert: function(p) {
		if (typeof(ajaxattrkeys)=='undefined') ajaxattrkeys = {};
		this.ps = [];
		if (typeof(p)=='string') return p;
		this.collect(p);
		return this.ps.join('&');
		},
	collect: function(p) { for (i in p) this[typeof(p[i])](i,p[i]); },
	boolean: function(key,val) { this.pushsimple(key,(val)?1:0); },
	string: function(key,val) { this.pushsimple(key,val); },
	number: function(key,val) { this.pushsimple(key,val); },
	object: function(key,val) { this.pushobject(key,val); },
	undefined: function(key,val) { this.pushsimple(key,''); },
	pushsimple: function(key,val) { this.ps.push(key + "=" + encodeURIComponent(val)); },
	pushobject: function(key, o) { 
		if (o.length) for (j in o) this.pushsimple(key,o[j]);
		else for (j in o) {
			k = j;
			if (key in ajaxattrkeys) k = key + '_' +j;
			this.pushsimple(k,o[j]);
			}
		}
	}

// ====================
// Console logging
// ====================

function ConsoleLog(msg, obj) {
	if (!msg) msg = "ajax log"
	else if (typeof(msg)=='object') msg = msg.join(' ');
	else if (typeof(msg)=='string') msg = msg;
	else msg = "ajax log"
	//obj = obj || this;

	//From MochiKit
	if (typeof(window) != "undefined" && window.console && window.console.log) {		// Safari and FireBug 0.4 / Percent replacement is a workaround for cute Safari crashing bug
		if (typeof(obj)=='object') window.console.log("%s: %o", msg.replace(/%/g, '\uFF05'), obj);
		else window.console.log(msg.replace(/%/g, '\uFF05'));
		}
	else if (typeof(opera) != "undefined" && opera.postError) opera.postError(msg);  	// Opera
	else if (typeof(printfire) == "function") printfire(msg); 							// FireBug 0.3 and earlier
	else if (typeof(Debug) != "undefined" && Debug.writeln) Debug.writeln(msg); 		// IE Web Development Helper (?) / http://www.nikhilk.net/Entry.aspx?id=93
	else if (typeof(debug) != "undefined" && debug.trace) debug.trace(msg);				// Atlas framework (?) / http://www.nikhilk.net/Entry.aspx?id=93
	}

// ====================
// Spamsafe mail
// ====================

function jsMailClient(){
	args = jsMailClient.arguments;
	data = args[0];
	argscount = 1;
	sep = '';
	mail = 'mailto:';
	mc = parseInt(data.charAt(0));
	mail = _jsMailData(mail,args,argscount,mc,sep);
	argscount += (2*mc);
	if (data.length>1) {
		mail += '?'
		mc = parseInt(data.charAt(1));
		if (mc) { mail += 'subject='+args[argscount]; sep = '&'; }
		argscount += 1;
		if (data.length>2) {
			mc = parseInt(data.charAt(2));
			if (mc) { mail = _jsMailData(mail,args,argscount,mc,sep+'cc='); sep = '&'; }
			argscount += (2*mc);
			if (data.length>3) {
				mc = parseInt(data.charAt(3));
				if (mc) mail = _jsMailData(mail,args,argscount,mc,sep+'bcc=')
				argscount += (2*mc);
				}
			}
		}
	document.location = mail;
	}
function _jsMailData(mail,args,c,mc,sep) {
	mail += sep;
	for (i=0;i<mc;i++) {
		mail = mail + args[c]+'@'+args[c+1];
		if (i<mc-1) mail += ','
		c += 2;
		}
	return mail;
	}


