//general purpose namespace for bonzicentral specific stuff
var Bonzi = {base_url: ''};	
document.write('<style type="text/css">body{visibility:hidden;}</style>');
$(document).ready(function()
{
	Bonzi.fitNavMenu = function()
	{
		//Assist the horizontal menu with some padding so it fits the width of the page
		var
			div = $('#navbar'),
			ul = div.find('.cssMenu').first(),
			ulwidth = 0,
			pad = 0,
			round = 0,
			lis = ul.children('li');
		lis.each(function()
		{
			$(this).css('padding',0);
			ulwidth += $(this).outerWidth(true);
		});
		pad = (div.outerWidth() - ulwidth);
		round = pad;
		pad = Math.floor(pad / lis.length / 2) * 2;

		pad = Math.floor(pad / 2) * 2;
		round = (round - pad * lis.length);	///round off error.  Don't let good pixels go to waste
		lis.each(function()
		{
			var subli = $(this).children('ul').children('li');
			$(this).css({'paddingLeft': Math.floor(pad/2), 'paddingRight': Math.floor(pad/2)});

			//also set the sub-menu width if it is smaller than the parent menu
			/* Actually, don't.  Its not working and we don't need it.
			if(subli.width() < $(this).width())
			{
				subli.width($(this).width());
			}
			*/
		})
		.last().css({'paddingLeft': Math.floor(pad/2)+Math.floor(round/2), 'paddingRight': Math.floor(pad/2)+Math.ceil(round/2)});
	};

	//just be called when tab content is replaced
	Bonzi.setupTabs = function()
	{
		$('#wrap #upper .content').hide();
		$('#wrap #upper').find('#main').attr('id','').end().find('.tab1').attr('id','main').css('display','');

		$('div.divtab').unbind('click').click(function()
		{
			$('div.divtab').removeClass("tabon");
			$(this).addClass("tabon");
			$('#main').attr('id','').css('display','none');
			$('#upper .tab'+$(this).attr('tabid'))
				.attr('id','main')
				.css('display','')
				.effect('slide', {direction: 'right'});
			$(this).css('cursor','default').removeClass("tabhover");
			return false;
		}).unbind('hover').hover(
			function()	//the hover over
			{
				if(!$(this).hasClass("tabon"))
				{
					$(this).css('cursor','pointer').addClass("tabhover");
				}
			},
			function()	// when the hover goes away
			{
				$(this).css('cursor','default').removeClass("tabhover");
			}
		).first().addClass('tabon');
		Cufon.replace('.tabtext', { fontFamily: 'Calibri' });	//must be done 
	};

	//does Cufon stuff.  needs to be rerun if anything is changed (and at the end here).
	Bonzi.textReplace = function()
	{
		Cufon.replace('#navbar > ul.cssMenu > li > a, #navbar > ul.cssMenu > li > span', { fontFamily: 'Calibri' });
		Cufon.replace('h4.tboxheader', { fontFamily: 'Franklin' });
		Cufon.replace('.tabcontent', { fontFamily: 'Franklin' });
		Cufon.replace('.bc_cufon', { fontFamily: 'Franklin' });
		Cufon.replace('.wgt_container > .top .right .content', { fontFamily: 'Franklin' });

		Bonzi.fitNavMenu();	//always happens at the same time as text replace, and must come after.
	};

	Bonzi.lightbox = function()
	{
		if(!jQuery.fn.lightbox)
		{
			return;
		}
		$('a.lightbox').lightbox({
			show_helper_text: false,
			show_linkback: false,
			text: {
				image:		'Image',
				of:			'of',
				close:		'Close X',
				closeInfo:	'You can also click anywhere outside the image to close.',
				download:	'Download.',
				help: {
					close:		'Click to close',
					interact:	''
				},
				about: {
					text: 	'',
					title:	'',
					link:	''
				}
			}
		});
	};

	Bonzi.lightbox();

	Bonzi.supportChat = function()
	{
		$('#contact a').click(function()
		{
			var pos = 
				',left='+
				((isNaN(window.screenX) ? window.screenLeft : window.screenX) + $(this).offset().left)+
				',top='+
				((isNaN(window.screenY) ? window.screenTop : window.screenY) + $(this).offset().top);
			var w = window.open('','supportPopup','width=240,height=420,scrollbars=no,resizable=no,location=no,menubar=no,toolbar=no,status=no,titlebar=no'+pos);
			w.document.title = 'Live Chat With Bonzi';
			setTimeout(function(){
				w.document.write('<html><head><title>Live Chat With Bonzi</title></head><body><object id="pingboxftziwr687t8W0" type="application/x-shockwave-flash" data="http://wgweb.msg.yahoo.com/badge/Pingbox.swf" '+
							'width="240" height="420" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0"><param name="movie" value="http://wgweb.msg.yahoo.com/badge/Pingbox.swf" />'+
							'<param name="allowScriptAccess" value="always" /><param name="flashvars" value="wid=i6yJYfi5TGyyHYHAUnnGySbTLY4Olx8m4hMaQFA-" /></object></body></html>');
				$('body', w.document).css({margin:0,padding:0,border:0});
			},1000);
			return;
		});
	};

	Bonzi.textboxHints = function()
	{
		function blur($e)
		{
			if(!$e.val())
			{
				$e.val($e.attr('title'));
				$e.addClass('bc_hint_on');
			}
		}
		function focus($e)
		{
			if($e.val() === $e.attr('title'))
			{
				$e.val('');
				$e.removeClass('bc_hint_on');
			}
		}

		$('input[type=text].bc_hint')
			.live('focus.bonzi', function(){ focus($(this)); })
			.live('blur.bonzi', function(){ blur($(this)); })
			.each(function(){ blur($(this)); });
	};
	
	$('ul.drawers').accordion(
	{
		header: 'H2.drawer-handle',
		selectedClass: 'open',
		event: 'mousemove'
	});

	$('.slide-out-div').tabSlideOut({
		tabHandle: '.handle',                     //class of the element that will become your tab
		pathToTabImage: Bonzi.base_url+'/images/choose_your_sport.png', 			//path to the image for the tab //Optionally can be set using css
		imageHeight: '322px',                     //height of tab image           //Optionally can be set using css
		imageWidth: '45px',                       //width of tab image            //Optionally can be set using css
		tabLocation: 'left',                      //side of screen where tab lives, top, right, bottom, or left
		speed: 300,                               //speed of animation
		action: 'click',                          //options: 'click' or 'hover', action to trigger animation
		topPos: '10px',                          //position from the top/ use if tabLocation is left or right
//			leftPos: '20px',                          //position from left/ use if tabLocation is bottom or top
		fixedPosition: false                      //options: true makes it stick(fixed position) on scroll
	});
	$('body').css('visibility','visible');

	Bonzi.setupTabs();
	Bonzi.textReplace();
	Bonzi.supportChat();
	Bonzi.textboxHints();
});

