/*
	Copyright © Eleanor CMS
	URL: http://eleanor-cms.ru, http://eleanor-cms.com
	E-mail: support@eleanor-cms.ru
	Developing: Alexander Sunvas*
	Interface: Rumin Sergey
	=====
	*Pseudonym. See addons/copyrights/info.txt for more information.
*/

//Если сайт локальный и доступа к гуглю нет...
//Добавим jQuery
if(!Array.indexOf)
{
	Array.prototype.indexOf=function(obj)
	{
		for(var i=0; i<this.length; i++)
			if(this[i]==obj)
				return i;
		return -1;
	}
}
var not_loading,module_id,loading_id='loading';
function Trim(str){return $.trim(str)}
function Ajax(module,arr,func,cache,err)
{
	if(typeof JsHttpRequest=='undefined')
	{
		alert('JsHttpRequest error!');
		return;
	}
	if(typeof arr['module']=='undefined')
		arr['module']=module ? eval(module) : module_id;
	ShowLoad();
	JsHttpRequest.query(
						'ajax.php',
						arr,
						function(result,errors)
						{							HideLoad();							if (errors || result['status']=='error')
							{
								alert(errors || result['answer']);
								if(err)
									err();
							}
							else
							{								func(result);
								if (result['eval'])
									eval(result['eval']);
							}
						},
						!cache
	);
	return false;
}

function ShowLoad()
{	var load=$('#'+loading_id);
	if (!load || not_loading)
		return;
	var w=$(document).width();
	var h=$(document).height();
	load.css('left',w/2-100+"px").css('top',h/2-25+"px").show();
}

function HideLoad()
{	var load=Id(loading_id);
	if (not_loading || !load)
		return;
	$(load).hide();
}

function JumpToPage(links,pages,onclick)
{	var s=prompt(el_lang['page_jump'],'');
	if (!s)
		return false;
	pages=parseInt(pages);
	if (isNaN(s) || (s=parseInt(s))<=0)
		return false;
	if (s>pages)
		s=pages;
	if (onclick)
		eval(onclick.replace('{page}',s));
	else
		window.location=links.replace('{page}',s);
	return false;}

function SetCookie(name,value,ctime)
{	var data=new Date();
	data.setTime(data.getTime()+(ctime ? ctime : cookie_time)*1000);
	document.cookie=cookie_prefix+name+"="+escape(value)+';expires='+data.toGMTString()+";domain="+cookie_domain+";path="+cookie_path;
}

function GetCookie(name)
{
	var res;
	if(res=document.cookie.match(new RegExp(cookie_prefix+name+"=([^;]+)","i")))
		return res[1].split("=")[1];
	return false;
}

function Id(id)
{	return document.getElementById(id);}

function CheckGroup(obj)
{	var els=obj.getElementsByTagName('input');
	var can=false;
	for(var i=0;i<els.length;i++)
		if (els[i].getAttribute('type')=='checkbox' && els[i].checked)
		{
			can=true;
			break;
		}
	if (!can)
		alert(lang['nothid_selected']);
	return can;
}

function One2AllCheckboxes(parent,main_ch,names)
{
	parent=Id(parent);
	if (!parent)
		return;
	var checks=parent.getElementsByTagName('input');
	for (var i=0;i<checks.length;i++)
	{
		var check=checks[i];
		if (check.getAttribute("type")!="checkbox" || (names && check.getAttribute("name")!=names))
			continue;
		check.marked=true;
		if(typeof check.onclick!='function')
		{
			check.onclick=function()
			{
				MassClick();
				return true;
			}
		}
	}
	var main=Id(main_ch);
	if (!main)
		return;
	main.onclick=function()
	{
		MainClick();
		return true;
	}
	function MassClick()
	{
		var checked=true;
		for (var i=0;i<checks.length;i++)
		{
			var check=checks[i];
			if (!check.marked)
				continue;
			checked&=check.checked;
		}
		main.checked=checked;
	}
	function MainClick()
	{
		for (var i=0;i<checks.length;i++)
		{
			var check=checks[i];
			if (!check.marked)
				continue;
			check.checked=main.checked;
		}
	}
}

function LinkTo(s,ret)
{	s=(window.location.href.indexOf('#')==-1 ? window.location.href : window.location.href.substring(0,window.location.href.indexOf('#')))+s;	if (ret)
		return s;
	prompt('Copy & paste:',s);
	return false;}

function AddStyle(src,media)
{	if(!media)
		media='screen';
	var link=document.createElement("link");
	link.rel="stylesheet";
	link.type="text/css";
	link.href=src;
	link.media=media;
	document.getElementsByTagName("head")[0].appendChild(link);}
