TPA = {
	FORM_NOT_CHANGED : 'Kérem, adjon meg szűkítő feltételeket',
	alignText : function() {
		var t = document.viewport.getDimensions().height / 2;
		if (t == 0) {
			t = document.body.clientHeight / 2;
		}
		var divTop = t-150;
		if(!Prototype.Browser.IE) {
			divTop += 100;
		}
		if(divTop < 0) {
			divTop = 0;
		}
		$('movable').height = divTop + 'px';
		var l = document.viewport.getDimensions().width / 2;
		if (l == 0) {
			l = document.body.clientWidth / 2;
		}
		var divLeft = l-390;
		if(divLeft < 10) {
			divLeft = 10;
		}
		$$('.sideMovable').each(function(elem){
			elem.setStyle({'width': divLeft + 'px'});
		});
	},
	isChanged : function() {
		$$('.changeable').each(function(input) {
			if(input.type == 'text') {
				if(!input.value.blank()) {
					$('isChanged').value = '1';
				} else if(input.name == 'journalKeyWord' && $('journalName') != null) {
					$('isChanged').value = '1';
				} else if(input.name == 'instituteKeyWord' && $('instituteName') != null) {
					$('isChanged').value = '1';
				}
			} else if(input.type.startsWith('select')) {
				if(input.options[0].value != $F(input)) {
					$('isChanged').value = '1';
				}
			}			
		});
		if(!$('author') && $('isChanged')) {
			$('isChanged').value = '1';
		}
	},
	gotoSearchForm : function() {
		window.open('wrapper.jsp', '_top');
	},
	gotoSources : function() {
    	window.open('wrapper.jsp?contentType=sourceList', '_top');
    },
    gotoSourceFormat : function(pageType) {
    	window.open('wrapper.jsp?contentType=sourceFormat&pageType=' + pageType, '_top');
    },
    gotoSourceSort : function() {
    	window.open('wrapper.jsp?contentType=sourceSort', '_top');
    },
    gotoSourceStat : function() {
    	TPA.isChanged();
    	if($('isChanged') && $F('isChanged') == '0') {
        	alert(TPA.FORM_NOT_CHANGED);
        } else {
        	window.open('wrapper.jsp?contentType=sourceStat', '_top');
        }    	
    },
    gotoReferer : function() {
    	window.open('wrapper.jsp?contentType=refererList', '_top');
    },
    gotoKPAList : function() {
    	window.open('wrapper.jsp?contentType=kpaList', '_top');
    },
    gotoRefererFormat : function(pageType) {
    	TPA.isChanged();
    	if($('isChanged') && $F('isChanged') == '0') {
        	alert(TPA.FORM_NOT_CHANGED);
        } else {
        	window.open('wrapper.jsp?contentType=refererFormat&pageType=' + pageType, '_top');
        }    	
    },
    gotoRefererSort : function() {
    	TPA.isChanged();
    	if($('isChanged') && $F('isChanged') == '0') {
        	alert(TPA.FORM_NOT_CHANGED);
        } else {
        	window.open('wrapper.jsp?contentType=refererSort', '_top');
        }    	
    },
    gotoRefererStat : function() {
    	TPA.isChanged();
    	if($('isChanged') && $F('isChanged') == '0') {
        	alert(TPA.FORM_NOT_CHANGED);
        } else {
        	window.open('wrapper.jsp?contentType=refererStat', '_top');
        }    	
    },
    _moveMenu : function() {
    	var menuTop = document.viewport.getScrollOffsets().top + 10;
    	if($('resizableMenuCell')) {
    		$('resizableMenuCell').setStyle({height : menuTop + 'px'});
    	}
    },
    fireScroll : function() {
    	TPA._moveMenu();
    }
}

TPA.Sort = {
	updateBackgound : function(link, color){
		link.style.backgroundColor = color;
	}
}

