// Vars that we will be using on this page
var first_string='';
var last_view_string='';
var visible_ball='staticBall';
var ball_status='clear';
var view_counter=0;
var last_views=[]
var first_page='';
var from_images='n';
var open_form_section=null;

/* Timer objects */
var ss_to=null;
var ci_to=null;
var as_to=null;

/* CI style path variables for both base and image directories. */
locale=='local' ? base='http://localhost/promomailers.com/index.php/': base='http://www.promomailers.com/index.php/';
locale=='local' ? base_images='http://localhost/promomailers.com/images2/': base_images='http://www.promomailers.com/images2/';
locale=='local' ? base_images1='http://localhost/promomailers.com/images/': base_images1='http://www.promomailers.com/images/';

/* This has to be here as it kicks the whole thing off from  the index page. */
function load_sub_cats(category)
	{
	/* Figure out the URL */
	var url=base+'gallery/categories/'+category;

	set_last_view();

	/* Set the loading image */
	d_outer=document.getElementById('dynamic_outer');
	d_outer.innerHTML="<div><img src='"+base_images+"loading.gif' border='0'></div>";

	/* Now send the data */
	var ajU = new Ajax.Updater
		(
		'dynamic_outer',
		url,
		{ success: 'dynamic_outer', failure: 'notice', asynchronous:true, onComplete:'completed'  }
		)
	// set_last_view();
	}


/* Start managing sub-categories here.*/
function subcats(category)
	{
	/* Build the url */
	var url=base+'gallery/subcats/'+category;

	/* Set the loading image */

	/* Send the data */
	var ajU = new Ajax.Updater
		(
		'dynamic_outer',
		url,
		{ success: 'dynamic_outer', failure: 'notice', asynchronous:true, onComplete:'completed'  }
		)
	from_images='y';
	set_last_view();
	}


function completed(response)
	{ alert(response); }

/* This has to be here as it kicks the whole thing off from  the index page. */
function startover()
	{
	/* Figure out the URL */
	var url=base+'main/ajax_index';

	/* Set the loading image */
	d_outer=document.getElementById('dynamic_outer');
	d_outer.innerHTML="<div><img src='"+base_images+"loading.gif' border='0'></div>";

	/* Now send the data */
	var ajU = new Ajax.Updater
		(
		'dynamic_outer',
		url,
		{ success: 'dynamic_outer', failure: 'notice', asynchronous:true, onComplete:'completed'  }
		)
	// set_last_view();
	}


/* **************************************************************************************
Manage views here
************************************************************************************** */
function first_view(action)
	{
	/* Local and non-local variable setup */
	var fv=document.getElementById('dynamic_center');
	action='save' ? first_string=fv.innerHTML: fv.innerHTML=first_string;
	}

function post_first_view()
	{
	// alert();
	var fv=document.getElementById('dynamic_outer');
	fv.innerHTML=first_string;

	/* Set all things anew */
	view_counter=0;
	}

function post_last_view(full)
	{
	/* Local setup */
	if(full=='y')
		{ fv=document.getElementById('dynamic_outer'); }
	else
		// { fv=document.getElementById('dynamic_center'); }
		{ fv=document.getElementById('dynamic_outer'); }
	fv.innerHTML=last_views[(view_counter-1)];
	last_views.pop();
	--view_counter;
	}

function post_last_view2(full)
	{
	/* Local setup */
	if(full=='y')
		{ fv=document.getElementById('dynamic_outer'); }
	else
		{ fv=document.getElementById('dynamic_center'); }
	fv.innerHTML=last_views[(view_counter-1)];
	last_views.pop();
	--view_counter;
	}

function set_last_view()
	{
	from_images=='y' ?
		lvData=document.getElementById('dynamic_outer'):
		lvData=document.getElementById('dynamic_outer');

	if(from_images=='y')
		{
		last_view_str=lvData.innerHTML;
		last_views[view_counter]=lvData.innerHTML;
		// last_views.pop();
		++view_counter;
		from_images='n';
		}
	else
		{
		last_view_string=lvData.innerHTML;
		last_views[view_counter]=lvData.innerHTML;
		// last_views.pop();
		++view_counter;
		}

	/* Check if the first page has been set. If not, set it here */
	if(first_page=='')
		{
		fp=document.getElementById('dynamic_outer');
		first_page=fp.innerHTML;
		}
	}

function set_last_view_2()
	{
	from_images=='y' ?
		lvData=document.getElementById('dynamic_outer'):
		lvData=document.getElementById('dynamic_center');

	if(from_images=='y')
		{
		last_view_str=lvData.innerHTML;
		last_views[view_counter]=lvData.innerHTML;
		++view_counter;
		from_images='n';
		}
	else
		{
		last_view_string=lvData.innerHTML;
		last_views[view_counter]=lvData.innerHTML;
		++view_counter;
		}

	/* Check if the first page has been set. If not, set it here */
	if(first_page=='')
		{
		fp=document.getElementById('dynamic_outer');
		first_page=fp.innerHTML;
		}

	}


function showStart()
	{
	var dO=document.getElementById('dynamic_outer');
	dO.innerHTML=first_page;
	last_views=[];
	}


function submit_zip_search()
	{
	var zip_formObj=document.getElementById('zipcode');
	var url='http://localhost/promomailers.com/index.php/main/findDealer/'+zip_formObj.value;
	var thezip=zip_formObj.value;

	// Check length
	if(thezip.length<5)
		{ alert("The Zip code you entered is too short."); return; }
	if(thezip.length>5)
		{ alert("The Zip code you entered is too long."); return; }

	if(IsNumeric(zip_formObj.value))
		{
		var ajU = new Ajax.Updater
			(
			'dynamic_center',
			url,
			{ success: 'dynamic_center', failure: 'notice', asynchronous:true, onComplete:'completed'  }
			);
		}
	else
		{ alert("The Zip code you entered is not valid."); }
	}


/* Show the login box with leet fade in shizzle */
function show_login()
	{
		$('login_div').slideDown({duration:.5});
	}

/* Have the login box go away in some way */
function hide_login()
	{
		$('login_div').slideUp({duration:.5});
	}


/* Make the static ball bounce */
function toggleSpot2(el)
	{
	var sb=document.getElementById('staticBall');
	var img=document.getElementById('bouncingSRC');

    if(ball_status=='clear' && el=='bouncingBall')
        {
		img.src=base_images1+'Spot-On-DM.gif';
        ball_status='running';
        var ss_to=setTimeout ( function() {toggleSpot2('show_static')}, 1000 );
        }
    else if(el=='show_static')
        {
		$('bouncingSRC').fade({duration:4.0});
        var ci_to=setTimeout ( function() {toggleSpot2('change_image')}, 4000 );
        }
	else if(el=='change_image')
		{
		img.src=base_images1+"spot-On-6_04.jpg";
        var as_to=setTimeout ( "toggleSpot2('appear_static')", 50 );
		}
	else if(el=='appear_static')
		{
        // sb.appear({duration:0.250});
        img.appear({duration:0.250});
		ball_status='clear';
		}
	}


/* Let's continue to do leet shizzle here with the dealer order form */
function toggle_form_subsection(section)
	{
	if(section==open_form_section)
		{
		$(section).fade();
		open_form_section=null;
		}
	else if (open_form_section==null)
		{
		$(section).style.display='block';
		open_form_section=section;
		}
	else
		{
		$(open_form_section).fade();
		$(section).style.display='block';
		open_form_section=section;
		}


	}

function go_back()
{
	history.back();
}

