// requires jquery

var windowZindex = 20;
if( typeof calendar_script == 'undefined' )
{
	var calendar_script = '/calendar.php';
}

function show_calendar_selector( script,numericID,imgobj )
{
	
	var sep = '?';
	if( script.indexOf(sep) > -1)
	{
		sep = '&';
	}

	script += sep + 'htmlID=' + numericID;
	var yearselect = $('#Year'+numericID).val();
	var monthselect = $('#Month'+numericID).val();
	if( typeof yearselect != 'undefined' )
	{
		script += '&year=' + yearselect;
	}

	if(typeof monthselect != 'undefined' )
	{
		script += '&month=' + monthselect;
	}

	var pos = find_pos(imgobj);
	var pos_left = pos[0] + 16;
	var pos_top = pos[1] + 16;
	var width = 'auto';
	if( isIE ) width = 150; 
	widget_window( 'calWin'+numericID, script, width,'auto',pos_left,pos_top );
}

function cal_selector_change( script,htmlID, year, month )
{
	var sep = '?';
	if( script.indexOf(sep) > -1 )
	{
		sep = '&';
	}
	
	script += sep + 'htmlID=' + htmlID + '&month=' + month + '&year=' + year;
	$('#'+htmlID).load( script );
	//new Ajax.Updater( 'calWin'+htmlID+'inner', script,{asynchronous: true } );

}

function select_calendar_date(htmlID, year, month, day )
{
	$('#Year'+htmlID).val(year);

	$('#Month'+htmlID).val(month);

	$('#Day'+htmlID).val(day);
	close_widget_window('calWin'+htmlID);
}

function widget_window( wwID,loadurl,width,height,pos_left,pos_top )
{
	// figure out the position
	if( !pos_left && width != 'auto')
	{
		var wsize = get_window_size();
		var window_width = wsize[0];
		var remain_size = window_width - width;
		var pos_left = Math.round(remain_size / 2);
		if( pos_left < 1 )
		{
			pos_left = 1;
		}
	}

	if( !pos_top )
	{
		pos_top = 100;
	}

	if( typeof document.documentElement.scrollTop != 'undefined')
	{
		pos_top += document.documentElement.scrollTop;
	}
	else
	{
		pos_top += window.pageYOffset;
	}
	
	var htmlstr = '<div id="'+wwID+'" class="widgetwindow" style="z-index:'+windowZindex+';display:none;position:absolute;left:'+pos_left+'px;top:'+pos_top+'px';
	
	if( width != 'auto' )
	{
		htmlstr += 'width:' + width + 'px';
	}

	if( height !='auto' )
	{
		htmlstr += 'height:' + height +'px';
	}
	htmlstr += '"><div id="'+wwID+'handle" class="widgethandle"><img src="/admin/icons/cancel.png" onClick="close_widget_window(\'' + wwID + '\')" /></div>';
	htmlstr += '<div class="widgetinner" id="'+wwID+'inner"></div></div>';
	$('body *:first-child').before(htmlstr);

	$('#'+wwID+'inner').load(loadurl);
	$('#'+wwID).fadeIn('slow');
	$('#'+wwID).draggable({handle:'#'+wwID+'handle'});
	
	windowZindex ++;
}

function close_widget_window(wwID)
{
	if( $('#' + wwID).css('z-index') >= windowZindex )
	{
		windowZindex --;
	}
	$('#' + wwID).fadeOut('slow').remove();
}

function get_window_size() 
{
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) 
	{
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} 
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
	{
    	//IE 6+ in 'standards compliant mode'
	    myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} 
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
	{
    	//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
  	}
	
	var size = new Array(myWidth, myHeight);
	return size;
}



function show_cal( month, year, size )
{
	$('#serveCal').load(calendar_script + '?size='+ size + '&ajax=1&year='+ year +'&month='+ month );
}

function find_pos(obj) 
{
	var curleft = curtop = 0;
	if (obj.offsetParent) 
	{
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) 
		{
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
