/**
table class="sortable" id="cxartprodtable"
script type="text/javascript" src="/js/sorttable.js"...script
 */
function cpbkladdEvent(elm, evType, fn, useCapture) {
  if (elm.addEventListener) {
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent) {
    var r = elm.attachEvent("on"+evType, fn);
    return r;
  } 
} 
cpbkladdEvent(window, "load", xtsortables_init);
var activehourspot = false;
function xtsortables_init() {
    // Find all tables with class sortable and make them sortable
    if (!document.getElementsByTagName) return;
    tbls = document.getElementsByTagName("td");
    for (ti=0;ti<tbls.length;ti++) {
        thisTbl = tbls[ti];
        if (((' '+thisTbl.className+' ').indexOf("weekhourspot") != -1) && (thisTbl.id)) {
						sscpAddEditLink(thisTbl);
        }
    }

    tbls = document.getElementsByTagName("div");
    for (ti=0;ti<tbls.length;ti++) {
        thisTbl = tbls[ti];
        if (((' '+thisTbl.className+' ').indexOf("hourspotevent") != -1) && (thisTbl.id)) {
						// sscpAddEditLink(thisTbl);
						thisTbl.onclick=function(){
							activehourspot = this.id;
						}
        }
    }
}

function sscpAddEditLink(thisTbl) {
	//var str = '<div id="#{tab_id}editlink">[<a href="#" onclick="return xstcpRed(\'#{tab_id}\');">Redigere lokal tekst</a>]</div>';
	//str = str.interpolate({tab_id: thisTbl.id});
	//$(thisTbl).insert({after:str});
	thisTbl.onclick=function(){xclick(this);}
	thisTbl.style.cursor='pointer';
	thisTbl.title='Klikk for booking';
}

function xclick(el, raw, objid) {
	if (document.getElementById('bookingobjects')) document.getElementById('bookingobjects').style.display = 'none';
	document.getElementById('icalwkselector').style.display = 'none';

	if (raw) {
		id = '';
		spotid = el;
	} else {
		id = el.id;
		// do we have an event in this slot already?
		var spotid = '';
		x = $$('#'+id+' div.hourspotevent');
		if (x.length && activehourspot) {
			spotid = x[0].id;
		}
		// display edit box
	}
	ajaxWindow('/plugins/cpbookingcalendar/ajaxbooking.php','key='+id+'&datestr='+id+'&spotid='+spotid+'&objid='+objid);
	activehourspot = false;
	return false;
} 

function xstcpLagre(id) {
    var url = '/plugins/cpbookingcalendar/ajaxbooking.php';
    var pars = Form.serialize('form'+id);
    if (1==1) {
			new Ajax.Request( url, {
					method: 'post',
					parameters: pars,
					onSuccess: function(transport){
							var response = transport.responseText || "no response text";
							// $$('#'+id+' div.lokaltekst').innerHTML = transport.responseText;
							$('bookingpayloadwin').innerHTML = transport.responseText;
							// if (response=='OK') {
							var re = new RegExp('OK');
							var m = re.exec(response);
							if (m!=null) {
								location.reload(true);
							}
					 }
				});
				//location.reload(true);
			}
		// closeAjaxWindow();
}

function stopBookWindow() {
	closeAjaxWindow();
	if (document.getElementById('bookingobjects')) document.getElementById('bookingobjects').style.display = '';
	document.getElementById('icalwkselector').style.display = '';

}  

var sscpshowing = null;
function xstcpRed(id) {
	if (sscpshowing) {
		var tmp = sscpshowing;
		closeAjaxWindow();
		if (tmp==id) return;
	}

	sscpshowing = id;
	new Ajax.Updater(id+'', '/plugins/cpbookingcalendar/ajaxbooking.php', {
		  method: 'get',
			parameters: { key: id },
			onComplete: function(transport){
				tinyMCE.execCommand('mceAddControl', false, $('lokaltekstarea').id);
		  }
		});
	return false;
}

function xstcpRedStop(id) {
	var cell = document.getElementById(id);
	if ( cell.hasChildNodes() ) {
		while ( cell.childNodes.length >= 1 ) {
			cell.removeChild( cell.firstChild );       
		} 
	}
	sscpshowing = null;
	return;
}

								 			 								

