var animateX = -20;
var animateInterval = 24;

var currentPage = null;
var currentDialog = null;
var currentWidth = 0;
var currentHash = location.hash;
var hashPrefix = "#_";
var pageHistory = [];
var checkOrientAndLocation;
var checkOrientAndLocationResized;
var resized=false;
var lastTarget='home';
window.addEvent('domready',  function() {


    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;
        }
    }

    setTimeout(scrollTo, 0, 0, 1);
    
    body.addEvents({
        'click': function(event) {
            event.stop();
	
            var link = event.target;
			 while (link && link.localName.toLowerCase() != "a")
                link = link.parentNode;

            if (link && link.hash)
            {	 
				
				checkOrientAndLocation();
				
				var orient = document.body.getAttribute("orient");
                lastTarget=link.hash.substr(1)
				var page = ($(link.hash.substr(1)+"_"+orient)) ? $(link.hash.substr(1)+"_"+orient) : $(link.hash.substr(1));

				 showPage(page);
            }
        }
    });
    
checkOrientAndLocationResized = function(){
	resized=true;
	checkOrientAndLocation();
}

checkOrientAndLocation = function()
{
	
	
	/*window.orientation returns a value that indicates whether iPhone is in portrait mode, landscape mode with the screen turned to the
	  left, or landscape mode with the screen turned to the right. */
	var orientation=window.orientation;
	var orient='';
	if (orientation != null)
	{
	switch(orientation)
	{
	
		case 0:
				/* If in portrait mode, sets the body's class attribute to portrait. Consequently, all style definitions matching the body[class="portrait"] declaration
				   in the iPhoneOrientation.css file will be selected and used to style "Handling iPhone or iPod touch Orientation Events". */
				
				orient='profile';
				break;  
				
		case 90:
				/* If in landscape mode with the screen turned to the left, sets the body's class attribute to landscapeLeft. In this case, all style definitions matching the
				   body[class="landscapeLeft"] declaration in the iPhoneOrientation.css file will be selected and used to style "Handling iPhone or iPod touch Orientation Events". */
				
				orient='landscape';
				break;  
		case -90:
				/* If in landscape mode with the screen turned to the left, sets the body's class attribute to landscapeLeft. In this case, all style definitions matching the
				   body[class="landscapeLeft"] declaration in the iPhoneOrientation.css file will be selected and used to style "Handling iPhone or iPod touch Orientation Events". */
				
				orient='landscape';
				break;  	
		
	}
	}else{
					currentWidth = window.outerWidth;
					
					orient = currentWidth <= 350 ? "profile" : "landscape";
				
				 
	}
	
	if (!resized)
			document.body.setAttribute("orient", orient);
	else{
		
		resized=false;

        var pageId =lastTarget;
		
        var page = ($(pageId+"_"+orient)) ? $(pageId+"_"+orient) : $(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.className.indexOf("dialog") != -1)
        showDialog(page);
    else
    {        
        location.href = currentHash = hashPrefix + page.id;
        pageHistory.push(page.id);

        var fromPage = currentPage;
        currentPage = page;

        var pageTitle = $("pageTitle");
        pageTitle.innerHTML = page.title || "";

        var homeButton = $("homeButton");
        if (homeButton)
            homeButton.style.display = ("#"+page.id) == homeButton.hash ? "none" : "inline";

        if (fromPage){
            setTimeout(swipePage, 0, fromPage, page, backwards);
			
		}
    }
}

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

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

function showDialog(form)
{
    currentDialog = form;
    form.setAttribute("selected", "true");
    
    form.onsubmit = function(event)
    {
        event.preventDefault();
        form.removeAttribute("selected");

        var index = form.action.lastIndexOf("#");
        if (index != -1)
            showPage($(form.action.substr(index+1)));
    }

    form.onclick = function(event)
    {
        if (event.target == form)
            form.removeAttribute("selected");
    }
}

});

