var checked = false;
var clicked = false;
var container_height = 0;
var posx = 0;
var posy = 0;
var tmp_posx = 0;
var tmp_posy = 0;
var polzunok_top = 10;
var scroll_height = 0;
var click_step = 1/10;

var function1 = '';
var function2 = '';

document.onmousemove = mousemove;
document.onmouseup = mouseup;

var middle = Array();
middle['first'] = Array();
middle['first']['image'] = 'assets/templates/drg/images/photo.jpe';
middle['first']['title'] = 'Cyril E. Geacintov, President and CEO of DRG, awarded the Ellis Island Medal of Honor';
middle['first']['preview'] = 'The founder of the global medical device technology company was honored as an American Citizen, as of one several medal honorees representing diverse ethnic origin';
middle['first']['link'] = '[~75~]';

function change_middle(id){
    $('#video_box').hide();
    $('#image_box').show();
    
    if(middle[id]['image']!='')
        $('#middle_photo').attr('src',middle[id]['image']);
    else
        $('#middle_photo').attr('src',middle['first']['image']);
    $('#middle_title').html(middle[id]['title']);
    $('#middle_preview').html(middle[id]['preview']);
    $('#middle_link').attr('href',middle[id]['link']);
}
//$(document).ready(init());
window.onload = init();
function init(){
    function1 = document.onselectstart;
    function2 = document.onmousedown;
    if(document.getElementById("news_container") != null )
        container_height = document.getElementById("news_container").clientHeight;
    if(document.getElementById("scroll") != null )
        scroll_height = document.getElementById("scroll").clientHeight-40;
    if(document.getElementById("checkbox_span") != null ){
        document.getElementById("checkbox").checked = false;
        document.getElementById("checkbox_span").style.backgroundImage = "url(assets/templates/drg/images/checkbox_clear.gif)";
    }
    if(document.getElementById("cicle") != null ){
        $('#cicle').cycle({ fx:    'scrollRight'});
        $("div.menu2 > ul > li > a").mouseover(function() {
            change_middle($(this).attr('id'));});
        change_middle('hybridxl');
    }
}

function remember_click(){
    if(checked == true){
        document.getElementById("checkbox").value=0;
        document.getElementById("checkbox_span").style.backgroundImage = "url(assets/templates/drg/images/checkbox_clear.gif)";
        checked = false;
    }
    else{
        document.getElementById("checkbox_span").style.backgroundImage = "url(assets/templates/drg/images/checkbox.gif)";
        document.getElementById("checkbox").value=1;
        checked = true;
    }
}

function getMouseXY(e){
    if (!e) var e = window.event;
    if (e.pageX || e.pageY) 	{
        tmp_posx = e.pageX;
        tmp_posy = e.pageY;
    }else if (e.clientX || e.clientY) 	{
            tmp_posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
            tmp_posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
    }return [tmp_posx, tmp_posy];
}

function mousedown(e){
    posy = tmp_posy;
    clicked = true;
    document.onselectstart = function() {return false;} // ie
    document.onmousedown = function() {return false;} // mozilla
}
function mouseup(){
    clicked = false;
    document.onselectstart = function1;
    document.onmousedown = function2;
}

function move_scroll(step){
    polzunok_top += step;
    if(polzunok_top<10)polzunok_top = 10;
    if(polzunok_top>scroll_height)polzunok_top = scroll_height;
    posy = tmp_posy;
    document.getElementById("news_container").style.margin = (-((polzunok_top*(container_height-scroll_height))/scroll_height)+15)+"px 0px 0px 0px";
    document.getElementById("polzunok").style.top = polzunok_top+'px';
}

function mousemove(e){
    getMouseXY(e);
    if(clicked)move_scroll((tmp_posy - posy));
}
function clickup(){
    move_scroll(-scroll_height*click_step);
}
function clickdown(){
    move_scroll(scroll_height*click_step);
}
function click_on_polotno(){
    pos = (tmp_posy - document.getElementById("scroll").offsetHeight);
    if (pos>polzunok_top+40)clickdown();
    else if (pos<polzunok_top) clickup();
}
