function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    };
  }
}

function getXmlHttp(){
  var xmlhttp;
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      xmlhttp = false;
    }
  }

  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
  }

  return xmlhttp;
}

function clear_field(obj, default_value) {
	if (obj.value == default_value) { obj.value = ''; }
}

function showMessageForm(txt)
{
    var top = Math.round((document.documentElement.clientHeight - $('send_message').getDimensions().height)/2) + document.documentElement.scrollTop
    $('send_message').setStyle({top:top+'px'})
    $('loader').setStyle({top:top+'px'})
	showLoader($('send_message'));
  	$('send_message').innerHtml = '';
  	new Ajax.Request('/main/new_message', {method:'get', onComplete: function(trasport) {        
        $('subject').value = 'Ошибка на странице ' + document.location.href;
        $('message').value = txt;
		hideLoader($('send_message'));
	} });
}

function showLoader(div) {
    div.hide();
    $('loader').show();
}

function hideLoader(div) {
    $('loader').hide();
    div.show();
}

function show_tip (event, element) {
    if (!Prototype.Browser.IE) {
        $('tip').update(element.alt.replace('\n', '<br />'));
        $('tip').setStyle({top:(Event.pointerY(event)+10)+'px'});
        $('tip').setStyle({left:Event.pointerX(event)+'px'});
        $('tip').show();
    }
}

function hide_tip (event, element) {
    $('tip').hide();
}

function getSelectedText() {
	if (Prototype.Browser.IE) {
		var sel = document.selection;
		if ("Text" == sel.type) {
			var range = sel.createRange();
			var txt = range.text;
		} else {
            var txt = '';
		}
	}
	else {
		sel = window.getSelection();
		if (sel.rangeCount > 0) {
			var range = sel.getRangeAt(0);
			var txt = range.toString();
		} else {
			var txt = '';
		}
	}

	if (txt.length > 500)
		alert("Выделено слишком много текста!");
	else {
		showMessageForm(txt);
	}
}
function send_vote(submit_vote_id, value) {
    $('submit_'+submit_vote_id).ancestors()[0].getInputs('hidden', 'value')[0].setValue(value)
    $('submit_'+submit_vote_id).click()
}

function paint_over(vote_id, stars_class, value) {
    $$('#' + vote_id + ' .' + stars_class).each (function(e){e.removeClassName(stars_class + '_red');});
    for (var i=0; i < value; i++) {
        $$('#' + vote_id + ' .' + stars_class)[i].addClassName(stars_class + '_red');
    }
}

function paint_off(vote_id, stars_class) {
    $$('#' + vote_id + ' .' + stars_class).each (function(e){e.removeClassName(stars_class + '_red');});
}

function paint_back(vote_id, stars_class, value) {
    for (var i=0; i < value; i++) {
        $$('#' + vote_id + ' .' + stars_class)[i].addClassName(stars_class + '_red');
    }
}

function show_tab(tab, div) {
    $$('.tab_block').each(function(e) { e.hide() })
    div.show()
    switch_tab(tab)
}

function switch_tab(tab) {
    Element.ancestors(tab)[2].select('li').each(function(e) { Element.removeClassName(e, 'active'); })
    Element.addClassName(Element.ancestors(tab)[1], 'active');
}

//document.observe('dom:loaded', function() {
//    document.observe('keyup', function(e){ if (e.keyCode == Event.KEY_RETURN && e.ctrlKey) getSelectedText(); });
//})


function show_modal_box(box) {
    if (!box) { box = $('modal_box')}
    top = Math.round((document.documentElement.clientHeight - box.getDimensions().height)/2) + document.documentElement.scrollTop
    box.setStyle({top:top+'px'})
    old_width = document.documentElement.clientWidth
    $$('HTML')[0].setStyle({overflow: 'hidden'});
    $$('HTML')[0].setStyle({marginRight: (document.documentElement.clientWidth - old_width) + 'px'})
    $('overlay').setStyle({top: document.documentElement.scrollTop + 'px'})
    $('overlay').show();
    box.show();
}

function show_unabusive_box(box) {
    if(!box) box = $('modal_box');
    new_top = Math.round((document.documentElement.clientHeight - box.getDimensions().height)/2) + document.viewport.getScrollOffsets()[1];
    old_width = document.documentElement.clientWidth;
    $('overlay').setStyle({top: document.viewport.getScrollOffsets()[1] + 'px'});
    $('overlay').show();
    box.setStyle({top:new_top+'px'});
    box.show();
}

function hide_unabusive_box() {
    $('overlay').hide();
    $$('.modal_box').each(function(e) { e.hide() });
}

function hide_modal_box() {
    $$('HTML')[0].setStyle({overflow: 'auto'});
    $$('HTML')[0].setStyle({marginRight: '0px'})
    $('overlay').hide();
    $$('.modal_box').each(function(e) { e.hide() });
}

function getURLParam(strParamName){
    var strReturn = "";
    var strHref = window.location.href;
    if ( strHref.indexOf("?") > -1 ){
        var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
        var aQueryString = strQueryString.split("&");
        for (var iParam = 0; iParam < aQueryString.length; iParam++){
            if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1) {
                var aParam = aQueryString[iParam].split("=");
                strReturn = aParam[1];
                break;
            }
        }
    }
    return unescape(strReturn);
}

  function showPreview(path,locale)
  {
      new Ajax.Request('/books/preview', {asynchronous:true, evalScripts:true, method:'get', parameters:{path: path, locale: locale}});
  }

