var regExp = /^\s+/;
var regExpEmail = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

var WriteReview = function(bus_id) { 
	new MUI.Modal({
		id: 'modalReview',
		title: 'Write a Review',
		loadMethod: 'xhr',
		contentURL: 'inc/listing.writereview.php?bus_id=' + bus_id,
		width: 400,
		height: 200,
		onContentLoaded: function(){
			$('sbtReview').addEvent('click', function(e) {
				new Event(e).stop(); 
				var req = new Request.HTML({ 
					method: 'post',
					data: $('frmReview'),
					url: '../inc/listing.writereview.php',
					update: $('msg')
				}).send();
			});
			$('content_text').addEvent('change', function(e) {
				if(this.value.length>200) {
					this.value = this.value.substring(0,200); 
				}
			});
		}
	});
}

var SendToFriend = function(bus_id, qs) { 
	qs = (qs) ? qs : '';
	new MUI.Modal({
		id: 'modalSendToFriend',
		title: 'Send To Friend',
		loadMethod: 'xhr',
		contentURL: 'inc/listing.sendtofriend.php?bus_id=' + bus_id + qs,
		width: 400,
		height: 170,
		onContentLoaded: function(){
			$('sbtSendToFriend').addEvent('click', function(e) {
				new Event(e).stop();
				if($('email_from').value && $('email_to').value) { 
					var req = new Request.HTML({ 
						method: 'post',
						data: $('frmSendToFriend'),
						url: '../inc/listing.sendtofriend.php',
						update: $('msg')
					}).send();
				} else {
					alert('Please enter your and friend\'s email addresses');
				}
			});
		}
	});
}

var SendToPhone = function(bus_id) { 
	new MUI.Modal({
		id: 'modalSendToPhone',
		title: 'Send To Phone',
		loadMethod: 'xhr',
		contentURL: 'inc/listing.sendtophone.php?bus_id=' + bus_id,
		width: 400,
		height: 250,
		onContentLoaded: function(){
			$('sbtSendToPhone').addEvent('click', function(e) {
				new Event(e).stop();
				if($('phone_number').value && $('carrier').options[$('carrier').selectedIndex].value!=0) { 
					var req = new Request.HTML({ 
						method: 'post',
						data: $('frmSendToPhone'),
						url: '../inc/listing.sendtophone.php',
						update: $('msg')
					}).send();
				} else {
					alert('Please enter your phone number and select a carrier.');
				}
			});
		}
	});
}

var Contact = function(bus_id) { 
	new MUI.Modal({
		id: 'modalContact',
		title: 'Contact Business',
		loadMethod: 'xhr',
		contentURL: 'inc/listing.contact.php?bus_id=' + bus_id,
		width: 400,
		height: 250,
		onContentLoaded: function(){
			$('sbtContact').addEvent('click', function(e) {
				new Event(e).stop();
				if($('name').value && $('email').value && regExpEmail.test($('email').value)) { 
					var req = new Request.HTML({ 
						method: 'post',
						data: $('frmContact'),
						url: '../inc/listing.contact.php',
						update: $('msg')
					}).send();
				} else {
					alert('Please enter your name and valid email address.');
				}
			});
		}
	});
}

var PopUpShare = function(el, title)
{
	var url = 'http://' + $('domain').value + '/search?bid=' + $('bus_id').value + '&car=y';
	var img = new Element('img', {
		'id': 'btnShare',
		'class': 'mochaShareButton',
		'title': 'Share',
		'alt': 'Share',
		'src': 'http://s7.addthis.com/static/btn/lg-share-en.gif'
	}).inject($(el + '_closeButton'), 'before');
	var a = new Element('a', {
		'title': 'Share',
		'href': 'http://www.addthis.com/bookmark.php',
		'events': {
			'click': function(){ return addthis_sendto(); },
			'mouseover': function(){ return addthis_open(this, '', url, title); },
			'mouseout': function(){ addthis_close(); }
		}
	}).wraps($('btnShare'));
}

var PopUpSendFriend = function(el, qs)
{
	var qs = (qs) ? qs : '';
	var email = new Element('img', {
		'id': 'btnEmail',
		'class': 'mochaEmailButton',
		'title': 'Email',
		'alt': 'Email',
		'src': '../assets/img/elements/emailfwd.gif'
	}).inject($(el + '_closeButton'), 'before');
	$('btnEmail').addEvent('click', function(e){
		new Event(e).stop();
		SendToFriend($('bus_id').value, qs);
	});
}

var PopUpPrint = function(el) 
{
	var print = new Element('img', {
		'id': 'btnPrint',
		'class': 'mochaPrintButton',
		'title': 'Print',
		'alt': 'Print',
		'src': '../assets/img/elements/print.gif'
	}).inject($(el + '_closeButton'), 'before');
	$('btnPrint').addEvent('click', function(e){
		new Event(e).stop();
		window.print();
	});
}

var PopUpTitleCar = function(el, bus_id, car_id)
{
	var title = '';
	var req = new Request({ 
		method: 'get',
		data: { "func" : "name", "car_id" : car_id, "bus_id" : bus_id },
		url: "../assets/page/page.buslisting.car.php",
		onSuccess: function(responseText) {
			title = responseText.replace(regExp,'');
			$(el + '_title').set('text',title);
			PopUpShare(el, title);
		}
	}).send();
}

var PopUpCar = function(bus_id, car_id) 
{ 
	new MUI.Modal({
		id: 'modalCarListing',
		title: 'View Vehicle Listing',
		loadMethod: 'xhr',
		contentURL: '../assets/page/page.buslisting.car.php?bus_id=' + bus_id + '&car_id=' + car_id,
		width: 800,
		height: 400,
		resizable: true,
		onContentLoaded: function(){
			PopUpTitleCar('modalCarListing', bus_id, car_id);
			PopUpSendFriend('modalCarListing', '&car=y');
			PopUpPrint('modalCarListing');
		}
	});
}




	



