var initMaxRight = 1000;

var right_space = 0;
var left_space = 116;
var menuW = 51;

var left1 = left_space + 0;
var left2 = left_space + menuW;
var left3 = initMaxRight - menuW - right_space;
var left4 = left_space + menuW;

var step = 20;
var sleep_time = 0;

var right1;
var right2;
var right3;
var right4;

var dno1;
var dno2;
var dno3;
var dno4;

var curpage = '';

function init() {
	dno1 = document.getElementById('page1');
	dno2 = document.getElementById('page2');
	dno3 = document.getElementById('page3');
	dno4 = document.getElementById('page4');

	document.getElementById('posi').innerHTML = ".";

	max_right = document.getElementById('posi').offsetLeft;
	right_space = Math.round((max_right - 1000) / 2)-50;

	max_bottom = document.getElementById('posi').offsetTop;// + 18;
	max_bottom2 = document.getElementById('img_page2').height + 330;
	max_bottom3 = document.getElementById('posi3').offsetTop;// + 18;

	document.getElementById('posi').innerHTML = "";

//	alert(max_bottom + ' vs ' + max_bottom2 + ' vs ' + max_bottom3);
	if (max_bottom2 > max_bottom) {
	    max_bottom = max_bottom2;
	}
	if (max_bottom3 > max_bottom) {
	    max_bottom = max_bottom3;
	}

	//max_bottom2 = document.getElementById('posi2').offsetTop + 18;
	//if (max_bottom2 > max_bottom) {
	//    max_bottom = max_bottom2+2;
	//}
	//alert('MR:' + max_right + ' RS:' + right_space);
	// SET PAGE HEIGHT
	dno1.style.height = max_bottom;
	dno2.style.height = max_bottom;
	dno3.style.height = max_bottom;
	dno4.style.height = max_bottom;

	if (max_right > initMaxRight) {
	    workArea = max_right;
	} else {
	    workArea = initMaxRight - 10;
	}
//	workArea = workArea - (3 * menuW);

	// SET PAGE WIDTH
	wa = 1000;//workArea - right_space - left_space;
	dno1.style.width = wa - menuW;
	dno2.style.width = wa - menuW;
	dno3.style.width = wa - menuW;
	dno4.style.width = wa - menuW;

	left1 = dno1.style.left = right_space + left_space + menuW - wa;
	left2 = dno2.style.left = right_space + left_space + (2 * menuW) - wa;
	left3 = dno3.style.left = right_space + left_space + (3 * menuW) - wa;
	left4 = dno4.style.left = right_space + (2 * menuW);

	document.getElementById('workbody').style.width = workArea;
	document.getElementById('workbody').style.height = max_bottom;

    right1 = right_space + left_space - (2 * menuW);
    right2 = right_space + left_space - menuW;
    right3 = right_space + left_space;
}

function reInit() {
    dno1 = document.getElementById('page1');
	dno2 = document.getElementById('page2');
	dno3 = document.getElementById('page3');
	dno4 = document.getElementById('page4');

	document.getElementById('posi').innerHTML = ".";

	max_right = document.getElementById('posi').offsetLeft;
	right_space = Math.round((max_right - 1000) / 2) - 50;
	max_bottom = document.getElementById('posi').offsetTop;// + 18;
	max_bottom2 = document.getElementById('img_page2').height + 330;
	max_bottom3 = document.getElementById('posi3').offsetTop;// + 18;
	
	document.getElementById('posi').innerHTML = "";
	
//	alert(max_bottom + ' vs ' + max_bottom2 + ' vs ' + max_bottom3);
	if (max_bottom2 > max_bottom) {
	    max_bottom = max_bottom2;
	}
	if (max_bottom3 > max_bottom) {
	    max_bottom = max_bottom3;
	}
	dno1.style.height = max_bottom;
	dno2.style.height = max_bottom;
	dno3.style.height = max_bottom;
	dno4.style.height = max_bottom;

	if (max_right > initMaxRight) {
	    workArea = max_right;
	} else {
	    workArea = initMaxRight - 10;
	}

	document.getElementById('workbody').style.height = max_bottom;
}

