

$(document).ready(function() {
	$("ul.sf-menu").supersubs({ 
		minWidth:    10,
		maxWidth:    15,
		extraWidth:  1
	}).superfish({
		speed:'fast',
		autoArrows:    false,
		dropShadows:   false
	});
	$('body').supersleight({
		apply_positioning:false,
		shim:'images/x.gif'
	});
	if ($('#page-content').height()<$('#sidebar').height()) {
		$('#page-content').css({height:($('#sidebar').height()-$('#footer').height())+'px'});
	}
	sweep_external_links();
});

function gl_ajax_dialog(id, url) {
	$("body").append("<div id='modal-window'>" + ajax_dialog(id) + "<div id='mask'></div></div>");
	
	//Get the screen height and width
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();

	//Set height and width to mask to fill up the whole screen
	$('#mask').css({'width':maskWidth,'height':maskHeight});
	$('#mask').css({opacity:0.25});
	$('#mask').click(function() {
		$('#modal-window').remove();
	});
	
	//Get the window height and width
	var winH = $(window).height();
	var winW = $(window).width();
             
	//Set the popup window to center
	//$('#'+id).css({'left':winW/2-$('#'+id).width()/2+100,'top':winH/2-$('#'+id).height()/2});
	$('#'+id).css({'left':100+winW/2-$('#'+id).width()/2,'top':$("body").scrollTop()+10});

	$.ajax({
	    url: url,
	    type: 'POST',
		data:	{'destination':id+'-body'},
		dataType: 'html',
	    timeout: 10000,
	    error: function(){
			$('#modal-window').remove();
	        alert('Error executing calculator request.');
	    },
		beforeSend: function(){
			//do nothing
		},
	    success: function(html){
			$('#'+id+'-body').html(html);
			$('#'+id+'-header').supersleight({
				apply_positioning:false,
				shim:'images/x.gif'
			});
			$('#mask').fadeIn(400);
			$('#'+id).fadeIn(400);
	    }
	});
	
	$(document).keyup(function(e) {   
		if(e.keyCode == 27) {   
			$('#modal-window').remove();
		}   
	});
}

function ajax_dialog(id) {
	html = '<div id="' + id + '" class="ajax-dialog">';
	
	html = html + '<div id="' + id + '-header" class="ajax-dialog-header">';
	html = html + '<a href="javascript:void(0)" onclick="$(\'#modal-window\').remove();">Close</a>';
	html = html + '</div>';
	
	html = html + '<div class="ajax-dialog-container">';
	html = html + '<div id="' + id + '-body" class="ajax-dialog-body">';
	html = html + '</div>';
	html = html + '</div>';
	
	html = html + '</div>';
	return html;
}

function submit_form(which_form, destination, actn) {
	var params = '';
	for(x=0;x<document.forms[which_form].elements.length;x++) {
		params += document.forms[which_form].elements[x].name + '=' + escape(document.forms[which_form].elements[x].value) + '&';
	}
	params += 'destination='+destination;
	
	$.ajax({
	    url: actn,
	    type: 'GET',
		data:	params,
		dataType: 'html',
	    timeout: 10000,
	    error: function(){
			$('#modal-window').remove();
	        alert('Error executing calculator request.');
	    },
		beforeSend: function(){
			//do nothing
		},
	    success: function(html){
			$('#'+destination).html(html);
	    }
	});
}

function sweep_external_links() {
	for(x=0;x<document.links.length;x++) {
		if ((document.links[x].href.indexOf('http')==0 && document.links[x].href.indexOf('leominstercu.com/')==-1 && document.links[x].href.indexOf('teggweb.com')==-1 && document.links[x].href.indexOf('homebanking.leominstercu.com')==-1 && document.links[x].title!='Continue on to external website') || document.links[x].href.indexOf('/loanapplications/')>-1) {
		//if ((document.links[x].href.indexOf('http')==0 && document.links[x].href.indexOf('wpb1.webproductionsinc.com/')==-1 && document.links[x].href.indexOf('teggweb.com')==-1 && document.links[x].href.indexOf('homebanking.leominstercu.com')==-1) || document.links[x].href.indexOf('/loanapplications/')>-1) {
			update_link(document.links[x]);
		}
	}
}

function update_link(obj, is_google_map) {
	obj.setAttribute('redirect', obj.href);
	if (is_google_map) {
		obj.setAttribute('onclick', 'gl_ajax_dialog(\'popup\', \'externallink.asp?redirect=\'+escape(this.getAttribute(\'redirect\'))+\'&target=\'+escape(this.getAttribute(\'target\')));');
	} else {
		obj.onclick = function() {gl_ajax_dialog('popup', 'externallink.asp?redirect='+escape(this.getAttribute('redirect'))+'&target='+escape(this.getAttribute('target')));};
	}
	obj.href = 'javascript:void(0)';
	obj.removeAttribute('target');
}