var _color_boxes = [];
var _div_info = false;
var _preloadImgs = {};
var _active_image = "";
var _fadeTimeout = false;
var _slideGo = true;

var videostitle = [];
var videos = [];

function initPage()
{
	initForm();

	var Tips1 = new Tips($$('.tip'));
	
	var _div = document.getElementById('color-box');
	_div_info = document.getElementById('info-box');
	_div_info_content = document.getElementById('info-box-content');
	_banner_box = document.getElementById('banner-box');
	_menu_box = document.getElementById('menu-box');
	_page_box = document.getElementById('page-box');
	_page = document.getElementById('page');
	_fyr_tv = document.getElementById('fyr-tv');

	_marguee_box = document.getElementById('marguee-box');
	_news_box = document.getElementById('news-box');


	_bg1 = document.getElementById('bg1');
	_bg2 = document.getElementById('bg2');

	if(_bg1)
	{
		_bg1._mooeff = new Fx.Styles( _bg1.id, {duration: 800,
					onStart: function(){
					},
					onComplete: function(){
							_page.style.background = this.element.style.background;					
					}			
			});
	}
	if(_bg2)
	{
		_bg2._mooeff = new Fx.Styles( _bg2.id, {duration: 800,
					onStart: function(){
					},
					onComplete: function(){						
					}			
			});
	}
	_close_info = document.getElementById('close-info');
	if(_close_info)
	{
		_close_info.href = "javascript:;";
		_close_info.onclick = hideInfo;
	}
	
	if(_div)
	{
		var __divs = _div.getElementsByTagName('div');
		for(var i=0; i<__divs.length; i++)
		{
			if(__divs[i].className.indexOf("color-box") != -1)
			{
				__divs[i].onclick = function()
				{
					showInfo(this._index);
				}
				__divs[i]._index = _color_boxes.length;
				var _as = __divs[i].getElementsByTagName('a');
				var _for_preload = [];
				for(var k=0; k<_as.length; k++)
				{
					if(_as[k].parentNode.parentNode.className.indexOf('thumb') != -1)
					{
						if(_as[k].parentNode.className.indexOf("active") != -1)
						{
							_active_image = _as[k].href;
						}
						else
						{
							_for_preload[_for_preload.length] = _as[k].href;
						}
					}
				}
				__divs[i]._for_preload = _for_preload;
				__divs[i]._active_image = _active_image;
				_color_boxes[_color_boxes.length] = __divs[i];
			}
		}
	}	
	if(_bg1 && _bg2)
	{
		initFadeSlideShow();
		clearTimeout(_fadeTimeout);
		_fadeTimeout = setTimeout('switchBG()',5000);
	}
}

function switchBG()
{	
	if(_slideGo && _color_boxes[_fades[0].getNext(1)])
	{
		_showImageBG(_color_boxes[_fades[0].getNext(1)]._active_image);
	}
}

function _showImageBG(_img)
{
	if(true)
	{
		var _imgObject = new Image();
		_imgObject.src = _img;
		if(_imgObject.complete)
		{
				showImageBG(_img);			
		}
		else
		{
			_imgObject.onload = function()
			{
				showImageBG(this.src);
			}
		}
	}
}

function showImageBG(_img)
{

	_bg1._mooeff.set({'opacity':0});
	_bg2._mooeff.set({'opacity':1});
	_bg1.style.background = 'url("'+_img + '")';
	_bg2.style.background = _page.style.background;
	_page.style.background = 'none';
	_bg1._mooeff.custom({'opacity':[0,1]});
	_bg2._mooeff.custom({'opacity':[1,0]});	
	_fades[0].slide(1);
	//_page.style.background = 'none';//'url("'+_img + '")';
	clearTimeout(_fadeTimeout);
	_fadeTimeout = setTimeout('switchBG()',5000);
		
}


function initImageGallery()
{
	var _as = _div_info_content.getElementsByTagName('a');
	for(var i=0; i<_as.length; i++)
	{
		if(_as[i].parentNode.parentNode.className.indexOf('thumb') != -1)
		{
			if(_as[i].parentNode.className.indexOf("active") != -1)
			{
				_active_image = _as[i].href;
			}
			_as[i]._href = _as[i].href;
			_as[i].href = 'javascript:;';
			_as[i].onclick = function()
			{
				clearActiveLi();
				this.parentNode.className = "active";
				_showImage(this._href);
			}
		}
	}
}

function clearActiveLi(_el)
{
	var _ul = _div_info_content.getElementsByTagName('ul');
	if(_ul[0])
	{
		var _lis = _ul[0].getElementsByTagName('li');
		for(var i=0; i<_lis.length; i++)
		{
			_lis[i].className = "";
		}		
	}
}

function _showImage(_img)
{
		var _imgObject = new Image();
		_imgObject.src = _img;
		if(_imgObject.complete)
		{
			showImage(_img);
		}
		else
		{
			_imgObject.onload = function()
			{
				showImage(this.src);
			}
		}
}

function showImage(_img)
{
	_page.style.background = 'url("'+_img + '")';
}

