function in_array(needle, haystack, strict) {
    var found = false, key, strict = !!strict;
    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
    }
    return found;
}

Array.prototype.observe=function(ev, func){
	this.each(function(obj){obj.observe(ev, func);});
	return this;
}
Array.prototype.stopObserving=function(ev){
	this.each(function(obj){obj.stopObserving(ev);});
	return this;
}
Array.prototype.addClassName=function(ev, func){
	this.each(function(obj){obj.addClassName(ev, func);});
	return this;
}
Array.prototype.removeClassName=function(ev, func){
	this.each(function(obj){obj.removeClassName(ev, func);});
	return this;
}

String.prototype.trim=function(){
 return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function simplePreloadIMG(name){
 var img=new Image();
 img.src=name;
}

var fixPNG=function(){
 if(/MSIE (5\.5|6).+Win/.test(navigator.userAgent)) return function(element, force){
  var src;
  if (element.tagName=='IMG'){
   if (/\.png$/.test(element.src)||force>0){
    src = element.src;
    element.src = "/images/0.gif";
   }
  }
  else {
   src = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i)
   if (src){
    src = src[1];
    element.runtimeStyle.backgroundImage="none";
   }
   else if(force>0){
    src = element.currentStyle.backgroundImage.match(/url\("(.+)"\)/i);
//    alert(src);
    src = src[1];
    element.runtimeStyle.backgroundImage="none";
   }
  }
  if(src) element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='no-repeat')";
 }
 else return function(){
  //do nothing
 }
}();

function fixAllPNG(){
	if (/MSIE (5\.5|6|7).+Win/.test(navigator.userAgent)){
		$$('img').each(function(s){fixPNG(s);});
		$$('div').each(function(s){fixPNG(s);});
		$$('img[class~=fixpng]').each(function(s){fixPNG(s, 1);});
		$$('div[class~=fixpng]').each(function(s){fixPNG(s, 1);});
	}
}

function Semaphor(){
 this.timeout=0;
 this.timer=0;
 this.flag=false;
 this.unblock=function(){this.flag=false;clearTimeout(this.timer);};
 this.block=function(mtimeout){if(this.flag==false){this.flag=true;tempvar=this;this.timer=setTimeout(function(){tempvar.unblock();}, mtimeout?mtimeout:tempvar.timeout)}};
 this.blocked=function(){return this.flag;};
}

function setCookie(c_name,value,expiredays){
 var exdate=new Date();
 exdate.setDate(exdate.getDate()+expiredays);
 document.cookie=c_name+ '=' +escape(value)+((expiredays==null) ? '' : ';expires='+exdate.toGMTString()+';path=/');
}

function getCookie(name){
 var cookies=document.cookie.split(";");
 for(var i=0;i<cookies.length;i++){
  var params=cookies[i].split("=");
  if(params[0].trim()==name){
   return params[1];
  }
 }
 return false;
}

function hide_block(sub,last_upd){
 block = 'sd'+ sub;
 setCookie(block,last_upd,36500);
 document.getElementById(block).style.visibility ='hidden';
 document.getElementById(block).innerHTML ='';
}

function noaction(e){
 var e=e||window.event;
 if(typeof e!='undefined'){
  Event.extend(e);
  e.stop();
 }
 return false;
}

