// JavaScript Document
var currentMenuPage=0;
var nblAlertCB=null;
var preLoadImg=-1;
var slideNum=0;
var slideLength=		5000;
var firstSlideLength=	5000;
var infoSpeed=			0.4;
var slideSpeed=			0.1;
var slideElems=[];
var infoElems=[];
var currentSlide=null;
var inSlide=null;
var currentInfo=null;
var inInfo=null;
var outInfo=null;
var slideContainer=null;
var isIE=window.navigator.appName.toLowerCase().indexOf('microsoft internet explorer')!=-1;

function nbl_onload(){
	if(doc('nbl_home_slide_show_info')){
		startTween(doc('nbl_home_slide_show_info').style,'left','?px',830,500,0.2,500);
		startTween(doc('nbl_home_slide_show_ad_b').style,'top','?px',395,308,0.2);
	}
	
	startSlideShow();
	
	if(doc('nbl_poll'))
		startTween(doc('nbl_poll').style,'left','?px',-185,0,0.2,1000);
	
	if(doc('nbl_ordernow'))
		startTween(doc('nbl_ordernow').style,'left','?px',273,0,0.2);
	
	if(doc('nbl_newsletter'))
		setTimeout(showNewsletter,5000);
}
function startSlideShow(){
	preLoadSlideImg();
	if(isIE)
		document.getElementById('nblSlidePreLoadContainer').style.filter='alpha(opacity=1)';
	inSlide=document.getElementById('nblFirstSlide');
	inInfo=document.getElementById('nbl_home_slide_show_info');
	slideElems[0]=inSlide;
	infoElems[0]=inInfo;
	slideContainer=document.getElementById(page?'nbl_slide_show_container':'nbl_home_slide_show_container');
	setTimeout(nextSlide,firstSlideLength);
}
function nextSlide(){
	var first=false;
	if(currentSlide){
		first=true;
		slideContainer.removeChild(currentSlide);
		slideContainer.removeChild(currentInfo);
	}
	
	currentSlide=inSlide;
	currentInfo=inInfo;
	slideNum++;
	if(slideNum>=slides.length)
		slideNum=0;
	if(slideElems[slideNum]){
		inSlide=slideElems[slideNum];
		inInfo=infoElems[slideNum];
	}else{
		inSlide=document.createElement('div');
		inSlide.style.backgroundImage='url('+host+'images/slide_show'+spfix+'/'+slides[slideNum]+')';
		inSlide.className='nbl_slide';
		slideElems[slideNum]=inSlide;
		
		inInfo=document.createElement('div');
		inInfo.style.backgroundImage='url('+host+'images/slide_show_info'+spfix+'/'+slides[slideNum].substr(0,slides[slideNum].length-3)+'png)';
		inInfo.className='nbl_slide_info';
		infoElems[slideNum]=inInfo;
		inInfo.style.zIndex=101;
	}
	currentSlide.style.zIndex=1;
	inSlide.style.zIndex=2;
	
	if(isIE){
		inSlide.style.filter='alpha(opacity=0)';
		startTween(inSlide.style,'filter','alpha(opacity=?)',0,100,slideSpeed,0,waitForNextSlide);
	}else{
		inSlide.style.opacity=0;
		startTween(inSlide.style,'opacity','?',0,100,slideSpeed,0,waitForNextSlide,100);
	}
	if(currentInfo)
		startTween(currentInfo.style,'left','?px',500,830,infoSpeed,0,nextInfo);
	slideContainer.appendChild(inSlide);
	slideContainer.appendChild(inInfo);
}
function nextInfo(){
	startTween(inInfo.style,'left','?px',830,500,infoSpeed);
}
function waitForNextSlide(){
	setTimeout(nextSlide,slideLength);
}
function preLoadSlideImg(){
	preLoadImg++;
	if(preLoadImg>=slides.length)
		return;
	document.getElementById('nblSlidePreLoadContainer').innerHTML='<img src="'+host+'images/slide_show'+spfix+'/'+slides[preLoadImg]+'" onload="preLoadSlideImg()" />';
}
function sendNews(){
	send('index.php?news=1',{
		name:doc('nbl_news_name').value,
		email:doc('nbl_news_email').value,
		birth:doc('nbl_news_birth').value
	},onNewsComplete);
}
function onNewsComplete(result){
	if(result.length==1){
		if(result=='1')
			nbl_alert('Thank you for signing up for Noce\'s News Letter',closeNewsletter);
		else
			nbl_alert('Sorry there was a problem when trying to sign you up for Noce\'s News Letter. Please try agin latter.',closeNewsletter);
	}else{
		nbl_alert(result);
	}
	
}
function showNewsletter(){
	doc('nbl_messsage_container').style.top='-380px';
	doc('nbl_newsletter').style.display='block';
	var style=doc('nbl_message_spacer').style;
	style.height='30px';
	doc('nbl_messsage_container').style.display='block';
	startTween(style,'height','?px',30,430,0.4);
}
function closeNewsletter(){
	startTween(doc('nbl_message_spacer').style,'height','?px',430,30,0.4,0,hideNewsletter);
	send('index.php?setnews=1');
}
function hideNewsletter(){
	doc('nbl_newsletter').style.display='none';
	doc('nbl_messsage_container').style.display='none';
}
function nbl_alert(message,callback){
	doc('nbl_alert_container').style.display='block';
	doc('nbl_alert_spacer').style.height='0px';
	doc('nbl_alert_content').innerHTML=message;
	nblAlertCB=callback;
	startTween(doc('nbl_alert_spacer').style,'height','?px',-219,450,0.4);
}
function closeAlert(){
	startTween(doc('nbl_alert_spacer').style,'height','?px',450,0,0.4,0,hideAlert);
}
function hideAlert(){
	doc('nbl_alert_container').style.display='none';
	doc('nbl_alert_content').innerHTML='';
	if(nblAlertCB)
		nblAlertCB();
}

