//setup timer
//set up 

TargetDate = "05/30/2008 10:00 AM";
LeadingZero = true;
DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
FinishMessage = "It is finally here!";

if (typeof(DisplayFormat)=="undefined")
  DisplayFormat = "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
if (typeof(CountActive)=="undefined")
  CountActive = true;
if (typeof(FinishMessage)=="undefined")
  FinishMessage = "";
if (typeof(CountStepper)!="number")
  CountStepper = -1;
if (typeof(LeadingZero)=="undefined")
  LeadingZero = true;


CountStepper = Math.ceil(CountStepper);
if (CountStepper == 0)
  CountActive = false;
  
var SetTimeOutPeriod = ( Math.abs(CountStepper) -1)*1000 + 990 ;

//get the date...
var dthen = new Date(TargetDate);
var dnow = new Date();
if(CountStepper>0)
  ddiff = new Date(dnow-dthen);
else
  ddiff = new Date(dthen-dnow);

//get the difference
gsecs = Math.floor(ddiff.valueOf()/1000);


var guide = 
{
	setUpFxTargets: function()
	{
		$$('div.fxTarget').each(function(target)
		{
			target.makeDraggable();
		});
	},
	
	showFxTarget: function(target)
	{
		$(target).setStyles({
			'display': 'block',
			'visibility':'visible'
		});
	},
	
	/* accordion */
	makeAccordion: function ()
	{
		$$('ul.Accordion').each(function(el)
		{
			var minHeight = 0;
			el.getElements('ul.stretcher').each(function(stretcher)
			{
				if (minHeight < stretcher.scrollHeight) minHeight = stretcher.scrollHeight;
			});
			el.setStyle('height', minHeight+200+'px');

			new Accordion(el.getElements('a.stretchtoggle'),el.getElements('ul.stretcher'), {alwaysHide:true, start:'all-close'},
			{
				onActive: function()
				{
					(function()
					{
						if(this.previousClick > 0) 
						{
							var top = $(this.elements[this.previousClick]).getTop()-50;
							scroller.scrollTo(0, top);
						}
					}).bind(this).delay(500);
				}
			});	
		});
	},
	
	makeAccordion2: function ()
	{
		$$('div.Accordion2').each(function(el)
		{
			var minHeight = 0;
			el.getElements('div.stretcher2').each(function(stretcher)
			{
				if (minHeight < stretcher.scrollHeight) minHeight = stretcher.scrollHeight;
			});
			el.setStyle('height', minHeight+200+'px');

			new Accordion(el.getElements('h2.stretchtoggle2'),el.getElements('div.stretcher2'), {alwaysHide:true, start:'all-close'},
			{
				onActive: function()
				{
					(function()
					{
						if(this.previousClick > 0) 
						{
							var top = $(this.elements[this.previousClick]).getTop()-50;
							scroller.scrollTo(0, top);
						}
					}).bind(this).delay(500);
				}
			});	
		});
	},
	
	/* slider V1 */
	makeSlider: function()
	{
		this.sliders = $$('ul#slider li div.slide').map(function(ul)
		{
			return new Fx.Slide(ul, 
				{ 
					mode: 'vertical',
					onComplete: function()
					{
						if (this.now[0] >= 0) this.wrapper.setStyle('height', 'auto');
					}					
				} 
			).hide();
		});
		$$('ul#slider h2.slideOpener').each(function(lnk, index)
		{
			lnk.addEvent('click', function()
			{
				this.sliders.each(function(slider, sliderIndex)
				{
					if (sliderIndex == index) this.sliders[sliderIndex].toggle();
				}, this); /*	Here's the bind "this" for .each	*/
			}.bind(this)); /*	and here's the explicit .bind(this) for .addEvent's function	*/
		}, this); /* and this is the last .each, so it has a "this" too	*/
	},
		
	get_tips: function ()
	{
		var as = [];
		$$('.tip').each(function(a)
			{
		  	if (a.getAttribute('title')) as.push(a);
			}
		);

		new Tips(as, 
		{
		  maxTitleChars: 100,
		  onShow: function(tip)
		  {
			tip.setStyle('opacity', '0.8');
		  },
		  onHide: function(tip)
		  {
			tip.setStyle('visibility', 'hidden');
		  },
		  offsets: {'x': -10, 'y': 15}
		});
	},
	
		
	init: function()
	{
		this.setUpFxTargets();
		this.makeSlider();
		this.makeAccordion();
		this.makeAccordion2();
		this.get_tips();
	}


}

