function $(id) { return document.getElementById(id) };
function removeSelf(node) { node.parentNode.removeChild(node); };


function __getBehaviour (elem) {
    if (elem.hasAttribute) {
        if (elem.hasAttribute('behaviour')) return elem.getAttribute('behaviour');
        else return false;
    }
    else {
        if (elem.attributes && elem.attributes['behaviour']) return elem.attributes['behaviour'].value;
        else return false;
    }
};

getElementsByBehaviour = function (behaviour) {
    var a_buf = document.getElementsByTagName('*');
    result = [];
    for (var i=0; i<a_buf.length; i++) if (__getBehaviour(a_buf[i]) == behaviour) result.push(a_buf[i]);
    return result;
}


toggleClass = function (block, className) {
    var buf = block.className;
    if (buf.indexOf(className)==-1) block.className += ((block.className>'')?' ':'')+className;
    else { if (buf.indexOf(' '+className)>-1) block.className = block.className.replace(' '+className, '');
        else block.className = block.className.replace(className, '');
    }
    return true;
}

setClass = function (block, className) {
    var buf = block.className;
    if (buf.indexOf(className)==-1) block.className += ((block.className>'')?' ':'')+className;
    return true;
}

removeClass = function (block, className) {
    var buf = block.className;
    if (buf.indexOf(className)>-1) if (buf.indexOf(' '+className)>-1) block.className = block.className.replace(' '+className, '');
    else block.className = block.className.replace(className, '');
    return true;
}

hasClass = function (block, className) {
    var buf = block.className;
    return (buf.indexOf(className)==-1)?false:true;
}

var currentThumb = null;
var Thumb = null;
var photoContainer = null;
var isIE = (navigator.appName.indexOf("Microsoft") != -1);
var currentNewsId = null;
var thumbsArray = [];
var debug = null;

