///////////////////////////////////
// Функции JQuery
(function($){

// Ajax запросы
$.fn.contGet = function(Do,Params){

	return $(this).load('/js_get.php',{ main_type: 'userGet', actType: Do, Params: Params });

}// - function


// Подмена изображения
$.fn.setImage = function(options){

	var options = $.extend({
		thumbs: '#thumbs>a',
		hrefattr: 'href',
		setloc: false,
		startimg: 1,
		effect: 0
	}, options || {});
	var bigimg = $(this);
	var thumbs = $(options.thumbs);
	if(bigimg.size() > 0){
	if(options.startimg && thumbs.size() > 0)
		bigimg.attr('src',thumbs.get(parseInt(options.startimg)-1))
		.bind('load',function(){
			$(this).css({width:($(this)[0].naturalWidth||$(this)[0].width),height:($(this)[0].naturalHeight||$(this)[0].height)});
		});
	var cache = [];
	return thumbs.each(function(i,d){
		$(d).css('cursor','pointer').click(function(event){
			var a = $(this);
			bigimg.fadeOut(options.effect,function(){
				$(this).bind('load',function(){
					$(this)
					.css({width:($(this)[0].naturalWidth||$(this)[0].width),height:($(this)[0].naturalHeight||$(this)[0].height)})
					.fadeIn(options.effect);
				}).attr('src',a.attr('tmp_href') || a.attr(options.hrefattr));
			});
			a.attr('tmp_href',a.attr('tmp_href') || a.attr(options.hrefattr)).removeAttr(options.hrefattr,'target');
		});
	});
	}

}// - function


// Кэш изображений
$.preloadImage = function(options){

	var options = $.extend({
		images: typeof options.images=='object' ? options.images : !options.images ? [] : [options.images],
		src_only: true,
		prefix: ''
	}, options || {});
	var images = [];
	$.each(options.images,function(i,d){
		var image = new Image();
		image.src = options.prefix+d;
		images[i] = options.src_only ? image.src : image;
	});
	return images;

}// - function


// Рандом массива
$.shuffle = function( ar ){

	return ar.sort( function(){ return (Math.round(Math.random())-0.5); } );

}// - function


// Ограничение длины ввода
$.fn.limitInput = function(limit,repl){

	var inp = $(this);
	return inp.bind('keyup',function(){
		var inpc
		if(inpc.val().length > limit){
			inpc.val(inpc.val().substring(0,limit));
			alert('Можно вводить не более '+limit+' символов!');
		}
		if(repl!=undefined && repl!=''){
			var morein = $(repl);
			var count = (limit-inpc.val().length)+' &frasl; '+limit;
			morein.html(count);
		}
	});

}// - function

// Запись cookie
$.setCookie = function(name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}// - function

// Чтение cookie
$.getCookie = function (name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}// - finction


})(jQuery);

/*
// Debug
var a='';
$.each($(this)[0],function(i,d){ a=a+i+" : "+d+"\n"; });
alert(a);
*/

var hostname = window.location.hostname;


///////////////////////////////////////////////////
///////////////////////////////////////////////////
// код JQuery



$(function(){
		   
		   
// Сбор изображений для кэша и подключение увеличителя изображений
	var cache_imgs = [];
	$('a[href$=.jpg]:has(img):not(.gallery), a[href$=.JPG]:has(img):not(.gallery), a[href$=.png]:has(img):not(.gallery), a[href$=.gif]:has(img):not(.gallery)')
	.attr('title','Нажмите, чтобы увеличить')
	.each(function(){ cache_imgs.push($(this).attr('href')); })
	.foxislide({title:false});
	
// Подключение галереи
	$('a.gallery').foxibox({overlayOpacity:0.8});

// PNG fix
	$(document).pngFix();

// Делаем картинки со скруглёнными уголками
$('#content img:not(.noround)').wrap(function(){
	root = $(this);
	var style = (root.attr('style')) ? root.attr('style') : '';
	var w = root.width();
	var h = root.height();
	var image = root.attr('src');
	root.removeAttr('style').css({width:w+'px',height:h+'px'});
	return '<div class="rounder" style="background: url('+image+') top left no-repeat; '+style+'">'+root.text()+'</div>';
});

// Автор
	$('#footer #author').css('opacity',0.5).hover(
		function(){ $(this).stop().animate({opacity:1},300); },
		function(){ $(this).stop().animate({opacity:0.5},300);
	});

/*
// Подгрузка изображений в кэш
//	var prelimg_doc = $.preloadImage( {images:cache_imgs} );

// Путь по сайту
//	$('#sw').contGet('siteway',0);


$.ajalinks = function(options){
	var options = $.extend({
		selector: 'a',
		filter: '[href^="http://'+hostname+'"], [href^="http://www.'+hostname+'"], :not([href^="http://"])',
		exclude: '[href^="http://"], [href^="#"], [href^="/uploads"], [href^="uploads"]',
		loading_class: 'loading25'
	}, options || {});
	$(options.selector).filter(options.filter).not(options.exclude).unbind('click.ajal')
	.bind('click.ajal', function(){
		var go = $(this).attr('href');
		if(go){
		var cont_fx = '#content_col .material-content #fx';
		$('#title h1').fadeOut('normal');
		$(cont_fx).fadeOut('normal').parent().addClass(options.loading_class);
		$.get(go,function(data){
			data = $(data);
			cont = data.find(cont_fx);
			h1 = data.find('#title h1').text();
			$(this).html(cont);
			document.title = h1;
			$('#title h1').text(h1);
			$('#title h1').fadeIn('normal');
			$(cont_fx).parent().removeClass(options.loading_class).end().fadeIn('normal');
			window.history.pushState({}, h1, go);
			window.history.replaceState({}, h1, go); 
			$.ajalinks(options);
		});
		return false;
		}
	});
}

//$.ajalinks();
*/
	
});

