


var jswinLoginWindow = null;
var urlLoginCallBack = null;

function showLoginWindow(url){

	if(typeof(url)=='undefined'){
		url = document.location.href;
	}
	urlLoginCallBack = url;

	if(!jswinLoginWindow){
		jswinLoginWindow = new jsWin({width:250,height:120,modal:true});
	}
	
	
	
	
	jswinLoginWindow.setTitle("Login");
	jswinLoginWindow.setContent("<p style='color:red' id='ajaxLoginInfo'></p><table>	<tr>		<td>			<label>Email</label>		</td>		<td>			<input id='login_info' type='text' value='' name='login'/>		</td>	</tr>	<tr>		<td>			<label>Password</label>		</td>		<td>			<input id='password_info' type='password' value='' name='password'/>		</td>	</tr>		</table><button id='btnAjaxLoginSubmit' onclick='login()'>Login</button><br /><br /><a href='en/parent'>Create an account</a>");
	jswinLoginWindow.show();
	
}


function login(){
	
	if(!urlLoginCallBack){
		return false;
	}
	
	$('btnAjaxLoginSubmit').hide();
	
	
	var urlAjax = "http://www.thekidscoop.com/admin/form.php?action=useCustomClass&m=ajaxLogin&login="+$F('login_info')+"&password="+$F('password_info');
	
	var ar = new Ajax.Request(urlAjax, {});
	ar.options.onSuccess = function(origReq){
		console.warn(origReq.responseText);
		var json = origReq.responseText.evalJSON();
		if(json['login']=='success'){
			document.location.href=urlLoginCallBack;
		}else{
			$('btnAjaxLoginSubmit').show();	
			$('ajaxLoginInfo').update(json['msg']);
		}
	}
}









var firstTryInput = new Array();

function tryInput(inputId)
{
	if(firstTryInput.indexOf(inputId) == -1)
	{
		$(inputId).value = '';
		firstTryInput.push(inputId);
	}
}


function categoryPopup(idCallback, winTitle){
	var url = 'php/ajax.php?action=showCategoryPopup&callback='+idCallback;
	if($('categories')){
		url +='&categories='+$F('categories');
	}

	jsWin.instance().setOptions({width:'555', height:'400'});
	
	jsWin.instance().setContentFromUrl(url,winTitle);

}

function selectSubCat(inp){
	inp = $(inp);
	inp.up('div',0).up('div',0).select('input[type="checkbox"]').each(function(el){
		if(inp.value!=el.value){
			el.checked=inp.checked?'checked':'';	
		}
	});
	return inp.checked;
}


function fakeSelect(id,onclick,width){
	if(!width){
		width =200;
	}
	//var sel = new Element('div',{className:'selector'}).setStyle({width:width+'px'});
	
	var sel = Builder.node('div', {className:'selector', style:'width:'+width+'px', onclick:onclick}, Builder.node('div', null, $(id).innerHTML));



	$(id).update(sel);

	//sel.writeAttribute({onclick:onclick});
	//sel.update(new Element('div').update($(id).innerHTML));
	
	
	//$(id).update(sel);
	
}

function closePopupCategory(id){
	var html = new Array();
	var values = new Array();
	$('categoryForm').select('input[type="checkbox"]').each(function(el){
		if(el.checked){
			html.push(el.name);
			values.push(el.value);
		}
	});
	
	if(!$('categories')){
		var hid = new Element('input',{id:'categories',type:'hidden',name:'categories',value:values.join(',')});
	}else{
		var hid = $('categories');
		hid.value = values.join(',');
	}
	$(id).appendChild(hid);
	$(id).select('div div')[0].innerHTML=html.join(', ');
	
	
	jsWin.instance().close();
}



