
function ragamuffin( boj ) // use the ragamuffin!
{
	return boj.replace(/[a-zA-Z]/g, function(c){
	  return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
	});
}

function CreateAddEmAt( Site , Name )
{
	return ragamuffin( Name ) + '@' + ragamuffin( Site );
}

function UpdateThePropCount()
{
	var myAjax = new Ajax.Request( 'ajax/update_propcount.php',
	{ method: 'post', onFailure: null, onSuccess: function ( ro ) { $('thePropCount').innerHTML = ro.responseText; } } );
}

function adv_CheckAll( val )
{
	var Set=document.getElementsByName('sType[]');
	
	for ( i=0; i<Set.length; i++ )
	{
		Set[i].checked=val;
	}
	
	adv_CalulateChecks();
}

function adv_CalulateChecks()
{
	var all=true;
	
	var Set=document.getElementsByName('sType[]');
	for ( i=0; i<Set.length; i++ )
	{
		all = all && Set[i].checked;
	}
	
	$('C*').checked=all;
}

function updatePageSearchCriteria()
{
	$('PageSearchControlsForm').submit();
}

function RecalcMagicWindow()
{
	var o = $('overlay_pane');
	o.style.width='auto';
	o.style.height='auto';
	o.style.position='absolute';
	o.style.left='-9999px';
	o.style.top='-9999px';
	
	setTimeout( function() {
		var ow=o.offsetHeight;
		var oh=o.offsetWidth;
		//o.style.marginTop='0px'; //(-ow/2)+'px';
		o.style.marginLeft=(-oh/2)+'px';
		o.style.height=ow+'px';
		o.style.width=oh+'px';
		o.style.left='50%';
		o.style.top = '0px';
		
		var vph = document.viewport.getHeight();
		var newTop = vph / 2 - ow/2;
		if ( newTop < 0 ) newTop = 0;
		
		if ( fixedMWindowTop != undefined ) 
			o.style.marginTop = fixedMWindowTop;
		else
			o.style.marginTop = newTop+'px';
	}, 100 );
}

var magickActive = false;
function IsMagicWindowOpen()
{
	return magickActive;
	//var o=$('overlay_pane');
	//return (''+o.style.display).toUpperCase() == 'BLOCK';
}

var fixedMWindowTop = undefined;
function OpenMagicWindow( id , calls , UseAutoScroll, fixedTop )
{
	magickActive = true;
	fixedMWindowTop = fixedTop;
	var b=$('blackout_pane');
	var c=$('container_pane');
	var o=$('overlay_pane');

	/*
		b.style.width='auto';
		b.style.height='auto';
		c.style.width='auto';
		c.style.height='auto';
	*/


	o.style.width='auto';
	o.style.height='auto';
	o.style.position='absolute';
	o.innerHTML='';
	o.style.display='none';
	o.style.left='-100%';
	o.style.top='-100%';
	
	setTimeout( function() {
		c.style.marginLeft='-99999px';
		c.style.position='absolute';
		
		o.innerHTML = id;
		o.style.display='block';
		
		setTimeout( function() {
			
			id.evalScripts();
			
			setTimeout( function() {
				
				var Size=document.viewport.getDimensions();
				
				b.style.zIndex=1000;
				b.style.width=Size.width+'px';
				b.style.height=Size.height+'px';
				b.style.position='fixed';
				b.style.left='0px';
				b.style.top='0px';
				b.setOpacity( 0.01 );
				// b.style.opacity=0.01;
				// b.style.filter="alpha(opacity='0')";
				b.style.background='#000000';
				b.style.display='block';
				
				c.style.zIndex=1005;
				c.style.width=Size.width+'px';
				c.style.height=Size.height+'px';
				c.style.position=UseAutoScroll?'fixed':'absolute';
				c.style.left='0px';
				c.style.top='0px';
				c.style.display='block';
				
				var ow=o.offsetHeight;
				var oh=o.offsetWidth;
				
				o.style.zIndex=1015;
				o.style.border='0px none black';
				//o.style.marginTop='0px'; // (-ow/2)+'px';
				o.style.marginLeft=(-oh/2)+'px';
				o.style.height=ow+'px';
				o.style.width=oh+'px';
				o.style.backgroundColor='transparent';
				o.style.display='block';
				
				var vph = document.viewport.getHeight();
				var newTop = vph / 2 - ow/2;
				if ( newTop < 0 ) newTop = 0;
				if ( fixedMWindowTop != undefined ) 
					o.style.marginTop = fixedMWindowTop;
				else
					o.style.marginTop = newTop+'px';
				
				c.style.marginLeft='0px';
				
				if (browser == 'Internet Explorer') {
					lightbox.prototype.getScroll();
					lightbox.prototype.prepareIE('100%', 'hidden');
					lightbox.prototype.setScroll(0,0);
					lightbox.prototype.hideSelects('hidden');
					b.style.position='absolute';
					c.style.position='absolute';
				}
				
				Effect.Appear( b , { duration:0.1, to:0.4 } );
				Effect.Appear( o , { duration:0.1, afterFinish: function() { 
					
					if (browser == 'Internet Explorer') {
						selects = o.select('select');
						for(i = 0; i < selects.length; i++) {
							selects[i].style.visibility = 'visible';
						}
					
					}
					
				} } );
				
				Event.observe( c, 'click', function( e ) {
					if ( Event.findElement(e) == c ) CloseMagicWindow();
				} );
				
				Event.observe( b, 'click', function( e ) {
					if ( Event.findElement(e) == b ) CloseMagicWindow();
				} );
				
				//c.onclick = CloseMagicWindow;
				//b.onclick = CloseMagicWindow;
				
				setTimeout( function() {
					if ( calls )
						calls(o); 
					o.style.left='50%';
					o.style.top='0%';
				} , 100 );
				
			} , 50 );
		} , 50 );
	} , 50 );
}

