$(document).ready(function() {

	/* set query value */
	if ($('input[name="query"]').length){
		//$('input[name="query"]').val(q.replace(/&quot;/g, '"').replace(/&apos;/g, "'").replace(/&lt;/g, "<").replace(/&gt;/g, ">"));
		if ($('input[name="query"]').val() == '') $('input[name="query"]')[0].focus();
	}


	/* set new query for markered projects links */
	$('input[name="query"]').keyup(function() {
		$("a.mb_filled,a.jq_sv").each(function(){
			if (pr_l_s[this.name]==null) this.href = this.href.replace(/(query=)[^&]*/,"$1"+$('input[name="query"]').val());
			else if ($('input[name="query"]').val() != '') this.href = pr_l_s[this.name]+$('input[name="query"]').val()
			else this.href = pr_l[this.name]
		});
	});
	
	
	/* keyboard navigation */
	var _input = null;
	$('input[name="query"]').blur(function() {
		_input=false;
	})
	.focus(function() {		
		_input=true;
	});
	
	$(document).keydown(function(event) {
	    if (event.ctrlKey) {
	        var link = null;
	        var href = null;
	        if (!(_input)) {
	            switch (event.keyCode ? event.keyCode : event.which ? event.which : null) {
	            case 0x27:
	                link = $('#nextPage');
	                break;
	            case 0x25:
	                link = $('#prevPage');
	                break;
	            }
	            if (link && link.attr("href")) {
	            	setTimeout(function(){document.location=link.attr("href");},100);
				}
	            if (href) {
	            	setTimeout(function(){document.location=href;},100);
	            }
	        }
	    }
	});


	/* hide popup layers by class name */
	$(document).click(function(e) {
		var target = (e && e.target) || (event && event.srcElement);
		while (target.parentNode){
			if (target.className.match('pl')||target.className.match('pc')) return;
			target=target.parentNode;
		}
		$(".pl").css('display','none');
	});
	

	/* menu handler */
	$('.fstl #moreBox').css('display','block');
	$('.fstl #moreBox_js').css('display','none');
	$('.fstl #moreBox').click(function() {
		$(".pl").css('display','none');
		toggle('p_list');
	});
	

	/* user menu handler */
	$('.fstl .login').click(function() {
		$(".pl").css('display','none');
		toggle('u_list');
	});
	var _nsh = new Image();
	_nsh.src = 'http://www.rambler.ru/cl?nv&u='+uniq+'&'+nmblr;
});


var sample = '';
var local_url = '';

function init(query) {
    if (sample != '') query = sample;
}
var im = new ImageManager();

function ImageManager() {
    this.handlers = {};
    this._imageCount = 0;
    this.removeBad = false;
};
ImageManager.prototype.wrapper = function (link, cl, wr, id) {
    document.write("<div id='" + wr + "' class='rr-wrapper'><div class='img'><a href='" + link + "' target='_blank' class='" + cl + " img_js");
    document.write("' id='" + id + "'></a></div></div>")
};
ImageManager.prototype.put = function (url, h, p, c, t) {
    this._imageCount++;
    var containerID = "__delayed_image_" + this._imageCount;
    var wrapperID = "__delayed_wrapper_" + this._imageCount;
    var containerClass = "n_image_" + this._imageCount;
    h = h.replace(/'/g, "\"");
    h = h.replace(/>/g, "&gt;");
    h = h.replace(/</g, "&lt;");
    this.href = h;
    var img = new Image();
    img.src = url;
    this.wrapper(h, containerClass, wrapperID, containerID);
    this._registerImage(url, wrapperID, containerID, t);
};
ImageManager.prototype._setHandlers = function (img) {
    for (var name in this.handlers) {
        img[name] = this.handlers[name];
    }
};
ImageManager.prototype._registerImage = function (imgUrl, wr, id, t) {
    var img = new Image();
    img.alt = t;
    img.title = t;
    img._loaded = false;
    img._scaled = false;
    var manager = this;
    img.show = function () {
        this.style.display = "block";
        this.style.width = this.width + "px";
        this.style.height = this.height + "px";
        cont = getEl(id);
        cont.appendChild(this);
        manager._setHandlers(this);
    };
    img.onload = function () {
        if (manager.removeBad && this.width == 1) {
            this.remove();
        } else {
            if (this._loaded) {
                this.onload = null;
                return
            }
            this._loaded = true;
            this.show();
        }
    };
    img.remove = function () {
        if (manager.removeBad) {
            var cont = getEl(wr);
            cont.parentNode.removeChild(cont);
        }
    }
    img.onerror = function () {
        this.remove();
    }

    window.setTimeout(function () {
        img.src = imgUrl;
    }, 600);
};

ra = {
    'Event': function (eventType, aNodeSelectors, intoSelectorsCallback, outSelectorsCallback) {
        this.attach(eventType, document, aNodeSelectors, intoSelectorsCallback, outSelectorsCallback)
    },
    'getID': function (oNode) {
        var getAttr = 'getAttribute',
            getAttrNode = getAttr + 'Node';
        return oNode[getAttr]('id', 2) || ((oNode[getAttrNode] && (oNode = oNode[getAttrNode]('id'))) ? oNode.nodeValue : '')
    }
};
ra.Event.prototype = {
    'watcher': function (currentNode, aNodeSelectors, intoSelectorsCallback, outSelectorsCallback, e) {
        var k = 0,
            selectedNode = currentNode;
        do {
                if (aNodeSelectors[k](currentNode)) {
                    !k && (selectedNode = currentNode);
                    if ((++k) == aNodeSelectors.length) {
                        if (intoSelectorsCallback) {
                            return intoSelectorsCallback(currentNode, selectedNode, e, this)
                        }
                        return
                    }
                }
            } while (currentNode = currentNode.parentNode);
        if (outSelectorsCallback) {
                return outSelectorsCallback(selectedNode, e, this)
            }
        return
    },
    'attach': document.addEventListener ?
    function (eventType, rootEventNode, aNodeSelectors, intoSelectorsCallback, outSelectorsCallback) {
        var handler = (function (obj) {
            return function (e) {
                obj.watcher(e.target, aNodeSelectors, intoSelectorsCallback, outSelectorsCallback, e) === false && e.preventDefault()
            }
        })(this);
        rootEventNode.addEventListener(eventType, handler, false)
    } : function (eventType, rootEventNode, aNodeSelectors, intoSelectorsCallback, outSelectorsCallback) {
        var handler = (function (obj) {
            return function () {
                var e = window.event;
                obj.watcher(e.srcElement || document, aNodeSelectors, intoSelectorsCallback, outSelectorsCallback, e) === false && (e.returnValue = false)
            }
        })(this);
        rootEventNode.attachEvent('on' + eventType, handler)
    }
};

var last_selector = null;
var click_event = false;

function getEl(id) {
    return document.getElementById(id);
}

function styleEl(id) {
    if (el = getEl(id)) {
        return el.style;
    }
    else return false;
}

function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }
    curleft = (curleft < -800) ? curleft + 945 : curleft;
    return {
        "left": curleft,
        "top": curtop
    };
}