function locationPopup(idCallback, winTitle, inputId){

	if(typeof(inputId) == 'undefined' || !inputId)
	{
		inputId = 'locations';
	}

	var url = 'php/ajax.php?action=showLocationPopup&callback='+idCallback+'&inputId='+inputId;
	if($(inputId)){
		url +='&locations='+$F(inputId);
	}

	jsWin.instance().setOptions({width:'555', height:'400'});

	jsWin.instance().setContentFromUrl(url,winTitle);

}


function closePopupLocation(id, inputId){

	if(typeof(inputId) == 'undefined' || !inputId)
	{
		inputId = 'locations';
	}

	var html = new Array();
	var values = new Array();
	$('locationForm').select('input[type="checkbox"]').each(function(el){
		if(el.checked){
			html.push(el.name);
			values.push(el.value);
		}
	});
	if(!$(inputId)){
		var hid = new Element('input',{id:inputId,type:'hidden',name:'locations',value:values.join(',')});
	}else{
		var hid = $(inputId);
		hid.value = values.join(',');
	}
	$(id).appendChild(hid);
	if(id!='upcoming_locations'){
		$(id).select('div div')[0].innerHTML=html.join(', ');
	}else{
		$('upcoming_locations').submit();
	}
	jsWin.instance().close();
}

function popCalendar(id, withTime, yearRange, defaultDate){

	if(typeof(withTime) == 'undefined')
	{
		withTime = true;
	}

	if(typeof(yearRange) == 'undefined')
	{
		yearRange = [1980,2011];
	}

	new CalendarDateSelect(id, {
			popup:"force",
			time:withTime,
			year_range:yearRange,
			default_date:defaultDate
			//time:$F(id)
	});

	Date.prototype.toFormattedString = function(include_time){

		str = this.getFullYear() + '-' + doublezero(this.getMonth()+1) + "-" + doublezero(this.getDate());

		if (include_time)
		{
			hour = this.getHours();
			str += " " + this.getAMPMHour() + ":" + this.getPaddedMinutes() //" " + this.getAMPM()
		}

		return str
	}
}

function doublezero(num){
	if(num < 10 ){
		return '0'+num.toString();
	}
	return num;
}


function toggleReminder(id,a){
	jsWin.instance().setTitle('Reminder');
	jsWin.instance().setContent('Loading...');
	jsWin.instance().show();
	
	
	var aj = new Ajax.Request('php/ajax.php?action=setReminder',{postBody:'id='+id});
	aj.options.onSuccess = function(rep){
		jsWin.instance().setContent(rep.responseText);
		if($(a).hasClassName('alarmOff')){
			$(a).removeClassName('alarmOff');
			$(a).addClassName('alarmOn');
		}else{
			$(a).removeClassName('alarmOn');
			$(a).addClassName('alarmOff');
		}
	}
}

/*function removeEvent(id){
	
	jsWin.instance().setTitle('Remove event from my calendar');
	jsWin.instance().setContent('<div style="text-align:center;">Do you want to remove the occurence of the event or the entire event from your calendar?<br/><a href="php/ajax.php?action=removeEventCalendar&type=occ&id='+id+'">Remove this occurence</a>&nbsp;&nbsp;&nbsp;<a href="php/ajax.php?action=removeEventCalendar&type=full&id='+id+'">Remove all the occurences from my calendar</a></div>');
	jsWin.instance().show();
}
*/

function initTooltipNotice()
{
	$$('.tooltip_notice').each(function(el)
	{	//console.debug(el.identify());

		if(!$('tooltip_'+el.identify()))
		{
			var span = Builder.node('span',{id:'tooltip_'+el.identify(),className:'tooltipBtn'},'?');
			new Insertion.After(el, span);
	
			new Effect.Tooltip(span, '<div style="font-weight:bold;">'+el.readAttribute('title') + '</div><div style="margin-top:3px;">' + el.innerHTML + '</div>', {className: 'tooltip', offset:{x:4, y:4}});
		}
	});
}

Event.observe(window,'load', function(e)
{
	initTooltipNotice();
});
