function htmlspecialchars_decode (string, quote_style) {
    var optTemp = 0, i = 0, noquotes= false;
    if (typeof quote_style === 'undefined') {
        quote_style = 2;
    }
    string = string.toString().replace(/&lt;/g, '<').replace(/&gt;/g, '>');
    var OPTS = {
        'ENT_NOQUOTES': 0,
        'ENT_HTML_QUOTE_SINGLE' : 1,
        'ENT_HTML_QUOTE_DOUBLE' : 2,
        'ENT_COMPAT': 2,
        'ENT_QUOTES': 3,
        'ENT_IGNORE' : 4
    };
    if (quote_style === 0) {
        noquotes = true;
    }
    if (typeof quote_style !== 'number') { // Allow for a single string or an array of string flags
        quote_style = [].concat(quote_style);
        for (i=0; i < quote_style.length; i++) {
            // Resolve string input to bitwise e.g. 'PATHINFO_EXTENSION' becomes 4
            if (OPTS[quote_style[i]] === 0) {
                noquotes = true;
            }
            else if (OPTS[quote_style[i]]) {
                optTemp = optTemp | OPTS[quote_style[i]];
            }
        }
        quote_style = optTemp;
    }
    if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) {
        string = string.replace(/&#0*39;/g, "'"); // PHP doesn't currently escape if more than one 0, but it should
        // string = string.replace(/&apos;|&#x0*27;/g, "'"); // This would also be useful here, but not a part of PHP
    }
    if (!noquotes) {
        string = string.replace(/&quot;/g, '"');
    }
    // Put this in last place to avoid escape being double-decoded
    string = string.replace(/&amp;/g, '&');

    return string;
}

function enlargeImage(imgPath,name)
{
    $.facebox('<img src="'+imgPath+'" title="'+name+'" alt="'+name+'"><br><br><h3>'+name+'</h3>');
}

function rand()
{
    var now=new Date()
    var num=(now.getSeconds())%10
    var num=num+1
    return num;
}


function refreshFormCode()
{
    if (document.getElementById('form_code_image'))
    {
        document.getElementById('form_code_image').src="/project/templates/public_default/images/heart.png";
        setTimeout('document.getElementById(\'form_code_image\').src="/project/form_code.php?hash='+rand()+'";', 300); 
    }
}


function getElementPosition(elemId)
{
    var elem = document.getElementById(elemId);
	
    var w = elem.offsetWidth;
    var h = elem.offsetHeight;
	
    var l = 0;
    var t = 0;
	
    while (elem)
    {
        l += elem.offsetLeft;
        t += elem.offsetTop;
        elem = elem.offsetParent;
    }

    return {"left":l, "top":t, "width": w, "height":h};
}

function showHelp(symbol_code)
{
    var width=400;
    var height=500;
    var iHeight= document.body.clientHeight, iWidth = self.innerWidth;
    var left = (self.opera ? iWidth : screen.availWidth)/2 - width/2;
    var top =  (self.opera ? iHeight : screen.availHeight)/2 - height/2;
    var win = window.open('/help_'+symbol_code+'.html','help','width='+width+',height='+height+',left='+left+',top='+top+',resizable=1,scrollbars=1,status=0');
}

$(function(){
  $("a.comment-parent").click(function(){
    var level=$(this).attr("level");
    var parents=$(this).parents(".comment").prevAll('div.comment[level="'+(level-1)+'"]');
    var parent=parents[0].id;
    
    document.location="#"+parent;
    return false;
  })
})


function showFragment(src,x,y,x2,y2)
{
  destroyJcrop();
  hideFragment();
  $.scrollTo("#photo",500);
  $("#photo-div").css("background-color","#ffffff");
  
  var position = $("#photo").position();
  
  var fg_img = '<img src="'+src+'" id="fg_photo" style="position:absolute;display:none;border:1px solid #666;z-index:10;" class="photo" onclick="return hideFragment()">';
  var fg_darkner = '<div style="position:absolute;display:none;width:'+($("#photo").width())+'px;height:'+($("#photo").height())+'px;z-index:5;background-color: #000;" id="fg_photo_darkner" onclick="hideFragment();"></div>';
  var hide_button = "<a href='#' onclick='return hideFragment()' id='hide-fragment-button' style='position:absolute;left:"+(position.left + x2)+"px;top:"+(position.top + y - 16)+"px;z-index:10;'><img src='/images/cancel.png' width='16' height='16' alt='Скрыть фрагмент' title='Скрыть фрагмент'/></a>";
  
  
  $("#photo").after(fg_img);
  $("#photo").after(fg_darkner);
  $("#photo").after(hide_button);
  
  $("#fg_photo_darkner").css("opacity",0.8);
  $("#fg_photo_darkner").css({left: position.left, top: position.top});
  $("#fg_photo_darkner").show();
  
  
  $("#fg_photo").css({left: position.left + x-1, top: position.top + y-1});
  $("#fg_photo").show();
}

function hideFragment()
{
  $("#fg_photo").remove();
  $("#fg_photo_darkner").remove();
  $("#hide-fragment-button").remove();
  return false;
}