function displayContact(){
	doc('nbl_messsage_container').style.top='-438px';
	doc('nbl_contact').style.display='block';
	var style=doc('nbl_message_spacer').style;
	style.height='30px';
	doc('nbl_messsage_container').style.display='block';
	startTween(style,'height','?px',30,550,0.4);
}
function closeContact(){
	startTween(doc('nbl_message_spacer').style,'height','?px',500,30,0.4,0,hideContact);
}
function hideContact(){
	doc('nbl_contact').style.display='none';
	doc('nbl_messsage_container').style.display='none';
}

function submitContact(){
	var data={
		name:doc('nbl_contact_name').value,
		phone:doc('nbl_contact_phone').value,
		email:doc('nbl_contact_email').value,
		birth:doc('nbl_contact_birth').value,
		message:doc('nbl_contact_message').value
	}
	send('index.php?contact=1',data,onSubmitContactComplete);
}
function onSubmitContactComplete(result){
	if(result.length==1){
		nbl_alert(result=='1'?
				  'Thank you. Your message has been sent and a representative will be in contact with you within the next few business days.':
				  'Sorry your message was unable to be sent a this time. Please try agin later.',closeContact);
	}else{
		nbl_alert(result,null);
	}
}

function showMenuPage(page){
	doc('nbl_menu_page_'+currentMenuPage).style.display='none';
	doc('nbl_menu_tab_'+currentMenuPage).style.color='';
	doc('nbl_menu_page_'+page).style.display='block';
	doc('nbl_menu_tab_'+page).style.color='#000';
	currentMenuPage=page;
}

function startTween(obj,dim,dimVal,start,end,speed,delay,callback,div){
	var tweenObj={
		obj:obj,
		dim:dim,
		dimVal:dimVal,
		start:start,
		end:end,
		pos:start,
		speed:speed,
		callback:callback,
		div:div?div:1,
		intv:0,
		running:true
	}
	var i=dimVal.indexOf('?');
	var dimValA=dimVal.substr(0,i);
	var dimValB=dimVal.substr(i+1);
	function doTween(){
		if(Math.abs(Math.abs(end)-Math.abs(tweenObj.pos))<=2){
			clearInterval(tweenObj.intv);
			tweenObj.pos=end;
			obj[dim]=dimValA+tweenObj.pos+dimValB;
			tweenObj.running=false;
			if(tweenObj.callback)
				tweenObj.callback(tweenObj);
		}
		tweenObj.pos+=(end-tweenObj.pos)*speed;
		obj[dim]=dimValA+(tweenObj.pos/tweenObj.div)+dimValB;
	}
	function startDoTween(){
		tweenObj.intv=setInterval(doTween,65);
	}
	if(delay){
		setTimeout(startDoTween,delay);
	}else{
		startDoTween();
	}
	return tweenObj;
	
}

function voteForTopping(topping){
	startTween(doc('nbl_poll').style,'left','?px',0,-200,0.4);
	send('index.php?vote_for_topping='+topping);
}

function send(url,data,callback){
	if(window.XMLHttpRequest){
		xmlhttp=new XMLHttpRequest();
	}else if(window.ActiveXObject){
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		alert('Sorry your browser does not support direct client to server communication.');
		return false;
	}
	if(data){
		var query='';
		for(e in data){
			query+='&'+e+'='+encodeURIComponent(data[e]);
		}
		url+=url.indexOf('?')>-1?query:'?'+query.substr(1);
	}
	if(callback){
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4 && xmlhttp.status==200){
				callback(xmlhttp.responseText);
			}
		}
	}
	xmlhttp.open('POST',url,true);
	xmlhttp.send();
}



function doc(id){
	return document.getElementById(id);
}
