var animateX = -30;
var animateInterval = 10;

var currentPage = null;
var currentDialog = null;
var currentWidth = 0;
var currentHash = location.hash;
var hashPrefix = "#_";
var pageHistory = [];

var default_page_title = "Jack in the Box - ";

addEventListener("load", function(event)
{
    var body = document.getElementsByTagName("body")[0];
    for (var child = body.firstChild; child; child = child.nextSibling)
    {
        if (child.nodeType == 1 && child.getAttribute("selected") == "true")
        {
            showPage(child);
            break;
        }
    }

    setInterval(checkOrientAndLocation, 300);
    setTimeout(scrollTo, 0, 0, 1);
}, false);


function checkOrientAndLocation()
{
	var orient;
	
    switch(window.orientation)
    {
        case 0:
            orient = "portrait";
            break;
        case -90:
            //orient = "Landscape (right, screen turned clockwise)";
            orient = "landscape";
            break;
        case 90:
            //orient = "Landscape (left, screen turned counterclockwise)";
            orient = "landscape";
            break;
        case 180:
            //orient = "Portrait (upside-down portrait)";
            orient = "portrait";
            break;
        default:
        	orient = "unknown";
    }
	
    document.body.setAttribute("orient", orient);
    //document.getElementById('orientation_value').innerHTML = orient + "---" + window.outerWidth;

    if (location.hash != currentHash)
    {
        currentHash = location.hash;

        var pageId = currentHash.substr(hashPrefix.length);
        var page = document.getElementById(pageId);
        if (page)
        {
            var index = pageHistory.indexOf(pageId);
            var backwards = index != -1;
            if (backwards)
                pageHistory.splice(index, pageHistory.length);
                
            showPage(page, backwards);
        }
    }
}	
    
function showPage(page, backwards)
{
    if (currentDialog)
    {
        currentDialog.removeAttribute("selected");
        currentDialog = null;
    }

    if (!page)
    {
    	return;
    }
    
    location.href = currentHash = hashPrefix + page.id;
    pageHistory.push(page.id);

    var fromPage = currentPage;
    currentPage = page;
        
    if (fromPage)
        setTimeout(swipePage, 0, fromPage, page, backwards);
}


function swipePage(fromPage, toPage, backwards)
{        
    toPage.style.left = "100%";
    scrollTo(0, 1);
    
    var percent = 100;
    var timer = setInterval(function()
    {
        percent += animateX;
        if (percent <= 0)
        {
            percent = 0;
            fromPage.removeAttribute("selected");
            clearInterval(timer);
        }
	    
        toPage.setAttribute("selected", "true");

        fromPage.style.left = (backwards ? (100-percent) : (percent-100)) + "%"; 
        toPage.style.left = (backwards ? -percent : percent) + "%"; 
    }, animateInterval);
}


function set_back_button(new_text)
{
	$('sectionButton').innerHTML = new_text;
	if (new_text == '')
	{
		$('sectionButton').style.display = "none";
	}
	else
	{
		$('sectionButton').style.display = "block";
	}
}


function set_page_title(section)
{
	document.title = default_page_title + section;
}

function check_lead_form()
{
	var errorcount = 0;
	
	if (document.leadform.first_name.value == ''){errorcount++;}
	if (document.leadform.last_name.value == ''){errorcount++;}
	if (document.leadform.company.value == ''){errorcount++;}
	if (document.leadform.title.value == ''){errorcount++;}
	if (document.leadform.phone.value == ''){errorcount++;}
	if (document.leadform.email.value == ''){errorcount++;}		
	
	if (errorcount == 0)
	{
		document.leadform.submit();
		return true;
	}
	else
	{
		alert("Please complete the entire form.");
		return false;
	}
}
