var _moving = null;

function showArchiveBrowserDiv() {
	_moving = 'opening';
	document.getElementById('archiveBrowserDiv').style.display='block';
	document.getElementById('archiveBrowserDiv').style.padding='5px';
	if(isBrowsing()) {
		document.getElementById('archiveBrowserDiv').style.background='#FEF6DB url(sites/mnv/images/archivesmini_bg_big.png) no-repeat scroll left top';
		document.getElementById('cancelBrowsing').style.display='block';
	} else {
		document.getElementById('archiveBrowserDiv').style.background='#FEF6DB url(sites/mnv/images/archivesmini_bg.png) no-repeat scroll left top';
		document.getElementById('cancelBrowsing').style.display='none';
	}
	element = document.getElementById('archiveBrowserDiv');
	//document.getElementById('archiveBrowserDiv').onmouseout='';
	openDiv();
}

var element = null;
var initial_clientX = 0;
var initial_clientY = 0;

function getQueryParam(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function setBrowsingTitle() {
	var contentDIV = document.getElementById('archivebrowserstatediv');
	if(isBrowsing()) {
		var title = getLocString('calendar.archivesbrowser.title') + ' ' + getQueryParam('ayear');
		if(getQueryParam('amonth') != '') {
			title += ' ' + getLocString('calendar.month.' + getQueryParam('amonth'));
		}
		contentDIV.innerHTML = title;
		contentDIV.style.display='block';
		document.getElementById('cancelBrowsingImage').style.display='';
	} else {
		contentDIV.style.display='none';
		document.getElementById('cancelBrowsingImage').style.display='none';
	}
}

function isBrowsing() {
	if(window.location.href.indexOf('ayear=') > -1) {
		return true;
	} else {
		return false;
	}
}

function stopBrowsing() {
	window.location.href = window.location.href.substring(0, window.location.href.indexOf('?'));
	return false;
}
/*
function jopenDiv() {
	jQuery("img.ShowArchives").mouseover(function(){});
	// show
	jQuery("#archiveBrowserDiv").css({
		'display': 'block',
		'opacity': '0'
	});
	jQuery("#innerDiv").css({
		'display': 'block'
	});
	jQuery("#archiveBrowserDiv").animate({
		'width': '200',
		'height': '100',
		'left': '-=200',
		'opacity': '1'
		}, {
			duration : 500,
			easing: 'linear',
			complete : function() {
				jQuery("#archiveBrowserDiv").mouseleave(function(){
					// hide
					jQuery("#archiveBrowserDiv").animate({
						'width': '0',
						'height': '0',
						'left': '+=200',
						'opacity': '0'
						}, 500, 'linear',   function(){jQuery("img.ShowArchives").mouseover(jopenDiv);}
					);
				});
			}
		}
	);
}*/

function jABrowserInit() {

	//temp
	/*jQuery("img.ShowArchives").mouseover(function(){});
	jQuery("#archiveBrowserDiv").css({
		'display': 'block',
		'opacity': '0'
	});
	jQuery("#innerDiv").css({
		'display': 'block'
	});*/
	
	setBrowsingTitle();

	var height = '110';
	if(isBrowsing()) {
		height = '118';
		jQuery('#archiveBrowserDiv').css({ background : '#FEF6DB url(sites/mnv/images/archivesmini_bg_big.png) no-repeat scroll left top'});
		jQuery('#cancelBrowsing').css({ display : 'block'});
	} else {
		jQuery('#archiveBrowserDiv').css({ background : '#FEF6DB url(sites/mnv/images/archivesmini_bg.png) no-repeat scroll left top'});
		jQuery('#cancelBrowsing').css({ display : 'none'});
	}

  jQuery("img.ShowArchives").mouseenter(function(){
					// show
					jQuery("#archiveBrowserDiv").animate({
						'width': '208',
						'height': ''+height,
						'opacity': '1'
						}, 500, 'linear'
					);
				});
  jQuery("#archiveBrowserDiv").mouseleave(function(){
					// hide
					jQuery("#archiveBrowserDiv").animate({
						'width': '0',
						'height': '0',
						'opacity': '0'
						}, 500, 'linear'
					);
				});
}

function openDiv() {
	if(_moving == 'opening') {
		var _left = parseInt(element.style.left.substring(0, 3));
		var _width = parseInt(element.style.width.substring(0, element.style.width.indexOf('px')));
		var _height = parseInt(element.style.height.substring(0, element.style.height.indexOf('px')));
		if(_left > 500) {
			_left -= 20;
			_width += 20;
			if(isBrowsing()) {
				_height += 11;
			} else {
				_height += 10;
			}
			element.style.left = _left + 'px';
			element.style.width = _width + 'px';
			element.style.height = _height + 'px';
			setTimeout('openDiv()', 50);
			return;
		} else {
			element.style.left = '500px';
			document.getElementById('innerDiv').style.display='block';
			_moving = null;
			setTimeout('setOnMouseOut()', 700);
		}
	}
}

function setOnMouseOut() {
	/*document.getElementById('archiveBrowserDiv').onmouseout=function(event) {
		mouseExited(event);
	}*/
	
	var IE = document.all?true:false;
	if (!IE) document.captureEvents(Event.MOUSEMOVE);
	document.onmousemove = getMouseXY;
}

var tempX = 0;
var tempY = 0;

function getMouseXY(e) {
  var IE = document.all?true:false;
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft;
    tempY = event.clientY + document.body.scrollTop;
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX;
    tempY = e.pageY;
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0};
  if (tempY < 0){tempY = 0};
  var minX = document.getElementById('portal').offsetLeft+700;
  var maxX = document.getElementById('portal').offsetLeft+950;
  var minY = 100;
  var maxY = 300;
  if(isBrowsing()) {
	maxY = 310;
  }
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
  if(!((tempX > minX && tempX < maxX
				&& tempY > minY && tempY < maxY))) {
	hideArchiveBrowserDiv();
  }
  return true;
}

