/*--------------------------------------------
/ Design for Webmaster.spb.ru
--------------------------------------------*/



/* исчезновение текста в инпутах */

(function($){
	function inputValue (el){
		var val = el.value;
		$(el).focus(function(){
			el.value!=val?el.value=el.value:el.value='';
			if (el.createTextRange){
				var r = el.createTextRange();
				r.collapse(false);
				r.select();
			}
		});
		$(el).blur(function(){el.value==''?el.value=val:el.value=el.value});
		return el;
	}
	$.fn.inputValue1 = function(){
		$(this).each(function(){
			inputValue(this);
		});
	}
})(jQuery);



/* дефолтная таблица */

$(document).ready(function(){

	$('.default table').not('.noBg').each(function(n){
		  if($('thead', this)[0]){
			  $('tbody tr:odd', this).addClass('bgN');
			  }
		else{
			$('tbody tr:first', this).addClass('thead');
			$('tbody tr:even', this).addClass('bgN');
			}
	});
	
});


/* дефолтные подписи к картинкам */

$.fn.defImg = function(){
	$('img', $(this)).each(function(){
		var align = this.align;
		this.align = '';
		var t = $(this).attr('alt');
		$(this).wrap('<div class="imgDef"></div>');
		$('<div>'+t+'</div>').insertAfter($(this));
		$(this).next().display='none';
			$(this).next().css('display','block').end().parent().css('width',$(this).width()+'px');
		$(this).load(function(){
			$(this).next().css('display','block').end().parent().css('width',$(this).width()+'px');
		});
		switch(align){
			case 'left':
				$(this).parent().addClass('imgLeft');
				break;
			case 'right':
				$(this).parent().addClass('imgRight');
				break;
			case '':
				$(this).parent().addClass('simbol');
				break;
		}
	});
}


/* gallery by Pasha Pulin */

$.fn.slideGallery = function(w, nEl, ef){
	$('.slideBlock', this).width((w*nEl)+'px')
	var photo = $('.slideBlockHr1 .el', this);
	var n=photo.length;
	var elWidth = w;//photo[0].clientWidth;//Ширина одного елемента
	//_p(n);
	var slideWidth = w*n;//Ширина блока, который будет смещатся
	//_p(slideWidth);
	var elDo = $('.slideBlockHr1', this);
	elDo.width(slideWidth+'px')
	elDo.attr('rel', '1')
	var numElementOneShift = ($('.slideBlock', this).width()/elWidth).toFixed(0);//Количество элементов в доном смещении
	//_p(numElementOneShift);
	var oneShift = numElementOneShift*elWidth;//Величина одного смешения
	var numSift = (slideWidth/oneShift).toString().split('.')[0]; //Количество смещений
	
	var restPhoto = 0;
	if(numSift!=1){
		restPhoto = photo.length%numSift*w; //Величина последнего смещения
	}
	else{
		restPhoto = (n-nEl)*w;
	}
	//_p(photo.length+ ' ' +numSift+ ' ' + w + ' ' + restPhoto)
	//var numElementEndOneShift = photo.length%;
	$('.buttons a.toBack', this).click(function(){
										$('.toFront').removeClass('none');
										 var el = $(this).parents('.photoGalery').children('.slideBlock').children('.slideBlockHr1');//Определяем элемент
										 var pos = el.attr('rel');
										 if(pos>=2){
										 	el.animate({left:'+='+oneShift},{ duration: 1000, easing: ef });
											el.attr('rel', --pos);
										 }
										 else{
											 if(pos==1.5){
											 	el.animate({left:'+='+restPhoto},{ duration: 1000, easing: ef });
												el.attr('rel', pos=Number(pos)-0.5);
											 }
										}
										if(pos==1){
											if(prevDo)prevDo();
										};
										$(this).blur();
										return false;
										 });
	$('.buttons a.toFront', this).click(function(){
										$('.toBack').removeClass('none');
										 var el = $(this).parents('.photoGalery').children('.slideBlock').children('.slideBlockHr1');//Определяем элемент
										 var pos = el.attr('rel');
										 //if(pos==numSift) _(pos+' '+numSift + ' ' + restPhoto);
										 //_(pos+' '+numSift + ' ' + restPhoto);
										 if(pos<numSift){
										 	el.animate({left:'-='+oneShift},{ duration: 1000, easing: ef });
											el.attr('rel', ++pos);
										 }
										 else{
											 if(restPhoto && pos==numSift){
												 
											 	el.animate({left:'-='+restPhoto},{ duration: 1000, easing: ef });
												el.attr('rel', pos=Number(pos)+0.5);
											 }
											 }
											 if(pos>numSift){
												 if(nextDo)nextDo();
												 }
											 $(this).blur();
											 return false;
										 });
}

