/*
-----------------------------------------------
UBhomeloan
Global Javascript
----------------------------------------------- */

$(function() {

	var global = {
		scope : {
			content : $('#main-content'),
			sidebar : $('#modules')
		},
		$s : function() {return global.scope},
		fix_page_heights : function() {
			var modules_h = $s().sidebar.height();
			var main_h = $s().content.height();
			var banner_h = $('#banner').height();
			if (main_h < modules_h) {
				$s().content.minHeight(modules_h - 60 - banner_h);
			}
		},
		catch_popups : function() {
			// Anchor rel attribute in format: popup[width,height]
			$('a[rel^=popup]').each(function() {
				var href = $(this).attr('href');
				var prefs = $(this).attr('rel');
				var pu_width = 800;
				var pu_height = 600;
				prefs = prefs.match(/\[(\d+),(\d+)\]/);
				if (prefs) {
					pu_width = prefs[1];
					pu_height = prefs[2];
				}
				$(this).click(function() {
					window.open(href,'_blank','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width='+pu_width+',height='+pu_height);
					return false;
				});
			});
		},
		faq_list : function() {
			$('.faq_nav').remove();
			$('.faqs').addClass('js_en');
			$('.faqs li').each(function(index) {
				$this = $(this);
				var title = $this.find('h3');
				var title_link = $('<a href="#"></a>')
				var content = $this.find('.content');

				content.hide();
				title.innerWrap(title_link);
				title_link.click(function() {
					content.toggle();
					$(this).parent().toggleClass('active');
					return false;
				});
			});
		},
		init : function() {
			global.fix_page_heights();
			global.catch_popups();
			if ($("body").is('.faq')) {global.faq_list()}
		}
	}
	// scope shortcut
	var $s = function() {return global.scope}

	global.init();

});

jQuery.fn.extend({
	minHeight : function(height) {
		if ($.browser.msie && !window.XMLHttpRequest && !document.all) { // IE 6 only
			$(this).height(height);
		}
		else {$(this).css('min-height', height)}
	},
	innerWrap : function(e) {
		this.each(function(index) {
			var content = $(this).html();
			$(this).empty();
			e.html(content).appendTo($(this));
		});
	}
});