// photo frame size (720 height for portrait, 480 height for landscape)
var smaler_size, bigger_size, navigation_width, navigation_height, scrollbar_size, min_border, portrait, opera_scrollbar;
smaler_size = 490;
bigger_size = 740
navigation_width = 260;
navigation_height = 255;
min_border = 20
scrollbar_size = 17;
// opera neumi schovavat jen jeden posuvnik...
opera_scrollbar = (navigator.userAgent.search(/Opera/) < 0 ) ? 0 : 6;
portrait = false;

var w_main, w_photo, s_photo, photo, s_copyright, w_info, s_title, s_info, s_navig, google_ads, map_link;
w_main = document.getElementById('w_main');
w_photo = document.getElementById('w_photo');
s_photo = document.getElementById('s_photo');
photo = document.getElementById('photo');
s_copyright = document.getElementById('s_copyright');
w_info = document.getElementById('w_info');
s_title = document.getElementById('s_title');
s_info = document.getElementById('s_info');
s_navig = document.getElementById('s_navig');
google_ads = document.getElementById('google_ads');

var cur_pic = null;
var scroller = 0;
var direct = 3;
var max_scroll_turns = 2;

var IE = document.all ? true : false;
var oldIE = (!window.XMLHttpRequest && navigator.userAgent.search(/MSIE/) >= 0 )? true : false;

if (IE) {
	// internet explorer
	document.body.onresize = place_wnds;
	document.body.onload = init;
    document.body.onkeydown = photo ? hotkeys_photo : hotkeys_previews;
} else {
	window.onresize = place_wnds;
	window.onload = init;
	window.onkeydown = photo ? hotkeys_photo : hotkeys_previews;
}

function prefetch(smer, typ) {
    // smer = {prev, next}, typ = {web, map}
    var prev = document.getElementById(smer);
    var prev_pic = createPicFromLink(prev.href, typ);
    window.status = "Pre-loading image " + prev_pic;
    var img = document.createElement("IMG");
    img.className = "preload";
    img.onload = img_onload;
    img.src = prev_pic;
    w_photo.appendChild(img);
}
function init() {
	if (oldIE) {return;}
	place_wnds();
    if (photo) {
        // prefetch next picture
        prefetch("next", "web");
        prefetch("next", "map");
        // prefetch previous picture
        prefetch("prev", "web");
        prefetch("prev", "map");
    }
    if (!photo) {       // focus preview photo
        var photo_id = "";
        if (location.hash.length > 0) {
            var params = location.hash.substring(1).split("&");
            for (var i=0; i<params.length; i++) {
                var name = params[i].split("=")[0];
                var value = params[i].split("=")[1];
                if (name == 'photo_id') { cur_pic = value;}
            }
        }
        focusRelPic(0);
        if (document.getElementById(cur_pic)) {
            focusPic(document.getElementById(cur_pic));
        }
    }
}

function hotkeys_photo(evnt) {
    var ek;
    if (IE) {
        ek = event.keyCode;
    }  else {
        ek = evnt.which;
    }
    var prev = document.getElementById('prev');
    var next = document.getElementById('next');
    var previews = document.getElementById('previews');
    var main = document.getElementById('main');
    // 37 - left, 39 - right, 38 - up, 40 - down, 13 - enter, 32 - spacebar
    // 188 - comma, 190 - period, 76 - l, 77 - m, 72 - h
    if (ek == 37 || ek == 188) {location.href = prev.href;}
    if (ek == 39 || ek == 190 || ek == 13) {location.href = next.href;}
    if (ek == 38 || ek == 76) {location.href = previews.href;}
    if (ek == 72) {location.href = main.href;}
}

function hotkeys_previews(evnt) {
    var ek;
    if (IE) {
        ek = event.keyCode;
    }  else {
        ek = evnt.which;
    }
    var prev = document.getElementById('prev');
    var next = document.getElementById('next');
    var main = document.getElementById('main');
    // 37 - left, 39 - right, 38 - up, 40 - down, 13 - enter, 32 - spacebar
    // 188 - comma, 190 - period, 76 - l, 77 - m, 72 - h
    if (ek == 38 || ek == 188) {location.href = prev.href;}
    if (ek == 40 || ek == 190) {location.href = next.href;}
    if (ek == 72) {location.href = main.href;}
    if (ek == 13 && cur_pic) {location.href = document.getElementById(cur_pic).href;}
    if (ek == 39) {focusRelPic(1);}
    if (ek == 37) {focusRelPic(-1);}
}

function createPicFromLink(s, adr) {
    if (s.indexOf("#") > 0) {
        s = s.substring(0, s.indexOf("#"));
    }
    s = s.replace('/html/', '/'+adr+'/');
    s = s.replace('.en.html', '.jpg');
    s = s.replace('.html', '.jpg');
    return s;
}