window.onload = function () {
    pngfix();

    a_buf = getElementsByBehaviour('expand_posting');
    for (i=0; i<a_buf.length; i++) {
        a_buf[i].onclick = function () {
            for (j=0; j<this.parentNode.parentNode.childNodes.length; j++) {
                b = this.parentNode.parentNode.childNodes[j];
                if (b.className.indexOf('text')>-1) { toggleClass(this, 'selected'); expand_block(b); }

            }
        };
        if (a_buf[i].captureEvents) a_buf[i].captureEvents(Event.CLICK);
    }

    a_buf = getElementsByBehaviour('expand_orange_block');
    for (i=0; i<a_buf.length; i++) {
        a_buf[i].onclick = function () {
            for (j=0; j<this.parentNode.parentNode.childNodes.length; j++) {
                b = this.parentNode.parentNode.childNodes[j];
                if (b.className.indexOf('text')>-1) { toggleClass(this.parentNode.parentNode, 'transparent'); expand_block(b); }
            }
        };
        if (a_buf[i].captureEvents) a_buf[i].captureEvents(Event.CLICK);
    }

    a_buf = getElementsByBehaviour('expand_comments');
    for (i=0; i<a_buf.length; i++) {
        a_buf[i].onclick = function () {
/*
            var buf = $('message');
            if (buf&&buf.className.indexOf('invisible')==-1) {
                setClass(buf, 'invisible');
                removeClass(buf.nextSibling, 'invisible');
            }
            else
*/
            for (j=0; j<this.parentNode.parentNode.childNodes.length; j++) {
                b = this.parentNode.parentNode.childNodes[j];
                if (b.className.indexOf('comments_container')>-1) { toggleClass(this, 'selected'); expand_block(b); }
            }
        };
        if (a_buf[i].captureEvents) a_buf[i].captureEvents(Event.CLICK);
    }
/*
    a_buf = getElementsByBehaviour('expand_comments_form');
    for (i=0; i<a_buf.length; i++) {
        a_buf[i].onclick = function () {
            for (j=0; j<this.parentNode.parentNode.childNodes.length; j++) {
                b = this.parentNode.parentNode.childNodes[j];
                if (b.className.indexOf('comments_form_container')>-1) { toggleClass(this, 'selected'); expand_block(b); }
            }
        };
        if (a_buf[i].captureEvents) a_buf[i].captureEvents(Event.CLICK);
    }
*/
    a_buf = getElementsByBehaviour('album_cover');
    for (i=0; i<a_buf.length; i++) {
        a_buf[i].onmouseover = function () { setClass(this, 'album_cover_hover'); };
        if (a_buf[i].captureEvents) a_buf[i].captureEvents(Event.MOUSEOVER);
        a_buf[i].onmouseout = function () { removeClass(this, 'album_cover_hover'); };
        if (a_buf[i].captureEvents) a_buf[i].captureEvents(Event.MOUSEOUT);
    }

    a_buf = getElementsByBehaviour('quote');
    for (i=0; i<a_buf.length; i++) {
        a_buf[i].innerHTML = '&#x2193; ответить';
        a_buf[i].onclick = function () {
            var id = this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.id;
            var comment = this.parentNode.nextSibling.nextSibling;
            var quote = this.previousSibling.nodeValue;
            quote += ': '+comment.innerHTML;
            quote = quote.replace(/<blockquote>/g, '[[quote]]');
            while (quote.indexOf('  ')>-1) quote = quote.replace(/  /g, ' ');
            quote = quote.replace(/\n\n/g, "\n");
            quote = quote.replace(/\r/g, '');
            quote = quote.replace(/<\/blockquote>/g, '[[/quote]]');
            quote = quote.replace(/<BLOCKQUOTE>/g, '[[quote]]');
            quote = quote.replace(/<\/BLOCKQUOTE>/g, '[[/quote]]');
            quote = quote.replace(/(<BR>)|(<br>)/g, "\n");
            $('text'+id).value += '[[quote]]'+quote+'[[/quote]]'+"\n\n";
            var buf = $('form'+id);
            removeClass(buf, 'invisible');
            setClass($('message'), 'invisible');
            if (buf.name) buf.name.focus(); else buf.text.focus();
        };
        if (a_buf[i].captureEvents) a_buf[i].captureEvents(Event.CLICK);
    }


    a_buf = getElementsByBehaviour('submit_form');
    for (i=0; i<a_buf.length; i++) {
        var button = a_buf[i];
        button.onclick = function () {
            var form = this.parentNode.parentNode.parentNode;
//alert(form);
            if (form.name) {
                if (form.name.value=='') { alert('Прежде чем отправлять комментарий, укажите своё имя.'); return false; }
                if (form.text.value=='') { alert('Прежде чем отправлять комментарий, напишите его.'); return false; }
            }
            JsHttpRequest.query(
                '',
                { 'form': form},
                function(js, text) {
//                    if (responseText>'') alert(responseText);
                    form.previousSibling.innerHTML = text;
                    if (form.name) {
                        removeClass($('message'), 'invisible');
                        setClass(form, 'invisible');
                        form.name.value = '';
                        form.text.value = '';
                        form.email.value = '';
                    }
                    else if (!js.error) setTimeout(function() {location.href=""}, 1000);
                },
                false // disable caching
            );
        };
        if (button.captureEvents) button.captureEvents(Event.CLICK);
    }



    if (location.hash=="#comments") {
        toggleClass($('comments_button'), 'selected');
        expand_block($('comments').lastChild);
    }

/*
    buf = $('album_banner_container');
    if (buf && window.devicePixelRatio) {
        buf.style.position = 'absolute';
        buf.style.left = buf.nextSibling.offsetLeft+50+'px';
        buf.style.top = ((version >= 5.01) && (version < 6.0))?'0px':'15px';
        buf.style.width = buf.nextSibling.offsetWidth+'px';
        window.onresize = function () { buf.style.left = buf.nextSibling.offsetLeft+50+'px'; buf.style.width = buf.nextSibling.offsetWidth+'px'; }
    }
*/
    Thumb = {minDist: 80, maxDist: 200};
    buf = getElementsByBehaviour('thumbs_container');
    if (buf[0]) {
        debug = $('debug');
        debug.style.position = 'fixed';
        for (i=0; i<buf[0].childNodes.length; i++) { thumbsArray.push(buf[0].childNodes[i]); }
        document.onmousemove = function (e) {
            __m = __getMousePos(e);
            mouse = {x: __m.x, y: __m.y};
//            debug.innerHTML = 'x: '+__m.x+'; y: '+__m.y+' thumbsArray.length='+thumbsArray.length;
            for (i=0; i<thumbsArray.length; i++) {
                t = thumbsArray[i];
                t.x = Math.abs(thumbsArray[i].offsetLeft+50-mouse.x);
                t.y = Math.abs(thumbsArray[i].offsetTop+50-mouse.y);
                if (t.x<Thumb.maxDist&&t.y<Thumb.maxDist) {
                    if (t.x>Thumb.minDist||t.y>Thumb.minDist) __transformThumb(thumbsArray[i], (t.x>t.y)?t.x:t.y);
                    else __transformThumb(thumbsArray[i], Thumb.minDist, true);
                }
                else {
                    __transformThumb(thumbsArray[i], Thumb.maxDist);
                }
            }
        }
/*
        for (i=0; i<buf[0].childNodes.length; i++) {
            thumb = buf[0].childNodes[i];
            thumb.onclick = function () {
                toggleClass (this, 'fullsize');
                if (hasClass(this, 'fullsize')) {
                    var img = this.firstChild.firstChild.firstChild;
                    var div = this;
                    var dh = (500-130)/4;
                    var h1 = 130+dh*1+'px';
                    var h2 = 130+dh*2+'px';
                    var h3 = 130+dh*3+'px';
                    var h4 = 130+dh*4+'px';
                    var dt = 30;
                    setTimeout(function (){div.style.zIndex=1000; img.style.width = img.style.height = h1; }, dt);
                    setTimeout(function (){img.style.width = img.style.height = h2; }, dt*2)
                    setTimeout(function (){img.style.width = img.style.height = h3; }, dt*3)
                    setTimeout(function (){img.style.width = img.style.height = h4; }, dt*4)
                }
                else {
                    var img = this.firstChild.firstChild.firstChild;
                    img.style.width = img.style.height = '130px';
                    this.removeAttribute('style');
                }
                return false;
            }

        }
*/
    }


/*
    a_buf = getElementsByBehaviour('thumb_expander');
    if (a_buf) { document.onmousemove = getMousePos; }
    for (i=0; i<a_buf.length; i++) {
        a_buf[i].onmouseover = function () { setClass(this, 'album_cover_hover'); };
        if (a_buf[i].captureEvents) a_buf[i].captureEvents(Event.MOUSEOVER);
        a_buf[i].onmouseout = function () { removeClass(this, 'album_cover_hover'); };
        if (a_buf[i].captureEvents) a_buf[i].captureEvents(Event.MOUSEOUT);
    }
*/

    if (typeof(proceedOnLoad)!='undefined') proceedOnLoad();

}

