// JavaScript Document
if (!Array.prototype.forEach)
{
  Array.prototype.forEach = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        fun.call(thisp, this[i], i, this);
    }
  };
}

function hide(id) {
	el = document.getElementById(id).style;
	el.display = (el.display == 'block' || el.display == '' )? 'none' : 'block';
}

function inhide(id) {
	el = document.getElementById(id).style;
	if( el.display == "" ) el.display = 'inline-block';
	el.display = (el.display != 'inline-block')?'inline-block':'none';
}

function form_submit(name) {
	document.forms[name].submit();
}

function sub_select() {
	father		= document.getElementsByName("advert_type")[0];
	
	subtype		= document.getElementsByName("advert_subtype");
	for(x = 0; x <= subtype.length -1; x++) {
		subtype[x].disabled = "disabled";
	}
	
	
	var childSelects = Array();
	   childSelects[1] = "byt";
	   childSelects[3] = "pozemky";
	   /*childSelects[2] = "dum";
	   childSelects[4] = "komer";
	   childSelects[5] = "ostatni";*/
		childSelects.forEach(groupHide);
	
	document.getElementById(childSelects[father.value]).style.display = "block";
	document.getElementById(childSelects[father.value]).removeAttribute("disabled");
}

function groupHide(element, index, array) {
	document.getElementById(array[index]).style.display = "none";	
}

function getChild(id, tag) {
	elements = document.getElementById(id).getElementsByTagName(tag);
	return element[0];
}

function clearonclick(element, text) {
	if(element.value == text) {
		element.value = "";
	}
}

function show_part( id, element ) {
	if( element.value == 2 || element.value == 3 ) // nahradit necim jako IN ARRAY
		document.getElementById(id).style.display = "block";
	else
		document.getElementById(id).style.display = "none";
	
	if( element.value == 3 ) {
		document.getElementsByName("menu")[0].disabled = true;
		document.getElementById("upoutavka").style.display = "block";
		document.getElementById("sub_article").style.display = "block";
	}else{
		document.getElementsByName("menu")[0].disabled = false;
		document.getElementById("upoutavka").style.display = "none";
		document.getElementById("sub_article").style.display = "none";
	}
	
}

function resize_image( id, step ) {
	elm	= document.getElementById(id);
	if( parseInt( elm.getAttribute("width") ) + parseInt( step ) > 0 ) 
		elm.setAttribute("width", parseInt( elm.getAttribute("width") ) + parseInt( step ) );
	else
		elm.setAttribute("width", 0);
}

var button = 0;
function edit_item ( id, in_button ) {
	elm		= document.getElementById(id);
	elm.childNodes;
	text	= elm.innerHTML;
	elm.innerHTML = '<textarea id="'+id +'_edit" class="w400 h100">'+ text +'</textarea>'
	elm.innerHTML +='<br /><input type="button" value="Uložit" onclick="save(\''+ id +'\')" />';
	button = in_button;
}

function save( id ) {
	text = document.getElementById(id +'_edit');
	document.getElementById(id).innerHTML = text.value;
	document.getElementById(id).appendChild( button );
}

function redirect( url ) {
	window.location = url;
}

function load_image( id, src ) {
	document.getElementById(id).style.backgroundImage = "url("+ src +")";
}

var y = -1;
function slideshow( images, show_id, delay ) {
	var x = images.length;
	
	if( y == -1 )
		y = Math.floor( Math.random()* parseInt(x) + 1 );
	
	load_image( show_id, images[y] );
	y++;
	
	if( y == x )
		y = 0;
	
	setTimeout('slideshow(images, "'+show_id+'", "'+delay+'")', delay);
}

function intval (mixed_var, base) {
    // Get the integer value of a variable using the optional base for the conversion  
    // 
    // version: 1009.2513
    // discuss at: http://phpjs.org/functions/intval
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: stensi
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Matteo
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: intval('Kevin van Zonneveld');
    // *     returns 1: 0
    // *     example 2: intval(4.2);
    // *     returns 2: 4
    // *     example 3: intval(42, 8);
    // *     returns 3: 42
    // *     example 4: intval('09');
    // *     returns 4: 9
    // *     example 5: intval('1e', 16);
    // *     returns 5: 30
    var tmp;
 
    var type = typeof( mixed_var );
 
    if (type === 'boolean') {
        return (mixed_var) ? 1 : 0;
    } else if (type === 'string') {
        tmp = parseInt(mixed_var, base || 10);
        return (isNaN(tmp) || !isFinite(tmp)) ? 0 : tmp;
    } else if (type === 'number' && isFinite(mixed_var) ) {
        return Math.floor(mixed_var);
    } else {
        return 0;
    }
}