function focusPic(fpic) {
    if (fpic) {
        for (var i=0; i<AllPictures.length; i++) {
            var apic = document.getElementById(AllPictures[i]);
            apic.style.backgroundColor = "";
            apic.style.borderColor = "#555555"
        }
        //fpic.focus();		nepotrebuju, protoze scrolluju manualne
        //fpic.blur();
        fpic.style.backgroundColor = "#222222"
        fpic.style.borderColor = "#aaaaaa"
    }
    if (w_photo.scrollHeight > w_photo.offsetHeight) {
		w_photo.scrollTop = (fpic.offsetTop-Math.round(w_photo.offsetHeight/2) > 0) ? fpic.offsetTop-Math.round(w_photo.offsetHeight/2) : 0;
	}
}

function focusRelPic(pic) {
    var cur_pic_id = 0;
    for (var i=0; i<AllPictures.length; i++) {
        if (AllPictures[i] == cur_pic) {
            cur_pic_id = i + pic;
            break;
        }
    }
    if (cur_pic_id >= 0 && cur_pic_id < AllPictures.length) {
        cur_pic = AllPictures[cur_pic_id];
        focusPic(document.getElementById(cur_pic));
    }
    if (cur_pic) {      // set href for Zobrazit link
        var sp = document.getElementById('show_photo')
        sp.className = "";
        sp.href = document.getElementById(cur_pic).href;
    } else {    // this should not happen
        var sp = document.getElementById('show_photo')
        sp.className = "disabled";
        sp.href = "#";
    }
}

function img_onload () {
  window.status = 'Done';
}

function place_wnds() {
	if (oldIE) {return;}
	if (photo) {
        place_photo()

    } else {
        place_previews();
    }
	place_info();

	// must be here because explorer looses info about height if i change border color...
	w_main.style.borderColor = "#555555";
	w_main.style.overflow = "hidden";
}

function place_photo() {
	var pw, ph, ww, wh, w, h, s;
	// photo size
	pw = photo.offsetWidth;
	ph = photo.offsetHeight;
	// assure minimal width and height
	//mw = 750 > pw + 250 ? 750 : pw + 250;
	//mh = photo.offsetHeight + 10;
	//if (w_main.clientWidth < mw) w_main.style.width = mw + "px";
	//if (w_main.clientHeight < mh) w_main.style.height = mh + "px";

	// portrait or landscape?  -20 je kvuli posuvniku
	portrait = (ph > pw) || (smaler_size > w_main.clientHeight - navigation_height);
	// window size
	ww = w_main.clientWidth;
	wh = w_main.clientHeight;

	// general style
	w_info.style.overflow = "hidden";
	s_photo.style.position = "absolute";
	s_title.style.position = "absolute";
	s_info.style.position = "absolute";
	w_photo.style.backgroundColor = "#111111";
	s_photo.style.backgroundColor = "#111111";
	// photo frame size (720 height for portrait, 480 height for landscape)
	if (portrait) { // bigger_size x w_main.clientHeight
		w = (ww - navigation_width < smaler_size) ? w = smaler_size : ww - navigation_width;
		h = wh;

		w_photo.style.width = w + "px";
		w_photo.style.height = wh + "px";
		s_copyright.style.left = w - 100 + "px";
		s_copyright.style.top = wh - 13 + "px";

		s_photo.style.left = w > pw ? (w - pw)/2 - 2 + "px" : "0px";
		s_photo.style.top = h > ph ? (h - ph)/2 - 2 + "px" : "0px";
        s_photo.style.width = w + "px";

		if (h < ph ) {  // vyska fotky nesmi byt vetsi nez vyska okna
			s_photo.style.height = h + "px";
            s_photo.style.width = pw + scrollbar_size + "px"; // misto na posuvnik
            s_photo.style.left = w > pw ? (w - pw - scrollbar_size)/2 - 2 + "px" : "0px";
            s_photo.style.overflow = "auto";
            s_photo.style.overflowX = "hidden";
            s_photo.style.overflowY = "scroll";
            if (!scroller) {scroller = window.setInterval(scroll_vert, 20);}
            w_photo.onclick = cancel_scroller;
            w_info.onclick = cancel_scroller;
            s_navig.onclick = cancel_scroller;
		} else if (w < pw) {    // fotka je sirsi nez okno
			s_photo.style.height = ph + scrollbar_size + opera_scrollbar + "px"; // misto na posuvnik
            s_photo.style.overflow = "auto";
            s_photo.style.overflowX = "scroll";
            s_photo.style.overflowY = "hidden";
            if (!scroller) {scroller = window.setInterval(scroll_horiz, 20);}
            w_photo.onclick = cancel_scroller;
            w_info.onclick = cancel_scroller;
            s_navig.onclick = cancel_scroller;
		} else {        // +7 je kvuli okraji fotky
			s_photo.style.height = ph + 7 + "px";
            s_photo.style.overflow = "hidden";
            s_photo.style.overflowX = "hidden";
            s_photo.style.overflowY = "hidden";
		}
	} else { // landscape
		w = ww;
		h = (wh - navigation_height < smaler_size) ? h = smaler_size : h = wh - navigation_height;

		w_photo.style.width = ww + "px";
		w_photo.style.height = h + "px";
		s_copyright.style.left = w - 100 + "px";
		s_copyright.style.top = wh - navigation_height - 13 + "px";

        s_photo.style.width = w + "px";

        if (w < pw) {  // potrebuju horizontalni posuvnik
            if (ph + scrollbar_size > h) {
                s_photo.style.top = "-2px";
            } else {
                s_photo.style.top = (h - ph - scrollbar_size)/2 - 2 + "px";
            }
            s_photo.style.left = "-2px";
			s_photo.style.height = ph + scrollbar_size + opera_scrollbar + "px"; // misto na posuvnik
            s_photo.style.overflow = "auto";
            s_photo.style.overflowX = "scroll";
            s_photo.style.overflowY = "hidden";
            if (!scroller) {scroller = window.setInterval(scroll_horiz, 20);}
            w_photo.onclick = cancel_scroller;
            w_info.onclick = cancel_scroller;
            s_navig.onclick = cancel_scroller;
        } else {
            s_photo.style.left = (w - pw)/2 - 2 + "px";
            s_photo.style.top = (h - ph)/2 - 2 + "px";
            s_photo.style.overflow = "hidden";
            s_photo.style.overflowX = "hidden";
            s_photo.style.overflowY = "hidden";
        }
	}
}