function CloseMagicWindow()
{
	magickActive = false;
	var b=$('blackout_pane');
	var c=$('container_pane');
	var o=$('overlay_pane');
	Effect.Fade( b , { duration:0.2, from:0.4, to:0.0 } );
	Effect.Fade( o , { duration:0.2, afterFinish:function() { 

		b.style.display='none'; 
		c.style.display='none'; 
		o.style.display='none'; 

		if (browser == "Internet Explorer"){
			lightbox.prototype.setScroll(0,lightbox.prototype.yPos);
			lightbox.prototype.prepareIE("auto", "auto");
			lightbox.prototype.hideSelects("visible");
		}
	
	} } );
}

Event.observe( window , 'load' , function() {
	
	Event.observe( window , 'resize' , function() 
	{
		var Size=document.viewport.getDimensions();
		var b=$('blackout_pane');
		var c=$('container_pane');
		var o=$('overlay_pane');
		if ( b && c && o )	{
			b.style.width=Size.width+'px';
			b.style.height=Size.height+'px';
			c.style.width=Size.width+'px';
			c.style.height=Size.height+'px';
			
			var ow=o.offsetHeight;
			var vph = document.viewport.getHeight();
			var newTop = vph / 2 - ow/2;
			if ( newTop < 0 ) newTop = 0;
			
			if ( fixedMWindowTop != undefined ) 
				o.style.marginTop = fixedMWindowTop;
			else
				o.style.marginTop = newTop+'px';
		}
	} );
	
} );

var MenuOffsetTop = 0;
var MenuOffsetLeft = 0;
function show_menu(a){
	if($('submenu'+a)){
		$('submenu'+a).style.top = '-9999px';
		$('submenu'+a).style.left = '-9999px';
		$('submenu'+a).style.display = 'block';
		$('submenu'+a).clonePosition($('button'+a),{ offsetLeft: MenuOffsetLeft, offsetTop: MenuOffsetTop+$('button'+a).offsetHeight-1, setHeight: false, setWidth: false });
	}
}
function hide_menu(a){
	if($('submenu'+a)){
		$('submenu'+a).style.display = 'none';	
	}
}

function OpenLoginWindow()
{
	var LB=new lightbox( $('fishy_link_king') );
	LB.activate();
}

function OpenRegisterWindow()
{
	var LB=new lightbox( $('fishy_link_king2') );
	LB.activate();
}

