var siteWidth = "100%";
var boxShowed = false;
var minHeight = '316px';
			
$(document).ready(function(){

	// to work with buttons
	$('input.go_forward').click( function(e){
		e.preventDefault();
		window.location = $(this).attr('rel');	
	});
	
	$("a[id$='Lnk']").click( function(e){
		id = $(this).attr('id').slice( 0, $(this).attr('id').lastIndexOf('Lnk') )
		if ( !$('#'+id+'Box') ) 
			return;
		e.preventDefault();
		if ( boxShowed==id) {
			//we already showing that page, lets hide it
			$('#'+boxShowed+'Box').animate({ 
			        left: $('#'+boxShowed+'Box').hasClass('boxesLeft') ? "-"+siteWidth : siteWidth
			      }, 500 );
			boxShowed = false;
			return;
		} else if ( boxShowed ) {
	    		$('#'+boxShowed+'Box').animate({ 
			        left: $('#'+boxShowed+'Box').hasClass('boxesLeft') ? "-"+siteWidth : siteWidth
			      }, 500 );
        		$('#'+id+'Box').animate({ 
			        left:"0px"
			      }, 500 );
				
	    } else {
	    	$('#iphone_body').css('min-height', $('#'+id+'Content').height()+'px' );
			$('#'+id+'Box').animate({ 
		        left:"0px"
		      }, 500 );
			
	    }
	    
	    //save current box showed
	    boxShowed = id;
	});
	
	 
	$("*[class^='boxes'] a.back").click( function(e){
		e.preventDefault();
		$('#iphone_body').css('min-height', minHeight );
		$(this).parent().parent().animate({ 
	        left: $('#'+boxShowed+'Box').hasClass('boxesLeft') ? "-"+siteWidth : siteWidth
	      }, 500 );
	    boxShowed = false;
	});

	/*
    $(".top_menu a").hover(
        function(){
            $(this).next().show();
        },
        function(){
            $(this).next().hide();
        }
    );
	
	var myLinks = document.getElementsByTagName('a');
	for(var i = 0; i < myLinks.length; i++){
	   myLinks[i].addEventListener(’touchstart’, function(){this.className = “hover”;}, false);
	   myLinks[i].addEventListener(’touchend’, function(){this.className = “”;}, false);
	}
	*/
    
    var menu = new imageMenu();
    menu.build('#menu','.menuOption',7,'.menuOptionHover');
    jQuery('.btn_mid').click(function() { this.style.cursor = "wait"; });

    $(window).resize(function(){
        centerImageMenu();
    });

    function centerImageMenu()
    {
        currentWidth = window.innerWidth;
        if ( currentWidth>320 ) currentWidth -= 20; else return;
        $('#menu').css('marginLeft', (currentWidth - 320)/2-10 );
    }
    centerImageMenu();
});



addEventListener("load", function()
{
	setTimeout(updateLayout, 0);
}, false);

var currentWidth = 0;

function updateLayout()
{
	if (window.innerWidth != currentWidth)
	{
		currentWidth = window.innerWidth;

		var orient = currentWidth <= 320 ? "profile" : "landscape";
		document.body.setAttribute("orient", orient);
		setTimeout(function()
		{
			window.scrollTo(0, 1);
		}, 100);            
	}
}

setInterval(updateLayout, 400);

