var fxFooter;
var slideshow = false;
var multiplier;
function setPage(){
    if(document.documentElement.clientHeight - 50 < 585){ // 549 = 900 * 0.61 (wat de verhouding is tussen hoogte en breedte)
        var contentHeight = document.documentElement.clientHeight - 50; // height - menu height - combined height above and under page
    } else {
        var contentHeight = 585;
    }
    var contentWidth = (contentHeight * 1.458).floor(); 
	if(contentWidth > document.documentElement.clientWidth-300){
		contentWidth = document.documentElement.clientWidth-300;
		contentHeight = (contentWidth/1.458).floor(); 
	}
    var marginLeft = contentWidth / 2 * -1;
    var marginTop = (contentHeight / 2 + 5) * -1;
    var pageWidth = contentWidth - 72;
    var pageHeight = contentHeight;
	var footerHeight = $('footer').getStyle('height').toInt();
    $('content_inner').setStyles({
        'width':contentWidth,
        'height':contentHeight,
        'margin-left':marginLeft,
        'margin-top':marginTop
    });
    $('page').setStyles({
        'width':pageWidth,
        'height':pageHeight
    });
	if($('blog_container')){
		$('blog_container').setStyles({
			'width':pageWidth+30,
			'height':document.documentElement.clientHeight-30
		});
		$('blog_content').setStyles({
			'width':pageWidth
		});
	}
    $('menu').setStyles({
        'width':(pageWidth + 20)
    });
	
	fxFooter = new Fx.Morph($('footer'), {duration: 500, transition: Fx.Transitions.Sine.easeInOut});
	$('footer').addEvent('mouseenter', function() {
		slideMenu(0, footerHeight);
	});
	$('footer').addEvent('mouseleave', function() {
		slideMenu(1, 27);
	});
    setTimeout(function(){
		slideMenu(1, 27);
    },1000);
}

var portfolioTexts = new Array();
var timeout = null; 
window.addEvent('resize', function() {
    if (Browser.Engine.trident) {
        $clear(timeout);
        timeout = resizePage.delay(300);
    } else {
        resizePage(); 
    } 
});

var slideObj = false;

function resizePage(){
	
	//if(slideshow){
	//	slideshow.pause(1);
	//}
	
    if(document.documentElement.clientHeight - 50 < 585){
        var contentHeight = document.documentElement.clientHeight - 50; // height - menu height - combined height above and under page
    } else {
        var contentHeight = 585;
    }

	var contentWidth = (contentHeight * 1.458).floor(); 
	if(contentWidth > document.documentElement.clientWidth-300){
		contentWidth = document.documentElement.clientWidth-300;
		contentHeight = (contentWidth/1.458).floor(); 
	}

    var marginLeft = contentWidth / 2 * -1;
    var marginTop = (contentHeight / 2 + 5) * -1;
    var pageWidth = contentWidth - 72;
    var pageHeight = contentHeight;
     
     
    $('page').set('morph',{duration: 200});
    $('page').morph({'opacity':0});
    setTimeout(function(){
        // page size
        $('page').setStyles({
            'width':pageWidth,
            'height':pageHeight
        });
        $('menu').morph({'width':(pageWidth + 20)});
    
        //call content 'switch to good size' function
        resizeContent();
        
    
        //container size
        var myEffect = new Fx.Morph($('content_inner'), {duration: 500, transition: Fx.Transitions.Sine.easeOut});
        myEffect.start({
            'width':contentWidth,
            'height':contentHeight,
            'margin-left':marginLeft,
            'margin-top':marginTop
        });   
    },200);
    
    setTimeout(function(){
        $('page').morph({'opacity':1});
    },700);

    //setTimeout(function(){
	//	if(slideshow){
	//		slideshow.pause(0);
	//	}
    //},1000);

    //on resize, werkt in ie7/ff. In ie8 zou hij moeten werken in RC.
}

