
var gtrigger_coupon_on = 0; // Currently not on coupon

function mouseover_listing(listing)
{
	var titlebar = listing.firstDescendant();

	titlebar.addClassName('listing_top_on');

	if(titlebar.hasClassName('listing_top'))
		titlebar.removeClassName('listing_top');

	var desc = $A(titlebar.descendants());

	desc.each( function (c_desc) {
		if(c_desc.readAttribute('src'))
			if( (c_desc.readAttribute('src')).indexOf('.gif') != -1)
				c_desc.writeAttribute('src', '/images/yellow/template/circle_plus.gif');
	} );
}

function mouseout_listing(listing)
{
	var titlebar = listing.firstDescendant();

	titlebar.addClassName('listing_top');

	if(titlebar.hasClassName('listing_top_on'))
		titlebar.removeClassName('listing_top_on');

	var desc = $A(titlebar.descendants());

	desc.each( function (c_desc) {
		if(c_desc.readAttribute('src'))
			if( (c_desc.readAttribute('src')).indexOf('.gif') != -1)
				c_desc.writeAttribute('src', '/images/yellow/template/plus.gif');
	} );
}

function click_listing(listing, duns, oquery)
{
	window.location.href = '/content/details.html?text=' + duns + '&amp;original_query=' + oquery;
}

function select_tab(tab, duns, oquery)
{
	var tab_id = $(tab).readAttribute('id');

	if(tab_id.indexOf('map') != -1 && duns && oquery)
	{
		window.location.href = '/content/details.html?player_large=1&amp;text=' + duns + '&amp;original_query=' + oquery + '&amp;smap=1';
		return;
	}

	if(tab_id.indexOf('coupon') != -1)
	{
		gtrigger_coupon_on = 1; // printing now goes to coupon
	}
	else
	{
		gtrigger_coupon_on = 0; // printing now goes to profile
	}

	$A(tab.siblings()).each( function (h_tab)
	{
		$(h_tab).setStyle( { display: 'none' } );
	} );

	tab.setStyle( { display: 'block' } );

	if(tab_id.indexOf('gallery') != -1)
	{
		var ext_photo = $('gallery_photo');

		if(ext_photo.readAttribute('src'))
			choose_thumb(ext_photo.readAttribute('src'));
	}
}

function choose_thumb(src)
{
	var ext_photo = $('gallery_photo');

	ext_photo.setStyle( { width: 'auto', height: 'auto' } );

	ext_photo.writeAttribute('src', src);

	var photo_width = ext_photo.getWidth();
	var photo_height = ext_photo.getHeight();
	var aspect_ratio = 1;

	if(photo_height > 250)
	{
		photo_height = 250;
		aspect_ratio = 250 / photo_height;
		photo_width *= aspect_ratio;
	}

	if(photo_width > 500)
	{
		photo_width = 500;
		aspect_ratio = 500 / photo_width;
		photo_height *= aspect_ratio;
	}

	photo_width = Math.round(photo_width);
	photo_height = Math.round(photo_height);

	ext_photo.setStyle( { height: photo_height + 'px', width: photo_width + 'px' } );
}

function resize_logo(obj)
{
	var ext_obj = $(obj);

	var width = ext_obj.getWidth();
	var height = ext_obj.getHeight();
	var ratio = height / width;


	if(height > 85)
	{
		height = 85;
		width = Math.round(85 / ratio);
	}

	if(width > 110)
	{
		width = 110;
		//height = Math.round(110 * ratio);
	}

	ext_obj.setStyle( { width: width + 'px', height: height + 'px' } );
}

function contact_business()
{
	var name = $F('contactus_name');
	var message = $F('contactus_message');
	var duns = $F('contact_duns');
	var sendself = $F('contactus_sendself');

	var params =  {
		message: message,
		name: name,
		original_query: 'boats',
		DUNSNumber: duns,
		subject: 'From YellowMarketing.com User'
		};

	if(sendself)
	{
		params.send_copy = 1;
	}

	new Ajax.Request('/content/listing_options/contactbusiness.html', { method: 'post', parameters: params ,

		onCreate: function ()
		{
			$('contactus_form').setStyle( { display: 'none' } );
			$('contactus_processing').setStyle( { display: 'block' } );
		},

		onComplete: function (xfer)
		{
			$('contactus_success').setStyle( { display: 'block' } );
		} 

		} );
}

function send_to_friend(company_name, company_phone, company_address, company_url)
{
	var econtent = "<b>" + company_name + "</b><br />" + company_phone + "<br />" + company_address + '<br /><a href="' + company_url + '">' + company_url + "</a><br />";
	var subject = company_name;
	var toAddress = $F('sendtofriend_email');
	var duns = $F('contact_duns');
	var message = $F('sendtofriend_message');
	var name = $F('sendtofriend_name');
	var sendself = $F('sendtofriend_sendself');

	var params = {
		econtent: econtent,
		subject: subject,
		toAddress: toAddress,
		fromName: name,
		DUNSNumber: duns,
		message: message,
		original_query: 'boats'
	};

	if(sendself)
	{
		params.send_copy = 1;
	}

	new Ajax.Request('/content/listing_options/sendtoemail.html', { method: 'post', parameters: params, 

		onCreate: function() {
			$('sendtofriend_form').setStyle( { display: 'none' } );
			$('sendtofriend_processing').setStyle( { display: 'block' } );
		},

		onComplete: function (xfer) {
			$('sendtofriend_success').setStyle( { display: 'block' } );
		}

	} );
}


Event.observe(window, 'load', function () {
	var ext_obj_listing = $('listing_container');
	var ext_obj_popout = $('popout_menu');

	var uri = window.location.href;

	if(uri.indexOf('search.html') != -1)
	{
		var listing_images = $A(document.getElementsByTagName('img'));

		listing_images.each( function (c_img) {
			if($(c_img).hasClassName('resize_img_trigger'))
				resize_logo(c_img);
		} );
	}

	if(ext_obj_popout)
	{
		var width = ext_obj_listing.cumulativeOffset().left + ext_obj_listing.getWidth() + 5;

		y_offset = ext_obj_listing.cumulativeOffset().top + Math.round(ext_obj_listing.getHeight() / 4);

		ext_obj_popout.setStyle( { width: '120px', height: '300px' } );
		ext_obj_popout.setStyle( { top: y_offset + 'px', left: width + 'px' } );

		ext_obj_listing.observe('mouseover', function() {
			ext_obj_popout.setStyle( { display: 'block' } );
		} );
	}
} );


function printer_friendly(duns)
{
	var url = "/content/details.html?text=" + duns + "&amp;original_query=boats&amp;pfriend=1";

	var x_offset = (screen.width / 2) - 125;
	var y_offset = (screen.height / 2) - 175;

	if(gtrigger_coupon_on == 1)
	{
		url += '&amp;coupon=1';
	}

	window.open(url, 'PRINTER_FRIENDLY', 'width=620,height=500,scrollbars=no,toolbar=no,status=no,menubar=no,screenX=' + x_offset + ',screenY=' + y_offset);
}