function switchPage(dn) {
    cdn = document.getElementById(dn);
    cndp = cdn.style.left.replace('px','')/1;

    try {
//        document.getElementById('video_' + curpage).style.width = '1px';
//        document.getElementById('video_' + curpage).style.height = '1px';
//        document.getElementById('video_' + curpage).innerHTML = '&nbsp;';
    }catch (e) {}

//    alert("C:" + cndp);
    curpage = '';
    normalMenu('page1', 'mast');
    normalMenu('page2', 'prof');
    normalMenu('page3', 'isk');

    if (cndp > -200) {
        setSmall(dn);
    } else {
        setBig(dn);
    }

    if (curpage == 'page1') hoverMenu(curpage, 'mast');
    if (curpage == 'page2') hoverMenu(curpage, 'prof');
    if (curpage == 'page3') hoverMenu(curpage, 'isk');

}

function setBig(dn) {
//    alert('DN' + dn);
	curpage = dn;
	dno1_np = dno1_p = dno1.style.left.replace('px','')/1;
	dno2_np = dno2_p = dno2.style.left.replace('px','')/1;
	dno3_np = dno3_p = dno3.style.left.replace('px','')/1;
//alert(dno1_p + ' / ' + dno2_p + ' / ' + dno3_p + ' / ' + dno4_p);
	if (dn == 'page1') {
		if (dno1_p < right1) dno1_np += step;
		if (dno2_p < right2) dno2_np += step;
		if (dno3_p < right3) dno3_np += step;
		document.getElementById('page3_t3g').src = "img/t3g.png";
		document.getElementById('page2_t3g').src = "img/t3g.png";
		document.getElementById('page1_t3g').src = "img/t3g.png";
	}
	if (dn == 'page2') {
		if (dno2_p < right2) dno2_np += step;
		if (dno3_p < right3) dno3_np += step;
		document.getElementById('page2_t3g').src = "img/t3g.png";
		document.getElementById('page3_t3g').src = "img/t3g.png";
	}
	if (dn == 'page3') {
		if (dno3_p < right3) dno3_np += step;
		document.getElementById('page3_t3g').src = "img/t3g.png";
	}

	dno1.style.left = dno1_np;
	dno2.style.left = dno2_np;
	dno3.style.left = dno3_np;

	if ((dno2_np != dno2_p) || (dno3_np != dno3_p) || (dno1_np != dno1_p)) {
//		setBig(dn);
		setTimeout("setBig('"+dn+"')", sleep_time);
//	} else
//    	if (curpage == 'page2') {
//    	    openProfPart('film');
    	}
}

function setSmall(dn) {
//    alert('DN' + dn);
	dno1_np = dno1_p = dno1.style.left.replace('px','')/1;
	dno2_np = dno2_p = dno2.style.left.replace('px','')/1;
	dno3_np = dno3_p = dno3.style.left.replace('px','')/1;
//alert(dno1_p + ' / ' + dno2_p + ' / ' + dno3_p + ' / ' + dno4_p);
	if (dn == 'page3') {
	    curpage = '';
		if (dno1_p > left1) dno1_np -= step;
		if (dno2_p > left2) dno2_np -= step;
		if (dno3_p > left3) dno3_np -= step;
		document.getElementById('page3_t3g').src = "img/t3gr.png";
		document.getElementById('page2_t3g').src = "img/t3gr.png";
		document.getElementById('page1_t3g').src = "img/t3gr.png";
	}
	if (dn == 'page2') {
		if (dno1_p > left1) dno1_np -= step;
		if (dno2_p > left2) dno2_np -= step;
		curpage = 'page3';
		document.getElementById('page2_t3g').src = "img/t3gr.png";
		document.getElementById('page1_t3g').src = "img/t3gr.png";
	}
	if (dn == 'page1') {
		if (dno1_p > left1) dno1_np -= step;
		curpage = 'page2';
        document.getElementById('page1_t3g').src = "img/t3gr.png";
	}
//	hoverMenu()

	dno1.style.left = dno1_np;
	dno2.style.left = dno2_np;
	dno3.style.left = dno3_np;

	if ((dno2_np != dno2_p) || (dno3_np != dno3_p) || (dno1_np != dno1_p)) {
//		setBig(dn);
		setTimeout("setSmall('"+dn+"')", sleep_time);
	}
}

