function rebuild_ie_flash() {
	var pg_objects = document.getElementsByTagName("object");
	for (var i=0; i<pg_objects.length; i++) {
		pg_objects[i].outerHTML = pg_objects[i].outerHTML;
	}
	return true;
}

var Funcs = {
	gEBI : function(id)
	{
		return document.getElementById(id);
	},
	gEBN : function(name)
	{
		return document.getElementsByName(name);
	},
	appC : function(parent, child)
	{
		return parent.appendChild(child);
	},
	cElm : function(elm)
	{
		return document.createElement(elm);
	},
	cTxt : function(txt)
	{
		return document.createTextNode(txt);
	},
	rmC : function(parent,child)
	{
		return parent.removeChild(child);
	},
	rmE : function(elm)
	{
		return elm.parentNode.removeChild(elm);
	},
	rmChilds : function(parent)
	{
		while(parent.hasChildNodes()){
			parent.removeChild(parent.childNodes[0]);
		}
		return false;;
	},
	preloadImgs : function(imgsUrl)
	{
		for(i=0;i<imgsUrl.length;i++){
			var pic = new Image();
			pic.src = imgsUrl[i];
		}
		return true;
	}
}

var xmlhttp = {
	xt : null,
	url : null,
	_parent : null,

	request : function(url,content,method,parentID)
	{
		if(!url || !content || !parentID){return false;}
		if(!method){method = 'GET';}
		
		var tm = new Date();
		this.url = url + content + "&t=" + tm.getTime();
		this._parent = Funcs.gEBI(parentID);
		if(this.checkHost(url)){
			this.xt = this.xt ? this.xt : this.setRequestType();
			if(this.xt){
				this.xt.open(method,this.url,true);
				this.xt.setRequestHeader("Content-Type","text/xml;Charset=utf-8");
				this.xt.onreadystatechange = function(){xmlhttp.state_changed();}
				this.xt.send(null);
			}
		}
		return true;
	},

	requestDOM : function()
	{
		var loader = Funcs.cElm("script");
		loader.type = "text/javascript";
		loader.src = this.url;
		try{
			Funcs.appC(this._parent,loader);
		}catch(e){
			return false;
		}
	},

	state_changed : function()
	{
		if(this.xt.readyState == 4){
			if(this.xt.status == 200){
				eval(this.xt.responseText);
			}else{
				this.requestDOM();
			}
		}
	},

	setRequestType : function()
	{
		if(window.XMLHttpRequest){
			out = new XMLHttpRequest();
		}else if(window.createRequest){
			out = window.createRequest();
		}else{
			try{
				out = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){
				try{
					out = new ActiveXObject("Msxml2.XMLHTTP");
				}catch(e){
					return this.requestDOM();
				}
			}
		}
		return out;
	},

	checkHost : function()
	{
		var host = false;
		if(this.url.indexOf('http') != -1){
			host = this.url.substr(7);
			if(host.indexOf('/') != -1){
				host = host.substr(0,host.indexOf('/'));
			}
			if(document.location.hostname == host){
				return true;
			}else{
				return false;
			}
		}
		return true;
	}
}

