var Core=function() {
	/* wyszukiwarka */
	this.search = new function() {
		this.init = function() {
			$('.find form input[type=text]').each(function(a,b){
				eval("b.onfocus=function(){if(this.value=='"+b.value+"') this.value='';this.style.color='#000'};");
				eval("b.onblur=function(){if(!this.value){this.value='"+b.value+"';this.style.color='#bbb'}};");
			});
		}
		this.tab = function(v) {
			$('.find span a').each(function(a,b){b.className=b.rel==v?"sel":""});
			$('.find form').each(function(a,b){b.style.display="none"});
			$('#s_'+v).show();
			$('#s2_'+v).show();
			return false;
		}
		this.set = function(q,t) {
			var d='investmap';
			switch(t) {
				case 'investmap.pl/wiadomosci/': d='wiadomosci';break;
			}
			this.tab(d);
			$('#s_'+d+' input[type=text]').val(q);
			$('#s2_'+d+' input[type=text]').val(q);
		}
	}
	
	/* newsy */
	this.news = new function() {
		this.init = function() {
			if (!document.location.href.match('investmap.pl/wiadomosci/(.+)\,([0-9]+)\.html')) return; 
			$('dl.wiadomosc dd.txt img').each(function(a,b){ if(b.width>600) b.width=600 })
		}
	}
	
	
	/* inwestycje */
	this.inwestycje = new function() {
		this.init = function() {
			if (!document.location.href.match('inwestycje.investmap.pl/opis-')) return; 
			$('#center-column input[type=image]').attr('width', 520).attr('height', 'auto').css({border:0});
		}
	}

	
	/* forum */
	this.forum = new function() {
		this.init = function() {
			if (!document.location.href.match('forum.investmap.pl(.+)\.html')) return; 
			$('div.postbody div.content img').each(function(a,b){ if(b.width>700) b.width=700 })
		}
	}
	
	/* galeria */
	this.galeria = new function() {
		this.init = function() {
			if (!document.location.href.match('investmap.pl/galeria/')) return;
			match=document.location.href.match('\/([0-9]+)\.html');
			if (match) this.move_to_photo(match[1]);
			if ($('.galltool').width()>$('.galltool div').width()) {
				$('.galltool_nav').hide();
				$('.galltool').width('595px');
				return;
			}
			$('.galltool_nav').each(function(a,b){ b.onclick = a==0 ? function(){ im.galeria.move_left() } : function(){ im.galeria.move_right() } })
		}
		this.move_left = function() {
			left = $(".galltool div").css('left');
			if (left.substr(0, left.indexOf('px'))>=0) return;
			$(".galltool div").animate({"left": "+=425px"}, "fast");
			left = $(".galltool div").css('left');
			if (left.substr(0, left.indexOf('px'))>=0) $(".galltool div").css('left', '0px');
		}
		this.move_right = function() {
			left = $(".galltool div").css('left');
			if (left.substr(0, left.indexOf('px'))<=-$(".galltool div").width()+$('.galltool').width()) return;
			$(".galltool div").animate({"left": "-=425px"}, "fast");
			left = $(".galltool div").css('left');
		}
		this.move_to_photo = function(id) {
			$('.galltool img').each(function(a,b){ if(b.src.match('/'+id+'\.tiny')) {
				b.className='sel';
				if ($('.galltool').width()>$('.galltool div').width()) return;
				if (a*84>84*4) $(".galltool div").animate({"left": "-="+(84*(a-1))+"px"}, "fast");
				return;
			}});
		}
		this.edit = function(mode) {
			mode = typeof(mode)=='undefined'?'init':mode;
			switch (mode) {
			default:
			case 'init':
				$('#galleryEdit').show('fast');
				$('#galleryEdit .nform').css({left:($(window).width()-$('#galleryEdit .nform').width())/2, top:($(window).height()-$('#galleryEdit .nform').height())/2});
				break;
				
			case 'save':
				$.post('/ajax/galleryEdit.php', {
					mode:'save', 
					galleryId:$('#galleryEdit select[name=id]').val(),
					description:$('#galleryEdit textarea').val(),
					source:$('#galleryEdit input[name=source]').val(),
					tags:$('#galleryEdit input[name=tags]').val(),
					id:$('#galleryEdit input[name=photo_id]').val(),
					type:$('#galleryEdit #filetype')[0].checked?0:1,
					user:$('#galleryEdit select[name=user_id]').val()
					}, function(data){
					eval('var json='+data);
					if (json.status=='ok') {
						document.location = json.url;
					} else {
						alert(json.message);
					}
				});
				break;
				
			case 'delete':
				if (confirm('Na pewno chcesz usun±ć zdjęcie?')) {
					$.post('/ajax/galleryEdit.php', {mode:'delete', id:$('#galleryEdit input[name=photo_id]').val()}, function(data){
						eval('var json='+data);
						if (json.status=='ok') {
							document.location = json.url;
						} else {
							alert(json.message);
						}
					});
				}
				break;
			}
		}
	}
	
	this.init=function() {
		this.search.init();
		this.news.init();
		this.galeria.init();
		this.inwestycje.init();
		this.forum.init();
	}	
}

var im=new Core();
$(document).ready(function(){im.init()});