TPA.Sort.Source = {
	init : function() {
		TPA.Sort.Source.clearSortByCount(5);
	},
	clearSortByCount : function(count){
	  	for(var i=1; i<=count; i++){
	  		var link = $('SORTDIV' + i);
	  		$('SORT' + i).update('');
	  		link.observe('click', TPA.Sort.Source.fillInnerText);
	  		link.observe('mouseover', TPA.Sort.Source.toSelected);
	  		link.observe('mouseout', TPA.Sort.Source.toUnSelected);
	  		TPA.Sort.updateBackgound(link, '#E7F2B8');
	  	}
		$('sortindex').value = 1;
	  	$('ordering').value = '';
	},
	toSelected : function(event) {
		var link = event.findElement('td');
		TPA.Sort.updateBackgound(link, '#C5E168');
	},
	toUnSelected : function(event) {
		var link = event.findElement('td');
		TPA.Sort.updateBackgound(link, '#E7F2B8');
	},
	arrowClick : function(index){
		var s = $('SORTIMG' + index).src;
		var direction = $F('direction');
	  	if(s.indexOf('arrow_up.gif') > 0){
	  		$('SORTIMG' + index).src = 'images/source_arrow_down.gif';
			direction = direction.substr(0, index-1) + '0' + direction.substr(index);
	  	} else {
	  		$('SORTIMG' + index).src = 'images/source_arrow_up.gif';
	  		direction = direction.substr(0, index-1) + '1' + direction.substr(index);
	  	}
	  	$('direction').value = direction;
	},
	fillInnerText : function(event){
		var link = event.findElement('td');
		link.stopObserving('click', TPA.Sort.Source.fillInnerText);
	    link.stopObserving('mouseover', TPA.Sort.Source.toSelected);
	    link.stopObserving('mouseout', TPA.Sort.Source.toUnSelected);
	    var index = $F('sortindex');
	    var text = link.textContent || link.innerText;
	    $('SORT' + index).update(text);
		$('ordering').value += link.title;
	  	index++;
	  	$('sortindex').value = index;
	}
}
TPA.Sort.Referer = {
	init : function(){
		TPA.Sort.Referer.clearSortByCount(2);
	},
	clearSortByCount : function(count){
	  	for(var i=1; i<=count; i++){
	  		var link = $('SORTDIV' + i);
	  		$('SORT' + i).update('');
	  		link.observe('click', TPA.Sort.Referer.fillInnerText);
	  		link.observe('mouseover', TPA.Sort.Referer.toSelected);
	  		link.observe('mouseout', TPA.Sort.Referer.toUnSelected);
	  		TPA.Sort.updateBackgound(link, '#FFE3B3');
	  	}
		$('sortindex').value = 1;
	  	$('ordering').value = '';
	},
	toSelected : function(event){
		var link = event.findElement('td');
		TPA.Sort.updateBackgound(link, '#FBC370');
	},
	toUnSelected : function(event){
		var link = event.findElement('td');
		TPA.Sort.updateBackgound(link, '#FFE3B3');
	},
	arrowClick : function( index ){
	  	var s = $('SORTIMG' + index).src;
	  	var direction = $F('direction');
	  	if(s.indexOf('arrow_up.gif') > 0){
	  		$('SORTIMG' + index).src = 'images/referer_arrow_down.gif';
			direction = direction.substr(0, index-1) + '0' + direction.substr(index);
	  	} else {
	  		$('SORTIMG' + index).src = 'images/referer_arrow_up.gif';
	  		direction = direction.substr(0, index-1) + '1' + direction.substr(index);
	  	}
	  	$('direction').value = direction;
	},
	fillInnerText : function(event){
		var link = event.findElement('td');
		link.stopObserving('click', TPA.Sort.Referer.fillInnerText);
	    link.stopObserving('mouseover', TPA.Sort.Referer.toSelected);
	    link.stopObserving('mouseout', TPA.Sort.Referer.toUnSelected);
	    var index = $F('sortindex');
	    var text = link.textContent || link.innerText;
	    $('SORT' + index).update(text);
		$('ordering').value += link.title;
	  	index++;
	  	$('sortindex').value = index;
	}
}
