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

var pic = 5;
var picstart = 0;
var myinterval = null;
var newpics = null;

function fadePics()
{
	$('pic' + pic).fade(); 
	if(pic == 4)
	{
		clearInterval(myinterval);
		pic = 0;

		var url = 'ajax_gallery.php';
		var pars = 'cat=' + $('gallcat').value;
		
		var myAjax = new Ajax.Request(
		url, 
		{
			method: 'post', 
			parameters: pars, 
			onComplete: function(originalRequest){ newpics = eval(originalRequest.responseText); myinterval = setInterval('loadnewimages()', 400);}
		});

		
	}
	pic++;
}

function loadnewimages()
{

	$('pic' + pic).src = 'cms/classes/phpThumb/phpThumb.php?src=../../../images/' + newpics[(pic - 1)]["type"] + '/image_' + newpics[(pic - 1)]["id"] + 'a.jpg&w=196&h=117&zc=1';
	$('pic' + pic).appear(); 

	if($('piclink' + pic))
		$('piclink' + pic).href = "gallery.php?id=" + newpics[(pic - 1)]["id"] + '&type=' + newpics[(pic - 1)]["type"];

	if(pic == 4)
	{
		clearInterval(myinterval);
	}
	pic++;
}

Event.onDOMReady( function(){ 

	if($('selectmonth-listen'))
	{
		Event.observe($('selectmonth-listen'), 'click', function(){ if(!$('monthmenu').visible()){ $('selectmonth-listen').update('<img src="img/jump_month_on.gif" alt="затвори" class="hand"/>'); } else { $('selectmonth-listen').update('<img src="img/jump_month.gif" alt="отвори" class="hand"/>'); }$('monthmenu').toggle(); } );
	}
	
	if($('home-article1'))
	{
		Event.observe($('home-article1'), 'mouseover', function(){ this.style.backgroundColor = '#081a28'; } );
	}
	if($('home-article1'))
	{
		Event.observe($('home-article1'), 'mouseout', function(){ this.style.backgroundColor = '#112738'; } );
	}

	for(var i = 2; i < 6; i++)
	{
		if($('home-article' + i))
		{
			Event.observe($('home-article' + i), 'mouseover', function(){ this.style.backgroundColor = '#112738'; } );
		}
		if($('home-article' + i))
		{
			Event.observe($('home-article' + i), 'mouseout', function(){ this.style.backgroundColor = '#14334a'; } );
		}
	}
	for(var i = 6; i < 8; i++)
	{
		if($('home-article' + i))
		{
			Event.observe($('home-article' + i), 'mouseover', function(){ this.style.backgroundColor = '#050c0f'; } );
		}
		if($('home-article' + i))
		{
			Event.observe($('home-article' + i), 'mouseout', function(){ this.style.backgroundColor = '#081116'; } );
		}
	}

	if($('gall-down'))
	{
		Event.observe($('gall-down'), 'click', function(){ 
			if(pic == 5)
			{
				pic = 1;
				myinterval = setInterval('fadePics()', 400);
			}
		} );
	}
	if($('gall-up'))
	{
		Event.observe($('gall-up'), 'click', function(){ 
			if(pic == 5)
			{
				pic = 1;
				myinterval = setInterval('fadePics()', 400);
			}
		} );
	}
	
	for(var i = 1; i < 6; i++)
	{
		if($('cat-article' + i))
		{
			Event.observe($('cat-article' + i), 'mouseover', function(){ this.style.backgroundColor = '#ebebeb'; } );
		}
		if($('cat-article' + i))
		{
			Event.observe($('cat-article' + i), 'mouseout', function(){ this.style.backgroundColor = '#fefefe'; } );
		}
	}

	if($('searchbtn'))
	{
		Event.observe($('searchbtn'), 'click', function(){ 
			$('searchform').submit();
		} );
	}

} );

function view_thumb(id, thumb, dir)
{
	var dir = (dir == null) ? "articles" : dir;

	$('article-pic').src = 'img/loading.gif';
	//$('article-pic').style.marginTop = '20px';
	//$('article-pic').style.marginLeft = '20px';
	
	var img = 'images/' + dir + '/image_' + id + thumb + '.jpg';
	temp = new Image();
	temp.onload = function (){  
		//$('article-pic').style.marginTop = '0px'; 
		//$('article-pic').style.marginLeft = '0px'; 
		$('article-pic').src = this.src;
	}
	temp.src = 'cms/classes/phpThumb/phpThumb.php?src=../../../' + img + '&w=437&h=250&zc=1';

	if($('gallpiclink'))
		$('gallpiclink').href = img;
}