/*
function checkBrowser(){
alert('asd')
if ( Prototype.Browser.Gecko != true ){
alert('System nie wspiera wybranej przeglądarki.');
}
}
*/
window.onresize = function () {
	CRM.fixWindowSize();
}


var CRM = {
	Version: '1.0',

	fixWindowSize: function(){
		var windowH = window.innerHeight;
		var windowW = window.innerWidth;
		var containerH = $('container').offsetHeight;
		if ( $('navigon') ){//poprawki dla navigon
			var width = windowW - 400;
			$('navigon').style.width = width+'px';
		}
		if ( $('b1_Content')){
			$('b1_Content').style.height = '100%';
		}
		if ( $('bgContent')){
			$('bgContent').style.height = '100%';
		}

		//poprawka jesli jest navigon i b1
		if ( $('bgContent') && $('b1_Content')){
			var bgContentH = $('bgContent').offsetHeight;
			var b1_ContentH = $('b1_Content').offsetHeight;
			if ( bgContentH >= b1_ContentH ){
				$('b1_Content').style.height = bgContentH-21+'px';
				var height = bgContentH-21;
			}else{
				$('bgContent').style.height = b1_ContentH+'px';
				var height = b1_ContentH;
			}
		}
		if ( $('taskListWindow') && $('b1_Content')){
			var bgContentH = $('taskListWindow').offsetHeight;
			var b1_ContentH = $('b1_Content').offsetHeight;
			if ( bgContentH >= b1_ContentH ){
				$('b1_Content').style.height = bgContentH-21+'px';
				var height = bgContentH-21;
			}else{
				$('taskListWindow').style.height = b1_ContentH-9+'px';
				var height = b1_ContentH;
			}
		}
		if ( height )
		$('a1').style.height = height+200+'px';
		if ( $('a1').offsetHeight < windowH ){
			$('a1').style.height = containerH-40+'px';
		}

		//dla webmail
		if( $('webmailFrame') ){
			$('webmailFrame').style.height = windowH-150+'px';
			$('a1').style.height = windowH-40+'px';
		}
	},
};

function actionLogin( actionName, actionType ){
	new Ajax.Request( 'admin.php?class=' + actionName + '&type=' + actionType,
	{
		method: 'post',
		parameters: {login: $F('login'), password: $F('password')},
		evalScripts: true,
		onComplete: function(transport){
			response = transport.responseText;
			//var temp = response.match(/<script>.*<\/script>/, '');
			//var script = temp[0].replace('<script>','');
			//script = script.replace('</script>','');
			//eval("(" + script + ")");
			
			$('container').innerHTML = response;
			//zaladownie funkcji po zalogowaniu
			actionControl( 'viewAdminCalendarDay', 'view', false, 'navigon' );
			actionControl( 'viewAdminCalendar', 'view', false, 'b1' );
			if( $('script') ){
				var script = $('script').innerHTML.replace('<script>','');
				script = script.replace('</script>','');
				eval(script);
			}
		}
	}
	);
	return false;
}

function actionLogout( actionName, actionType ){
	$('loader').show();
	new Ajax.Request( 'admin.php?class=' + actionName + '&type=' + actionType,
	{
		method: 'get',
		evalScripts: true,
		onComplete: function(transport){
			response = transport.responseText;
			$('container').innerHTML = response;
			$('loader').hide();
		}
	}
	);
}

function actionControl( actionName, actionType, intId, div, strUser, arrParam, strSearch ){
	if ( !$('b1') && !$('navigon') && ( div == 'b1' || div == 'navigon' )){
		$('a2').innerHTML = '<div id="b1"></div><div id="navigon"></div><div style="clear: both;"></div>';
	}
	$('loader').show();
	$('divMessage').innerHTML = '';
	new Ajax.Request( 'admin.php?class=' + actionName + '&type=' + actionType,
	{
		method: 'post',
		evalScripts: true,
		parameters: { intId: intId, arrParam: arrParam,strSearch:strSearch  },
		onComplete: function(transport){
			response = transport.responseText;
			$(div).innerHTML = response;
			$('loader').hide();
			CRM.fixWindowSize();
			switch(actionName){
				case 'viewAdminTaskList':
				case 'viewAdminCalendarDay':
				case 'viewAdminCalendarWeek':
				case 'viewAdminCalendarMonth':
				case 'viewAdminTaskAddForm':{
					var arrBookmarkElements = $$('#navigon_menu .BookmarkLine01');
					arrBookmarkElements.each ( function(item){
						$(item).setStyle({
						'borderTop' : '1px solid #7E7E7E'
						});
					}
					);
					if ( $(actionName+'Bookmark') ){
						$(actionName+'Bookmark').setStyle({
						'borderTop' : '1px solid #FFF'
						});
					}
				}
				break;
				case 'logicAdminTaskAdd':
				break;
				default:
				break;
			}
			if ( actionName == 'viewAdminCalendarDay' || actionName == 'viewAdminCalendarWeek' ){
				startObserve();
				make_dropable_and_dragable();
			}
			if ( actionName == 'viewAdminTaskList' ){
				//make_dragable();
			}
			if ( actionName == 'viewAdminContextMenu' ){
				Sortable.create("ulNavigon",{constraint:false,dropOnEmpty:true});
			}
			if ( actionName == 'viewAdminCalendarWeek' ){
				var myWidth = getWidth();
				if ( myWidth > 1510 ){
					var count = 3;
				}
				else if ( myWidth > 1200 ){
					var count = 2;
				}
				else{
					var count = 1;
				}
				var arrElements = $$('#navigon_menu .boxFix');
				arrElements.each ( function(item){
					var arrTemp = item.id.split("|");
					var i = arrTemp[2];
					var x = i/count;
					var h = 25*x;
					$(item).setStyle({
					'height' : h+'px'
					});
					//alert(item.id);
				}
				);
			}
			self.scrollTo(0, 0);
		}
	}
	);
}