function resizeContent(){
    var pageWidth = $('page').getStyle('width').toInt();
    var pageHeight = $('page').getStyle('height').toInt();
    
    
    // overview images get scaled
    if ($('overview')){
        var images =  $('overview').getElements('img');
        images.setStyles({
            'width':(pageWidth / 5).floor()
        });
		$('overview').getElements('a').each(function(obj, index){
			obj.addEvent('mouseenter', function(){
				$('portinfo').set('html', this.getElement('img').getProperty('alt'));
			});
			obj.addEvent('mouseleave', function(){
				$('portinfo').set('html', '');
			});
		});
		$('overview').addEvent('mouseleave', function(){
			$('portinfo').set('html', '');
		});
    }
	var objects = $('page_container').getElements('object').each(function(obj, index){
		Element.setStyle(obj, 'width', pageWidth);
		Element.setStyle(obj, 'height', ((Element.getProperty(obj, 'iheight').toInt()/Element.getProperty(obj, 'iwidth').toInt())*pageWidth).floor());
		//Element.setStyle(obj, 'height', ((obj.getProperty('iheight').toInt()/obj.getProperty('iwidth').toInt())*pageWidth).floor());
		//obj.setStyles({
		//   'width':pageWidth,
		//   'height':((obj.getProperty('iheight').toInt()/obj.getProperty('iwidth').toInt())*pageWidth).floor()
		//});
	});

	resizeElements(pageWidth);

    // portfolio images up close get scaled
    if ($('portfolio_slideshow')){

        $('portfolio_info').setStyles({
            'width':pageWidth,
            'height':(pageWidth/1.5).floor()
        });
        // images 
		
        // icons
		
        var arrowTop = (pageHeight - 70) / 2;
        if($('arrow_right')){
			$('arrow_right').setStyle('top',arrowTop+'px');
		}
        if($('arrow_left')){
			$('arrow_left').setStyle('top',arrowTop+'px');
		}
        
        
        // info
        var infoPadding = (pageHeight / 10).floor();
        var infoWidth = (pageWidth / 2 - infoPadding * 2).floor();
        var infoHeight = (pageHeight / 2 - infoPadding * 2).floor(); 
        var infoTop = pageHeight / 2;
        $('portfolio_overlay').setStyles({
           'width':pageWidth,
           'height':(pageWidth/1.5).floor()
        
        });
		
        if ($('slideshow-images')){
			$('slideshow-images').setStyles({
				'width':pageWidth,
				'height':(pageWidth/1.5).floor()
			});
			var images = $('portfolio_slideshow').getElements('img');
			images.setStyles({
			   'width':pageWidth,
			   'height':(pageWidth/1.5).floor()
			});
			images.setProperties({
			   width:pageWidth+'px',
			   height:(pageWidth/1.5).floor()+'px'
			});

			slideshow.width = pageWidth;
			slideshow.height = (pageWidth/1.5).floor();
			slidePortfolio('resize');

		}

    };
	if($('blog_container')){
		$('blog_container').setStyles({
			'width':pageWidth+30,
			'height':document.documentElement.clientHeight-30
		});
		$('blog_content').setStyles({
			'width':pageWidth
		});
	}
	if(myScroll) myScroll.refresh();
	
}


function resizeElements(pageWidth){
	var ar = 1;
	var add = 0;
	$('page_container').getElements('.resizeme').each(function(obj, index){
		add = 0;
		if(obj.match('.ar1')){
			ar = 1;
		}else if(obj.match('.ar2')){
			ar = 1/2;
		}else if(obj.match('.ar3')){
			ar = 1/3;
		}else if(obj.match('.ar4')){
			ar = 1/4;
		}else if(obj.match('.ar5')){
			ar = 1/5;
		}else if(obj.match('.ar6')){
			ar = 1/6;
		}else if(obj.match('.ar7')){
			ar = 1/7;
		}
		var parent = obj.getParent('td');
		if(parent && (parent.hasClass('left') || parent.hasClass('middle') || parent.hasClass('right'))){
			add = 5;
		}
		obj.setStyle('width', (pageWidth*ar).floor()-add)
		if(obj.getProperty('aspectratio')){
			obj.setStyle('height', ((pageWidth*ar-add)/obj.getProperty('aspectratio')).floor());
		}
	});	
}