function NumberField( field )
{
	var r = field.value;
	
	r=r.replace( /[^0-9]+/g , '' );
	
	do
	{
		l=r.length;
		r=r.replace( /([0-9])([0-9]{3})(,|$)/ , '$1,$2$3' );
	}
	while ( l != r.length );
	
	field.value=r;
}

function show_details(originalRequest){
	OpenMagicWindow(originalRequest.responseText, undefined, true );
}

function get_details(a){
	var pdata = "MLS="+a;
	// alert(pdata);
	var myAjax = new Ajax.Request( 'showdetails.php',
	{ method: 'post', parameters: pdata, onFailure: null, onSuccess: show_details } );
}

function getImageLocation( func , f , w , h )
{
	var myAjax = new Ajax.Request( 'ajax/getThumb.php',
	{ method: 'get', parameters: { f:f, w:w, h:h } , onFailure: null, onSuccess: function( ro ) { func( ro.responseText ); } } );
}

var AdvSearchData = {};
function PrepJavascript( obj, ajaxPath )
{
	AdvSearchData = {};
	obj = $(obj);
	
	ajaxPath = ajaxPath || '';
	var useAjax = ajaxPath == '';
	
	var ObjStart=obj.select('.ohStart');
	var ObjEnd=obj.select('.ohEnd');
	
	var ObjMLS=$$('.cl_mls');
	var ObjCounty=$$('.cl_county');
	var ObjSubDiv=obj.select('.cl_subdiv');
	var ObjAddress=obj.select('.cl_sAddress');
	var ObjAddressNum=obj.select('.cl_sAddressNum');
	var ObjCities=obj.select('.cl_sCity');
	var ObjSchoolDist=obj.select('.cl_schooldist');
	
	AdvSearchData.Address = ObjAddress[0];
	AdvSearchData.AddressNum = ObjAddressNum[0];
	
	var A=new Autocomplete( ObjSubDiv[0] , { 
		serviceUrl:'ajax/subdivisions.php',
		minChars:1, 
		maxHeight:300,
		width:550,
		useAjax: useAjax,
		extraField:'null_field'
	});
	
	if ( ObjCities[0].hasClassName('vis_dropdown') )
	{
		
	}
	else
	{
		if ( ObjCities[0].hasClassName('vis_automatic') )
		{
			var B=new Autocomplete( ObjCities[0] , { 
				serviceUrl:ajaxPath+'ajax/cities.php',
				minChars:1, 
				maxHeight:300,
				width:550,
				useAjax: useAjax,
				extraField: 'null_field'
			});
		}
		else
		{
			var B=new Autocomplete( ObjCities[0] , { 
				serviceUrl:ajaxPath+'ajax/mcities.php',
				minChars:1, 
				maxHeight:300,
				width:550,
				useAjax: useAjax,
				extraField: 'null_field'
			});
		}
	}
	
	var C=new Autocomplete( ObjAddress[0] , { 
		serviceUrl:ajaxPath+'ajax/addresses.php',
		minChars:1, 
		morethanone: false,
		maxHeight:300,
		width:440,
		useAjax: useAjax,
		extraField:  ObjAddressNum[0].identify()
	});
	AdvSearchData.AddressAuto = C;
	
	C=new Autocomplete( ObjAddressNum[0] , { 
		serviceUrl:ajaxPath+'ajax/numaddresses.php',
		minChars:1, 
		morethanone: false,
		maxHeight:50,
		width:400,
		useAjax: useAjax,
		extraField: ObjAddress[0].identify()
	});
	AdvSearchData.AddressNumAuto = C;
		
	var D=new Autocomplete( ObjSchoolDist[0] , { 
		serviceUrl:ajaxPath+'ajax/schooldistrict.php',
		minChars:1, 
		maxHeight:300,
		width:550,
		useAjax: useAjax,
		extraField:'null_field'
	});

	var E=new Autocomplete( ObjCounty[0] , { 
		serviceUrl:ajaxPath+'ajax/county.php',
		minChars:1, 
		maxHeight:300,
		width:550,
		useAjax: useAjax,
		extraField:'null_field'
	});

	Event.observe( ObjCities[0] , 'keypress' , function( e ) {
		if ( 13 == e.keyCode && !B.enabled )
		{
			document.forms['TheAdvSearch'].submit();
		}
	} );
	
	Event.observe( ObjAddress[0] , 'keypress' , function( e ) {
		if ( 13 == e.keyCode && !C.enabled )
		{
			document.forms['TheAdvSearch'].submit();
		}
	} );
	
	Event.observe( ObjAddressNum[0] , 'keypress' , function( e ) {
		if ( 13 == e.keyCode && !C.enabled )
		{
			document.forms['TheAdvSearch'].submit();
		}
	} );
	
	Event.observe( ObjSchoolDist[0] , 'keypress' , function( e ) {
		if ( 13 == e.keyCode && !D.enabled )
		{
			document.forms['TheAdvSearch'].submit();
		}
	} );
	
	Event.observe( ObjSubDiv[0] , 'keypress' , function( e ) {
		if ( 13 == e.keyCode && !A.enabled )
		{
			document.forms['TheAdvSearch'].submit();
		}
	} );
	
	try {
		send_values( obj );
	} catch( err ) { }
	
	MakeDatePicker( ObjStart[0].identify(), 'Start' , 'Start', ''+ObjStart[0].attributes.value.value );
	MakeDatePicker( ObjEnd[0].identify(), 'End' , 'End', ''+ObjEnd[0].attributes.value.value );
}

