jQuery.fn.center = function (fixed) {
	fixed = (typeof fixed == 'undefined') ? false : fixed;
    this.css("position", fixed ? "fixed" : "absolute");
    this.css("top",  ( $(window).height() - this.height() ) / 2+$(window).scrollTop()  + "px");
    this.css("left", ( $(window).width() - this.width() )   / 2+$(window).scrollLeft() + "px");
    return this;
}

$(document).ready(function() {

	/*
		Handle locale selection
	*/
	
	$('div#header div div#header-locale a#header-locale-selector').click(function(e) {
		e.stopPropagation();
		var position = $(this).position();
		$('div#header div div#header-locale div#header-locale-territories')
		.css('left', position.left).toggle($.browser.msie ? 0 : 100, function() {
			var that = this;
			$(document).click(function() {
				$(that).hide();
			});
		});
	});
	
	
	/*
		Handle territory selection
	*/
	
	$('div#header div div#header-locale div#header-locale-territories table tr td ul li a').click(function() {
		var t = $(this).attr('href').substring(4, 6);
		var l = $('div#header div div#header-locale div#header-locale-territories:first').attr('location');
		if (t != '00' && t != l) {
			return confirm(
					'It appears that you are currently not in ' + $(this).text()
				+ '\n\n'
				+ 'In most cases you should choose the country according to your citizenship.'
				+ '\n\n'
				+ 'Do you want to continue?'
			);
		}
		return true;
	});
	

	/*
		Handle the quickseach
	*/
		
	$('form#quick').submit(function() {
		var inp = $('input[name="find-name"]', this);
		if (inp.val() == inp.attr('label')) {
			if (!($.browser.msie && $.browser.version <= 7)) {
				inp.css('background', '#ffe6dd');
				setTimeout(function() {
					inp.css('background', 'white');
				}, 300);
			}
			return false;
		}
		return true;
	});
	
	$('form#quick input[name="find-name"]').focus(function() {
		$(this).siblings('div.find-panel').slideDown('fast');
	}).blur(function() {
		var that = this;
		setTimeout(function() {
			if (that != document.activeElement){
				$(that).siblings('div.find-panel').slideUp('fast');
			}
		}, 200);
	});
	
	$('div.find-panel').click(function() {
		$('form#quick input[name="find-name"]').focus();
		return true;
	});
	
	
	
	/*
		Handle the institution log-in form ad associated UIs
	*/

	$('form#loginInstitution input')
		
		.focus(function() {
			if (this.value == 'institution code' || this.value == '........')	{
				this.value = '';
				try { this.type = 'password'; } catch(err) {}
				this.style.color = '#000000';
				this.focus();
				this.select();
			}
		})
		
		.blur(function() {
			if (this.value == '') {
				this.value = 'institution code';
				try { this.type = 'text'; } catch(err) { this.value = '........'; }
				this.style.color = '#aeaeae';
			}
		})
		
		.val('institution code');
		
	try {
		$('form#loginInstitution input').get(0).type = 'text';
	} catch(err) {
		$('form#loginInstitution input').val('........');
	}
	
	$('form#loginInstitution').submit(function() {
		if($('input', this).val() == '' || $('input', this).val() == 'institution code') {
			return false;
		}
	});


	/*
		Handle the feedback sections
	*/
	
	$('div.feedback input').click(function() {
		
		var fb = $(this).parent();
		var vl = $(this).val();
		
		fb.find('input,label').remove();
		
		if (vl == 'Yes') {
			fb.find('span:first').show();
			$.get('/index/feedback/helpful/' + vl);
				
		} else {
			
			fb.find('span:eq(1), p:first').show();
			$.get('/index/feedback/helpful/' + vl);
						
			$('button', fb).click(function() {
				
				$.get('/index/feedback/helpful/' + vl + '/comments/' + $(this).siblings('textarea:first').val());
				
				fb.find('textarea,button,span,p').hide();
				fb.find('span:last').show();
				
			});
		}
		
	});
	
	
	/*
	 *	Handle the toggle label/inputs
	 */
	
	$('input.toggle-label').focus(function(){
		
		if ($(this).val() == $(this).attr('label')) {
			$(this).val('');
		}
		
		$(this).siblings('.input-tip').slideDown('fast');
		
	}).blur(function() {
		
		if ($(this).val() == '') {
			$(this).val($(this).attr('label'));
		}
		
		$(this).siblings('.input-tip').slideUp('fast');
		
	}).each(function() {
		$(this).triggerHandler('blur');
	});

	
});



function viewApplication(applicationID, uriHash) {
	return openA4('/application/view/id/' + applicationID, uriHash);	
}

function openA4(uri, uriHash) {
	return openWnd(uri, uriHash, 720, screen.availHeight-150, 50, (screen.width-720) / 2);
}

function openMax(uri, uriHash) {
	
	var w = screen.width - 10;
	if (w > 950) { w = 950; }
	
	var h = screen.availHeight-100;
	if (h > 700) { h = 700; }
	
	return openWnd(uri, uriHash, w, h, 50, (screen.width-w) / 2);	
	
}

function openWnd(uri, uriHash, w, h, t, l) {
	
	// If URI has is not specified
	if (typeof uriHash == 'undefined') {
		uriHash = '';
	}
	
	// If using buggy Chrome
	var isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;

	// Generate the window name
	var wndName = uri.replace(/\//g, '_');
	
	// Complite the options
	var wndOptions = 'top=' + t + ',left=' + l + ',height=' + h + ',width=' + w + ',status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1';
	
	// Open up a stup wit the proper name but no uri
	var wnd = window.open('',wndName, wndOptions);
	
	// If the URI is about:blank or the same as opener (Opera)
	if (wnd.location == 'about:blank' || wnd.location.href == self.location.href) {
		
		// Load up the URI
		wnd.location = uri + uriHash;
		
	}
	
	// If using Chrome, blur the parent first
	if (isChrome) { wnd.parent.blur(); }
	
	// Bring the window to focus
	wnd.focus();
	
	return false;
	
}


function openLayer(html, w, h) {
	
	var css = {
		cursor: 'auto',
		overflow: 'auto',
		border: 'none',
		textAlign: 'left',
		padding: '30px',
		'-webkit-border-radius': '6px',
		'-moz-border-radius': '6px',
		'border-radius': '6px'
	};
	
	if (typeof w != 'undefined') {
		css.width = w + 'px';
	}
	
	if (typeof h != 'undefined') {
		css.height = h + 'px';
	}
	
	$('body>h2.layer-title').remove();
	
	$.blockUI({
		message: html,
		css: css,
		overlayCSS: {
			cursor: 'auto'
		}
	});

	var title = $('div.blockMsg').center().find('h2.layer-title');
	
	if (title.length) {
		
		$('div.blockMsg').css({
			'-webkit-border-top-left-radius': '0',
			'-webkit-border-top-right-radius': '0',
			'-moz-border-radius-topleft': '0',
			'-moz-border-radius-topright': '0',
			'border-top-left-radius': '0',
			'border-top-right-radius': '0'
		});
		
		title.find('a').click(function() {
			$.unblockUI();
			title.fadeOut();
			return false;
		});
	
		title.appendTo(document.body).css({
			width: (w + 40) + 'px',
			top: $('div.blockMsg').offset().top,
			left: $('div.blockMsg').offset().left
		});
	
	}

	$('div.blockOverlay').css('cursor', 'pointer').click(function() {
		$.unblockUI();
		title.fadeOut();
		return false;
	});
	
}
