var jlib = {
		safari2: false,
		minFlashVer: '9.0.0',
		haveFlash: false
	};

// fix jquery / prototype conflict when in page editor mode
var $jq = jQuery.noConflict();

/************************************************************
 * image button
 */
jlib.imageButtonInit = function () {
	$jq(':submit[src],:reset[src]').each(function() {	
		var s = $jq(this);
		$jq([
			'<a class="imgButton" href="#">',
				'<img class="',s.attr('class'),'" src="',s.attr('src'),'" alt="',s.attr('value'),'" />',
			'</a>'
		].join(''))
		.insertAfter(s)
		.click(function(e){
			e.preventDefault();
			s.trigger('click');
		});
		s.addClass('offscreen').attr('tabIndex','-1');
	});
};

/************************************************************
 * Display the homepage flash banner
 */
jlib.hpFlashBanner = function () {
	var lang,
		strXML;
	if (location.href.indexOf('/en/') > -1) {
		lang = "en";
	}
	else if (location.href.indexOf('/tc/') > -1) {
		lang = "tc";
	}
	strXML = "/" + lang + "/GetAllXml.aspx?path=/Flash";
	var flashvars = {
		xml:strXML
	};
	var params = {
		menu: "false",
		scale: "noScale",
		allowFullscreen: "true",
		allowScriptAccess: "always",
		bgcolor: "#FFFFFF"
	};
	var attributes = {
		id: "MiramarGroup"
	};
	swfobject.embedSWF("/files/group/swf/flash_banner.swf", "hpFlashBanner", "934", "335", "9.0.0", "/files/group/swf/expressInstall.swf", flashvars, params, attributes);

};

/************************************************************
 * clickable labels
 */
jlib.clickableLabels = function () {
	$jq('label')
		.css('cursor', 'default')
		.click(function (e) {
			var el = $jq('#' + this.htmlFor), f;
			if (el.length != 1 || el[0].disabled) {
				return;
			}
			f = (el.is(':checkbox, :radio')) ? 'click' : 'focus';
			if (el[0][f]) {
				el[0][f]();
				e.preventDefault();
			}
		});
};

/************************************************************
 * open link in new window
 */
jlib.openNewWindow = function () {
	$jq('a[rel*="external"]').click(function (e) {
		e.preventDefault();
		window.open($jq(this).attr('href'));
	});
};

/************************************************************
 * image rollovers and on state
 */
jlib.imageRollover = function () {
	$jq('img.rollover').each(function () {
		var img = $jq(this),
			paths = img.attr('src').split('#'),
			off = paths[0],
			over = paths[1];
		if (paths.length != 2) {
			return;
		}
		this.overSrc = over;
		this.src = this.offSrc = off;
	}).hover(function () { // over
		if (this.overSrc) {
			this.src = this.overSrc;
		}
	}, function () { // out
		if (this.offSrc) {
			this.src = this.offSrc;
		}
	});
};

/************************************************************
 * turn filterBox buttons into links
 */
jlib.filterBox = function () {
	var url = location.protocol + '//' + location.host + location.pathname;

	function enc(s) {
		return encodeURIComponent(s).replace(/%20/g, '+');
	}

	$jq('div.filterBox').each(function () {
		var box = $jq(this), inputs, buttons;

		function update() {
			var buf = [];

			inputs.each(function () {
				var input = $jq(this);
				if (input.val() != '') {
					buf.push('&', enc(input.attr('name')), '=', enc(input.val()));
				}
			});

			buttons.attr('href', url + buf.join('').replace(/^&/, '?'));
		}

		// replace image buttons with links
		box.find(':image').each(function () {
			var input = $jq(this);
			input.replaceWith('<a href="' + url + '" rel="' + input.attr('value') + '"><img src="' + input.attr('src') + '" alt="' + input.attr('alt') + '" /></a>');
		});

		inputs = box.find(':input');
		buttons = box.find('a[rel!="reset"]');

		inputs.change(update).eq(0).change();
	});
};

/************************************************************
 * contact us form
 */
jlib.contactUsForm = function () {
	function getSel(n) {
		return (n == '') ? 'div.contactBox div:first' : '#' + n;
	}

	$jq('div.contactForm').each(function () {
		var form = $jq(this),
			nature = form.find('#nature'),
			cur_nature;

		nature.change(function () {
				if (cur_nature != null) {
					$jq(getSel(cur_nature)).hide();
				}
				cur_nature = $jq(this).val().replace(/\s+/g, '');
				$jq(getSel(cur_nature)).show();
			})
			.change();

		if (nature.length > 0) {
			$jq('div.contactBox').show();
		}

		form = nature = null;
	});
};

/************************************************************
 * handle the enter key
 */
jlib.formKeyPress = function() {
	$jq("input:not(#search')")
	.keydown(function(e) {
		if (e.keyCode == 13) {
			e.stopPropagation();
			$jq(':submit').trigger('click');
		}
	 });
	 
	$jq("input#search")
	.keydown(function(e){
		if (e.keyCode == 13){
			e.stopPropagation();
			if (!($jq('#search').val() == "")){
				if ($jq("form").find(":reset").length > 0) {
					$jq('.searchBtn').trigger('click');
				}
			}
		}
	 });
};

/************************************************************
 * onload, onunload
 */
jlib.safari2 = ($jq.browser.safari && parseFloat($jq.browser.version) < 420);
jlib.haveFlash = (window.swfobject && swfobject.hasFlashPlayerVersion(jlib.minFlashVer));

jlib.init = function () {
	if (!jlib.haveFlash) {
		$jq('body').addClass('noflash');
	}

	jlib.imageButtonInit();
	jlib.openNewWindow();
	jlib.clickableLabels();
	jlib.imageRollover();
	jlib.filterBox();
	jlib.hpFlashBanner();
	jlib.contactUsForm();
	jlib.formKeyPress();
};

if (jlib.safari2) {
	// the last version of jQuery that works with Safari 2 is 1.2.6
	jQuery.noConflict(true);
	// for some reason this needs to be two calls to document.writeln()
	document.writeln('<script type="text/javascript" src="/files/group/js/jquery-1.2.6.min.js"></script>');
	document.writeln('<script type="text/javascript">$jq = jQuery.noConflict(); $jq(jlib.init);</script>');
} else {
	$jq(jlib.init);
}