function ClickMe( tab )
{
	var obj = $('advSearchTool');
	if ( tab )
		obj = tab.up('#advSearchBox');
	
	var ObjMLS=obj.select('.cl_mls');
	var ObjCounty=obj.select('.cl_county');
	var ObjSubDiv=obj.select('.cl_subdiv');
	var ObjAddress=obj.select('.cl_sAddress');
	var ObjAddressNum=obj.select('.cl_sAddressNum');
	var ObjCities=obj.select('.cl_sCity');
	var ObjSchoolDist=obj.select('.cl_schooldist');
	
	ObjMLS[0].value='';
	ObjCounty[0].value='';
	ObjSubDiv[0].value='';
	ObjAddress[0].value='';
	ObjAddressNum[0].value='';
	ObjCities[0].value='';
	ObjSchoolDist[0].value='';
	
	$('tab_Address').addClassName('tab_no');
	$('tab_Address').removeClassName('tab_yes');
	
	$('tab_mlsArea').addClassName('tab_no');
	$('tab_mlsArea').removeClassName('tab_yes');
	
	$('tab_Subdivision').addClassName('tab_no');
	$('tab_Subdivision').removeClassName('tab_yes');
	
	$('tab_City').addClassName('tab_no');
	$('tab_City').removeClassName('tab_yes');
	
	$('tab_MLS').addClassName('tab_no');
	$('tab_MLS').removeClassName('tab_yes');
	
	$('tab_County').addClassName('tab_no');
	$('tab_County').removeClassName('tab_yes');
	
	$(tab).removeClassName('tab_no');
	$(tab).addClassName('tab_yes');
	
	$('Subdivision_tab').style.display='none';
	$('mlsArea_tab').style.display='none';
	$('Address_tab').style.display='none';
	$('City_tab').style.display='none';
	$('County_tab').style.display='none';
	$('MLS_tab').style.display='none';
	
	$( Flip('_',$(tab).id) ).style.display='block';
}

