$(document).ready(function() {
    var minWidth = 1150;
    var maxWidth = 800;
    var offset = 0;
    var current = 0;
    var target = 0;
    var lastPage = $('#navigation > ul > li').size()-1;
    var dir;
    var viewWidth = ((($(window).width()-maxWidth)/2)+maxWidth);
    var $dialog, $overlay;
	$('.content').css('width', viewWidth+'px');
	
    $('#navigation > ul > li > a, .arrow.active a, #logo a').live('click',function(){
        
        if($(this).closest('div').parent().attr('id') == 'logo')
            current = 0;
        else if($(this).parent().hasClass('arrow'))
        {
            dir = $(this).parent().hasClass('left') ? -1 : 1;
            
            if(current + dir >= 0 && current + dir <= lastPage)
                current = current + dir;
        } else {
            current = $('#navigation ul li a').index($(this));
            
        }

        $('.content-container').animate({left: -1*viewWidth*current})
        
        $('#navigation li.active').removeClass('active');
        
        $('#navigation li:eq('+current+')').addClass('active');
        
        if(current == 0)
            $('.arrow.left').removeClass('active');
        else
            $('.arrow.left').addClass('active');
            
        if(current >= lastPage)
            $('.arrow.right').removeClass('active');
        else
            $('.arrow.right').addClass('active');
	    
	    return false;
    });
    
    $('.arrow a').click(function(){
    })

    var contentHeight = 0;
    $('.content-container .content').each(function(){
        if($(this).height() > contentHeight)
            contentHeight = $(this).height();
            
    });
    
    $('#content-wrapper').height(contentHeight);
    

    $(window).resize(function(){
        var width = $(window).width();

        if(width < minWidth)
        {
            offset = (((minWidth-width)/2)*-1);
            $('#coffee-mug, #coffee-shadow').css('right', offset+'px' );
        } else if(width > minWidth)
        {
            offset = 0;
            $('#coffee-mug, #coffee-shadow').css('right', offset+'px' );
        }
        
        
        viewWidth = ((($(window).width()-maxWidth)/2)+maxWidth);
        if(viewWidth > maxWidth)
        {
            $('.content').css('width', viewWidth+'px');
            $('.content-container').css({left: -1*viewWidth*current});
        } else {
            viewWidth = maxWidth;
        }
    });
    
    var count = $('.navi li').size();

	$('.navi li:not(.active) a').each(function(i){
		var content = '';
		$('<div class="content" style="display: none;"><div class="content-content"></div></div>').appendTo('.content-container');
			
		$.post($(this).attr('href')+'?disableLayout=1', {}, function(r){
			var $page = $('.content:eq('+(i+1)+')').find('.content-content').html(r);
			
			$page.find('a.dialog').click(function(){
				var url = $(this).attr('href');
				
				$.post(url+'?disableLayout=true', {}, function(response){
					
					$overlay = createOverlay();
					$dialog = createDialog();
					
					$dialog.find('#dialog > #dialog-content').html(response).end().find('#closeDialog').click(function(){ $overlay.remove(); $dialog.remove(); });
					
					$overlay.click(function(){ $overlay.remove(); $dialog.remove(); });
					    
					$dialog.appendTo('body');
					$overlay.appendTo('body');
					
										
					$('#dialog').find('form').live('submit',function(){
					
						$.post(url+"?disableLayout=true", $(this).serialize(), function(r){
							$dialog.find('#dialog-content').html(r);
						});			
						
						return false;
					});

				});	
				return false;
			});
			
			$('.content:eq('+(i+1)+')').css({'display': 'block', 'width': viewWidth+'px'});
			
			count--;
			
			if(count == 1)
			{
				$('.component.gallery a').lightBox({
					overlayBgColor: '#000',
					overlayOpacity: 0.2,
					imageLoading: staticUrl+'img/lightbox_images/lightbox-ico-loading.gif',
					imageBtnClose: staticUrl+'img/lightbox_images/lightbox-btn-close.gif',
					imageBtnPrev: staticUrl+'img/lightbox_images/lightbox-btn-prev.gif',
					imageBtnNext: staticUrl+'img/lightbox_images/lightbox-btn-next.gif',
					fixedNavigation: true
				});
			}
		});
	});
	
	$('.content').find('a.dialog').click(function(){
		var url = $(this).attr('href');
		
		$.post(url+'?disableLayout=true', {}, function(response){
			
			$overlay = createOverlay();
			$dialog = createDialog();
			
			$dialog.find('#dialog > #dialog-content').html(response).end().find('#closeDialog').click(function(){ $overlay.remove(); $dialog.remove(); });
			
			$overlay.click(function(){ $overlay.remove(); $dialog.remove(); });
			    
			$dialog.appendTo('body');
			$overlay.appendTo('body');
			
								
			$('#dialog').find('form').live('submit',function(){
			
				$.post(url+"?disableLayout=true", $(this).serialize(), function(r){
					$dialog.find('#dialog-content').html(r);
				});			
				
				return false;
			});

		});	
		return false;
	});
	
	function createOverlay()
	{
		$overlay = $('<div id="overlay"></div>');
		return $overlay;
	}
	
	function createDialog()
	{
		$dialog = $('<div id="dialog-wrapper"><div id="dialog"><a id="closeDialog" href="#"></a><div id="dialog-content"></div></div></div>');
		return $dialog;
	}
	
});


$(document).keydown(function(e){
    if (e.keyCode == 37 || e.keyCode == 39) { 
       return false;
    }
});