function changeLeftImg(shift) {

    try {
        /*if (stp > (gall_count-3)) {
            stp = gall_count - 4;
        }
        shift += stp;
        img = "" + shift;
        ipa = gall_name + '/' + img + '.jpg';

        document.getElementById('video_' + curpage).style.width = '1px';
        document.getElementById('video_' + curpage).style.height = '1px';
        document.getElementById('video_' + curpage).innerHTML = '&nbsp;';*/
        if (cur_media == 'img') {
            document.getElementById('video_' + curpage).style.width = '1px';
            document.getElementById('video_' + curpage).style.height = '1px';
            document.getElementById('video_' + curpage).innerHTML = '';
    	    document.getElementById('img_' + curpage).src = full_img[shift];
    	    document.getElementById('img_' + curpage).style.width = '450px';
    	    document.getElementById('title_' + curpage).innerHTML = title_img[shift];
        } else {
            document.getElementById('img_' + curpage).src = 'img/1px.gif';
            document.getElementById('img_' + curpage).style.width = '1px';
            document.getElementById('video_' + curpage).style.width = '450px';
            document.getElementById('video_' + curpage).style.height = '300px';
            document.getElementById('video_' + curpage).innerHTML = full_vid[shift];
            document.getElementById('title_' + curpage).innerHTML = '';
        }
    } catch(e) {}
    setTimeout('reInit()', 500);
    setTimeout('reInit()', 2000);
    setTimeout('reInit()', 10000);
}

function hoverMenu(page, pref) {
    document.getElementById(page + '_td').style.background = "url(img/" + pref + "_a.png)";
    document.getElementById(page + '_img').src = "img/" + pref + "_txt_w_"+cur_lang+".png";
//    document.getElementById(page + '_t3g').src = "img/t3g.png";
}

function normalMenu(page, pref) {
    if (page != curpage) {
        document.getElementById(page + '_td').style.background = "url(img/" + pref + "_i.png)";
        document.getElementById(page + '_img').src = "img/" + pref + "_txt_b_"+cur_lang+".png";
//        document.getElementById(page + '_t3g').src = "img/t3gr.png";
//    } else {
//        document.getElementById(page + '_td').style.background = "url(img/" + pref + "_i.png)";
//        document.getElementById(page + '_img').src = "img/" + pref + "_txt_b.png";
    }
}

function getContent(url, div) {
  var http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari, ...
    http_request = new XMLHttpRequest();
  } else if (window.ActiveXObject) { // IE
    try {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }
  if (!http_request) return false;
  http_request.onreadystatechange = function() {
  	if ((http_request.readyState == 4) && (http_request.status == 200))
  		eval('loadCont(http_request, div)');
  	};
  http_request.open('GET', url, true);
  http_request.send(null);
}

var cdiv;
var working = 0;
function loadCont(http_request, dv) {
  cdiv = document.getElementById(dv);

  var div = document.getElementById(dv);
  div.innerHTML = '';

  div.innerHTML = http_request.responseText;
  var x = div.getElementsByTagName("script");
  var bdy = document.body;
  for (var i=0; i < x.length; i++) {
		var scr = document.createElement('script');
		scr.type = 'text/javascript';
		scr.defer=true;
  	    scr.text = x[i].text;
  	    bdy.appendChild(scr);
  }
  reInit();
}

function openProfPart(fn) {
    getContent(fn + '.htm', 'text_content');
}
function show_cont(cont, c) {
    getContent('cont.php?cont='+cont+'&c='+c, 'short_info');
}

var videolink = '';
function showVideo() {
    document.getElementById('img_' + curpage).style.width = '1px';
//    document.getElementById('video_' + curpage).style.width = '400px';
//    document.getElementById('video_' + curpage).style.height = '270px';
//    document.getElementById('video_' + curpage).innerHTML = videolink;
}

function showVideoGall()
{
    cur_media = 'vid';

    for (var i = 1; i <=12; ++i) {
//        alert(i + ' ' + prev_vid[i]);
        document.getElementById('img' + i).src = prev_vid[i];
    }
}

function showImgGall()
{
    cur_media = 'img';
    for (var i = 1; i <=12; ++i) {
        document.getElementById('img' + i).src = prev_img[i];
//        document.getElementById('title' + i).innerHTML = b_title_img[i];
    }

}

function show_main_gall()
{
    full_img = b_full_img.slice();
    prev_img = b_prev_img.slice();
    title_img = b_title_img.slice();
    cur_media = 'img';
    showImgGall();
    changeLeftImg(1);
    document.getElementById('media_sel').style.display = 'none';
}