function showInfo(_index)
{
	_slideGo = false;
	clearTimeout(_fadeTimeout);
	if(_color_boxes[_index] && _div_info)
	{
		var _div = _color_boxes[_index].getElementsByTagName('div');
		if(_div[0])
		{
			_div_info_content.innerHTML = _div[0].innerHTML;
			initImageGallery();
			displayEls([_banner_box,_menu_box,_page_box,_fyr_tv,_bg1,_bg2,_marguee_box,_news_box],'none');
			displayEls([_div_info],'block');
		}	
		preloadImages(_index);
		try{close_popup()}catch(e){};
	}
}

function hideInfo()
{
			displayEls([_div_info],'none');
			displayEls([_banner_box,_menu_box,_page_box,_fyr_tv,_bg1,_bg2,_marguee_box,_news_box],'block');	
			if(_active_image)
			{
				showImage(_active_image);
			}
		clearTimeout(_fadeTimeout);
		_slideGo = true;
		_fadeTimeout = setTimeout('switchBG()',5000);
}

function preloadImages(_index)
{
	var _imgObject = [];
	if(_color_boxes[_index]._for_preload)
	{
		for(var k=0; k<_color_boxes[_index]._for_preload.length; k++)
		{		
			var _cnt = _imgObject.length;
			_imgObject[_cnt] = new Image();
			_imgObject[_cnt].src = _color_boxes[_index]._for_preload[k];
		}		
	}
}

function displayEls(els,val)
{
	for(var i=0; i < els.length; i++)
	{
		if(els[i])
		{
			els[i].style.display = val;
		}
	}	
}

var slideshows = ['div.slideshow'];
var _fades = [];

function initFadeSlideShow()
{		
	_curr = getCookie('fyr_bg_num');
	if(!_curr) _curr = 0;
	//_curr = _curr + 1 - 1;
	for(var i=0; i < slideshows.length; i++)
	{
		_fades[i] = new FadeSlideShow("" + slideshows[i],{_current: _curr,duration:800,
					onStart: function(){
						this.elements[this.options._current].className += " active";
						setCookie('fyr_bg_num',this.options._current);
					},
					onComplete: function(){							
					}		
			});
	}
}

function setCookie(name,value,days,path,domain,secure) {
  var expires, date;
  if (typeof days == "number") {
    date = new Date();
    date.setTime( date.getTime() + (days*24*60*60*1000) );
		expires = date.toGMTString();
  }
  document.cookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function getCookie(name) {
  var nameq = name + "=";
  var c_ar = document.cookie.split(';');
  for (var i=0; i<c_ar.length; i++) {
    var c = c_ar[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameq) == 0) return unescape( c.substring(nameq.length, c.length) );
  }
  return null;
}

function deleteCookie(name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

if (window.addEventListener)
	window.addEventListener("load", initPage, false);
else if (window.attachEvent)
	window.attachEvent("onload", initPage);
	

function initForm()
{
	var _s = $('form-submit');
	if(_s)
	{
		_s.href = "javascript:;";
		_s.onclick = validateForm;			
	}
	var _contact_dealer = $('contact-dealer');
	if(_contact_dealer)
	{
		_contact_dealer.onclick = contactHandler;
	}	
}

function contactHandler()
{
	var _contact_phone = $('contact-by-phone');
	var _contact_phone_i = $('contact-by-phone-input');
	if(_contact_phone && _contact_phone_i)
	{
		if(this.checked)
		{
			displayEls([_contact_phone],'block');
		}
		else
		{
			_contact_phone_i.checked = false;
			displayEls([_contact_phone],'none');
		}
			
	}
	
}

function validateForm()
{

	if(!validateEmpty($('form-first').value))
	{
		alert('Please tell us your First Name.');
		return false;	
	}
	if(!validateEmpty($('form-last').value))
	{
		alert('Please tell us your Last Name.');
		return false;	
	}
	if(!validateEmpty($('form-email').value))
	{
		alert('Please tell us your Email.');
		return false;	
	}
	if(!validateEmail($('form-email').value))
	{
		alert('Your email is invalid.');
		return false;	
	}
	if($('contact-dealer').checked && !validateEmpty($('contact-by-phone-input').value))
	{
		alert('Please tell us your home or business phone number where you can be reached.');
		return false;	
	}
	if($('dialer-mobile-alerts').checked && !validateEmpty($('form-mobile').value))
	{
		alert('Please tell us your Mobile Number.');
		return false;	
	}
	if($('dialer-mobile-alerts').checked && !validatePhoneNumber($('form-mobile').value))
	{
		alert("Your Mobile Number is invalid.\nFormat: XXX-XXX-XXXX");
		return false;	
	}

	if($('sign-up-form'))
	{
		$('sign-up-form').submit();
	}
	return false;
}

function validateEmpty(str)
{
	var emptyReg = new RegExp("^.+$", "g");
	return emptyReg.test(str);
}

function validateEmail(email)
{
	return (email.indexOf(".") > 0) && (email.indexOf("@") > 0);
}

function validatePhoneNumber(str)
{
	var cellreg=/^\d{3}\-\d{3}\-\d{4}$/;
	var parts=str.split('-');
	if (!cellreg.test(str))
	{
		return false;
	}
	else if(parseInt(parts[0])==0 || parseInt(parts[1])==0 || parseInt(parts[2])==0)
	{
		return false;
	}
	return true;

}