function actionControl2( actionName, actionType, intId2, div, search, strUser, arrParam ){
	if ( !$('b1') && !$('navigon') && ( div == 'b1' || div == 'navigon' )){
		$('a2').innerHTML = '<div id="b1"></div><div id="navigon"></div><div style="clear: both;"></div>';
	}
	$('loader').show();
	$('divMessage').innerHTML = '';
	new Ajax.Request( 'admin.php?class=' + actionName + '&type=' + actionType,
	{
		method: 'post',
		evalScripts: true,
		parameters: { intId2: intId2, arrParam: arrParam, search:search },
		onComplete: function(transport){
			response = transport.responseText;
			$(div).innerHTML = response;
			$('loader').hide();
			CRM.fixWindowSize();
			switch(actionName){
				case 'viewAdminTaskList':
				case 'viewAdminCalendarDay':
				case 'viewAdminCalendarWeek':
				case 'viewAdminCalendarMonth':
				case 'viewAdminTaskAddForm':{
					var arrBookmarkElements = $$('#navigon_menu .BookmarkLine01');
					arrBookmarkElements.each ( function(item){
						$(item).setStyle({
						'borderTop' : '1px solid #7E7E7E'
						});
					}
					);
					if ( $(actionName+'Bookmark') ){
						$(actionName+'Bookmark').setStyle({
						'borderTop' : '1px solid #FFF'
						});
					}
				}
				break;
				case 'logicAdminTaskAdd':
				break;
				default:
				break;
			}
			if ( actionName == 'viewAdminCalendarDay' || actionName == 'viewAdminCalendarWeek' ){
				startObserve();
				make_dropable_and_dragable();
			}
			if ( actionName == 'viewAdminTaskList' ){
				//make_dragable();
			}
			if ( actionName == 'viewAdminContextMenu' ){
				Sortable.create("ulNavigon",{constraint:false,dropOnEmpty:true});
			}
			if ( actionName == 'viewAdminCalendarWeek' ){
				var myWidth = getWidth();
				if ( myWidth > 1510 ){
					var count = 3;
				}
				else if ( myWidth > 1200 ){
					var count = 2;
				}
				else{
					var count = 1;
				}
				var arrElements = $$('#navigon_menu .boxFix');
				arrElements.each ( function(item){
					var arrTemp = item.id.split("|");
					var i = arrTemp[2];
					var x = i/count;
					var h = 25*x;
					$(item).setStyle({
					'height' : h+'px'
					});
					//alert(item.id);
				}
				);
			}
			self.scrollTo(0, 0);
		}
	}
	);
}

function changePosition()
{
	new Ajax.Request( 'admin.php?class=logicAdminPageChangePosition&type=logic',
	{
		method: 'post',
		parameters: { list: Sortable.serialize('firstlist') }
	}
	);
}

function navi( state, bookmarkActive, strUser )
{
	var arrElements = $$('#bgContentTop .BookmarkLine');
	arrElements.each ( function(item){
		if ( item.id != bookmarkActive.id )
		item.style.display = 'none';
	}
	);
	switch(state){
		case 'calendar':
		actionControl( 'viewAdminCalendar', 'view', false, 'b1' );
		actionControl( 'viewAdminCalendarDay', 'view', false, 'navigon', strUser );
		bookmarkActive.style.display = 'block';
		break;
		case 'database':
		actionControl( 'viewAdminDatabase', 'view', false, 'a2' );
		bookmarkActive.style.display = 'block';
		break;
		case 'settings':
		actionControl( 'viewAdminMySettings', 'view', false, 'a2' );
		bookmarkActive.style.display = 'block';
		break;
		case 'construction':
		actionControl( 'viewAdminConstruction', 'view', false, 'a2' );
		bookmarkActive.style.display = 'block';
		break;
		case 'webmail':
		actionControl( 'viewAdminWebmail', 'view', false, 'a2' );
		bookmarkActive.style.display = 'block';
		break;
		case 'admin':
		actionControl( 'viewAdminContextMenu', 'view', false, 'b1' );
		actionControl( 'viewAdminUsersList', 'view', false, 'navigon' );
		bookmarkActive.style.display = 'block';
		break;
		default:
		break;
	}
}

