var PJMatch = {
	resultData: false,
	resultInfo: false,
	resultUls: false,
	html: false, body: false, cont: false,
	optionBox: false, optionLink: false, optionNewContact: false, optionMerge: false, optionImg: false,
	inOptionBox: false,
	optionBoxInt: false, optionBoxInc: 50, optionBoxTime: 20, optionBoxLimH: 150, optionBoxLimW: 500,
	events: Array('click', 'mouseover', 'close', 'keydown', 'unload'),
	
	init: function() {
		PJMatch.resultData = document.getElementById('resultData');
		
		if(!PJMatch.resultData) {
			return;
		}		
		
		PJMatch.resultInfo = PJMatch.resultData.getElementsByTagName('tr');
		PJMatch.optionBox = document.getElementById('optionBox');
		PJMatch.resultUls = Array();		
		
		for(var i = 0; i < PJMatch.resultInfo.length; i++) {
			tr = PJMatch.resultInfo[i];
			
			// just assign the mouseovers and mouseouts
			// if it isn't the "No Results" row...
			if(tr.id != 'noResults') {
				Lib.addEvent(tr, 'mouseover', PJMatch.hoverUi);
				Lib.addEvent(tr, 'mouseout', PJMatch.unhoverUi);
				Lib.addEvent(tr, 'click', PJMatch.enableUi);
			}
		}
		
		// init some more objects...
		PJMatch.html = document.getElementsByTagName('html')[0];
		PJMatch.body = document.getElementsByTagName('body')[0];
		PJMatch.cont = document.getElementById('container');
		
		// get our links
		PJMatch.optionLink = document.getElementById('option_link');
		PJMatch.optionNewContact = document.getElementById('option_newContact');
		PJMatch.optionMerge = document.getElementById('option_merge');
		PJMatch.optionImg = document.getElementById('optionImg');
		
		// now add some events to the option box...
		Lib.addEvent(document.getElementById('optionClose'), 'click', PJMatch.disableUi);
		PJMatch.optionImg.style.display = 'none';
	},
	
	hoverUi: function(e) {
		if(PJMatch.inOptionBox || !(el = Lib.getTarget(e))) {
			return;
		}
		
		if(!(el = Lib.ascendDOM(el, 'tr'))) {
			return;
		}
		
		Lib.addClass(el, 'hover');
	},
	
	unhoverUi: function(e) {
		if(!(el = Lib.getTarget(e))) {
			return;
		}
		
		if(!(el = Lib.ascendDOM(el, 'tr'))) {
			return;
		}
		
		Lib.removeClass(el, 'hover');
	},
	
	enableUi: function(e) {
		if(PJMatch.inOptionBox || !(el = Lib.getTarget(e))) {
			return;
		}
		
		if(!(el = Lib.ascendDOM(el, 'tr'))) {
			return;
		}
		
		PJMatch.enableOptionBox(el);
	},
	
	disableUi: function(e) {
		if(!(el = Lib.getTarget(e))) {
			return;
		}
		
		PJMatch.disableOptionBox();
	},
	
	enableOptionBox: function(tr) {
		PJMatch.optionBox.uptoWidth = PJMatch.optionBoxLimW;
		PJMatch.optionBox.uptoHeight = PJMatch.optionBoxLimH;
		PJMatch.optionBox.style.width = PJMatch.optionBox.curWidth = 0;
		PJMatch.optionBox.style.height = PJMatch.optionBox.curHeight = 0;
		Lib.addClass(PJMatch.optionBox, 'enable');
		PJMatch.inOptionBox = true;
		
		// insert place holders...
		placeHolders = PJMatch.optionBox.getElementsByTagName('strong');
		for(var i in placeHolders) {
			placeHolders[i].innerHTML = tr.getAttribute('companyName');
		}
		
		// insert our links...
		PJMatch.optionLink.href = './admin.php?f=pendjob&do=link&j=' + tr.getAttribute('myJobId') + '&comp=' + tr.getAttribute('companyId') + '&con=' + tr.getAttribute('contactId') + '&confirm=1';
		PJMatch.optionNewContact.href = './admin.php?f=pendjob&do=link_newContact&j=' + tr.getAttribute('myJobId') + '&comp=' + tr.getAttribute('companyId') + '&con=' + tr.getAttribute('contactId') + '&confirm=1';
		PJMatch.optionMerge.href = './admin.php?f=pendjob&do=link_merge&j=' + tr.getAttribute('myJobId') + '&comp=' + tr.getAttribute('companyId') + '&con=' + tr.getAttribute('contactId');
		
		if(tr.getAttribute('weight') == 100) {
			PJMatch.optionLink.parentNode.style.display = 'list-item';
			PJMatch.optionNewContact.parentNode.style.display = 'none';
			PJMatch.optionMerge.parentNode.style.display = 'none';
		}
		
		else {
			PJMatch.optionLink.parentNode.style.display = 'none';
			PJMatch.optionNewContact.parentNode.style.display = 'list-item';
			PJMatch.optionMerge.parentNode.style.display = 'list-item';
		}
		
		// fancy display...
		PJMatch.optionBoxInt = window.setInterval(PJMatch.showOptionBox, PJMatch.optionBoxTime);
		
		// nothing should happen on the outside world...
		PJMatch.disableWorld();
	},
	
		showOptionBox: function() {
			// quick access for limits...
			limW = PJMatch.optionBox.uptoWidth;
			limH = PJMatch.optionBox.uptoHeight;
			
			incW = 0;
			incH = 0;
			
			// calculate ratio...
			if(limW == limH) {
				incW = incH = PJMatch.optionBoxInc;
			}
			
			else if(limW > limH) {
				incW = PJMatch.optionBoxInc;
				incH = limH / (limW / incW);
			}
			
			else {
				incH = PJMatch.optionBoxInc;
				incW = limW / (limH / incH);
			}
			
			PJMatch.optionBox.curWidth += incW;
			PJMatch.optionBox.curHeight += incH;
			
			// stop!
			if(PJMatch.optionBox.curWidth >= limW || PJMatch.optionBox.curHeight >= limH) {
				PJMatch.optionBox.style.width = limW + 'px';
				PJMatch.optionBox.style.height = limH + 'px';
				window.clearInterval(PJMatch.optionBoxInt);
				PJMatch.optionImg.style.display = 'block';
				return;
			}
			
			PJMatch.optionBox.style.width = PJMatch.optionBox.curWidth + 'px';
			PJMatch.optionBox.style.height = PJMatch.optionBox.curHeight + 'px';
		},
	
	disableOptionBox: function() {
		Lib.removeClass(PJMatch.optionBox, 'enable');
		PJMatch.inOptionBox = false;
		PJMatch.optionImg.style.display = 'none';
		
		// back to reality...
		PJMatch.enableWorld();
	},
	
	disableWorld: function() {
		Lib.addClass(PJMatch.cont, 'opaque');
	},
	
	enableWorld: function() {
		Lib.removeClass(PJMatch.cont, 'opaque');
	}
}

Lib.addEvent(window, 'load', PJMatch.init);