// JavaScript Document

function checkFields()
{
	if( document.komentar.predmet.value == "" )
	{
		alert( "Doplňte předmět příspěvku, prosím!\n(povinný údaj)" );
		document.komentar.predmet.focus();
		return false;
	}
	if( document.komentar.komentar.value == "" )
	{
		alert( "Napište alespoň nějaký komentář, když už jste začal/a :)\n(povinný údaj)" );
		document.komentar.komentar.focus();
		return false;
	}
	if( document.komentar.vysledek.value == "" )
	{
		alert( "Napište správnou odpověď, prosím...\n(bez ní se příspěvek neuloží)" );
		document.komentar.vysledek.focus();
		return false;
	}
	return true;
}

g_aktualni_radek = 0;

function getIFrameDocument( aID )
{
    var rv = null;

    // if contentDocument exists, W3C compliant (Mozilla)
    if( document.getElementById(aID).contentDocument )
    {
        rv = document.getElementById(aID).contentDocument;
    }
    else
    {
    // IE
        rv = document.frames[aID].document;
    }
    return rv;
}

function showLayer( id )
{
    obj = document.getElementById( id );
    class_name = obj.className;
    obj.className = class_name.replace( /hide/, "show" );
}

function showLayerWithIframe( _id, _iframe_src )
{
    obj_iframe = getIFrameDocument( _id + "_iframe" );
    if( obj_iframe && _iframe_src != "" )
    {
        obj_iframe.location = _iframe_src;
    }
    obj = document.getElementById( _id );
    class_name = obj.className;
    obj.className = class_name.replace( /hide/, "show" );
}

function hideLayer( id )
{
    obj = document.getElementById( id );
    class_name = obj.className;
    obj.className = class_name.replace( /show/, "hide" );
}

// nastavit id aktivniho radku
/**
 *
 * @access public
 * @return void
 **/
function nastavAktivniRadek( id_aktivni )
{
	g_aktualni_radek = id_aktivni;
}

/**
 *
 * @access public
 * @return void
 **/
function vyplnPredstaveni( nazev_predstaveni, id_predstaveni )
{
	if( g_aktualni_radek != 0 )
	{
		obj_nazev = document.getElementById( "nazev_"+g_aktualni_radek );
		obj_nazev_id = document.getElementById( "nazevid_"+g_aktualni_radek );
		obj_nazev.value = nazev_predstaveni;
		obj_nazev_id.value = id_predstaveni;
	}
	hideInput();
}

//+ display input
function displayInput(e, id)
{
	if (!e) var e = window.event;

    obj = document.getElementById( "data_input" );
    obj.style.display = "block";
    obj.style.left = ( mouseX(e) + 20 ) + "px";
    obj.style.top = ( getScrollY() + mouseY(e) - 40 ) + "px";
}

function hideInput()
{
    obj = document.getElementById( "data_input" );
    obj.style.display = "none";
}
//- display input

//+ help functions
function getScrollY()
{
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return scrOfY;
}

function mouseX(e)
{
    curX = 0;
    //ie
    if(document.all) { curX = event.clientX; }
    //netscape 4
    if(document.layers) { curX = e.pageX; }
    //mozilla
    if(document.getElementById) { curX = e.clientX; }
    return curX;
}

function mouseY(e)
{
    curY = 0;
    //ie
    if(document.all) { curY = event.clientY; }
    //netscape 4
    if(document.layers) { curY = e.pageY; }
    //mozilla
    if(document.getElementById) { curY = e.clientY; }
    return curY;
}

function keyEscape(e)
{
    if (!e) var e = window.event;

    if(e.keyCode == 27) return true; //keyCode returns the ASCII value of the key pressed.
    else return false;
}

function conditionalClose(e)
{
    if( keyEscape(e) ) hideInput();
}
//- help functions

function checkState( obj )
{
	if( obj.checked )
	{
		obj.value = "ano";
	}
	else
	{
		obj.value = "ne";
	}
}

function addMenuItem( menu_id, position, e )
{
    if (!e) var e = window.event;

    document.getElementById( "menu_id" ).value = menu_id;
    document.getElementById( "menu_pos" ).value = position;
    if( position == "before" )
    {
        document.getElementById( "menu_typ_sub" ).disabled = "disabled";
        document.getElementById( "menu_typ_sub" ).checked = "";
        document.getElementById( "menu_typ_men" ).checked = "checked";
    }
    else
    {
        document.getElementById( "menu_typ_sub" ).disabled = "";
    }

    displayInput( e, "");
//    	alert( "id = " + menu_id + ", position = '" + position + "'" );
}

function deleteMenuItem( menu_id, position, e )
{
    if (!e) var e = window.event;

    document.getElementById( "menu_id" ).value = menu_id;
    document.getElementById( "menu_pos" ).value = position;
    if( position == "before" )
    {
        document.getElementById( "menu_typ_sub" ).disabled = "disabled";
        document.getElementById( "menu_typ_sub" ).checked = "";
        document.getElementById( "menu_typ_men" ).checked = "checked";
    }
    else
    {
        document.getElementById( "menu_typ_sub" ).disabled = "";
    }

    displayInput( e, "");
//    	alert( "id = " + menu_id + ", position = '" + position + "'" );
}