var eleanor_dd_hide=[];
function DropDown(sel,pos,event,hl)
{	pos=pos.split("-");
	sel=$(sel);
	var showed=false;
	if(typeof hl=="undefined")		hl='#'+sel.attr('rel');
	hl=$(hl);
	var TopLeft=function()
	{		for(var i in eleanor_dd_hide)
			eleanor_dd_hide[i]();		hl.css('left',-10000).css('top',-10000).show();
		var h=hl.outerHeight();
		var w=hl.outerWidth();
		hl.hide();		hl.css('left',pos[0]=='left' ? sel.position().left+sel.outerWidth()-w : sel.position().left).css('top',pos[1]=='top' ? sel.position().top-h : sel.position().top+sel.height()+'px');
	}
	switch(event)
	{		case 'mouseover':
			sel.mouseover(function(){				TopLeft();
				hl.fadeIn('fast');
				showed=true;
			}).mouseout(function(){				hl.fadeOut('fast');
				showed=false;			});
		break;		case 'click':
		default:
			sel.click(function(){				if(showed)
					hl.fadeOut('fast');
				else
				{
					TopLeft();
					hl.fadeIn('fast');
				}
				showed=!showed;
				return false;			});
		break;	}
	this.Hide=function(){		if(showed)			hl.fadeOut('fast');
		showed=false;
		return false;
	}
	setTimeout(function(){
		$(document).click(function(e){			if(!showed)
				return;
			e=e||window.event;
			var target=e.target||e.srcElement;
			while(target)
			{
				if(target==hl.get(0))
					return;
				target=target.parentNode;
			}
			hl.hide();
			showed=false;
		});
	},100);
	eleanor_dd_hide.push(this.Hide);
	if(pos[2]=="now")
	{		TopLeft();
		hl.fadeIn('fast');
		showed=true;
	}

}

/*
	Функций уменьшения картинок
	obj - объект в котором надо производить уменьшение картинок.
	width - граничная ширина после которой картинка считается слишком большой и урезается.
	percent - процент от width, до которого ужмется изображение
*/
function ResizeBigImages(obj,width,percent)
{
	obj=$(obj ? obj : document);
	if (!percent)
		percent=30;
	percent/=100;
	if(!width)
	{
		width=obj.width();
		var sw=screen.width*0.95;//Может ширина экрана меньше, чем контейнера.. Возьмем 95%
		if(sw<width)
			width=sw;
	}
	var must_width=width*percent;//Максимальная ширина, которая должна быть у картинки
	var padding=2;
	var ResizeFunc=function(img)
	{
		if(img.resized || img.width<=must_width)
			return;
		var r_percent=Math.ceil(must_width/img.width*100);
		var r_width=img.width;
		var r_height=img.height;
		$(img).width(must_width+'px');
		img.resized=true;
		var image=$(img.parentNode.nodeName=='A' ? img.parentNode : img);
		var div=$('<div>').html('<img src="images/img-resized.png" style="vertical-align:middle" alt="" />&nbsp; <b>'+r_percent+'%</b> [ '+r_width+' x '+r_height+' ] &raquo;&raquo; [ '+img.width+' x '+img.height+' ]')
		.width(img.width-(padding*2)+'px').addClass('resized-image-header').css('padding',padding+'px').css('cursor','pointer').click(function(){window.open(img.src)})
		var div2=$('<div>').addClass('posted-image').append(image.clone()).append(div);
		$(img).replaceWith(div2);
	}
	if(must_width>0)
		obj.find('img').each(function(){
			if(!this.resized && this.className=='posted-image')
			{				if($.browser.opera || $.browser.mozilla)
					ResizeFunc(this);
				$(this).load(function(){					ResizeFunc(this);				})
			}
	})
}

var EDITOR=
{	active:null,
	editors:[],
	Insert:function(text,id)
	{		if (!id)
			id=this.active;
		if (id && typeof this.editors[id]=='object' && typeof this.editors[id]['insert']=='function')
			return this.editors[id]['insert'](text);
		else
			return false;	},
	GetValue:function(id)
	{		if (!id)
			id=this.active;
		if (id && typeof this.editors[id]=='object' && typeof this.editors[id]['getvalue']=='function')
			return this.editors[id]['getvalue']();
		else
			return false;	},
	SetValue:function(text,id)
	{		if (!id)
			id=this.active;
		if (id && typeof this.editors[id]=='object' && typeof this.editors[id]['setvalue']=='function')
			return this.editors[id]['setvalue'](text);
		else
			return false;	},
	ReplaceSelection:function(pre,after,textfunc,id)
	{		if (!id)
			id=this.active;
		if (id && typeof this.editors[id]=='object')
			return typeof this.editors[id]['selection']=='function' ? this.editors[id]['selection'](pre,after,textfunc) : this.Insert(pre+after,id);
		else
			return false;	},
//Служебные функции
	NewEditor:function(id,insert_cb,setvalue_cb,getvalue_cb,insert_selection_cb)
	{		if (!id || typeof insert_cb!='function')
			return false;
		this.editors[id]={							'insert':insert_cb,
							'setvalue':setvalue_cb,
							'getvalue':getvalue_cb,
							'selection':insert_selection_cb
		};
		this.active=id;
		return true;	},
	RemoveEditor:function(id)
	{		if (!id)
			id=this.active;
		this.editors[id]=null;	},
	ActivateEditor:function(id)
	{		if (id && typeof this.editors[id]=='object')
			this.active=id;	}}
//Уменьшаем изображения до уровня страницы:
$(function(){	ResizeBigImages("#resizedimage",0,75)
});
