var changePage = {

	newfooter: '',
	newContent: '',
	bgContent: '',

	me: this,

	change: function(page, image, actualPage, idWork){
			
		var screen_img = image,
		me             = this,
		idWork         = parseInt(idWork) || 0;
		

		activity = null;
		me.setFooter( '' );
		me.setContent( '', '' ); 			

		//Switch dei vari tipi di pagina ed eventualmente istanziazione dell'oggeto necessario;						
		switch(actualPage){
			case 'ajaxWork':					
				activity = new Work(idWork);					
			break;

			case 'about':					
				$('#page-wrap').css('background-size', '100%');
				activity = new About(idWork);					
			break;

			case 'clienti':
				$('#page-wrap').css('background-size', '100%');
				activity = new Clienti();					
			break;				

			case 'contact':					
				$('#page-wrap').css('background-size', '100% auto');
				activity = new Contact();
			break;

			case 'tracking':					
				$('#page-wrap').css('background-size', '100% auto');
				activity = new Tracking();
			break;

			case 'test':										
				activity = new Home();
				$('#footer').css({height: '25%'});
			break;
		}

		$('#content').css({background:'transparent'});
		//..While loading the page via ajax

		$.get(base_url+'index.php/'+page+'/yes', function(data){									
			 

			 activity.generateArrayFromJson(data);			
			 try
				{
					activity.generateArrayFromJson(data);			

					 me.setFooter( activity.getFooterHtml() );
					 	
					 me.setContent( activity.getContentHtml(), data.contentBg ); 
				}
				catch(e)
				{
					activity.generateArrayFromJson(data);			

					 me.setFooter( activity.getFooterHtml() );
					 	
					 me.setContent( activity.getContentHtml(), data.contentBg ); 				 
				}	
			
		}, 'json');		
	
		//Starting Animation..			
		this.animate.slideBackground(screen_img, '', function(){
			setTimeout("activity.getJavascript()", 600);			
			try
			{
				activity.getJavascript();	
			} catch(e) {
				setTimeout("activity.getJavascript()", 500);
			}
			 
		});					
			
			

	},

	setFooter: function(data){		
		this.newfooter = data;
	},

	setContent: function(data, bg){		
		this.newContent = data;
		this.bgContent = bg;
	},
	
	animate: {

		slideBackground: function(screen_img, footer, callback, nofooter) {

			var nofooter = nofooter || false;
			$('html > *, #page-wrap, body').css({overflowX: 'hidden', overflowY: 'hidden'});

			if(actualPage == 'contact' || actualPage == 'clienti' || actualPage == 'tracking' || nofooter == true){
				nofooter = true;
			}			

			isAnimating = true; 

			var _W = myscreen.getW(),
					 effect = 'easeOutQuad',
					 speed = 1000,
					 t = $('#page-wrap').width();
			
			if(!nofooter){
				
				$('#footer').css({height: '25%', background: 'url('+base_url+'img/fondo_down.png)'}).show();

				//alert(defFooter[actualPage]);

				$('#anim-footer').css({height: '25%'}).hide().html(defFooter[actualPage]).css({
					    left: -_W+'px'
				});
				
				$('#anim-page-wrap').hide().css({
					    right: -_W+'px'
				});							 

				$('html > *').css({overflowX: 'hidden', overflowY: 'hidden'});

				//Setting desired image and the same with
				$('#anim-page-wrap').css({height: $('#page-wrap').height(), width:myscreen.getW()});

				$('#anim-page-wrap').css({backgroundImage: 'url('+base_url+'img/'+screen_img+')', backgroundRepeat:'no-repeat', backgroundSize: '100%'});
				$('.imageID').attr('src', '');
				$('.imageID2').attr('src', base_url+'img/'+screen_img);

				//Left param for anim-page div
				var left = 0;

				//Actual content slide
				$('#page-wrap').css({'position': 'relative', backgroundSize: '100%'}).animate({'left': -_W+'px' }, speed, effect, function(){
					$('#page-wrap').css({opacity: '0'});
					$('#content').html('');
					$('#content').html(changePage.newContent);
					$('#page-wrap').css({backgroundImage: 'url('+base_url+'img/'+screen_img+')'});
					$('.imageID').attr('src', '');
					$('.imageID').attr('src', base_url+'img/'+screen_img);
				});

				//Top param for anim-page div		

				var top = $('#header').outerHeight();
				//Safari prende la dimensione dell'header sbagliata


				//New content slide			
				var pw_ma = $('#page-wrap').css('margin-top'),
				pw_mr = myscreen.getH() - $('#header').outerHeight() - $('#footer').outerHeight();				

				$('#anim-page-wrap').css({top: top+'px', display:'block', marginTop: '0px',  height: ( pw_mr ) }).animate({'right': left+'px' }, speed, effect);		

				//# Footer Animation 
				//##################
				
					
				$('#footer').css('position', 'fixed').animate({'left': _W+'px'}, speed, effect, function(){
					$(this).hide().html('').html(changePage.newfooter);
				});


				$('#anim-footer').css({zIndex:99, bottom: 0+'px', display:'block', height: $('#footer').height() }).animate({'left': 0+'px' }, speed, effect, function(){											
					
					$('#anim-footer').hide().css({
					    left: -_W+'px'
					});

					$('#footer').css({'left': 0+'px', 'position': 'fixed' }).show();
					
					if (callback  && typeof(callback) === "function") {
					    callback();
					}

					$('html > *').css({overflowX: 'hidden', overflowY: 'hidden'});
					changePage.resetAnimation();					

				});			

			}else {															

				$('#anim-page-wrap').hide().css({
					    right: -_W+'px'
				});								
				//Setting desired image and the same with
				
				$('html > *').css({overflowX: 'hidden', overflowY: 'hidden'});

				$('html, body, #wrapper').css({overflow: 'visible'});

				//Left param for anim-page div
				var left = 0;				

				//Actual content slide
				$('#footer').animate({height:'0%'}, 400, function(){
					$(this).html('');
					$('#anim-page-wrap').css({height:  resizeLayout.testSize()+'px' , width:myscreen.getW()});
					$(this).hide();
				});

				$('#anim-page-wrap').css({padding: '1% 0%', backgroundImage: 'url('+base_url+'img/'+screen_img+')', backgroundSize: '100% auto', backgroundRepeat:'no-repeat', width:myscreen.getW(), height:  resizeLayout.testSize()+'px'});										
				$('.imageID').attr('src', '');
				$('.imageID2').attr('src', base_url+'img/'+screen_img);
				$('.badge').animate({opacity:'0'}, 400);
				
				//Controllo il caricamento dello sfondo
				$('body').append('<img style="display:none;" src="'+base_url+'img/'+screen_img+'" id="'+screen_img+'"/>');
				
				
				//Top param for anim-page div		
				var top = $('#header').outerHeight();// parseInt( $('#page').css('margin-top') ) + $('#header').outerHeight();		//++
				$('#anim-page-wrap').css({top: top+'px', display:'block', marginTop: '0px' }).animate({'right': left+'px' }, (speed), effect);

				$('#page-wrap').css('position', 'relative').animate({'left': -_W+'px' }, speed, effect, function(){
					$('#page-wrap').css({opacity: '0'});
					$('#content').html('');
					$('#content').html('').html(changePage.newContent);
					$('.imageID').attr('src', '');
					$('.imageID').attr('src', base_url+'img/'+screen_img);
					$('#page-wrap').css({backgroundImage: 'url('+base_url+'img/'+screen_img+')', backgroundRepeat:'no-repeat'});
				});
			
				//New content slide			
				var pw_ma = $('#page-wrap').css('margin-top');

				//$('#anim-page-wrap').css({top: top+'px', display:'block', marginTop: '0px' }).animate({'right': left+'px' }, (speed-300), effect);
				
				var maxVerticalSpace = Math.floor( ( myscreen.getH() - ( $('#header').outerHeight() )  - ( $('#footer').outerHeight() )   ) );

				$('#page-wrap').animate({height: maxVerticalSpace - ( parseInt( $('#page-wrap').css('padding-top') ) * 2 ) +'px'}, 1, function(){					
					
					if (callback && typeof(callback) === "function") {
					    callback();
					}

					$('html > *').css({overflowX: 'hidden', overflowY: 'hidden'});
					
					changePage.resetAnimation();					
				});
				$('html').css({overflow: 'hidden'});
			}


			var intervalID = window.setTimeout('resizeLayout.general()', 1000);
			var intervalID2 = window.setTimeout('resizeLayout.footer()', 1000);

		}

	},


	slideInternal: function(screen_img, footer, callback,  dir) {

			$('html > *, #page-wrap, body').css({overflowX: 'hidden', overflowY: 'hidden'});

			isAnimating = true; 

			var _W = myscreen.getW(),
					 effect = 'easeOutQuad',
					 speed = 1000,
					 t = $('#page-wrap').width(),
					 initPos;
		
				if(dir == 1){
					initPos = -_W;
				} else {
					initPos = _W;

				}

				$('#anim-page-wrap').hide().css({
					    right: initPos+'px'
				});							 

				$('html > *').css({overflowX: 'hidden', overflowY: 'hidden'});

				//Setting desired image and the same with
				$('#anim-page-wrap').css({height: $('#page-wrap').height(), width:myscreen.getW()});

				$('#anim-page-wrap').css({backgroundImage: 'url('+base_url+'img/'+screen_img+')', backgroundRepeat:'no-repeat', backgroundSize: '100%'});
				$('.imageID').attr('src', '');
				$('.imageID2').attr('src', base_url+'img/'+screen_img);

				//Left param for anim-page div
				var left = 0;

				//Actual content slide
				$('#page-wrap').css({'position': 'relative', backgroundSize: '100%'}).animate({'left': initPos+'px' }, speed, effect, function(){
					$('#page-wrap').css({opacity: '0'});
					$('#content').html('');
					$('#content').html(changePage.newContent);
					$('#page-wrap').css({backgroundImage: 'url('+base_url+'img/'+screen_img+')'});
					$('.imageID').attr('src', '');
					$('.imageID').attr('src', base_url+'img/'+screen_img);
					changePage.resetInternalAnimation();	


					if (callback && typeof(callback) === "function") {
					    callback();
					}
				});

				//Top param for anim-page div		

				var top = $('#header').outerHeight();
				//Safari prende la dimensione dell'header sbagliata


				//New content slide			
				var pw_ma = $('#page-wrap').css('margin-top'),
				pw_mr = myscreen.getH() - $('#header').outerHeight() - $('#footer').outerHeight();				

				
				$('#anim-page-wrap').css({top: top+'px', display:'block', marginTop: '0px',  height: ( pw_mr ) }).animate({'right': left+'px' }, speed, effect);					


			var intervalID = window.setTimeout('resizeLayout.general()', 1000);
			var intervalID2 = window.setTimeout('resizeLayout.footer()', 1000);


	},

	footerWorkSlide: function(a, dir){
		
		isAnimating = true;
		var speed = 900,
			initPos;
		var _W = myscreen.getW();

		if(dir == 1){
			initPos = -_W;
		} else {
			initPos = _W;
		}		

		var pag = parseInt( a.attr('alt') );

		activity.setFooterPagination(pag);


		$('#anim-footer').hide().css({
			    left: -initPos+'px'
		});

		var previusPosition = $('#footer').css('position'); 
		var previusBottomFootWrap = $('.foot-wrap').css('bottom');
				
		$('#footer').animate({'left': initPos+'px' }, speed, 'linear', function(){
			$(this).html( activity.getFooterHtml() ).css({'left': 0+'px' });;
		});

		$('#anim-footer').html( activity.getFooterHtml() ).css({position:  'fixed',  height: '25%', display:'block'}).animate({'left': 0+'px' }, speed, 'linear', function(){																
			$(this).hide();
			window.setTimeout('isAnimating = false;' , 500);
		});		
		
		
	},

	footerWorkFade: function(a){
		isAnimating = true;
		var speed = 900;

		var pag = parseInt( a.attr('alt') );

		activity.setFooterPagination(pag);

		var _W = myscreen.getW();		

		var previusPosition = $('#footer').css('position'); 
		var previusBottomFootWrap = $('.foot-wrap').css('bottom');
				
		$('#footer').css('position', 'relative').fadeOut(function(){
			$(this).html( activity.getFooterHtml() ).css('position', previusPosition).css({'left': 0+'px', 'bottom': 0+'px' }).fadeIn();
		});

		window.setTimeout('isAnimating = false;' , 500);
	},
	
	resetAnimation: function(){
		
		isAnimating = true;
		
		//$('#page-wrap > *').css('visibility', 'hidden');
		$('#page-wrap').css({opacity: '0'});

		$('#page-wrap').css('left', '0px');

		isAnimating = false;  

		resizeLayout.general();

		resizeLayout.footer();

		$('#page-wrap').css({opacity: '0'});		

		
		//NO IE $('#page-wrap').animate({opacity: '1', zIndex:'999'}, 1, function(){
		
		//IE
		$('#page-wrap').animate({opacity: '1'}, 100, function(){

			$('#anim-page-wrap').animate({
			    opacity: '0'
			}, 600, function(){
				$('#anim-page-wrap').css({
				    position: 'absolute',
				    right: '-2000px',
				    top: '0',
				    display:'none',
				    opacity: '1',
				    background: '#fff'
				});
			});
			
		});	
				
		$('body > *').css({overflowX: 'block', overflowY: 'block'});

		//isAnimating = true;

		this.checkContent();

		window.setTimeout('isAnimating = false;', 700);
		var intervalID = window.setTimeout('resizeLayout.general()', 1000);
		var intervalID = window.setTimeout('resizeLayout.footer()', 1000);
	},

	resetInternalAnimation: function(){
		
		isAnimating = true;
		
		//$('#page-wrap > *').css('visibility', 'hidden');
		$('#page-wrap').css({opacity: '0'});

		$('#page-wrap').css('left', '0px');

		isAnimating = false;  

		resizeLayout.general();

		resizeLayout.footer();

		$('#page-wrap').css({opacity: '0'});		

		
		//NO IE $('#page-wrap').animate({opacity: '1', zIndex:'999'}, 1, function(){
		
		//IE
		$('#page-wrap').animate({opacity: '1'}, 100, function(){

			$('#anim-page-wrap').animate({
			    opacity: '0'
			}, 600, function(){
				$('#anim-page-wrap').css({
				    position: 'absolute',
				    right: '-2000px',
				    top: '0',
				    display:'none',
				    opacity: '1',
				    background: '#fff'
				});
			});
			
		});	
				
		$('body > *').css({overflowX: 'block', overflowY: 'block'});

		//isAnimating = true;

		//this.checkContent();

		//window.setTimeout('isAnimating = false;', 700);
		var intervalID = window.setTimeout('resizeLayout.general()', 1000);
		var intervalID = window.setTimeout('resizeLayout.footer()', 1000);
	},	
	
	checkContent: function(){
		$('#content, #footer').html('');
		if(changePage.newContent == ''){
			$('#content').html('Caricamento...');
			setTimeout('changePage.checkContent()', 200);
		} else {
			$('#content').html(changePage.newContent);	
			resizeLayout.general();

			resizeLayout.footer();			
		}

		var nofooter = false;

		if(actualPage == 'contact' || actualPage == 'clienti' || actualPage == 'tracking' ){
			nofooter = true;
		}		
		
		if(changePage.newfooter == '' && !nofooter){
			$('#footer').html('Caricamento...');
			setTimeout('changePage.checkContent()', 200);
		} else {
			$('#footer').html(changePage.newfooter);
			resizeLayout.general();
			resizeLayout.footer();			
		}
	}	

}