// -------------------------
// GALLERY RELATED
g_actual_pic_id = "";

function showBigPic( number )
{
	if( g_actual_pic_id != "" )
	{
		hideBigPic( g_actual_pic_id );
	}
	position_top = getScrollY() + 50;
	document.getElementById( "pic" + number ).style.visibility = "visible";
	document.getElementById( "pic_back" + number ).style.visibility = "visible";
	document.getElementById( "pic" + number ).style.top = position_top + "px";
	document.getElementById( "pic_back" + number ).style.top = position_top + "px";
	document.getElementById( "pic" + number ).style.left = "50%";
	document.getElementById( "pic_back" + number ).style.left = "50%";
	g_actual_pic_id = number;
}

function hideBigPic( number )
{
	document.getElementById( "pic" + number ).style.visibility = "hidden";
	document.getElementById( "pic_back" + number ).style.visibility = "hidden";
	document.getElementById( "pic" + number ).style.top = "-800px";
	document.getElementById( "pic_back" + number ).style.top = "-800px";
	document.getElementById( "pic" + number ).style.left = "-2000px";
	document.getElementById( "pic_back" + number ).style.left = "-2000px";
}

function chcol( obj )
{
	document.getElementsByName( "mlink" ).color = "#FFFFFF";
	obj.style.color = "#888888";
}

function getScrollXY()
{
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function deleteMenuCheck()
{
    if( confirm( "Opravdu smazat?" ) )
    {
        return true;
    }
    else
    {
        return false;
    }
}

function deleteFormItemCheck( obj, val )
{
    if( confirm( "Opravdu smazat?" ) )
    {
        obj.value = val;
        return true;
    }
    else
    {
        return false;
    }
}

// PIC opacity  CHANGE
function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style;
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function shiftOpacity(id, millisec) {
	//if an element is invisible, make it visible, else make it ivisible
	if(document.getElementById(id).style.opacity == 0) {
		opacity(id, 0, 100, millisec);
	} else {
		opacity(id, 100, 0, millisec);
	}
}

function resetBack(id)
{
    document.getElementById(id).style.backgroundImage = "none";
}

function blendimage(divid, imageid, imagefile, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//set the current image as background
	document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";

	//make image transparent
	changeOpac(0, imageid);

	//make new image
//	var pic = new Image();
//    pic.src = imagefile;
//    if( pic.complete == true )
//    {
//        document.getElementById(imageid).src = pic.src;
//    }
//    else
//    {
//        pic.onload = document.getElementById(imageid).src = imagefile;
//    }
    document.getElementById(imageid).src = imagefile;

	//fade in image
	for(i = 0; i <= 100; i++) {
		setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
//		setTimeout("changeOpac(" + (100-i) + ",'" + divid + "')",(timer * speed));
		timer++;
	}
//    setTimeout("resetBack('" + divid + "')",Math.round(millisec / 10));
//	document.getElementById(divid).style.backgroundImage = "none";
}

function currentOpac(id, opacEnd, millisec) {
	//standard opacity is 100
	var currentOpac = 100;

	//if the element has an opacity set, get it
	if(document.getElementById(id).style.opacity < 100) {
		currentOpac = document.getElementById(id).style.opacity * 100;
	}

	//call for the function that changes the opacity
	opacity(id, currentOpac, opacEnd, millisec)
}


// setInterval( "swapImages()", 20000 );



// ------------------------------------------------------------------------
// dealing with parts
var total_parts = 0;

function showPart( num )
{
	for( j=1; j<=total_parts; j++)
	{
		if( j == num )
		{
			document.getElementById( "div_part"+j ).style.display = "block";
			document.getElementById( "a_part"+j ).style.fontWeight = "bold";
		}
		else
		{
			document.getElementById( "div_part"+j ).style.display = "none";
			document.getElementById( "a_part"+j ).style.fontWeight = "normal";
		}
	}
}

function makePartsHidden()
{
	obj = document.getElementsByName( "a_part" );
	total_parts = obj.length;
	for( i=1; i<=total_parts; i++)
	{
		if( i > 1 )
		{
			document.getElementById( "div_part"+i ).style.display = "none";
		}
		if( i == 1 )
		{
			document.getElementById( "a_part"+i ).style.fontWeight = "bold";
		}
		document.getElementById( "a_part"+i ).href = "#self";
		eval( 'document.getElementById( "a_part'+i+'" ).onclick = function(){ showPart('+i+'); };' );
	}
}

if (window.addEventListener) // W3C standard
{
  window.addEventListener('load', makePartsHidden, false); // NB **not** 'onload'
}
else if (window.attachEvent) // Microsoft
{
  window.attachEvent('onload', makePartsHidden);
}

// ------------------------------------------------------------------------