function closeDiv() {
	if(_moving == 'closing') {
		var _left = parseInt(element.style.left.substring(0, 3));
		var _width = parseInt(element.style.width.substring(0, element.style.width.indexOf('px')));
		var _height = parseInt(element.style.height.substring(0, element.style.height.indexOf('px')));
		if(_left < 700) {
			_left += 20;
			_width -= 20;
			if(isBrowsing()) {
				_height -= 11;
			} else {
				_height -= 10;
			}
			element.style.left = _left + 'px';
			element.style.width = _width + 'px';
			element.style.height = _height + 'px';
			setTimeout('closeDiv()', 50);
			return;
		} else {
			element.style.left = '700px';
			document.getElementById('archiveBrowserDiv').style.display='none';
			_moving = null;
			//element.onmouseout='';
		}
	}
}

function hideArchiveBrowserDiv() {
	_moving = 'closing';
	document.getElementById('innerDiv').style.display='none';
	document.onmousemove='';
	closeDiv();
}

function mouseExited(e) {
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	
	var isMSIE6 = false;
	var b_version=navigator.appVersion;
	if(b_version.indexOf('MSIE 6.0;') > -1) {
		isMSIE6 = true;
	}
	
	if(targ.id == 'archiveBrowserDiv') {
		/*if(!((e.clientX > 750 && e.clientX < 750+targ.clientWidth 
			&& e.clientY > 96 && e.clientY < 96+targ.clientHeight))) {
			hideArchiveBrowserDiv();
		}*/
		if(isMSIE6) {
			if(!((e.clientX > 875 && e.clientX < 875+targ.clientWidth 
				&& e.clientY > 135 && e.clientY < 135+targ.clientHeight))) {
				hideArchiveBrowserDiv();
			}
		} else {
			if(!((e.clientX > 875 && e.clientX < 875+targ.clientWidth 
				&& e.clientY > 122 && e.clientY < 122+targ.clientHeight))) {
				hideArchiveBrowserDiv();
			}
		}
	}
}

// a naptar leptetese
	function step_calendar( direction )
	{
	        var numYear = parseInt( document.getElementById( 'year' ).innerHTML );
		
		var steppedYear = numYear + direction;

		if (( START_YEAR <= steppedYear ) && ( CURRENT_YEAR >= steppedYear ))
			fill_calendar( steppedYear );
	}

	// feltolti a megfelelo adatokkel a kalendart
	function fill_calendar( _year, _currentmonth )
	{
		if ( pageURL.indexOf('.html') > 0 )
		{
			pageURL = pageURL.substring( 0, pageURL.lastIndexOf('/') );
		}
/*
		else if( document.getElementById('articlecontainer')!=null ) 
		{
			// ez csak defaultcontent lehet, ezert eggyel tobbet vag le
			pageURL = pageURL.substring( 0, pageURL.lastIndexOf('/') );
			pageURL = pageURL.substring( 0, pageURL.lastIndexOf('/') );
		}*/
		
		// levagjuk a parametereket
		if ( pageURL.indexOf('?') > 0 )
		{
			pageURL = pageURL.substring( 0, pageURL.indexOf('?') );

			// kiszedjuk egy korabbi kattintas parameteret
			var queryObj = parseQueryString( location.search );
			if ( queryObj.ayear!=null )
			{
				paramYear = parseInt(queryObj.ayear);
				if (( START_YEAR <= paramYear ) && ( CURRENT_YEAR >= paramYear ))
					_year = paramYear; 
			}
			if ( queryObj.amonth!=null )
			{
				paramMonth = parseInt(queryObj.amonth);
			}
		}

		// ev beirasa
		document.getElementById( 'year' ).innerHTML = ''+_year;

		// ev linkjenek feltoltese
		var yearLink = pageURL + '?' + YEAR_PARAM + '=' + _year;
		document.getElementById( 'yearlink' ).href = yearLink;

		// honapok feltoltese
		for (var i=0; i<12; i++) 
		{
			var monthLink = yearLink + '&' + MONTH_PARAM + '=' + i;

			var monthHTML = '<a href="'+monthLink+'">' + monthz[i] + '</a>';
			if ( _year==CURRENT_YEAR && i>CURRENT_MONTH)
				monthHTML = monthz[i];

			// szinezzuk az aktualisan kattintott honapot
			if (_year == paramYear 	&& i == paramMonth )
				monthHTML = '<span style="text-decoration=underline;  color:#D26115;">' + monthz[i] + '</span>';

			document.getElementById( 'month'+i ).innerHTML = monthHTML;
		}
	}
	
	// parameter parser 
	function parseQueryString(queryString) 
	{
		var queryObject = new Object();

		queryString = queryString.replace(/^.*\?(.+)$/,'$1');

		while ((pair = queryString.match(/([^=]+)=\'?([^\&\']*)\'?\&?/)) && pair[0].length) 
		{
			queryString = queryString.substring( pair[0].length );

			if (/^\-?\d+$/.test(pair[2])) pair[2] = parseInt(pair[2]);
				queryObject[pair[1]] = pair[2];
		}

		return queryObject;
	}

//addLoadEvent(setBrowsingTitle);
addLoadEvent(jABrowserInit);

