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: $('js-root-path').value + '/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: $('js-root-path').value + '/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: $('js-root-path').value + '/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: $('js-root-path').value + '/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: $('js-root-path').value + '/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: $('js-root-path').value + '/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: $('js-root-path').value + '/inc/listing.contact.php?bus_id=' + bus_id,
		width: 400,
		height: 280,
		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: $('js-root-path').value + '/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, bus_id)
{
	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) ? bus_id : $('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, car_id)
{
	var title = '';
	var req = new Request({ 
		method: 'get',
		data: { "func" : "name", "car_id" : car_id },
		url: $('js-root-path').value + "/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: $('js-root-path').value + '/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_id='+car_id);
			PopUpPrint('modalCarListing');
		}
	});
}

var PopUpCarFrame = function(bus_id, car_id) 
{ 
	new MUI.Modal({
		id: 'modalCarListing',
		title: 'View Vehicle Listing',
		loadMethod: 'iframe',
		contentURL: $('js-root-path').value + '/assets/page/page.buslisting.car.php?car_bus_id=' + bus_id + '&car_id=' + car_id,
		width: 800,
		height: 600,
		resizable: true,
		onContentLoaded: function(){
			PopUpTitleCar('modalCarListing', car_id);
			PopUpSendFriend('modalCarListing', '&car_id='+car_id, bus_id);
			//PopUpPrint('modalCarListing');
		}
	});
}

var PopUpProperty = function(bus_id, property_id) 
{ 
	new MUI.Modal({
		id: 'modalPropertyListing',
		title: 'View Property Listing',
		loadMethod: 'xhr',
		contentURL: $('js-root-path').value + '/assets/metroya/page/listing.property.php?bus_id=' + bus_id + '&property_id=' + property_id,
		width: 800,
		height: 750,
		resizable: true,
		onContentLoaded: function(){
			//PopUpTitleCar('modalCarListing', bus_id, car_id);
			//PopUpSendFriend('modalCarListing', '&car=y');
			//PopUpPrint('modalCarListing');
		}
	});
}

var PopUpListing = function(bus_id, tab) 
{ 
	var width = ($('site_listing_width').value) ? $('site_listing_width').value : 800;
	var tab = (tab) ? '&' + tab + '=y' : '';
	new MUI.Modal({
		id: 'modalBusListing',
		title: 'View Listing',
		loadMethod: 'xhr',
		contentURL: '../assets/metroya/page/member.admin.buslisting.php?tabs=1&bid=' + bus_id + tab,
		width: width,
		height: 400,
		resizable: true,
		onContentLoaded: function(){
			
		}
	});
}

var PopUpListingFrame = function(bus_id, tab, portal_id, bookmark, category_id, window_title) 
{ 
	var portal_id = (portal_id) ? portal_id : 0;
	var hidetabs = (tab=='map') ? '&hidetabs=y' : '';
	var type = ($('site_listing_type').value) ? $('site_listing_type').value : 0;
	var width = (type==2 && portal_id>0) ? 990 : 820;
	var height = 400;
	var bookmark = (bookmark) ? '&bookmark=' + bookmark : '';
	var category_id = (category_id) ? category_id : 0;
	var category = (category_id) ? '&category_id=' + category_id : '';
	var window_title = (window_title) ? window_title : 'View Listing';
	if(tab && tab.indexOf('=')==-1){
		tab = '&' + tab + '=y';
	} else if (tab) {
		tab = '&' + tab;
	}
	if(type==2 && portal_id>0){
		var url = 'http://www.portal.liongatebusiness.com/?bid=' + portal_id + '&language=1';
	//} else if(bus_id==7203){
	//	width = 800;
	//	height = 750;
	//	var url = '../assets/metroya/page/listing.2.php?tabs=1&bid=' + bus_id + tab + hidetabs + bookmark + category;
	} else {
		width = 840;
		height = 750;
		var url = '../assets/metroya/page/listing.2.php?tabs=1&bid=' + bus_id + tab + hidetabs + bookmark + category;
		//var url = '../assets/metroya/page/listing.php?tabs=1&bid=' + bus_id + tab + hidetabs + bookmark + category;
	}
	new MUI.Modal({
		id: 'modalBusListing',
		title: window_title,
		loadMethod: 'iframe',
		contentURL: url,
		width: width,
		height: height,
		resizable: true,
		onContentLoaded: function(){
			$('modalBusListing_controls').style.width = '55px';
			var span  = new Element('span').set('html','close').inject($('modalBusListing_controls'), 'top');
			span.style.fontSize = '12px'; 
			span.style.color = '#be0e26';
			span.style.fontWeight = 'bold';
			span.style.position = 'relative';
			span.style.top = '-4px';
		}
	});
}

var vote = function(bus_id, category_id) {
	//alert(bus_id);
	var req = new Request.HTML({ 
		method: 'get',
		data: { "func" : "vote", "category_id" : category_id, "bid" : bus_id },
		url: $('js-root-path').value + "/assets/metroya/page/listing.2.php",
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
			responseHTML = responseHTML.replace(regExp,'');
			if(responseHTML==1){
				alert('Thank you for your vote!');
				var req = new Request.HTML({ 
					method: 'get',
					data: { "func" : "votecount", "category_id" : category_id, "bid" : bus_id },
					url: $('js-root-path').value + "/assets/metroya/page/listing.2.php",
					onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
						responseHTML = responseHTML.replace(regExp,'');
						html = (responseHTML==1) ? responseHTML + ' person voted for this business' : responseHTML + ' people voted for this business';
						$('votecount' + bus_id).set('html', html);
					}
				}).send();
			}else if(responseHTML==2){
				alert('You have already voted for this business in this category');
			}else{
				alert('An error has occurred. Please try again or contact support');
			}
			//alert(responseHTML);
		}
		//update: $('friend')
	}).send();
}





	