function checkEnter(e){
	var characterCode; //literal character code will be stored in this variable;

	if(e && e.which){
		e = e;
		characterCode = e.which;
	}
	else{
		e = e;
		characterCode = e.keyCode ;
	}

	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		actionLogin( 'logicAdminLogin', 'logic' );
		return false;
	}
	else{
		return true;
	}

}

function addUserToMyCalendar( intId, strColor ){
	$('loader').show();

	new Ajax.Request( 'admin.php?class=logicAdminCalendarAddUser&type=logic',
	{
		method: 'post',
		evalScripts: true,
		parameters: { intId: intId, strColor: strColor },
		onComplete: function(transport){
			response = transport.responseText;
			//$('container').innerHTML = response;
			$('loader').hide();
			makeAlert( 'Prośba o udostępnienie kalendarza została wysłana.', 'divMessage' );
			//actionControl( 'viewAdminCalendar', 'view', false, 'b1' );
			//actionControl( 'viewAdminCalendarDay', 'view', false, 'navigon' );
			//bookmarkActive.style.display = 'block';
		}
	}
	);
}

function deleteUserCalendar( id, login )
{
	$('loader').show();
	new Ajax.Request( 'admin.php?class=logicAdminCalendarDeleteUser&type=logic',
	{
		method: 'post',
		evalScripts: true,
		parameters: { intId: id },
		onComplete: function(transport){
			response = transport.responseText;
			$('loader').hide();
			$('myCalendar_'+login).remove();
			var arrElements = $$('#bgContent .icoTask_'+id);
			arrElements.each ( function(item){
				item.remove();
			}
			);
		}
	}
	);
}

function makeAlert( msg, div )
{
	var id = parseInt(Math.random(100)*20);
	$(div).innerHTML = '<div class="Message" id="Message|'+id+'">'+msg+'</div>';
	setTimeout( divMessageFade, 2000, 'Message|'+id+'' );
}

function divMessageFade( div ) {
	if ($(div))
	Effect.Fade(div);
}


function getWidth() {
	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;
	}
	return myWidth;
}


function markUnmarkItem( obj, color ){
	var c = $(obj).className;
	if ( c == 'NotMarked' ){
		$(obj).setStyle({
		'background' : color
		});
		$(obj).className = 'IsMarked'
	}else{
		$(obj).setStyle({
		'background' : 'none'
		});
		$(obj).className = 'NotMarked'
	}
}

function saveMySettings()
{
	$('loader').show();
	var form = $('settingsForm');
	var arrNotice = form.getInputs('radio', 'intNotice');
	var intNotice = 0;
	arrNotice.each ( function(item){
		if( $(item).checked == true){
			intNotice = $(item).value;
		}
	}
	);
	var strNoticeForm = '';
	var arrNoticeForm = $$('#settingsForm .noticeForm');
	arrNoticeForm.each ( function(item){
		strNoticeForm = strNoticeForm+'|'+item.value;
	}
	);
	var strNoticeTime = '';
	var arrNoticeTime = $$('#settingsForm .noticeTime');
	arrNoticeTime.each ( function(item){
		strNoticeTime = strNoticeTime+'|'+item.value;
	}
	);
	new Ajax.Request( 'admin.php?class=logicAdminMySettings&type=logic',
	{
		method: 'post',
		parameters: { blnSave: $F('blnSave'), strFirstLastName: $F('strFirstLastName'), strEmail: $F('strEmail'), strEmailSms: $F('strEmailSms'), intNotice: intNotice, strNoticeForm: strNoticeForm, strNoticeTime: strNoticeTime },
		evalScripts: true,
		onComplete: function(transport){
			response = transport.responseText;
			makeAlert( response, 'divMessage' );
			$('loader').hide();
			//$('a2').innerHTML = response;
		}
	}
	);
}

function addAnotherNotice( i ){
	var strInsert = $('addField').innerHTML;
	strInsert = strInsert.replace(/aHrefDelete/gi, "aHrefDelete_"+i);
	strInsert = strInsert.replace(/noticeFormElse/gi, "noticeForm");
	strInsert = strInsert.replace(/noticeTimeElse/gi, "noticeTime");
	strInsert = strInsert.replace(/addedField/gi, "addedField_"+i);
	strInsert = strInsert.replace(/onclick=\"DeleteNotice\(\);\"/gi, "onclick=\"DeleteNotice("+i+");\"");
	new Insertion.Bottom('anotherNotice', strInsert);
	i = i + 1;
	var link = 'addAnotherNotice('+i+');';
	$('addAnotherNotice').innerHTML = '<a class="link10_black" onclick="'+link+'">Dodaj kolejne przypomnienie</a>';
}

function DeleteNotice( i ){
	$('addedField_'+i).remove();
}

function showNoticeTable( obj ){
	if ( obj.value == 2 ){
		$('NoticeTable').show();
	}
}

function startPopUp(){
	new Ajax.Request( 'skrypty/popUp.php',
	{
		method: 'post',
		parameters: {  },
		evalScripts: true,
		onComplete: function(transport){
			response = transport.responseText;
			if ( response.length > 10 )
			autoContentWindow(response);
		}
	}
	);
	setTimeout( startPopUp, 10000 );
}

document.onmousemove = function(e){x=(e=e||event).clientX;y=e.clientY};
