var jlib = {};

/************************************************************
 * open link from dropdown
 */
jlib.dropdownLink = function () {
	$('select').change(function () {
		var select = $(this), href = select.val();
		if (href == '') {
			return;
		}
		if (select.hasClass('openNewWindow')) {
			window.open(href);
		}
		else if (select.hasClass('openCurrentWindow')) {
			window.location.href = href;
		}
	});
};

/************************************************************
 * generate dropdown list from ul>li
 */
jlib.generateDropdown = function (sel) {
	$(sel).each(function () {
		var el = $(this),
			items = el.children(),
			buf = ['<select class="', el.attr('class'), '">'],
			title = el.attr('title');

		if (title != '') {
			buf.push('<option value="">', title, '</option>');
		}
		items.each(function () {
			var a = $(this).children('a');
			buf.push('<option value="', a.attr('href'), '">', a.text(), '</option>');
		});
		buf.push('</select>');

		el.replaceWith(buf.join(''));

		el = items = buf = null;
	});
};

/************************************************************
 *  open link in new window
 */
jlib.openNewWindow = function () {
	var rel, 
		href;
	$("a").click(function (e) {
		rel = $(this).attr('rel');
		href = $(this).attr('href');
		if (rel.indexOf('external') > -1) {
			e.preventDefault();
			window.open(href);
		}
	});
}

/************************************************************
 *  show / hide content from dropdown
 */
jlib.showHideContent = function (sel) {
	$(sel).each(function () {
		var cur;
		$(this).children().each(function () {
			if (!this.value) {
				return;
			}
			this.div = $(this.value.match(/#\w+$/)[0]);
		})
		.end()
		.change(function () {
			var o = this.options[this.selectedIndex];
			if (cur == o || !o.div) {
				return;
			}
			if (cur) {
				cur.div.addClass('hidden');
			}
			o.div.removeClass('hidden');
			cur = o;
		})
		.change();
	});
};

/************************************************************
 * home page tabs
 */
jlib.home = function () {
	var cur;

	if (!$('body').hasClass('home')) {
		return;
	}

	$('ul.tabs a').each(function () {
		this.img = $(this).find('img')[0];
		this.div = $(this.hash);
	})
	.click(function (e) {
		e.preventDefault();
		if (cur == this) {
			return;
		}
		if (cur) {
			cur.img.src = cur.img.src.replace(/_(?:current|on)\./, '_off.');
			cur.div.addClass('hidden');
		}
		this.img.src = this.img.src.replace(/_(?:off|on)\./, '_current.');
		this.div.removeClass('hidden');
		cur = this;
	})
	.eq(0).click();
};



/************************************************************
 * onload, onunload
 */
$(function () {
	var homeImages = [
			{
				src: 'images/Home.GIF',
				alt: ''
			},
			{
				src: 'images/Home.GIF',
				alt: ''
			},
			{
				src: 'images/Home.GIF',
				alt: ''
			},
			{
				src: 'images/Home.GIF',
				alt: ''
			}
		],
		body = $('body'), countries, p;

	if (!window.swfobj || !swfobj.hasFlashPlayerVersion('9.0.28')) {
		body.addClass('noflash');
	}

	if (body.hasClass('home')) {
		$('img.homeimg')
			.attr('src', '') // for safari
			.load(function () { $(this).removeClass('loading').unbind('load', arguments.callee); })
			.attr(homeImages[Math.floor(Math.random() * homeImages.length)]);
	}

	jlib.generateDropdown('ul.selectMenu');
	jlib.dropdownLink();
	jlib.showHideContent('select.showHideContent');
	jlib.openNewWindow();
	jlib.home();
	
	// image rollovers
	$('img.rollover')
		.mouseover(function () { this.src = this.src.replace(/_off\./, '_on.'); })
		.mouseout(function () { this.src = this.src.replace(/_on\./, '_off.'); });

	homeImages = body = null;
});