function videosListing(style, days) {
	createCookie("videosListingStyle", style, days);
	var l = document.getElementById('videos-listing');
	if(l) {
		l.className = style;
		var links = document.getElementById('options-bar').getElementsByTagName("a");
		for(var i = 0; i < links.length; i++) {
			if(links[i].id == 'styl-' + style) {
				links[i].className = 'disabled';
			} else {
				links[i].className = '';
			}
		}
	}
	return false;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else {
		 var expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
	if (document.cookie.length>0) {
		start = document.cookie.indexOf(name + "=");
		if (start != -1) { 
			start = start + name.length+1; 
			end = document.cookie.indexOf(";", start);
			if (end == -1) {
				end = document.cookie.length;
			}
			return unescape(document.cookie.substring(start, end));
		}
	}
	return "";
}

function startAnimation(image, anim_url) {
	if (image.name != 'animation_loaded') {
		showAnimationClockImage(image);
	}
	image.name='animation_loaded';
	var anim = new Image(); anim.src = anim_url;
	var zast = new Image(); zast.src = image.src; 
	image.src = anim.src;	
}

function getAnimationClockImage(image) {
	for (var i=0; i < image.parentNode.childNodes.length; i++) {
		if (image.parentNode.childNodes[i].name=='loading') {
			return image.parentNode.childNodes[i];
		}
	}
}

function showAnimationClockImage(image) {
	var clock = getAnimationClockImage(image);
	clock.style.display='inline';
}

function hideAnimationClockImage(image) {
	var clock = getAnimationClockImage(image);
	if (eval(clock)) {
		clock.style.display='none';
	}
}

/**
 * onclick handler (with important return value!) for a redirecting
 * into some url. If user isn't logged-in, try to log via ajax
 * and then redirects to commandUrl
 * which makes friendship change and return new page.
 * @param string commandUrl - url where is the friendship change action
 */
function goToCommandUrl(commandUrl) {
	if(!usersys_logged) {
		showLoginLayer('login',
			function(){
				if (!usersys_logged) return false;
				else  window.location=commandUrl;
			},
			null
		);
		return false;
	}
	return true;
}

/**
 * selects all checkboxes in the favourites page on the profile
 */
function selectAllFavourites(checkVal)
{
	var fObj = document.forms['favouritesForm'];
	for (var i=0;i<fObj.elements.length;i++) {
		var e = fObj.elements[i];
		if ( (e.type=='checkbox') && (!e.disabled) ) {
			e.checked = checkVal;
		}
	}
}

/**
 * color value ('rgb(255, 0, 255)') to HEX format #FF00FF
*/
function colorToHex(color) {
	var reg=/(\d{1,3})/g; 
		x = color.match(reg);
		var s = "";
		for (i=0;x.length>i;i++) {
			x[i] = parseInt(x[i]);
			s += x[i].toString(16);
			// kdyz nastane 0 zformatuje se pouze jednou proto ji vlozime 2x
			if (s.length != (i+1)*2) {
				s += x[i].toString(16);
			}
		}
	return s.toUpperCase();
}
/**
 * najeti mysi na barvicku
*/
function mouseOverColor(object) {
	
	 var x = document.getElementById('tableColor').getElementsByTagName('td');
	 for (i=0;x.length>i;i++) {
	 	x[i].style.borderColor = '#000000';
	 }
	 object.style.borderColor = '#FFFFFF';
}

function mouseColorClick(color) {
	document.getElementById('colorInput').value = "#" + colorToHex(color);
	document.getElementById('divPreview').style.backgroundColor = color;
}

function showEpicentrumLayer(link) {
	if(link) {
		link.className = "tiper";
	}
	if($('#epicentrum-layer').length == 0) {
		var h = '<div id="epicentrum-layer">';
		h += '<div id="epicentrum-area"></div>';
		h += '</div>';
		$('body').append(h);
	}
	$('#epicentrum-layer').modal({
		overlay: 66,
		onShow: function() {
			$('#epicentrum-area').load('/epicentrum?js=1', null, null );
		},
		onClose: function (dialog) {

			/* pomale animovane zavirani */
			/*
			dialog.data.fadeOut('slow', function () {
				dialog.container.slideUp('slow', function () {
					dialog.overlay.fadeOut('slow', function () {
						$.modal.close();
						dialog.container.remove();
						dialog.overlay.remove();
						if (dialog.iframe) { dialog.iframe.remove(); }
						$('#epicentrum-layer').remove();
					});
				});
			});
			*/
			/* rychle zavreni layeru */
			dialog.data.remove();
			$.modal.close();
			dialog.container.remove();
			dialog.overlay.remove();
			if (dialog.iframe) { dialog.iframe.remove(); }
			$('#epicentrum-layer').remove();
			/* konec rychle varianty zavreni layeru */

		}
	});
	$('#modalOverlay')[0].onclick = function () {
		$.modal.close();
		$('#epicentrum-layer').remove();
	}
}

function footerPromoClick(link, id) {
	if(link == undefined || id == undefined) {
		return true;
	}
	if(window.XMLHttpRequest){
		xmlhttp = new XMLHttpRequest();
	}else if(window.createRequest){
		xmlhttp = window.createRequest();
	}else{
		try{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(e){
			try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
				return true;
			}
		}
	}
	var t = new Date();
	xmlhttp.open('GET',"/click.php?id="+id+"&t="+t.getTime(),false);
	xmlhttp.setRequestHeader("Content-Type","text/xml;Charset=utf-8");
	xmlhttp.setRequestHeader("X-Requested-With", "XMLHttpRequest");
	xmlhttp.send('');
	window.location = link.href;
	return false;
}