function findSize(objId) {
    var obj = getEl(objId);
    return {
        "width": obj.offsetWidth,
        "height": obj.offsetHeight
    };
}


function popup(popupId, boxObj, x, y) {
    if (styleEl(popupId).visibility == 'hidden') {
        if (last_selector != null) hide();
        click_event = false;
        var posObj = findPos(boxObj);
        styleEl(popupId).top = y + posObj.top + 'px';
        styleEl(popupId).left = x + posObj.left + 'px';
        styleEl(popupId).visibility = 'visible';
        last_selector = popupId;
    }
    else {
        styleEl(popupId).visibility = 'hidden';
        last_selector = null;
    }
}


function showHide(id) {
    if (styleEl(id).display == 'none') {
        if (last_selector != null) hide();
        click_event = false;
        styleEl(id).display = 'block';
        last_selector = id;
    }
    else {
        styleEl(id).display = 'none';
        last_selector = null;
    }
}

function hide() {
    if (click_event == true) {
        if (styleEl(last_selector).visibility == 'visible') styleEl(last_selector).visibility = 'hidden';
        else styleEl(last_selector).display = 'none';
        last_selector = null;
    }
    else click_event = true;
}



function showBox(elementId) {
    styleEl(elementId).display = 'block';
}

function hideBox(elementId) {
    styleEl(elementId).display = 'none';

}

function isUrl(s) {
    var regexp = /([a-z0-9]+)\.([a-z0-9\-]+)\.[a-z]/
    return regexp.test(s);
}

function toggle(id) {
	var lar = document.getElementById(id);
	if (lar && lar.style){
		lar.style.display = (lar.style.display == '')? 'none':'';
	}
}

function begunFlashClick() {if(typeof(window.begunBannerClickURL) != 'undefined') window.location.href = window.begunBannerClickURL;}
function R(ths,p) {_i=new Image();prefix=p.split('&');if(prefix[2].split('=')[1].indexOf('begun')==-1)url=ths.href;else if(prefix[2].split('=')[1].indexOf('begunT')!=-1)if(isUrl(BegunA.begunBanners[BegunA.TopB[prefix[3].split('=')[1]-1]][4].split(' -')[0]))url=BegunA.begunBanners[BegunA.TopB[prefix[3].split('=')[1]-1]][4].split(' -')[0];else url='no_domain';else if(prefix[2].split('=')[1].indexOf('begunR')!=-1)if(isUrl(BegunA.begunBanners[BegunA.BottomB[prefix[3].split('=')[1]-1]][4].split(' -')[0]))url=BegunA.begunBanners[BegunA.BottomB[prefix[3].split('=')[1]-1]][4].split(' -')[0];else url='no_domain';else if(prefix[2].split('=')[1].indexOf('begunBm')!=-1)if(isUrl(BegunA.begunBanners[BegunA.FooterB[prefix[3].split('=')[1]-1]][4].split(' -')[0]))url=BegunA.begunBanners[BegunA.FooterB[prefix[3].split('=')[1]-1]][4].split(' -')[0];else url='no_domain';_i.src='http://www.rambler.ru/cl?novaclk&rex='+uniq+p+'&rnd='+Math.random()+'&_URL='+url;}
new function(){var sendStat=function(block,id,url){new Image().src='http://www.rambler.ru/cl?novaclk&rex='+uniq+'&block='+block+'&id='+id+'&rnd='+Math.random()+'&_URL='+url},getClass=function(oNode,prefix){if(typeof oNode=='undefined'||typeof oNode.className=='undefined')return'';var i,cnames=oNode.className.split(' ');for(i=cnames.length;i--;)if(cnames[i].indexOf(prefix)==0)return cnames[i].slice(prefix.length);return''};aNodeSelectors=[function(oNode){return oNode.nodeName=='A'&&oNode.href.split('#')[0]!=document.location.href.split('#')[0]&&getClass(oNode,'n_').length>0},function(oNode){return oNode.nodeType==1&&getClass(oNode,'b_').length>0}];function intoCallback(topNode,watchedNode,e){if(e.button==2)return;sendStat(getClass(topNode,'b_'),getClass(watchedNode,'n_'),watchedNode.href);return true}new ra.Event('click',aNodeSelectors,intoCallback);if(window.opera){new ra.Event('mousedown',aNodeSelectors,function(){var a=arguments;return a[2].button==0||intoCallback.apply(this,a)})}};

