/*
	Easing Equations
*/
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('6.J=9(t,b,c,d){8 c*t/d+b};6.R=9(t,b,c,d){8 c*(t/=d)*t+b};6.M=9(t,b,c,d){8-c*(t/=d)*(t-2)+b};6.F=9(t,b,c,d){e((t/=d/2)<1)8 c/2*t*t+b;8-c/2*((--t)*(t-2)-1)+b};6.14=9(t,b,c,d){8 c*(t/=d)*t*t+b};6.Z=9(t,b,c,d){8 c*((t=t/d-1)*t*t+1)+b};6.W=9(t,b,c,d){e((t/=d/2)<1)8 c/2*t*t*t+b;8 c/2*((t-=2)*t*t+2)+b};6.T=9(t,b,c,d){8 c*(t/=d)*t*t*t+b};6.Q=9(t,b,c,d){8-c*((t=t/d-1)*t*t*t-1)+b};6.N=9(t,b,c,d){e((t/=d/2)<1)8 c/2*t*t*t*t+b;8-c/2*((t-=2)*t*t*t-2)+b};6.L=9(t,b,c,d){8 c*(t/=d)*t*t*t*t+b};6.K=9(t,b,c,d){8 c*((t=t/d-1)*t*t*t*t+1)+b};6.I=9(t,b,c,d){e((t/=d/2)<1)8 c/2*t*t*t*t*t+b;8 c/2*((t-=2)*t*t*t*t+2)+b};6.H=9(t,b,c,d){8-c*6.w(t/d*(6.f/2))+c+b};6.C=9(t,b,c,d){8 c*6.k(t/d*(6.f/2))+b};6.z=9(t,b,c,d){8-c/2*(6.w(6.f*t/d)-1)+b};6.y=9(t,b,c,d){8(t==0)?b:c*6.g(2,10*(t/d-1))+b};6.X=9(t,b,c,d){8(t==d)?b+c:c*(-6.g(2,-10*t/d)+1)+b};6.V=9(t,b,c,d){e(t==0)8 b;e(t==d)8 b+c;e((t/=d/2)<1)8 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*(t-=(1.5/2.h))*t+.h)+b}j e(t<(2.5/2.h)){8 c*(7.m*(t-=(2.13/2.h))*t+.12)+b}j{8 c*(7.m*(t-=(2.11/2.h))*t+.Y)+b}};6.15=9(t,b,c,d){e(t<d/2)8 6.v(t*2,0,c,d)*.5+b;8 6.u(t*2-d,0,c,d)*.5+c*.5+b};',62,68,'||||||Math||return|function|||||if|PI|pow|75|var|else|sin|sqrt|5625|70158|abs||asin|undefined|||easeOutBounce|easeInBounce|cos|525|easeInExpo|easeInOutSine|easeInOutBack|easeOutBack|easeOutSine|easeInBack|easeInOutElastic|easeInOutQuad|easeOutElastic|easeInSine|easeInOutQuint|linearTween|easeOutQuint|easeInQuint|easeOutQuad|easeInOutQuart|easeInElastic|easeInOutCirc|easeOutQuart|easeInQuad|easeOutCirc|easeInQuart|easeInCirc|easeInOutExpo|easeInOutCubic|easeOutExpo|984375|easeOutCubic||625|9375|25|easeInCubic|easeInOutBounce'.split('|'),0,{}));


eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('(5($){$.V.x({t:5(f){2(!f.d)f.d=$.7.d++;2(!$.7.l)$.7.l=[];j 1.A(5(){2(1.4)j 1.4.n(f);8 1.4=[];1.4.n(f);c s=1;1.6=5(e){e=$.7.R(e||o.7);$.x(e,1.p||{});c a=0,m=P;2(e.u)a=e.u/M;2(e.H)a=-e.H/3;2(o.I)a=-e.u;r(c i=0;i<s.4.q;i++)2(s.4[i])2(s.4[i].U(s,e,a)===9){m=9;e.T();e.S()}j m};2($.k.h&&!1.b){1.b=5(e){1.p={F:e.F,E:e.E,D:e.D,C:e.C}};$(1).Q(\'B\',1.b)}2(1.g)2($.k.h)1.g(\'z\',1.6,9);8 1.g(\'t\',1.6,9);8 1.y=1.6;$.7.l.n($(1))})},v:5(f){j 1.A(5(){2(f&&1.4){r(c i=0;i<1.4.q;i++)2(1.4[i]&&1.4[i].d==f.d)O 1.4[i]}8{2($.k.h&&!1.b)$(1).N(\'B\',1.b);2(1.g)2($.k.h)1.w(\'z\',1.6,9);8 1.w(\'t\',1.6,9);8 1.y=G;1.4=1.6=1.b=1.p=G}})}});$(o).L(\'K\',5(){c a=$.7.l||[];r(c i=0;i<a.q;i++)a[i].v()})})(J);',58,58,'|this|if||_mwHandlers|function|_mwHandler|event|else|false||_mwFixCursorPos|var|guid|||addEventListener|mozilla||return|browser|_mwCache|returnValue|push|window|_mwCursorPos|length|for||mousewheel|wheelDelta|unmousewheel|removeEventListener|extend|onmousewheel|DOMMouseScroll|each|mousemove|clientY|clientX|pageY|pageX|null|detail|opera|jQuery|unload|one|120|unbind|delete|true|bind|fix|stopPropagation|preventDefault|call|fn'.split('|'),0,{}))


var AlbumsShow = {
	init : function(){
		var me = this;
		this.scroller = $('#albums_thumbs');

		if (!this.scroller.length){
			return false;
		}

		this.scroller_init();
		//this.scroll_mousewheel();
	},

	scroller_init : function(){
		this.selected = $('#albums_thumbs td.selected');

		if (!this.selected.length){
			return;
		}

		this.scroller[0].scrollLeft = parseInt(this.selected[0].offsetLeft) - 100;
	},


	scroll_mousewheel : function(){
		var me = this;
		var delta_plus = 1;
		if ($.browser.opera && $.browser.version > '9.2'){
			delta_plus = -1;
		}

		this.scroller.mousewheel(function(event, delta){
			delta = delta * delta_plus;
			if (delta > 0)
			{
				me.scroller[0].scrollLeft -= 50;
			}
			else if (delta < 0)
			{
				me.scroller[0].scrollLeft += 50;
			}
			return false;
		});
	}
};


var CodeToPaste = {

	init : function(){

		var me = this;
		this.is_open = false;
		this.bKeep = false;

		this.pasteBlock = $('#code-to-paste');
		this.codeLink = $('#code_link');
		this.codeBB = $('#code_bb');
		this.codeFile = $('#code_file');

		if (!this.pasteBlock.length)
			return;

		$('#code-to-paste * a.js-href')
			.click(function(evt){ return me.open(evt); })
			.mousedown(function(){ return false; });

		$('#code-to-paste textarea')
			.click(function(evt){ this.select(); })
			.focus(function(evt){ this.select(); });

		this.keepHandler = function(evt){ me.keep(evt); };
		this.documentClickHandler = function(evt){ me.hide(evt); };
		this.documentKeyDownHandler = function(evt){ me.cancel(evt); };

		this.pasteBlock
			.click(this.keepHandler)
			.keydown(this.keepHandler);

		$(document)
				.click(this.documentClickHandler)
				.keydown(this.documentKeyDownHandler);
	},

	uninit : function() {

		$('#code-to-paste * a.js-href')
			.unbind('click', function(evt){ return me.open(evt); })
			.unbind('mousedown', function(){ return false; });

		$('#code-to-paste textarea')
			.unbind('click', function(evt){ this.select(); })
			.unbind('focus', function(evt){ this.select(); });

		this.pasteBlock
			.unbind('click', this.keepHandler)
			.unbind('keydown', this.keepHandler);

		$(document)
			.unbind('mousedown', this.documentClickHandler)
			.unbind('keydown', this.documentKeyDownHandler);
	},

	open : function(evt) {
		if (!this.is_open)
		{
			this.pasteBlock.addClass('open');
			this.is_open = true;
		}
		else if (this.is_open)
		{
			this.bKeep = false;
			this.hide(evt);
		}
		return false;
	},

	keep : function(evt) {
		this.bKeep = true;
	},

	hide : function(evt) {
		if (this.is_open)
		{
			if (this.bKeep)
			{
				this.bKeep = false;
				return;
			}
			this.pasteBlock.removeClass('open');
			this.is_open = false;
		}
	},

	cancel : function(evt) {
		if (this.is_open)
		{
			if (window.event) {
				evt = window.event;
			}
			var code = evt.keyCode ? evt.keyCode : evt.which ? evt.which : null;
			if (code == 27) {
				this.hide(evt);
			}
		}
	}
};


