// SCRIPT IS AANGEPAST TBV INGESNOERDE (VERSIMPELDE) BEWEGING
// voor originele bewegingen: zet de regels gemarkeerd met *** AAN
// voor originele bewegingen: zet de regels gemarkeerd met ### UIT


// ** Global variables ** //
// Final left position of gliding element
var stopPoint = -10000;
// Interval ID
var intervalID;
// 'Corrector' positioning factor for IE/Mac et al., but doesn't hurt others
var fudgeFactor = {top:-1, left:-1};
var vector_x = -1;
var vector_y =  1;
// bewegingsvectoren	
var x				= 0;
var y				= 0;
// beginpositie
var xmin		= 0;
var xmax		= 0;
var ymin		= 0;
var ymax		= 0;
// randen
var owidth		= 0;
var oheight		= 0;
var wwidth		= 960;
var wheight	= 644;

function stopGlide(layerName){
	var obj = getRawObject(layerName);
	hide(obj);
}

// Set initial position offscreen and show object and
// start timer by calling glideToCenter()
function startGlide(layerName) {
	// 'obj' is the positionable object
    var obj = getRawObject(layerName);
    // set fudgeFactor values only first time

	owidth		= getObjectWidth(obj);
	oheight		= getObjectHeight(obj);
	xmin			= Math.round(( getInsideWindowWidth() - wwidth ) /2 );
	ymin			= Math.round(( getInsideWindowHeight() - wheight ) /2 );
	xmax		= Math.round( getInsideWindowWidth()- (( getInsideWindowWidth() - wwidth ) /2 ) );
	//ymax		= Math.round( getInsideWindowHeight()- (( getInsideWindowHeight() - wheight ) /2 ) ); ***
	ymax		= ymin + 124; // ###
	if (ymin < 0) {
		//ymin = 1; ymax = wheight - 1; ***
		ymin = 1; //  ###
	}
	x				= xmax - owidth - 51;
	y				= ymin + 10;


	//document.getElementById('counter_x').value=ymax;


//	if (fudgeFactor.top == -1) {
//      if ((typeof obj.offsetTop == "number") && obj.offsetTop > 0) {
//            fudgeFactor.top = obj.offsetTop;
//            fudgeFactor.left = obj.offsetLeft;
//        } else {
//            fudgeFactor.top = 0;
//            fudgeFactor.left = 0;
//        }
//        if (obj.offsetWidth && obj.scrollWidth) {
//            if (obj.offsetWidth != obj.scrollWidth) {
//                obj.style.width = obj.scrollWidth;    
//            }
//        }
//    }


	//var y = Math.round((getInsideWindowHeight()/2) - (getObjectHeight(obj)/2));
	//stopPoint = Math.round((getInsideWindowWidth()/2) - (getObjectWidth(obj)/2));
    shiftTo(obj, x, y);
    show(obj);
    intervalID = setInterval("glide('" + layerName + "')", 30);
}
// Move the object 
function glide(layerName) {
	if  (checkContent()){
		set_vector_x();
		set_vector_y();
		setPosition(layerName);
	} else {
		stopGlide(layerName);
	}
}

function checkContent(){
	// controleert of de nieuwsknop aan moet staan aan de hand van variabele in projects_overall.html
	//document.getElementById('counter_x').value = window.projects_overall.nieuws;
	if (window.projects_overall.nieuws == "aan"){
		return true;
	}
	return false;
}

function setPosition(layerName){
	var obj = getRawObject(layerName);
	show(obj);
	shiftBy(obj,vector_x,vector_y);
	x = x + vector_x;
	y = y + vector_y;
	//document.getElementById('counter_x').value=x;
	//document.getElementById('counter_y').value=y;
}

function set_vector_x(){
	var cur_vector_x = vector_x;
	check_edges_x();
	if (cur_vector_x != vector_x)	 return;
	// check_inner_x(); UITGEZET ***
}


function set_vector_y(){
	var cur_vector_y = vector_y;
	check_edges_y();
	if (cur_vector_y != vector_y)	 return;
	//check_inner_y(); UITGEZET **
}


function check_edges_x(){
	if ((x) <= xmin){ 
		vector_x = vector_x * -1;
	}	
	if ((x+owidth) >= xmax){ 
		vector_x = vector_x * -1;
	}	
}

function check_edges_y(){
	if ((y) <= ymin){ 
		vector_y = vector_y * -1;
	}	
	if ((y+oheight) >= ymax){ 
		vector_y = vector_y * -1;
	}	
}

function check_inner_x(){
	if ((y+oheight) >= (ymin + 124) && y <= (ymax - 124)){
		if (((x-xmin)%80 == 0 && vector_x == -1) || ((x-xmin+owidth)%80 == 0) && vector_x == 1){ 				
			vector_x = vector_x * -1;
		}
	}
}

function check_inner_y(){
	if ((y+oheight) == (ymin + 124) && vector_y == 1){
	//alert ((x-xmin)%80);
	//alert ((x-xmin)%80+owidth);
		if ((x-xmin)%80 + owidth > 80){ 		
	//alert ('switch');
			vector_y = vector_y * -1;
		}
	}
	if (y == (ymax - 124) && vector_y == -1){
		if ((x-xmin)%80 + owidth > 80){ 		
			vector_y = vector_y * -1;
		}
	}
}