//proceedOnLoad = function() { alert('ok') };
function __getMousePos (e) {
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) {
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	return {x: posx, y: posy};
}


function __transformThumb(div, size, showCaption) {
    if (hasClass(div, 'fullsize')) return false;
    var a = parseInt((size-Thumb.maxDist)/(2*Thumb.maxDist/50));
    var b = parseInt(150-size/(Thumb.maxDist/50))>>1<<1;
    var c = (220-b)/120;
    c = (b-55)/75;
    div.firstChild.style.top = div.firstChild.style.left = a+'px';
    div.firstChild.firstChild.firstChild.style.width = div.firstChild.firstChild.firstChild.style.height = b+'px';
    div.firstChild.firstChild.firstChild.style.opacity = c;
    div.firstChild.firstChild.firstChild.style.filter = "alpha(opacity=" + (c*100) + ")";
/*
    if (showCaption) removeClass(div.firstChild.lastChild, 'invisible');
    else setClass(div.firstChild.lastChild, 'invisible');
*/
//    div.firstChild.lastChild.innerHTML = 'opacity='+c;
}



function expand_block (div) {

    if (div.className.indexOf('invisible')>-1) {
        div.style.height = '1px';
        setClass(div, 'rigid');
        removeClass(div, 'invisible');
        var innerHeight = div.firstChild.scrollHeight;
        var dh = innerHeight/4;
        var h1 = 10+dh*1+'px';
        var h2 = 10+dh*2+'px';
        var h3 = 10+dh*3+'px';
        var h4 = 10+dh*4+'px';
        var dt = 30;
        setTimeout(function (){div.style.height = h1; }, dt);
        setTimeout(function (){div.style.height = h2; }, dt*2)
        setTimeout(function (){div.style.height = h3; }, dt*3)
        setTimeout(function (){div.style.height = h4; div.removeAttribute('style'); removeClass(div, 'rigid'); }, dt*4)
    }
    else { setClass(div, 'invisible'); div.parentNode.removeAttribute('style'); }

}

