$(document).ready(function() {

	/*
		Handle locale selection
	*/
	
	$('div#header div div#locale a#selector').click(function(e) {
		e.stopPropagation();
		$('div#header div div#locale div#territories').css('left', $(this).offset().left).toggle($.browser.msie ? 0 : 100, function() {
			var that = this;
			$(document).click(function() {
				$(that).hide();
			});
		});
	});
	
	
	/*
		Handle territory selection
	*/
	
	$('div#header div div#locale div#territories table tr td ul li a').click(function() {
		var t = $(this).attr('href').substring(4, 6);
		var l = $('div#header div div#locale div#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 vertical tabs
	*/
	
	$('ul.vert-tab li a').click(function() {
		var rel = $(this).attr('rel');
		window.location.hash = rel;
		$(this).parent().addClass('vert-tab-sel').siblings().removeClass('vert-tab-sel');
		if (rel == '*') {
			$('div[id^="vert-tab-' + $(this).parent().parent().attr('id') + '"]').show();
		} else {
			$('div#vert-tab-' + $(this).parent().parent().attr('id') + '-' + rel).show().siblings().hide();
		}
		$('html,body').animate({scrollTop: 0}, 300);
		this.blur();
		return false;
	});
	
	var vertTabInit = $('ul.vert-tab li a[rel='+window.location.hash.substring(1)+']:first');
	
	if (vertTabInit.length) {
		vertTabInit.click();
	} else {
		$('ul.vert-tab li a:first').click();
	}

	/*
		Handle the quickseach
	*/
	
	$('div#quick form input')
		.focus(function() {
			if (this.value == $(this).attr('rel'))	{
				this.value = '';
				this.style.color = '#000000';
			}
		})
		.blur(function() {
			if (this.value == '') {
				this.value = $(this).attr('rel');
				this.style.color = '#aeaeae';
			}
		});
		
	$('div#quick form').submit(function() {
		var inp = $('input#find-name', this);
		if (inp.val() == $(this).attr('rel')) {
			if (!($.browser.msie && $.browser.version <= 7)) {
				inp.css('background', '#ffe6dd');
				setTimeout(function() {
					inp.css('background', 'white');
				}, 300);
			}
			return false;
		} 
		return true;
	});
	
	
	/*
		Handle the applicant log-in form ad associated UIs
	*/

	$('form#loginApplicant input')
		.focus(function() {
			if (this.value == '********')	{
				this.value = '';
				this.style.color = '#000000';
			}
		})
		.blur(function() {
			if (this.value == '') {
				this.value = '********';
				this.style.color = '#aeaeae';
			}
		});
	
	$('form#loginApplicant').submit(function() {
		var inp = $('input', this);
		if (inp.val() == '' || inp.val() == '********') {
			if (!($.browser.msie && $.browser.version <= 7)) {
				inp.css('background', '#ffe6dd');
				setTimeout(function() {
					inp.css('background', 'white');
				}, 300);
			}
			return false;
		}
		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();
				
			});
		}
		
	});


	
});



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;
	
}