var PhotoShow = {

	init : function()
	{
		var me = this;
		this.scroller = $('#photos_thumbs');


		if (!this.scroller.length)
			return false;

		this.first_init = true;

		this.thumbs = $('#photos_thumbs_content');
		this.thumbs_img = $('#photos_thumbs_content img');
		this.thumbs_td = $('#photos_thumbs td');

		this.photo_content = $('#pictures');

		this.filter_view_current = this.filter_view_default = 'thumbs_view_over';

		this.rFalse = function(){ return false; };

		this.photoLoadImage = $('#thumb_loading');
		this.PhotoLoadingHandle = function(evt){ me.photoLoadImage.css('display','none'); this.style.display = ''; };

		this.PhotoNextHandle = function(evt){ return me.thumbs_next_photo(this); };

		this.photo_rate = $('#photo_rate');
		this.photo_rate = this.photo_rate[0];

		this.selected_position = {
			next : 0,
			current : 0,
			dir: -1
		};

		$('#photos_thumbs a').click(this.rFalse).mousedown(this.rFalse);
		this.thumbs_td
			.each(function (i) {
				if (me.thumbs_img[i])
				{
					me.thumbs_img[i].ileft = this.offsetLeft;
					me.thumbs_img[i].i = this.i = i;
				}
			});

		this.thumbs_img
			.mouseover(function(evt){ return me.thumbs_show_over(this); })
			.mousedown(function(evt){ return me.thumbs_img_click(this); });

		this.scroller_init();
		//this.scroll_mousewheel();
		this.thumbs_view_init();

		this.thumbs_show_scroll();
		this.thumbs_show_init();

		CodeToPaste.init();

		this.scroller.scroll(function(evt){ me.thumbs_show_init(); });

		if (this.selected)
		{
			this.thumbs_img_click(this.thumbs_img[this.selected.i]);
		}

		this.first_init = false;

	},

	scroller_init : function()
	{
		if (!this.selected)
		{
			this.selected = $('#photos_thumbs td.selected');
			this.selected = this.selected[0];
		}

		if (!this.selected)
			return false;

		this.scroller[0].scrollLeft = parseInt(this.selected.offsetLeft) - 100;
	},

	scroll_mousewheel : function()
	{
		var me = this;
		var delta_plus = 1;
		if ($.browser.opera && $.browser.version > '9.2')
			delta_plus = -1;

		this.scroller.mousewheel(function(event, delta){
			delta = delta * delta_plus;
			if (delta > 0)
				me.scroller[0].scrollLeft -= 50;
			else if (delta < 0)
				me.scroller[0].scrollLeft += 50;
			return false;
		});
	},

	thumbs_img_click : function(oEvt)
	{
		if (!oEvt || (this.selected.i == oEvt.i && !this.first_init))
			return false;

		CodeToPaste.hide();

		var me = this;

		this.selected.className = '';
		this.selected = this.thumbs_td[oEvt.i];
		this.selected.className = 'selected';

		var current_photo = $('#v'+oEvt.i);
		if (current_photo.length)
		{
			current_photo.css('display','');
			if (this.first_init)
				current_photo.click(function(evt){ return me.thumbs_next_photo(this); });
		} else {
			this.photoLoadImage.css('display', 'block');
			this.photo_content.html('<img class="clickable" style="display: none;" id="v'+oEvt.i+'" src="'+ (ps_path+ps[oEvt.id].l.r ? ps[oEvt.id].l.f : ps_path+ps[oEvt.id].l.f) +'" width="'+ps[oEvt.id].l.w+'" height="'+ps[oEvt.id].l.h+'" />');
			current_photo = $('#v'+oEvt.i);
			current_photo
				.click(function(evt){ return me.thumbs_next_photo(this); })
				.load(this.PhotoLoadingHandle);
		}

		this.info_hires(oEvt.id);

		this.photo_rate.src = '/i/rate'+oEvt.id+'.gif';

		if (!this.first_init)
			this.thumbs_scroll();
	},

	thumbs_next_photo : function(oEvt){

		if (!oEvt && !oEvt.id)
			return false;

		var i = parseInt(oEvt.id.replace(/v/,''));
		var next = this.thumbs_img[i+1];
		if (!next)
			next = this.thumbs_img[0];

		this.thumbs_show_over(next);
		this.thumbs_img_click(next);

		return false;
	},

	thumbs_scroll : function()
	{
		if (!this.selected)
			return;

		var i = this.selected.i;
		var next = this.thumbs_td[i+1];
		var prev = this.thumbs_td[i-1];
		var iLeft = parseInt(this.selected.offsetLeft);

		this.selected_position.dir = 1;

		if (iLeft < parseInt(this.selected_position.current))
			this.selected_position.dir = -1;

		this.selected_position.current = iLeft;


		if (this.selected_position.dir == 1 && (i+1) == this.thumbs_td.length)
		{
			this.selected_position.next = iLeft;
		}
		else if (i == 0 || (this.selected_position.dir == 1 && !next) )
		{
			this.scroller[0].scrollLeft = this.selected_position.next = this.thumbs_td[0].offsetLeft;
		}
		else if (this.selected_position.dir == 1 && next)
		{
			this.selected_position.next = next.offsetLeft;
		}
		else if (this.selected_position.dir == -1 && !prev)
		{
			this.selected_position.next = this.thumbs_td[1].offsetLeft;
		}
		else
		{
			this.selected_position.next = prev.offsetLeft;
		}

		//return;
		this.thumbs_scroll_go();
	},

	thumbs_scroll_go : function(){

		var me = this;


		this.thumbs_scroll_duration = 7;
		this.thumbs_scroll_time = 0;
		this.thumbs_scroll_start = parseFloat(this.scroller[0].scrollLeft);
		this.thumbs_scroll_end = parseFloat(this.scroller[0].scrollLeft + (this.selected_position.next - this.selected_position.current));
		this.thumbs_scroll_change = this.thumbs_scroll_end - this.thumbs_scroll_start;

		this.thumbs_scroll_idPoll = setInterval(function(){ me.thumbs_scroll_poll() }, 30);

	},

	thumbs_scroll_poll : function(){
		if (this.thumbs_scroll_duration > this.thumbs_scroll_time) {
			this.thumbs_scroll_time++;
			this.scroller[0].scrollLeft = Math.easeOutQuad(this.thumbs_scroll_time, this.thumbs_scroll_start, this.thumbs_scroll_change, this.thumbs_scroll_duration, 50, 1.06);
		} else {
			clearInterval(this.thumbs_scroll_idPoll);
		}
	},

	thumbs_show_init : function(e)
	{
		switch (this.filter_view_current)
		{
			case 'thumbs_view_all':
				this.thumbs_show_all();
				break;
			case 'thumbs_view_scroll':
				this.thumbs_show_scroll();
				break;
			default:
				break;
		}
	},

	thumbs_show_over : function(e)
	{
		if (e.show) return;
		e.src = ps_path+ps[e.id].s.f;
		e.show = 1;
	},


	thumbs_show_all : function()
	{
		if (!this.thumbs_img)
			return false;
		for(var i = 0, length = this.thumbs_img.length; i < length; i++)
		{
			this.thumbs_show_over(this.thumbs_img[i]);
		}

	},


	thumbs_show_scroll : function(e)
	{
		if (!this.thumbs_img)
			return false;

		var start = this.scroller[0].scrollLeft;
		var end = start + this.scroller[0].offsetWidth;

		for(var i = 0, length = this.thumbs_img.length; i < length; i++)
		{
			if (this.thumbs_img[i].ileft >= start && this.thumbs_img[i].ileft <= end)
			{
				this.thumbs_show_over(this.thumbs_img[i]);
			}
		}

	},


	thumbs_view_init : function()
	{
		var me = this;

		this.filter_view = $('#thumbs_view');
		this.filter_view_filters = $('#thumbs_view dd');

		if (!this.filter_view_filters)
			return false;

		this.cookie_expires_default = '7';

		if ($.cookie('filter_view'))
		{
			this.filter_view_current = $.cookie('filter_view');
		}

		this.thumbs_view_set(this.filter_view_current);


		this.filter_view_filters
			.click(function(evt){ return me.thumbs_view_change(this); })
			.each(function (i) {
				if (this.id == me.filter_view_current)
				{
					this.className = 'selected';
					me.filter_view_selected = this;
				} else {
					this.className = '';
				}
			});

		this.filter_view.css('display','block');
		//alert($.cookie('filter_view'))
	},


	thumbs_view_change : function(oEvt)
	{
		this.filter_view_selected.className = '';
		oEvt.className = 'selected';
		this.filter_view_selected = oEvt;
		this.thumbs_view_set(oEvt.id);
		this.thumbs_show_init();

		return false;
	},


	thumbs_view_set : function(sValue)
	{
		this.filter_view_current = sValue;
		$.cookie('filter_view', sValue, { expires: this.cookie_expires_default });
	},


	status_bar : function(id){
		var text = '&nbsp;';
		if (!ps[id].h)
			return text;
		text = '<a href="'+ps[id].h.f+'" target="_blank"><i></i>Оригинал</a> ('+ps[id].h.w+'×'+ps[id].h.h+', '+this.filesize(ps[id].h.s)+')';

		return text;
	},


	filesize : function(size){
		for(var i = 0; size >= 1024; size /= 1024, i++);

		size = Math.round(size*10)/10;
		var str = ' КМГ';
	   	return size + ' ' + str.substr(i,1) + 'байт';
	},


	info_hires : function(id){
		$('#info_hires').html(this.status_bar(id));
		this.code_to_paste(id);
	},

	code_to_paste : function(id){
		var link = ps_domain + ps_person_path +'photo_id='+id;
		var image = (ps[id].l.r ? ps[id].l.f : ps[id].l.f);
		var title = ps_person_title;

		CodeToPaste.codeLink.attr("value", '<a href="'+link+'"><img src="'+image+'" width="'+ps[id].l.w+'" height="'+ps[id].l.h+'" title="'+title+'" alt="'+title+'" border="0"/></a><br/><a href="'+link+'">'+title+'</a> на <a href="'+ps_domain+'">Jpgs.ru</a>');
		CodeToPaste.codeBB.attr("value", '[url='+link+'][img]'+image+'[/img][/url]\n[url='+link+']'+title+'[/url] на [url='+ps_domain+']Jpgs.ru[/url]');
		CodeToPaste.codeFile.attr("value", image);
	},

	debug : function(str){
		$('#debug').append(str);
	}
};


PhotoShow.init();
AlbumsShow.init();