var flashOffsetLeft = 0;
var sailingObjContainer;
var objTopMargin = 0;
var startTop = 0;
var posTo;
var posBottom=250;
var headHeight = 215;
var underMenuBlock = 'bl_4';
//var botBlock1 = 'bl_5';
//var botBlock2 = 'bl_6';
var botLimit = 335;
function SalingIsExists() {
    if (document.getElementById('sailingContainer') != null)
        return true;
    return false;    
}
jQuery(document).ready(function() {

    if(SalingIsExists()){
        floatBlockInit();
        moveSailingBlock();
    }
});


jQuery(window).scroll(function() {
    if(SalingIsExists())
		moveSailingBlock();
});


//$(this).css({'background-color' : 'yellow', 'font-weight' : 'bolder'});
    

function floatBlockInit() {  
	//headHeight += document.getElementById('bl_1').offsetHeight;
	//botLimit = document.getElementById(botBlock1).offsetHeight + document.getElementById(botBlock2).offsetHeight;
	sailingObjContainer = document.getElementById('sailingContainer');	
	jQuery("#sailingContainer").css({'left' : flashOffsetLeft, 'display' : "block", "visibility" : "visible"});
}



function moveSailingBlock() {
	var dif = 0;
	menuHeight = document.getElementById('sailingContainer').offsetHeight;
	//menuHeight = document.getElementById('gPageMenu1').offsetHeight + document.getElementById(underMenuBlock).offsetHeight;
	menuWidth = (document.getElementById('gPageMenu1') || document.getElementById('gPageMenu2') || document.getElementById('gPageMenu3')).offsetWidth;
	
	if(document.getElementById('LeftPanel').offsetHeight < menuHeight + botLimit)
		return;
	
	//documentObjTop = window.document.body.scrollTop;
	documentObjTop = document.documentElement.scrollTop;		
	
	//documentObjTop = (documentObjTop < headHeight) ?  0 : window.document.body.scrollTop - headHeight;
        documentObjTop = (documentObjTop < headHeight) ?  0 : document.documentElement.scrollTop - headHeight;
	startTop = parseFloat(document.getElementById('sailingContainer').style.top);			
	
	// 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;	
	}	
	if(posTo > document.getElementById('LeftPanel').offsetHeight - botLimit - menuHeight)
		posTo = document.getElementById('LeftPanel').offsetHeight - botLimit - menuHeight;

        jQuery('#sailingContainer').animate({ top: posTo }, { duration: 1000, queue: false });
        //$('html, body').animate({ scrollTop: 0 }, 'slow');
}

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};
}