function cancel_scroller() {
    window.clearInterval(scroller);
    scroller = 0;
}

function scroll_vert() {
    s_photo.scrollTop = s_photo.scrollTop + direct;
    if (s_photo.scrollTop + s_photo.clientHeight >= s_photo.scrollHeight) {direct *= -1;}
    if (s_photo.scrollTop <= 0) {direct *= -1; max_scroll_turns -= 1;}
    if (max_scroll_turns <= 0 ) cancel_scroller();
}

function scroll_horiz() {
    s_photo.scrollLeft = s_photo.scrollLeft + direct;
    if (s_photo.scrollLeft + s_photo.clientWidth >= s_photo.scrollWidth) {direct *= -1;}
    if (s_photo.scrollLeft <= 0) {direct *= -1; max_scroll_turns -= 1;}
    if (max_scroll_turns <= 0 ) cancel_scroller();
}

function place_previews() {
	var ww, wh, w, h;
	portrait = 1;
	// window size
	ww = w_main.clientWidth;
	wh = w_main.clientHeight;

	// general style
	w_info.style.overflow = "hidden";
	s_title.style.position = "absolute";
	s_info.style.position = "absolute";
	w_photo.style.backgroundColor = "#111111";
    w_photo.style.padding = "10px";
    w_photo.style.overflow = "auto";
    w_photo.style.overflowY = "auto";
    w_photo.style.overflowX = "hidden";

    w = ww - navigation_width;
    //w = bigger_size;
    h = wh;

    w_photo.style.width = w - 20 + "px";	// -20 becausae of padding
    w_photo.style.height = wh - 20 + "px";	// -20 becausae of padding
    s_copyright.style.left = w - 100 + "px";
    s_copyright.style.top = wh - 13 + "px";
    if (google_ads) {
		google_ads.style.width = w - 30 + "px";
	}
}

function place_info() {
	var ww, wh, wpw, wph, th;
	// window size
	ww = w_main.clientWidth;
	wh = w_main.clientHeight;
	// photo frame size
	wpw = w_photo.offsetWidth;
	wph = w_photo.offsetHeight;
	// info column size
	if (portrait) {
		w_info.style.left = wpw  + "px";
		w_info.style.top = "-1px";
		w_info.style.width = ww - wpw - 1 + "px";
		w_info.style.height = wh + "px";
	} else {
		w_info.style.left = "-1px";
		w_info.style.top = wph + "px";
		w_info.style.width = ww + "px";
		w_info.style.height = wh - wph - 1 + "px";
	}
	// map and navigation
	s_navig.style.left = ww - navigation_width + "px";
	s_navig.style.top = wh - navigation_height + 2 + "px";
	// title
	if (portrait) {
		s_title.style.left = "0px";
		s_title.style.top = "0px";
		s_title.style.width = "100%";
	} else {
		s_title.style.left = "0px";
		s_title.style.top = "0px";
		s_title.style.width = (ww - navigation_width)/2 + "px";
	}
	// info
	th = s_title.offsetHeight;

	if (portrait) {
		s_info.style.left = "0px";
		s_info.style.top = th + "px";
		s_info.style.width = "100%";
		s_info.style.height = (wh - navigation_height - th > 0) ? wh - navigation_height - th + "px" : "500px";
	} else {
		s_info.style.left = (ww - navigation_width)/2  + "px";
		s_info.style.top = "0px";
		s_info.style.width = (ww - navigation_width)/2 + "px";
		s_info.style.height = wh - wph - 1 + "px";
	}
}

function popup(url, width, height, name)
{
    if (!url) {return false;}
    var newwin;
    var left   = (screen.width  - width)/2;
    var top    = (screen.height - height)/2;
    var params = 'width='+width+', height='+height;
    params += ', top='+top+', left='+left;
    params += ', directories=no';
    params += ', location=no';
    params += ', menubar=no';
    params += ', resizable=yes';
    params += ', scrollbars=no';
    params += ', status=no';
    params += ', toolbar=no';
    newwin = window.open(url, name, params);
    if (window.focus) {newwin.focus()}
    return false;
}