function slideMenu(dir, size){
	if(dir == 1){
		fxFooter.cancel();
		fxFooter.start({
			'height':size
		});
	}else{
		fxFooter.cancel();
		fxFooter.start({
			'height':size
		});
	}
}
var dots = [];
var currentdot;
Array.implement({
  reverseinject: function(){
    for (var i = 0, l = this.length; i < l; i++)
	   this[i].inject(this[i].getParent(),'top');
    return this;
  }
}); 

function slidePortfolio(resize, data){
	
	if(!data){
		data = {};
	}
	var pwidth = $('page').getStyle('width').toInt();
	var pheight = (pwidth/1.5).floor();
	currentdot = 0;

	setTimeout(function(){
		if(!resize){
			bindIcons();
			slideshow = new Slideshow('portfolio_slideshow', data, { center:false,paused:true,delay:5000,duration:350,controller:false,resize:'height',overlap:false,width: pwidth, height: pheight, hu: '../image_database/' });
			var dots = $$('[class=dot]').reverseinject();
			for (var i = 0; i < dots.length; i++){
			//	dots[i].inject(dots[i].getParent(),'top'); 
			}
			dots[currentdot].addClass('green');
			dots.each(function(el){
				if(!el.hasClass('green')){
				    el.addClass('grey');
				}
			});
			$('arrow_left').addEvent('click', function(){
				slideshow.prev();
				dots[currentdot].removeClass('green').addClass('grey');
				(currentdot>0 ? currentdot-- : currentdot = totaldots);
				dots[currentdot].removeClass('grey').addClass('green');
				if($('portfolio_overlay').hasClass('visible')){
					$('portfolio_overlay').removeClass('visible');
					$('portfolio_overlay').morph({opacity:0});
					$('portfolio_info').morph({opacity:0});
				}
			});
			$('arrow_right').addEvent('click', function(){
				slideshow.next();
				dots[currentdot].removeClass('green').addClass('grey');
				(currentdot<totaldots ? currentdot++ : currentdot = 0);
				dots[currentdot].removeClass('grey').addClass('green');
				if($('portfolio_overlay').hasClass('visible')){
					$('portfolio_overlay').removeClass('visible');
					$('portfolio_overlay').morph({opacity:0});
					$('portfolio_info').morph({opacity:0});
				}
			});
		}
    },1000);	
}

function bindIcons(){
    $('portfolio_info_button').addEvent('click',function(){
		if($('portfolio_overlay').hasClass('visible')){
            $('portfolio_overlay').removeClass('visible');
            $('portfolio_overlay').morph({opacity:0});
            $('portfolio_info').morph({opacity:0});
        } else {
            $('portfolio_overlay').morph({opacity:[0,0.8]});
            $('portfolio_info').morph({opacity:[0,1]});
            $('portfolio_overlay').addClass('visible');
        }
    });
	$('page_container').addEvents({
		'mouseout':function(){
			if($('arrow_left') && $('arrow_right')){
				$('arrow_left').morph({opacity: 0});
				$('arrow_right').morph({opacity: 0});
			}
			
		},
		'mouseover': function(){
			if($('arrow_left') && $('arrow_right')){
				$('arrow_left').morph({opacity: 1});
				$('arrow_right').morph({opacity: 1});
			}
		}
	});
}

function emptyPortfolio(){
    var portfolioTexts = new Array();
}

function addPortfolioInfo(text){
    portfolioTexts[portfolioTexts.length] = text;
}