function html2entities(str){
 var re=/[(<>"'&]/g;
 str=str.replace(re, function(m){return replacechar(m)});
 return str;
}

function replacechar(match){
if (match=="<") return "&lt;"
else if (match==">") return "&gt;"
else if (match=="\"") return "&quot;"
else if (match=="'") return "&#039;"
else if (match=="&") return "&amp;"
}

function entities2html(str){
 var re=/&(lt|gt|quot|#039|amp);/g;
 str=str.replace(re, function(m){return replaceentity(m)});
 return str;
}

function replaceentity(match){
if (match=="&lt;") return "<";
else if (match=="&gt;") return ">";
else if (match=="&quot;") return "\"";
else if (match=="&#039;") return "'";
else if (match=="&amp;") return "&";
}

//загрузка файлов с помощью iFrame
function createIFrame() {
  var id = 'f' + Math.floor(Math.random() * 99999);
  var div = document.createElement('div');
//  div.innerHTML = '<iframe style="display:none" src="about:blank" id="'+id+'" name="'+id+'" onload="sendComplete(\''+id+'\')"></iframe>';
  div.innerHTML = '<iframe style="display:none" src="about:blank" id="'+id+'" name="'+id+'"></iframe>';

  document.body.appendChild(div);
  Event.observe($(id), 'load', function(){
   sendComplete(id);
  });
  return document.getElementById(id);
}

function sendForm(form, url, func, args) {
  if (!document.createElement) return; // not supported
  if (typeof(form)=="string") form=document.getElementById(form);
  var frame=createIFrame();

  var old_target=form.getAttribute('target');
  var old_action=form.getAttribute('action');

  args[args.length]=frame.id;

  frame.onSendComplete = function(){
   form.setAttribute('target', old_target);
   form.setAttribute('action', old_action);
   func(args, getIFrameXML(frame));
  };
//add iframe_id as get parametr to url
  url=url+"?iframe_id="+frame.id;
  form.setAttribute('target', frame.id);
  form.setAttribute('action', url);
  form.submit();
}

function sendComplete(id) {
  var iframe=document.getElementById(id);
  if (iframe.onSendComplete && typeof(iframe.onSendComplete) == 'function')
    iframe.onSendComplete();
}

function getIFrameXML(iframe) {
  var doc=iframe.contentDocument;
  if (!doc && iframe.contentWindow) doc=iframe.contentWindow.document;
  if (!doc) doc=window.frames[iframe.id].document;
  if (!doc) return null;
  if (doc.location=="about:blank") return null;
  if (doc.XMLDocument) doc=doc.XMLDocument;

  return doc;
}

function get_list_class(obj, sub, cc, params){
	var url='/scripts/ajax_get_list_class.php';
	var obj=obj;
	var cc=cc||0;
	var params=params||'';

	if(typeof cache_subs[sub]=='undefined'){
		new Ajax.Request(url, {
			parameters: {sub: sub, cc: cc, AUTH_USER_ID: AUTH_USER_ID, params:params},
			onSuccess: function(transport){
				cache_subs[sub]=transport.responseText;
				$(obj).innerHTML=cache_subs[sub];
			}
		});
	}
	else {
		$(obj).innerHTML=cache_subs[sub];
	}
}

Event.observe(window, 'load', function(){
 fixAllPNG();
//overlib links to images

 $$('a[rel=overbox]').each(function(s){
  var img=new Image();
  img.src=s.href;

  Event.observe(s, 'mouseover', function(){
   return overlib('<img src="'+s.href+'" border="0">');
  });
  Event.observe(s, 'mouseout', function(){return nd();});
 });
});

//Favorites Menu
function findPos( obj ){
    if( !obj ) obj = document.getElementById( obj );
    if( !obj ) return [0,0];
	var curleft=0;
	var curtop=0;
	if(obj.offsetParent){
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while(obj = obj.offsetParent){
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [parseInt(curleft,10),parseInt(curtop,10)];
}

var jsft = (document.title != '' ? document.title : 'Себедом'); 
var jsf = new Array(
    '0|ВКонтакте|http://vkontakte.ru/share.php?title=' + encodeURIComponent(jsft) + '&url=',
    '5|LiveJournal|http://www.livejournal.com/update.bml?subject=' + encodeURIComponent(jsft) + '&event=',
    '9|Яндекс.Закладки|http://zakladki.yandex.ru/userarea/links/addfromfav.asp?bAddLink_x=1&lname=' + encodeURIComponent(jsft) + '&lurl=',
    '8|Закладки Google|http://www.google.com/bookmarks/mark?op=add&title=' + encodeURIComponent(jsft) + '&bkmk=',
    '4|Мой Мир|http://connect.mail.ru/share?share_url=',
    '6|Memori|http://memori.ru/link/?sm=1&u_data[name]=' + encodeURIComponent(jsft) + '&u_data[url]=',
    '10|Mister Wong|http://www.mister-wong.ru/index.php?action=addurl&bm_description=' + encodeURIComponent(jsft) + '&bm_url=',
    '7|БобрДобр|http://bobrdobr.ru/addext.html?title=' + encodeURIComponent(jsft) + '&url=',
    '1|Facebook|http://www.facebook.com/sharer.php?u=',
    '2|Twitter|http://twitter.com/home?status=' + encodeURIComponent(jsft) + ' ',
    '11|Delicious|http://del.icio.us/post?v=4&noui&jump=close&title=' + encodeURIComponent(jsft) + '&url=',
    '3|FriendFeed|http://friendfeed.com/?title=' + encodeURIComponent(jsft) + '&url='
);

function jump_favo( i ){
    if($( 'div_favo' )) $( 'div_favo' ).remove();
    if ( i == 's' ) {
        if ( Prototype.Browser.IE && window.external ) window.external.AddFavorite( location.href, jsft);
            else if ( window.sidebar ) window.sidebar.addPanel( jsft, location.href, '' );
    } else {
        i = jsf[ i ].split( '|' );
        window.open( i[ 2 ] + encodeURIComponent( location.href ), 'favo' );
    }
}

var jsfp = null;
function hide_favo() { if($( 'div_favo' )) $( 'div_favo' ).remove(); }
function to_favo(o){
    if ( !o ) return false;
	var t = new Element('div', {'id': 'div_favo', 'class': 'box-r4 box-shadow', 'style': 'display:none'});
	document.body.appendChild(t);
	t=$("div_favo");
	t.observe("mouseout", function(){jsfp=setTimeout('hide_favo()',800);});
	t.observe("mouseover", function(){clearTimeout( jsfp );});
   
    var c = '<p class="fvt">Добавить в:</p>';
    c += '<table class="fvp">';
    var p = ' onmouseover="fvc(1,\'b\')" onmouseout="fvc(0,\'b\')" ';//IE7 workaround;
    c += '<tr onmouseover="clearTimeout( jsfp );" class="fv_tr" onclick="jump_favo(\'s\')"><td ' + p + ' id="fv_t1_b" width="30px"><div class="fvb">&nbsp;</td><td id="fv_t2_b" ' + p + ' class="pr15">';
    if( Prototype.Browser.IE ) c += 'Избранное'; else c += 'Закладки';
    c += '</td></tr>';
    for ( var i = 0; i < jsf.length; i++ ) {
        var v = jsf[ i ].split( '|' );
        p = ' onmouseover="fvc(1,\''+i+'\')" onmouseout="fvc(0,\''+i+'\')" ';//IE7 workaround;
        c += '<tr onmouseover="clearTimeout( jsfp );" class="fv_tr" onclick="jump_favo(\'' + i + '\')"><td' + p + ' id="fv_t1_' + i + '"><div class="fv' + v[ 0 ] + '">&nbsp;</td><td' + p + 'class="pr15" id="fv_t2_' + i + '">' + v[ 1 ] + '</td></tr>';
    }
    c += '</table>';
    
    t.innerHTML=c;
    c = findPos( o );
    t.style.top=c[ 1 ] + 'px';
    t.style.left=(c[ 0 ]-50) + 'px';
    t.show();
}

function fvc( p, i ){//IE7 workaround;
    if( p == 1 ) {
        $( 'fv_t1_' + i ).addClassName( "cc-hv" );
        $( 'fv_t2_' + i ).addClassName( "cc-hv" );
    } else {
        $( 'fv_t1_' + i ).removeClassName( "cc-hv" );
        $( 'fv_t2_' + i ).removeClassName( "cc-hv" );
    }
}

function number_format( number, decimals, dec_point, thousands_sep ) {	// Format a number with grouped thousands
	// 
	// +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
	// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +	 bugfix by: Michael White (http://crestidg.com)

	var i, j, kw, kd, km;

	// input sanitation & defaults
	if( isNaN(decimals = Math.abs(decimals)) ){
		decimals = 2;
	}
	if( dec_point == undefined ){
		dec_point = ",";
	}
	if( thousands_sep == undefined ){
		thousands_sep = ".";
	}

	i = parseInt(number = (+number || 0).toFixed(decimals)) + "";

	if( (j = i.length) > 3 ){
		j = j % 3;
	} else{
		j = 0;
	}

	km = (j ? i.substr(0, j) + thousands_sep : "");
	kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep);
	//kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).slice(2) : "");
	kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : "");


	return km + kw + kd;
}