var Target = 0;
var BioPane = null;
var revertBioPane = function() { };
function InitBios()
{
	BioPane = new Element('div');
	BioPane.style.padding = '5px';
	BioPane.style.background = 'white';
	BioPane.style.border = '1px solid #ccc';
	BioPane.style.position = 'absolute';
	$$('.AgentBiography').each( function( o ) {
		var Bx = null;
		if ( Bx = o.getAttribute('Bio') )
		{
			o.onmouseover = function() {
				Target = Bx;
				
				var changeBack = function() {
					o.style.position='static';
					o.setOpacity(1);
					o.onmouseout = function() { };
					BioPane.style.display = 'none';
				};
				
				var changeOn = function() {
					BioPane.innerHTML = o.BioText;
					
					if ( o.BioText.indexOf( 'EnableBioGraphyOffsetControl' ) == -1 )
						BioPane.style.left = 'auto';
					else
						BioPane.style.left = '22px';
					
					BioPane.style.display = o.BioText.length > 2 ? 'block' : 'none';
					o.parentNode.insertBefore( BioPane, o.parentNode.firstChild );
					o.style.position='relative';
					o.style.zIndex=32;
					o.setOpacity(0.001);
					o.onmouseout = function() {
						revertBioPane();
						revertBioPane = function() { };
					}
				};
				
				if ( o.BioText )
				{
					revertBioPane();
					changeOn();
					revertBioPane = changeBack;
				}
				else
				{
					if ( ! o.Waiting )
					{
						o.Waiting = 1;
						new Ajax.Request( 'ajax/getPhoto.php?Bio='+Bx, {
							onSuccess: function(ro) {
								o.BioText = ro.responseText+' ';
								if ( Bx == Target )
								{
									revertBioPane();
									changeOn();
									revertBioPane = changeBack;
								}
							}
						} );
					}
				}
				
			};
			
			/*
			o.onmouseout = function() {
				if ( ix == Target )
					BioPane.style.display = 'none';
			};
			*/
		}
	} );
}

function ajax_onFailure() { }
function TripListMessge( text )
{
	var res = null;
	if ( res = /<!--NewCount: ([0-9]+)-->/.exec( text ) )
	{
		try {
			var ele = null;
			if ( ele = $('TripListCount') ) ele.innerHTML = res[1];
			if ( parent ) if ( ele = parent.getElementById('TripListCount') ) ele.innerHTML = res[1];
			if ( top ) if ( ele = top.getElementById('TripListCount') ) ele.innerHTML = res[1];
		} catch( err ) { }
	}
	if ( text.indexOf( '<!--BadLocation-->' ) != -1 )
	{
		alert('We are sorry however this listing does not have a mapable location, so it cannot be used with the trip map feature.');
	}
	if ( text.indexOf( '<!--ToManyListings-->' ) != -1 )
	{
		alert('Your trip map list is already full, you can only map up to 8 listings at a time.');
	}
	if ( text.indexOf( '<!--Added-->' ) != -1 )
	{
		alert('The Listing was added successfully.');
	}
	if ( text.indexOf( '<!--AlreadyPresent-->' ) != -1 )
	{
		alert('The Listing is already on your Trip List.');
	}
	if ( text.indexOf( '<!--Removed-->' ) != -1 )
	{
		// alert('The Listing was Removed Successfully.');
	}
	if ( text.indexOf( '<!--NotPresent-->' ) != -1 )
	{
		alert('The Listing was not on your Trip List.' );
	}
}

var UpdateTripList = function( ro ) { TripListMessge( ro.responseText ); };

function Flip( d , id )
{
	c=id.indexOf( d );
	var g=id.substring( c+1 )+d+id.substring( 0 , c );
	return g;
}

function set_tripitem(a){
	var pdata = {	
		process: "settripitem",
		MLS: a
	};
	var myAjax = new Ajax.Request( 'ajax/gettriplist.php',
	{ method: 'post', parameters: pdata, onFailure: ajax_onFailure, onSuccess: UpdateTripList } );	
}

function get_bio( Bx )
{
	var lk3 = $('fishy_link_king3');
	lk3.href = 'forms.php?Title=View+Biography&StyleSheetID='+lk3.getAttribute('StyleSheetID')+'&view=biography&Bio='+Bx;
	var LB=new lightbox( lk3 );
	LB.activate();
	return false;
}

function gotoTripList()
{
	if ( parseInt( $('TripListCount').innerHTML ) <= 0 )
		alert('You must have at least 1 listing in your trip list.');
	else
		location.href = 'index.php?view=tripmap';
}

function show_calculator(){
	$('calculator').style.display = 'block';	
	$('calculator').style.visibility = 'hidden';	
	var toast = function() {
		$('calculator').clonePosition($('estim'),{ offsetTop: 15,offsetLeft: -170, setHeight: false, setWidth: false });
		$('calculator').style.visibility = 'visible';	
	};
	toast.defer();
}

