$(document).ready(function(){

//Form submit
$('a.submit').click(function(){
	$(this).parents('form').submit();
	return false;
});


//Search form submit
$('#search_form form').submit(function(){
	window.location.href = $(this).attr('action') + $(this).find('input[name=query]').val() + '/';
	return false;
});


//Cols equalize
//colsEqualize();
/*
$('#center_bl img, #right_bl img').imgpreload({
	all: function() {colsEqualize();}
});
*/

//Select onchange relocation
$('select.relocation').change(function(){
	var $relocation = $(this).val();
	if ($relocation) location.href = $relocation;
});


//Form submit
$('a.submit').click(function(){
	$(this).parents('form').submit();
	return false;
});


//UI button
$("input:submit", ".content").button();



//Form value switcher /input
$('input.switch').focus(function(){
   var defaultVal = $(this).attr('data-default'),
       currentVal = $(this).val();
   $(this).removeClass('error');
   if (currentVal == defaultVal) $(this).val('');
}).blur(function(){
   var defaultVal = $(this).attr('data-default'),
       currentVal = $(this).val();
   if (currentVal == '') $(this).val(defaultVal);
});


//Login form toggle
$('span#lm_login a').toggle(function(){
   $('#login_bl').slideDown();
}, function(){
   $('#login_bl').slideUp();
});


//Photo list item hover
$('#photos_list .item').hover(function () {
		$(this).addClass('hover');
	},
	function () {
		$(this).removeClass('hover');
});


//Photo additional centered
$('.content.photo .additional').css('marginLeft',
	($('.content.photo .additional').parent().width() - $('.content.photo .additional').children('.in').width() - 13) / 2
);


//Photo additional click
$('.content.photo .additional a').click(function () {
	$('.content.photo .additional img').removeClass('sel');
	$('#photo img').attr('src', $(this).attr('href'));
	$(this).children('img').addClass('sel');
	$('#photo a').attr('href', $(this).attr('data-link'));
	$('#descr').html($(this).attr('title'));
return false;
});


$('.content.news img').each(function(){
	if ($(this).width() > 350) $(this).css('float', 'none');
});


//Signup select participant group
$('#signup_bl input[name="user[group]"]').change(function(){
	if ($(this).val() == 0) $('#sub_groups').slideDown();
	else $('#sub_groups').slideUp();
});


//Radio bold
$('label input:checked').parent('label').css('fontWeight', 'bold');
$('label input').click(function(){
	$('input[name='+ $(this).attr('name') +']').parent('label').css('fontWeight', 'normal');
	$(this).parent('label').css('fontWeight', 'bold');
});


/////RATING/////

//Rating slider
var $blink = '';
$(".slider").slider({
	value:15,
	min: 0,
	max: 30,
	step: 1,
	start: function(event, ui) {
		$(".yrating .v").removeClass('hidden');
		$(".rating_vote a.button").removeClass('disabled');
		if ($blink == '') $blink = buttonBlink();
	},
	slide: function(event, ui) {
		$(this).siblings('.h').children('.rating_val').text(ui.value);
		
		$votes = 0;
		$('.rating_val').each(function (i, domEle) {
		   $votes += parseInt($(this).text());
		});
		$("#rating_vote").text($votes);
	}
});

$('.slider').each(function (i, domEle) {
	$max = $(this).attr('data-max');
	$half = $max / 2;
	$(this).slider('option', 'max', $max);
	$(this).slider('option', 'value', $half);
	$(this).siblings('.h').children('.rating_val').text($half);
});

//Rating button blink
function buttonBlink() {
   setTimeout(function () {
		$(".rating_vote a.button").toggleClass('active');
   buttonBlink();
   }, 500);
}


//Rating slider reality
var $blink = '';
$(".slider_reality").slider({
	value:15,
	min: 0,
	max: 30,
	step: 1,
	start: function(event, ui) {
		$(this).parents('.stats').find(".yrating .v").removeClass('hidden');
		$(this).parents('.rating_vote').find("a.button").removeClass('disabled');
		if ($blink == '') $blink = buttonBlink_reality($(this).parents(".rating_vote").find("a.button"));
	},
	slide: function(event, ui) {
		$(this).siblings('.h').children('.rating_val').text(ui.value);

		$(this).parents('.stats').find(".rating_voteresult").text(ui.value);
	}
});

$('.slider_reality').each(function (i, domEle) {
	$max = $(this).attr('data-max');
	$half = $max / 2;
	$(this).slider('option', 'max', $max);
	$(this).slider('option', 'value', $half);
	$(this).siblings('.h').children('.rating_val').text($half);
});

//Rating button blink
function buttonBlink_reality($button) {
   setTimeout(function () {
		$button.toggleClass('active');
   buttonBlink_reality($button);
   }, 500);
}




//Rating send
$('.rating_vote a.button').click(function () {

	if ($(this).hasClass('disabled') == false) {

	var $href = location.pathname,
		 $votes = '';

		$votes = '';
		$('.rating_val').each(function (i) {
		   $votes += '&votes[]=' + $(this).text();
		});

		$('.rating_vote').slideUp('fast');

		$.getJSON(
			'/index.php?ajax=rating.ajax&act=vote&href='+ $href + $votes,
			function ($jsonOutput) {
				if (!$jsonOutput.resultError) {
					$('#rating').text($jsonOutput.resultMessage);
				}
			}
		);

	}
return false;
});

$('table.tbl_rating tr:odd').addClass('odd');
$('table.tbl_rating tr').hover(function() {
	$(this).addClass('hover');
},
function() {
	$(this).removeClass('hover');
});


//Freelancers remove item
$('.freelancers a.remove').click(function() {

	$id = $(this).attr('href').substr(1);
	$item = $(this).parents('.info');

	$(".dialog.remove").dialog({
		bgiframe: true,
		resizable: false,
		width:250,
		height:120,
		modal: true,
		overlay: {backgroundColor: '#000', opacity: 0.5},
		buttons: {
			'Отменить': function() {
				$(this).dialog('destroy');
			},
			'Удалить': function() {

				$.getJSON(
					'/index.php?ajax=freelancers&act=remove&id='+ $id,
					function ($jsonOutput) {
						if (!$jsonOutput.resultError) {
                     $item.slideUp('fast');
						}
						else {
/*
							$(".dialog.error p .msg").html($jsonOutput.resultError);
							$(".dialog.error").dialog({
								bgiframe: true,
								width:380,
								height:163,
								modal: true,
								buttons: {
									Ok: function() {
										$(this).dialog('destroy');
									}
								}
							});
*/
						}
					}
				);

				$(this).dialog('destroy');
			}
		}
	});

return false;
});




/////MODER/////


$('.moder_funcs .h a').click(function () {

	$('.moder_funcs .buttons_bl').slideToggle();

return false;
});


//MAIN!
});


//Cols equalize
function colsEqualize() {
	$cols_height = jQuery.makeArray($('.eq'));
	$cols_height = jQuery.map($cols_height, function(n, i){return n.offsetHeight;});
	$('.eq').height($cols_height.max());
}

Array.prototype.max = function(){
	return Math.max.apply({},this)
}

