var flashOffsetLeft = 0;
var sailingObjContainer;
var objTopMargin = 0;
var startTop = 0;
var posTo;

var headHeight = 190;
var underMenuBlock = 'bl_2';
var botBlock = 'bl_3';
var botLimit = 0;

function initMovie() {  
    init();	

	document.getElementById('sailingContainer').style.left = flashOffsetLeft;
	document.getElementById('sailingContainer').style.display='block';
	document.getElementById('sailingContainer').style.visibility='visible';

}


function init() {
	headHeight += document.getElementById('bl_1').offsetHeight;
	botLimit = document.getElementById(botBlock).offsetHeight;
	//alert(botLimit);
	sailingObjContainer = document.getElementById('sailingContainer');
	setInterval("moveSailingBlock()", 100);
}

function moveSailingBlock() {
	var dif = 0;
			
	menuHeight = document.getElementById('sailingContainer').offsetHeight;
	//menuHeight = document.getElementById('gPageMenu1').offsetHeight + document.getElementById(underMenuBlock).offsetHeight;
	menuWidth = document.getElementById('gPageMenu1').offsetWidth;
	
	if(document.getElementById('sailingBlockContainer').offsetHeight < menuHeight + botLimit)
		return;
	
	documentObjTop = window.document.body.scrollTop;		
	
	documentObjTop = (documentObjTop < headHeight) ?  0 : window.document.body.scrollTop - headHeight;			
	
	// if menu is higher than screen
	if(screenSize().h < menuHeight) {
		
		// move down
		if(documentObjTop > startTop) {
			if(documentObjTop < (startTop + (dif = menuHeight - screenSize().h))) {
				return;
			} else {
				posTo = documentObjTop + objTopMargin - dif;	
			}
		}
		// move up
		else {
			if(documentObjTop > startTop) {
				return;
			} else {
				posTo = documentObjTop + objTopMargin;	
			}
		}	
	} else {
		posTo = documentObjTop + objTopMargin;	
	}
	
	//screenSize().h < menuHeight
	/*if(documentObjTop > posTo && screenSize().h < menuHeight) {
		if(documentObjTop < (dif = menuHeight - screenSize().h)) {
			return;
		} else {
			posTo = documentObjTop + objTopMargin - dif;	
		}
	} else if (documentObjTop < posTo && screenSize().h < menuHeight){
		//===============================
		posTo = documentObjTop + objTopMargin;
		
		if(documentObjTop > posTo) {
			return;
		} else {
				
		}
		//===============================
	} else {
		posTo = documentObjTop + objTopMargin;	
	}*/
			
	//posTo = documentObjTop + objTopMargin;
	
	
	//alert(botLimit);
	//alert(menuHeight);
	if(posTo > document.getElementById('sailingBlockContainer').offsetHeight /*- botLimit*/ - menuHeight)
		posTo = document.getElementById('sailingBlockContainer').offsetHeight /*- botLimit*/ - menuHeight;
		//alert(posTo);
	
		
		ydiff = startTop - posTo;
	
		if (ydiff != 0) {
			movey=Math.floor(ydiff/3);
			startTop -= movey;
		}
	
	sailingObjContainer.style.top = startTop;
	
	//alert("W: " + screenSize().w + ", H: " + screenSize().h);
}


function screenSize() {
    var w, h; // Объявляем переменные, w - длина, h - высота
    w = (window.innerWidth ? window.innerWidth : (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.offsetWidth));
    h = (window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight));
    
    return {w:w, h:h};
}

var isOpera = self.opera
var isIE = document.all && !isOpera;

if(isIE || isOpera) {	
	window.attachEvent("onload", initMovie);
} else {	
	window.addEventListener("load", initMovie, true);
}