var scrollBox;
var scrollBox2;
var maxScroll;
var maxScroll2;
var scroll_block_start;
var scroll_block_start2;
var slide_interval;
var IE6;
window.addEvent('load', function(){


if (!Cookie.read('screen_x')){
    var w = screen.width;
    var h = screen.height;
    var i=0;
    var j=0;
    for(var l=4;l<20;l++) for(var m=3;m<12;m++)
    {
	if(((w/l) == (h/m)) && (i==0 && j==0)){
	    i=l;
	    j=m;
	}
    }
    if((i==0 && j==0) || (i==3 && j==2) || (i==4 && j==3) || (i==5 && j==4)) var wide = '';
    else var wide = 'w';
    if(screen.width > 1280)
    {
	var imgsrc = '/images/bg'+wide+'_'+(screen.width+50)+'.jpg';
	var myCookie = Cookie.write('screen_x', (screen.width+50), {duration: 365});
    }
    else
    {
        var imgsrc='/images/bg'+wide+'_1280.jpg';
	var myCookie = Cookie.write('screen_x', 1280, {duration: 365});
    }

	$('img_fon').dispose();
	var newImgFon = new Element(
	    'img',
	    {
		'src': imgsrc,
		'id': 'img_fon', 
		'title': '', 
		'class': 'fon', 
		'events':{
		    'load': function(){
			resize_bg();
		    }
		}
	    }
	);
	newImgFon.inject($('div_fon'));

	var myCookie = Cookie.write('screen_wide', wide, {duration: 365});
}
else
{
setTimeout('resize_bg()',500);
}

IE6 = (navigator.userAgent.toLowerCase().indexOf('msie 6') != -1) && (navigator.userAgent.toLowerCase().indexOf('msie 7') == -1) && (navigator.userAgent.toLowerCase().indexOf('msie 8') == -1) ;
if(IE6) {$(document.body).setStyle('background',"url('"+$('img_fon').src+"') top left no-repeat fixed");}

try{
if($('div_scroll').scrollHeight > $('div_scroll').offsetHeight)
{
    maxScroll = $('div_scroll').getScrollSize().y - $('div_scroll').offsetHeight;
    scrollBox = new Fx.Scroll($('div_scroll'));

    $('content_scr_bl').position({
	relativeTo:$('container_drag'),
	position: 'upperLeft',
	edge: 'upperLeft',
	offset: {x:0, y: (($('div_scroll').offsetHeight -25)/ maxScroll) * $('div_scroll').getScroll().y}
    });
    $('container_drag').setStyle('display','block');
    $('content_scr_bl').setStyle('display','block');
    $('container_drag').setStyle('height',$('div_scroll').offsetHeight);
    $('div_scroll').addEvent('DOMMouseScroll',scrollContent);
    $('div_scroll').addEvent('mousewheel',scrollContent);
    $('content_scroll_td').addEvent('DOMMouseScroll', scrollContent);
    $('content_scroll_td').addEvent('mousewheel', scrollContent);
    scroll_block_start = $('content_scr_bl').getStyle('top');
    var moveScrBl = new Drag.Move($('content_scr_bl'),{
	container:$('container_drag'),
	onDrag: function(el){
	    scrollBox.set(0, ((maxScroll / ($('div_scroll').offsetHeight - 60)) * (el.getStyle('top').toInt() - scroll_block_start.toInt())) );
	}
    });
}
}
catch(e){}

try{
if($('div_scroll_v').scrollHeight > $('div_scroll_v').offsetHeight)
{

    maxScroll2 = $('div_scroll_v').getScrollSize().y - $('div_scroll_v').offsetHeight;
    scrollBox2 = new Fx.Scroll($('div_scroll_v'));
    $('content_scr_bl_v').position({
	relativeTo:$('container_drag_v'),
	position: 'upperLeft',
	edge: 'upperLeft',
	offset: {x:0, y: (($('div_scroll_v').offsetHeight)/ maxScroll2) * $('div_scroll_v').getScroll().y}
    });
    $('container_drag_v').setStyle('display','block');
    $('content_scr_bl_v').setStyle('display','block');
    $('container_drag_v').setStyle('height',$('div_scroll_v').offsetHeight);
    $('div_scroll_v').addEvent('DOMMouseScroll',scrollContent2);
    $('div_scroll_v').addEvent('mousewheel',scrollContent2);
    $('content_scroll_td_v').addEvent('DOMMouseScroll', scrollContent2);
    $('content_scroll_td_v').addEvent('mousewheel', scrollContent2);
    scroll_block_start2 = $('content_scr_bl_v').getStyle('top');
    var moveScrBl2 = new Drag.Move($('content_scr_bl_v'),{
	container:$('container_drag_v'),
	onDrag: function(el){
	    scrollBox2.set(0, ((maxScroll2 / ($('div_scroll_v').offsetHeight-60)) * (el.getStyle('top').toInt() - scroll_block_start2.toInt())));
	}
    });
}
}
catch(e){}
if(!IE6) slide_interval = setInterval("start_slideshow()", 1000);
});

