$(function(){
	$('button, input[type=button], a.btn').button().css({'font-size':'80%'});
	$('button#back').click(function(){history.go(-1)})
	if ($('#gallery').length) $('#gallery a').lightBox();
	
	if (jQuery.fn.lightBox) $('.lightbox').lightBox();
	
	$('a[rel=external]').click(function(event){ event.preventDefault(); window.open(this.href); });
	
	var val = $('select#region').val()
	$('table#venues > tbody > tr').each(function(index,value){
		if (value.id==val) $(value).show().find('img').each(function(i,v){if($(v).attr('src')==undefined&&$(v).attr('alt')!=undefined)$(v).attr('src',$(v).attr('alt'));});
		else $(value).hide();
	});			
	if ($('table#venues > tbody > tr:visible').length == 0) $('table#venues > tbody > tr:last').show();
			
	$('td.ui-widget-header').click(function(){
		$('td.ui-widget-header').addClass('ui-widget-shadow').css({'margin':'0','padding':'5px'});
		$(this).removeClass('ui-widget-shadow');
	});
	
	$('select#country').change(function(){
		$('select#region').attr('disabled', true).addClass('ui-state-disabled');
		
		if ($(this).val() == 0)	{
			$('table#venues > tbody > tr').show().find('img').each(function(i,v){if($(v).attr('src')==undefined&&$(v).attr('alt')!=undefined)$(v).attr('src',$(v).attr('alt'));});
			$('select#region option:gt(0)').remove();
			
			$('select#region').attr('disabled', false).removeClass('ui-state-disabled');
		} else {
			$.getJSON('bin/ajax/get_region.php', {country:$('select#country').val()}, function(data) {
				$('select#region').attr('disabled', false).removeClass('ui-state-disabled');
				$('select#region option:gt(0)').remove();
				$(data).each(function(index, value) {
					$('select#region').append($('<option></option>').val(value.region_id).html(value.region_name));
				});
			});
		}
	});
	
	$('select#region').change(function(){
		if ($(this).val() == 0)	$('table#venues tbody tr').slideDown().css({'display':'table-row'});
		else {
			var val = $('select#region').val();
			$('table#venues > tbody > tr').each(function(index,value){
				if (value.id == val) $(value).show().find('img').each(function(i,v){if($(v).attr('src')==undefined&&$(v).attr('alt')!=undefined)$(v).attr('src',$(v).attr('alt'));});
				else $(value).hide();
			});			
			if ($('table#venues > tbody > tr:visible').length == 0) $('table#venues > tbody > tr:last').show();
		}
	});
	
	$('input#search').keyup(function(){
		var searchstr = $(this).val().toLowerCase()
		if (searchstr.length > 0) {
			$('table#venues > tbody > tr').each(function(index, value) {
				var text = $(value).find('td#name').html();
				if (text) text = $.trim(text).substr(0, searchstr.length).toLowerCase();
					
				if (text == searchstr) $(value).show().find('img').each(function(i,v){if($(v).attr('src')==undefined&&$(v).attr('alt')!=undefined)$(v).attr('src',$(v).attr('alt'))});
				else $(value).hide();
			});
			
			if ($('table#venues > tbody > tr:visible').length == 0) $('table#venues > tbody > tr:last').css({'display':'table-row'});
		}
	});
	
	$('button[name=book]').click(function(){
		var id = $(this).attr('id');
		location.href="book.php?id="+id;
	});
	
	$('input[type=submit]').button();
	
	$('input[type=button]#reset').button().css({'font-size':'11px'}).click(function(){
		$('input#search').val('');
		$('select#country option:gt(0)').attr('selected', 'false');
		$('select#country option:first').attr('selected', 'true');
		$('select#region option:gt(0)').remove();
		$('table#venues tr:not(:last)').show();
		$('img:visible').each(function(i,v){if($(v).attr('src')==undefined&&$(v).attr('alt')!=undefined)$(v).attr('src',$(v).attr('alt'))});
		$('.fllft.ui-widget-header').removeClass('ui-widget-shadow');
	});
	
	$('form#subscribe').find('input[type=button]').button().css({'font-size':'11px'}).click(function(){
		var user = $('form#subscribe input[name=subscribe_name]').val();
		var email = $('form#subscribe input[name=subscribe_email]').val();
		
		if (user.length == 0) message("Please enter your name");
		else {
			var emailregexp = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+$/;
			if (!emailregexp.test(email)) message("Please enter a valid email address");

			else {
				$('form#subscribe').addClass('ui-widget-shadow').find('input').attr('disabled', 'true');
				$.get('bin/ajax/subscribe.php', {user:user,email:email}, function(response) {
					if (response.length == 0) {
						message("Thank you for subscribing!");
						$('form#subscribe').slideUp();
					} else {
						message(response);
						$('form#subscribe').removeClass('ui-widget-shadow').find('input').attr('disabled', 'false');
					}
				});
			}
		}
	});
	
	(function(){
		var ma = document.createElement('script'); ma.type = 'text/javascript'; ma.async = true;
		ma.src = document.location.protocol + '//mstats.montage-media.com/ma.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ma, s);
	})();
});

function message(text, width, height) {
	if (width == undefined) width = 300;
	if (height == undefined) height = 80;
	
	var overlay = $('<div></div>').addClass('ui-message ui-overlay');
	$(overlay).append($('<div></div>').addClass('ui-widget-overlay')).append($('<div></div>').addClass('ui-widget-shadow ui-corner-all').css({'width':(width+9)+'px','height':(height+9)+'px','position':'absolute','margin-left':'-'+((width/2)+11)+'px','margin-top':'-'+((width/2)+11)+'px','left':'50%','top':'50%'}));
		
	var message = $('<div></div>').addClass('ui-message ui-widget ui-widget-content ui-corner-all').css({'text-align':'center','text-transform':'capitalize','position':'absolute','width':width+'px','height':height+'px','margin-left':'-'+((width/2)+6)+'px','margin-top':'-'+((width/2)+6)+'px','left':'50%','top':'50%','padding':'10px'});
	$(message).append($('<p></p>').html(text)).append($('<button></button>').html('Close').button().css({'font-size':'90%'}).click(function(){$('div.ui-message').remove()}));
	
	$('body').append(overlay).append(message);
}