function calc_mort_is_valid_char(char,valid_chars){
	var x = 0;
	var fnd = false;
	
	for (x in valid_chars){
		if (char == valid_chars[x]){
			fnd = true;
		}
	}
	
	return fnd;
}

function calc_mort_clean_number( amount ){	
	amount = amount.toString();
	var mx = amount.length;
	var ValidChars = new Array(1,2,3,4,5,6,7,8,9,0,'.');
	var Result = "";
	var y = 0;
	
	for (var x = 0; x < mx; x++){
		y = x+1;
		if (calc_mort_is_valid_char(amount.substring(x,y),ValidChars)){
			Result = Result + "" + amount.substring(x,y);
		}
	}
	
	Result = 1*Result;
	Result = Result.toFixed(2);
	return Result;
}

function MortCalc()
{
	var frm = $('mortgagecalculator_skin');
	var DwnPerc = calc_mort_clean_number( $F( frm.down('#dpperc') ) );
	var price = calc_mort_clean_number( $F( frm.down('#price') ) );
	var dwnpmt = calc_mort_clean_number( $F( frm.down('#dwnpmt') ) );
	var rate = calc_mort_clean_number( $F( frm.down('#rate') ) );
	var term = calc_mort_clean_number( $F( frm.down('#term') ) );
	var TaxRate = 1.42;
	var MIRate = 0.717;
	var HIRate = 0.75;
	new Ajax.Request( 'ajax/setMortCalc.php', {
		parameters: {
			term: term,
			rate: rate,
			dpperc: DwnPerc
		}
	} );

	var floorFunc = function(number)
	{
	  return Math.floor(number*Math.pow(10,2))/Math.pow(10,2);
	};
	
	var docalc = function( IR, YR, LA )
	{
	  var mi = IR / 1200;
	  var base = 1;
	  var mbase = 1 + mi;
	  for (i=0; i<YR * 12; i++)
	  {
		 base = base * mbase
	  }
	  return floorFunc(LA * mi / ( 1 - (1/base)))
	};
	
	//subtract the down payment from the price
	price -= dwnpmt;
	
	var PAI = (calc_mort_clean_number( docalc( rate, term, price ) )/1);
	var Tax = price*(TaxRate/1200);	
	var MI = price*(MIRate/1200);
	var HI = price*(HIRate/1200);
	var Payment = ((PAI/1)+(Tax/1)+(MI/1)+(HI/1));
	
	try
	{
		var defprice = calc_mort_clean_number( $F( frm.down('#defprice') ) );
		defprice -= dwnpmt;
		var defPAI = (calc_mort_clean_number( docalc( rate, term, defprice ) )/1);
		var defMI = defprice*(MIRate/1200);
		var defHI = defprice*(HIRate/1200);
		var defTax = defprice*(TaxRate/1200);	
		var defPayment = ((defPAI/1)+(defTax/1)+(defMI/1)+(defHI/1));
	} catch(err) {}
	
	// set input...
	try { frm.down('#monthlypayment').value = Payment.toFixed(0); } catch ( err ) {}
	
	// set span...
	try { frm.down('#payment').innerHTML = "$" + Payment.toFixed(0) + " per month"; } catch ( err ) {}
	try { $('estpayment').innerHTML = "$" + defPayment.toFixed(0) + "/mo"; } catch ( err ) {}
	
	try { frm.down('#mortcalcPayment').innerHTML = "$" + Payment.toFixed(0); } catch ( err ) {}
	try { frm.down('#mortcalcTax').innerHTML = "$" + Tax.toFixed(0); } catch ( err ) {}
	try { frm.down('#mortcalcPI').innerHTML = "$" + PAI.toFixed(0); } catch ( err ) {}
	try { frm.down('#mortcalcMI').innerHTML = "$" + MI.toFixed(0); } catch ( err ) {}
	try { frm.down('#mortcalcHI').innerHTML = "$" + HI.toFixed(0); } catch ( err ) {}
	try { drawChart(); } catch ( err ) {}
}

