﻿// JavaScript Document
var slideUrls = new Array;
var slideTimer;
var slideImage = 0;
var errorLoading = 0;
$(document).ready(function(){
    defaultStyles();
//$('section.photo div.view div.photo a').imagebox({ circular: false });
$('a.show-rating-stat').toggle(function(){
	  $(this).text('Скрыть детальную информацию об оценках').next('div.rating-stat').slideDown(300);
	}, function(){
	  $(this).text('Детальная информация об оценках').next('div.rating-stat').slideUp(300);
	});
	$('#menu ul li a.active').append('<span></span>');
	var searchInput = $('#search input[type=text]');
	var defaultSearch = searchInput.val();
	searchInput.focus(function(){
		if(searchInput.val() == defaultSearch){
	    searchInput.val('').removeClass('default');
		}
		showSearchParams();
	}).blur(function(){
	  if(!searchInput.val()){
			searchInput.val(defaultSearch).addClass('default');
		}
	});
	var searchParamsTimer;
	$('#search').hover(function(){
	  clearTimeout(searchParamsTimer);
	},function(){
	  searchParamsTimer = setTimeout(function(){hideSearchParams();}, 1000);
	});
	var clickIndex = 0;
	$('.scrollable div.nav a').click(function(){
		if($(this).hasClass('prev') && clickIndex == 0){
		  return;
		}
		if($(this).hasClass('next') && clickIndex == $(this).parent('div').siblings('div.items').children('div.item').size()-1){
		  return;
		}
	  var way = $(this).parent('div').siblings('div.items').children('div.item').width();
		var direction;
		if($(this).hasClass('next')){
			direction = '-';
			clickIndex++;
		}
		if($(this).hasClass('prev')){
			direction = '+';
			clickIndex--;
		}
		$(this).parent('div').siblings('div.items').animate({left: direction+'='+way+'px'});
	});
	$('div.dialog .close, div.dialog .cancel').click(function(){
		$(this).parents('div.dialog').fadeOut(300);
		$('#shtorka').remove();
	});
	$('div.residence-photo a.read-all').toggle(function(){
		$(this).addClass('opened').children('span').text('Скрыть');
		$('div.residence-photo div.thumbs').animate({height: $('div.residence-photo div.thumbs div').height()+'px'},300);
	},function(){
		$(this).removeClass('opened').children('span').text('Все фотографии');
		$('div.residence-photo div.thumbs').animate({height: '74px'},300);
	});
	$('div.residence-photo div.thumbs a').hover(function(){
	    $('div.residence-photo div.main a').attr({ href: $(this).attr('href') });
	    $('div.residence-photo div.main a img').attr({ src: $(this).attr('rel') });
	});
	
	$('span.sort').hover(function(){
		$(this).children('a').addClass('opened');
	  $(this).children('ul').slideDown(100);
	}, function(){
		$(this).children('a').removeClass('opened');
	  $(this).children('ul').slideUp(100);
	});
});
function defaultStyles(){
	$('ul, ol').each(function(){
	  $(this).children('li:first').addClass('first');
	  $(this).children('li:last').addClass('last');
	});
	$('table tr:odd').addClass('odd');
	$('table tr:even').addClass('even');
	$('table tr:first').addClass('first');
	$('table tr:last').addClass('last');
	$('table tr').each(function(){
		$(this).children('td:first').addClass('first');
		$(this).children('td:last').addClass('last');
});
}
function showSearchParams(checkbox){
	$('#search-params').slideDown({duration:500, easing:'easeOutExpo'});
	if(isInt(checkbox)){
	  $('#search-params input').eq(checkbox).attr({checked: true})
		.parent('li').siblings('li').children('input').attr({checked: false});
	} else {
		$('#search-params input[name='+checkbox+']').attr({checked: true})
		.parent('li').siblings('li').children('input').attr({checked: false});
	}
}
function hideSearchParams(){
	$('#search-params').slideUp({duration:500, easing:'easeOutExpo'});
}
function isInt(x) {
  var y=parseInt(x);
  if (isNaN(y)) return false;
  return x==y && x.toString()==y.toString();
}
function showCourseMap(id){
	//ajax loading from course id
	$('#course-map .content').append('<img src="img/map.jpg" alt="">');
	$('#course-map').fadeIn(300);
}
function hideCourseMap(){
	$('#course-map .content img').remove();
	$('#course-map').fadeOut(300);
}
function showDialog(id, shtorka, width){
	if(shtorka == true){
		$('body').append('<div id="shtorka"></div>');
		$('#shtorka').css({opacity: '0.4'});
    }
    $('#' + id + ' div.content').css({ overflow: 'auto', maxHeight: window.innerHeight - 210 + 'px' });
	$('#'+id).css({width: width+'px', marginLeft: -1*width/2-11+'px'}).fadeIn(300);
}
function hideDialog(id){
	$('#shtorka').remove();
	$('#'+id).fadeOut(300);
}