function start_slideshow() {
if(menu_ready) {
try {
$('main_slideshow').innerHTML = '';

var myShow = new Slideshow('main_slideshow', slideshowData, { captions: false, controller: false, height: sl_height, width: 400, loader: false});
}
catch(e){}
clearInterval(slide_interval);
}
}


function scrollContent(event) {
	try { 
	    event = new Event(event).stop();
	    var ev_wheel = event.wheel;
	}
	catch(e)
	{
	    var ev_wheel = event;
	}
        if (ev_wheel > 0) {
	    scrollBox.set(0,$('div_scroll').getScroll().y - 30);
	} 
	else if (ev_wheel < 0) {
	    scrollBox.set(0,$('div_scroll').getScroll().y + 30);
	}

	$('content_scr_bl').position({
	    relativeTo:$('container_drag'),
    	    position: 'upperLeft',
    	    edge: 'upperLeft',
	    offset: {x:0, y: (($('container_drag').offsetHeight - 25) / maxScroll) * $('div_scroll').getScroll().y}
	});

}

function scrollContent2(event) {
	try { 
	    event = new Event(event).stop();
	    var ev_wheel = event.wheel;
	}
	catch(e)
	{
	    var ev_wheel = event;
	}
        if (ev_wheel > 0) {
	    scrollBox2.set(0,$('div_scroll_v').getScroll().y - 30);
	} 
	else if (ev_wheel < 0) {
	    scrollBox2.set(0,$('div_scroll_v').getScroll().y + 30);
	}
	$('content_scr_bl_v').position({
	    relativeTo:$('container_drag_v'),
    	    position: 'upperLeft',
    	    edge: 'upperLeft',
	    offset: {x:0, y: (($('container_drag_v').offsetHeight - 25) / maxScroll2) * $('div_scroll_v').getScroll().y}
	});

}



function resize_bg() {
	var wide = false;
	if(Cookie.read('screen_wide') == 'w') wide = true;
	var img_w=2000;
	var img_h;
	if(wide) img_h=1300; else img_h=1027;
	var el = $('img_fon');
	var top;
	var width;
	var height;
	var cas;
	if(el.offsetWidth < getDocWidth()){
	    width='100%'; 
	    height='auto';
	    cas=1;
	    el.setStyle('width',width);
	    el.setStyle('height',height);
	}
	if(el.offsetHeight < getDocHeight()){
	    width='auto';
	    height='100%'; 
	    cas=2;
	    el.setStyle('width',width);
	    el.setStyle('height',height);
	}
	if(el.offsetHeight > getDocHeight()) top = '-'+(el.offsetHeight - getDocHeight())+'px'; 
	el.setStyle('top',top);

}

function getDocWidth() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollWidth, D.documentElement.scrollWidth),
        Math.max(D.body.offsetWidth, D.documentElement.offsetWidth),
        Math.max(D.body.clientWidth, D.documentElement.clientWidth)
    );
}


function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}