function calc_dwnpyment(){
	var frm = $('mortgagecalculator_skin');
	var DwnPerc = (frm.down('#dpperc').value/1);	
	var Price = frm.down('#price').value;
	Price = Price.replace("$","");
	Price = Price.replace(",","");
	frm.down('#dwnpmt').value = ( (Price/1)*(DwnPerc/100) ).toFixed(0);
}

var LB=null;

function AddFav( mls )
{
	var myAjax = new Ajax.Request( 'ajax/setFav.php',
	{ method: 'post', parameters: { process:'save_prop', MLS:mls } , onFailure: null, onSuccess: function( ro ) {
		if ( ro.responseText == 'Good' )
		{
			alert('This property has been added successfully.');
		}
		
		if ( ro.responseText == 'So-So' )
		{
			alert('This property is already in your favorites.');
		}
		
		if ( ro.responseText == 'Bad' )
		{
			LB=new lightbox( $('fishy_link_king') );
			LB.activate();
		}
	} } );
}

function Rotator_FadeAd( currentAd, FadeTime, callback )
{
	var d = new Date();
	var si = setInterval( function() {
		
		var n = new Date();
		var diff = n.getTime() - d.getTime();
		
		var opacity = 1.0 - ( diff / FadeTime );
		if ( opacity < 0 ) opacity = 0;
		
		currentAd.setOpacity( opacity );
		if ( opacity <= 0.001 )
		{
			currentAd.style.zIndex = 1;
			currentAd.style.display = 'none';
			clearInterval( si );
			
			if ( callback ) callback();
		}
		
	}, 30 );
}

var CurrentAd = '';
function EnableRotator( FlipTime, FadeTime)
{
	var inTransition = 0;
	var myFlip=0;
	var allAds = $$('.imgItem');
	CurrentAd = $$('.imgItemVisible').shift();
	CurrentAd.removeClassName( 'imgItemVisible' );
	CurrentAd.style.zIndex = 4;
	
	var off = 0;
	/*
	allAds.each( function( a ) {
		
		var i = new Element('div');
		i.style.backgroundImage = 'url(images/wcir.png)';
		i.className = 'AdSwitch';
		a.parentNode.appendChild( i );
		a.switcher = i;
		i.onclick = function() {
			if ( a != CurrentAd && !inTransition )
			{
				clearTimeout( myFlip );
				
				var nextAd = allAds.shift();
				allAds.push( CurrentAd );
				
				while ( nextAd != a )
				{
					allAds.push( nextAd );
					nextAd = allAds.shift();
				}
				
				CurrentAd.style.zIndex = 4;
				nextAd.style.zIndex = 3;
				nextAd.setOpacity( 1.0 );
				nextAd.style.display = 'block';
				
				nextAd.switcher.style.backgroundImage = 'url(images/gcir.png)';
				CurrentAd.switcher.style.backgroundImage = 'url(images/wcir.png)';
				
				inTransition = 1;
				Rotator_FadeAd( CurrentAd, FadeTime, function() {
					inTransition=0;
					myFlip = setTimeout( flipAds, FlipTime );
				} );
				
				CurrentAd = nextAd;
			}
		};
		
		i.style.right = ( ( allAds.length * 20 - off++ * 20 ) - 6 )+'px';
		
	} );
	*/
			
	// purge the first...
	allAds.splice( 0, 1 );
	//CurrentAd.switcher.style.backgroundImage = 'url(images/gcir.png)';
	
	var flipAds = function() {
		
		try {
			var nextAd = allAds.shift();
			
			CurrentAd.style.zIndex = 4;
			nextAd.style.zIndex = 3;
			nextAd.setOpacity( 1.0 );
			nextAd.style.display = 'block';
	
			//nextAd.switcher.style.backgroundImage = 'url(images/gcir.png)';
			//CurrentAd.switcher.style.backgroundImage = 'url(images/wcir.png)';
			
			allAds.push( CurrentAd );
	
			inTransition = 1;
			Rotator_FadeAd( CurrentAd, FadeTime, function() {
				inTransition=0;
				myFlip = setTimeout( flipAds, FlipTime );
			} );
			
			CurrentAd = nextAd;
		}
		catch( err )
		{
			
		}
		
	};
	
	myFlip = setTimeout( flipAds, FlipTime );
}