window.onDomReady(guide.init.bind(guide));


	
	//used to call alert in admin for deletion
function remove_confirm (id)
{
	if ( confirm ( 'Delete This Item?') )
	{
		$("ID_"+id).remove();
		// delete item from DB and page..
		xajax_remove(id);
	}
}

function toggleHeight(id)
{
	new Fx.Slide("ID_"+id ).toggle() ;
}

function toggleHeight_raw(id)
{
	new Fx.Slide(id).toggle() ;
}


function pop_open (url, height, width, b_scroll )
{
	if ( b_scroll == null )
		b_scroll = 0
			
	newwindow=window.open(url,'Image', 'height='+height+', width='+width+', location=0, status=1, scrollbars='+b_scroll+', toolbar=0' );
	s_width = screen.width ;
	s_height = screen.height - 50;
	
	y = (s_height - height )/2;
	x = (s_width - width)/2 ;
	
	newwindow.moveTo(x,y);
	if ( window.focus ) 
	{
		newwindow.focus()
	}
}


	function load_init ( )
	{
		//run these functions when dom is ready 
		if (typeof page_functions=="function")
		{	
			page_functions ();
		}	
		//CountBack( gsecs );
	}
		
	var alreadyrunflag=0 //flag to indicate whether target function has already been run

	if (document.addEventListener)
	  document.addEventListener("DOMContentLoaded", function(){alreadyrunflag=1; load_init() }, false)
	else if (document.all && !window.opera)
	{
	  document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>')
	  var contentloadtag=document.getElementById("contentloadtag")
	  contentloadtag.onreadystatechange=function()
	  {
		if (this.readyState=="complete")
		{
		  alreadyrunflag=1;
		  load_init();
		}
	  }
	}
	

	window.onload=function()
	{
		setTimeout("if (!alreadyrunflag){ load_init();}", 0) ;
	}
	
	
	function fade_text ( c1, id )
	{
		var myHide = new Fx.Slide( $(id), {duration: 1000, onComplete: function ( id ) { id.remove(); } } );
		var myTween = new Fx.Style( $(id), "color", 
				{ 
					"duration":10000, 
					onComplete: function () 
						{ 
							myHide.slideOut();									
						} 
				} 
			);
		myTween.start( c1 );	
	}


//process string for current number
// ( number of seconds to work with, how many seconds in unit, how many units to next unit )
function calcage( secs, num1, num2 ) 
{
	s = ( ( Math.floor (secs/num1) )%num2).toString();
  	if ( LeadingZero && s.length < 2 )
		s = "0" + s;
  	return s;
}

//replace the string..
function CountBack( secs ) 
{
  	if (secs < 0) 
	{
    	document.getElementById("cntdwn").innerHTML = FinishMessage;
		return;
  	}
	
	//calc and replace the string..
	el = $('days').innerHTML = calcage( secs,86400,100000 ); //days
	el = $('hours').innerHTML = calcage( secs,3600,24 ) ;  // hours
  	el = $('mins').innerHTML =  calcage( secs,60,60 ); // minutes
 	el = $('secs').innerHTML = calcage( secs,1,60 );  // seconds

  	if ( CountActive )
    	setTimeout(" CountBack (" + ( secs+CountStepper) + ")", SetTimeOutPeriod );
}

function putspan(backcolor, forecolor) 
{
 	document.write("<span id='cntdwn' style='background-color:" + backcolor + 
                "; color:" + forecolor + "'></span>");
}


function clearText(theText) 
{
	if (theText.value == theText.defaultValue)
	{
	theText.value = ""
	}
}

