function toggle(id){
    if (document.getElementById(id).style.display == 'none') {
        document.getElementById(id).style.display = 'block';
	}
    else {
        document.getElementById(id).style.display = 'none';		
    }
}

if (window.Node && Node.prototype && !Node.prototype.contains)
{
	Node.prototype.contains = function (arg) {
		return !!(this.compareDocumentPosition(arg) & 16)
	}
}

if (!Array.prototype.indexOf)
{
	Array.prototype.indexOf = function(elt /*, from*/)
	{
		var len = this.length;
		var from = Number(arguments[1]) || 0;
		from = (from < 0)
			? Math.ceil(from)
			: Math.floor(from);
		if (from < 0)
			from += len;

		for (; from < len; from++)
		{
			if (from in this && this[from] === elt)
				return from;
		}
		return -1;
	}
}

/**
 * Всплывающая подсказка
 *
 * @param {Object} parent Родительский элемент в котором будут формироваться всплывающие подсказки
 * @param {String} className Имя класса который будут назначаться DIV'у со всплывающей подсказкой
 * @param {String} id Идентификатор DIV'а со всплывающей подсказкой
 */
function Comment(parent, className, id, time){
    var comment;
    var timer;
    var self = this;
    var posX = 0;
    var posY = 0;
	if (!time) var time = 1000;
    
    /**
     * Показывает всплывающую подсказку (если текст не пустой)
     *
     * @param {String} text Текст всплывающей подсказки
     */
    this.show = function(text, posX, posY){
        if (text) {
            if (timer) 
                clearTimeout(timer);

            timer = setTimeout(function(){
            
                var tmp = comment.firstChild;
                if (tmp) {
                    comment.removeChild(tmp);
                }
                //comment.appendChild(document.createTextNode(text));
				comment.innerHTML = text;
                comment.id = id;
                comment.className = className;
                parent.appendChild(comment);
                if (posX) 
                    var tmpPosX = posX;
                else 
                    var tmpPosX = document.mousePosX + 10;
                if (posY) 
                    var tmpPosY = posY;
                else 
                    var tmpPosY = document.mousePosY + 10;
                
                if ((tmpPosY + comment.offsetHeight) > document.body.clientHeight) 
                    tmpPosY = document.body.clientHeight - comment.offsetHeight - 10;
                if ((tmpPosX + comment.offsetWidth) > document.body.clientWidth) 
                    tmpPosX = document.body.clientWidth - comment.offsetWidth - 10;
                
                comment.style.left = (tmpPosX + document.documentElement.scrollLeft) + 'px';
                comment.style.top = (tmpPosY + document.documentElement.scrollTop) + 'px';
                posX = 0;
                posY = 0;
            }, time);
        }
    }
    
    /**
     * Прячет всплывающую подсказку
     */
    this.hide = function(delay){
		if (arguments.length == 0) var delay = 500;
        posX = arguments[0];
        posY = arguments[1];
        if (timer) 
            clearTimeout(timer);
        timer = setTimeout(function(){
            if (document.getElementById(id)) {
                parent.removeChild(comment);
            }
        }, delay);
    }
    
    comment = document.createElement('div');
    comment.onmouseover = function(){
        clearTimeout(timer);
    }
    comment.onmouseout = function(){
        self.hide(comment.style.left, comment.style.top);
    }
    
}

// Получаем позицию мыши
function fixPosition(e){
    document.mousePosX = e.clientX;
    document.mousePosY = e.clientY;
}

$(document).bind("mousemove", fixPosition);

function label(string) {
	if (this.value == "") {
		this.value = string;
		return;
	}
	if (this.value == string) {
		this.value = "";
		return;
	}
}

$(document).ready(function (){
		var links  = $("a[href^='javascript:void(null|']");
		for (var i = 0;  i < links.length ; i++ ){
			var href = links.get(i).href;
			href = href.replace('javascript:void(null|', '' )
			href = href.replace('));', '' )
			var div = $("<span id='"+href+"' onclick='javascript:showPanarama(this)' >"+links.get(i).innerHTML+"</span>");
			div.addClass("panoLink");
			$(links.get(i)).replaceWith(div);
		}
	}
	);

function showPanarama(element){

	
	if ($('#swf_panorama').length) {		
		$('#swf_panorama').remove();
		createPanarama(element);
	} else {
		createPanarama(element);		
	}	
}

function createPanarama(element){
	var id = element.id;
	var showDiv = document.createElement("div");
	showDiv.id = 'swf_panorama';
	showDiv.innerHTML = '<div align="center" style="padding-top:100px; padding-bottom:100px;">Загрузка данных...<br><img src="images/loading.gif"></div>';
	$(showDiv).insertAfter(element);
	$(showDiv).load('index.php?page=panorama&DocID=' + id, '', '');
}

function showMapElement(id){
	$('#map').get(0).style.display = 'none';
	$('#mapElement').get(0).style.display = 'block';
	$('#mapElement').get(0).innerHTML = '<div align="center" style="padding-top:100px; padding-bottom:100px;">Загрузка данных...<br><img src="images/loading.gif"></div>';
	$('#mapElement').load('index.php?page=map&type=object&DocID='+id, '', '');	
}

function closeMapElement(){
	$('#map').get(0).style.display = 'block';
	$('#mapElement').get(0).style.display = 'none';	
}

