//$(document).ready(positions);
//$(window).bind('load', function(){ $('#curtain').show(); });

$(document).ready(function(){ 
			
	$('html').addClass('js');
	emptyImage = '../_img/pix.gif';
	setTimeout(loadPage, 1000);
			
	//preloader
		preloadImage('../_img/ajax-loader.gif');
	
	//js-links
		$(".js-link").removeAttr("href");
		$(".js-link").addClass("click dotted");
			
	//positions
		browser = "";
		if (jQuery.browser.version == "6.0" && jQuery.browser.msie) {
			browser = "ie6";
		}
		else if (jQuery.browser.opera) {
			browser = "opera";
			$('head').append('<link href="/_css/styles-opera.css" type="text/css" rel="stylesheet" media="all" />');
		}
								
	//text margins
		$("div p:last-child, div.photo:last-child").addClass('last');
		$("p, ul, ol, h1").next("h2").addClass('in-text');
		$("p").prev("div.waveline").addClass('waveline-in-text');				
		$("p, h1, ul, ol").next("h3").addClass('in-text');
		//$("p").next("h4").addClass('in-text');
		$("p, ul, ol").livequery(function(){ $(this).next("div.section-header").addClass('in-text'); });
		$("h1").next(".small").addClass('running-title');
		
	//fonts
		$('h1, .h1').each(function(){ if ( !$(this).hasClass('type')) { $(this).addClass('type t30'); } });
		$('h2, .h2').each(function(){ if ( !$(this).hasClass('type')) { $(this).addClass('type t24'); } });
		$('div.post h4').each(function(){ $(this).wrap('<div class="section-header faded x-small"><div class="in">').before('<div class="waveline"></div><div class="waveline wl2"></div>').replaceWith('<span>' + $(this).html() + '</span>'); });
		$('#menu1 a').addClass('t16');
		Cufon.replace(".type", { hover: true, hoverables: { a: true } });
					
		var typelinks = $('a.type:not(a.n-link)');
		typelinks.hover(
			function () {	
				$(this).addClass('orange'); 
				Cufon.replace('a.type', { hover: true, hoverables: { a: true } });
			},
			function () {
				$(this).removeClass('orange');
				Cufon.replace('a.type', { hover: true, hoverables: { a: true } });				
			}			
		);
		
	//icons, arrows
		$('a.icon').livequery(function(){
				$(this).hover(
				function () {	
					$(this).find('img').css('top','-100%');
				},
				function () {
					$(this).find('img').css('top','0px');					
				}			
			);
		});	
		
	//news	
	$('div.newsblock').hover(
		function () {			
			$(this).parent().addClass('hover');		
		},
		function () {
			$(this).parent().removeClass('hover');					
		}			
	);
	
	//teachers
	$('div.teacher').hover(
		function () {	
			$(this).parent().addClass('hover');
		},
		function () {
			$(this).parent().removeClass('hover');					
		}			
	);
					
	//slideshow
		currentPhoto = new Array();
		imgs = new Array();
		$('div.photo:not(div.photo-alone)').each(function(){
			var index = $(this).index();
			imgs[index] = $(this).children('div.img').find('img');  
			if (imgs[index].length > 1) {				
				currentPhoto[index] = 1;				
				imgs[index].addClass('click').bind('click',function(){ slideshow(index, currentPhoto[index] + 1) });				
				//$(this).children('div.img').find('img:gt(0)').hide();
				$(this).find('div.next a').bind('click',function(){ slideshow(index, currentPhoto[index] + 1) });
				$(this).find('div.prev a').bind('click',function(){ slideshow(index, currentPhoto[index] - 1) });				
			}
			else {
				$(this).addClass('photo-alone');
			}
		});	
		
	//forms
		$('.labeled input[value!=]').siblings('label').hide();
		$('.labeled').bind('click',function(){ $(this).children('label').hide(); $(this).children('input').focus(); });
		$('.labeled input').bind('focus',function(){ $(this).siblings('label').hide(); });
		$('.labeled input').bind('blur',function(){ if ($(this).val() == '') $(this).siblings('label').show(); });
		
	//fixedbar
		if( $('#fixedbar').length > 0 && !($.browser.msie && parseInt($.browser.version) < 7) ) {
			$(window).scroll(function() {
				
				var height_fix = parseInt($('#fixedbar').height(), 10);
				var height_rel = parseInt($('#postarea').height(), 10);				
				var levelY = parseInt($('#postarea').offset().top, 10);
				var levelStop = levelY + height_rel - height_fix;							
				var scrollY = $(window).scrollTop();			
				
				if ( scrollY > levelY ) { 
					$('#fixedbar').addClass('fixed').removeClass('absolute').css('marginTop', 'auto'); 
				}
				else { 
					$('#fixedbar').removeClass('fixed').removeClass('absolute').css('marginTop', 'auto'); 
				}					
				if ( scrollY > levelStop ) { 
					$('#fixedbar').removeClass('fixed').addClass('absolute').css('marginTop', levelStop - levelY + 'px'); 
				}  		
			});
		}
		
	//tabs
		$('div.tab-content').each(function(){ if ( !$(this).hasClass('opened')) { $(this).hide(); } });		
		$('a.tab').bind('click', function(){			
			tab_id = $(this).attr('href').substr( $(this).attr('href').lastIndexOf('#')+1 );
			//alert('click!');
			changeTab(tab_id);									
		});
		if (location.hash.length > 0) {
			//alert('start!');
			changeTab(location.hash.substr(1));	
		} 		
		$(window).bind('hashchange', function() {
			//alert('window!');
			changeTab(location.hash.substr(1));
		});
		
});

function loadPage() {	
	$('#curtain').fadeOut();
}

function changeTab(tab_id){
	
	tab_a = $('a[href $=' + tab_id + ']');
	tab_rel = tab_a.attr('rel');

	if (tab_a.hasClass('tab') && tab_a.hasClass('current-tab'))
		return;
			
	$('a[rel='+tab_rel+']').removeClass('current-tab');
	tab_a.each(function(){		
		if ($(this).hasClass('tab'))
			$(this).addClass('current-tab');				
	});
	
	$('div.tab-content[rel='+ tab_rel +']').removeClass('opened').hide();
	$('#tab_' + tab_id + '[rel='+ tab_rel +']').addClass('opened').show();	
}

function slideshow(index, photo) {
	var obj = $('div.photo').get(index);	
	var loader = $(obj).find('div.loader');
	var total = imgs[index].length;	 
	currentPhoto[index] = photo;
	if (photo > total) currentPhoto[index] = 1;
	else if (photo < 1) currentPhoto[index] = total;
		
	$(loader).show();
	
	var img = imgs[index][currentPhoto[index]-1];	
	var ii = new Image();
	ii.onload = function() {		
		$(loader).hide();
		imgs[index].hide(); 
		$(img).fadeIn();
		$(obj).find('span.title').html($(img).attr('title')).end().find('span.faded').html(currentPhoto[index]+'/'+total);				
	};
	ii.src = $(img).attr('src');
}

function scheduleDay(day) {
	//var day_obj = day.parents('div.day'); 
	var day_obj = $('#schedule > #' + day);
	day_obj.toggleClass('active');
	toggleAttr(day_obj.find('div.section-header a'),'title',title_open,title_close);
	if (day_obj.hasClass('active')) { 
		day_obj.find('div.day-schedule').slideDown();		
	} 
	else {
		day_obj.find('div.day-schedule').slideUp();
	}	
	return false;
}

function scheduleAllDays() {
	$('div.day').addClass('active').find('div.day-schedule').slideDown();
	$('div.day div.section-header a').attr('title',title_close);
	return false;
}

function scheduleHideAllDays() {
	$('div.day').removeClass('active').find('div.day-schedule').hide();
	$('div.day div.section-header a').attr('title',title_open);
}

function scheduleSelect(key, value) {
	if (value == 0) {
		scheduleAllDays();
		$('div.day-schedule > div.item').removeClass('active');
		Cufon.replace('div.day-schedule h2');		
		return;
	}
	scheduleHideAllDays();	
	$('div.day-schedule > div.item').removeClass('active').filter('[' + key + '*=' + value + ']').addClass('active');
	Cufon.replace('div.day-schedule h2');
	$('div.day:has(div.active)').each(function(){ scheduleDay($(this).attr('id')); });
	$("#scheduleFilters select[name!='" + key + "'] [value='']").attr("selected", "selected");
}

function scheduleStop() {
	$('#schedule-today ul').stop();
	if ($('#schedule-today').offset().left - $('#schedule-today ul').offset().left != 0) { 
		$('#schedule-today a.i-arrow-l').fadeIn(); 
	}
	else {
		//$('#schedule-today a.i-arrow-l').hide();
	}
	//if ($('#schedule-today').offset().left + $('#schedule-today').width() - schedule.width() - schedule.offset().left - 130 != 0)
}

function visualSlide(index) {	
	if ( index > $('#menu2 li').length)
		index = 1;	
	var current_li = $('#menu2 li a.small[slide=' + index + ']').parent('li');	
	var note = $('#visual-note');

	if ( current_li.length > 0 && (note.html() == '' || index != currentSlide)) {
		
		note_top = current_li.offset().top - $('#menu2').offset().top + 20 + 'px'
		note_left = current_li.offset().left - $('#menu2').offset().left + 'px';	
		note_width = current_li.width() + 'px';
		
		if ( note.html() == '') {			
			$('#menu2 li').removeClass('current');
			current_li.addClass('current');
			note.html( current_li.children('div.visual-note').html() );	
			note.css('width', note_width);
			note.css('left', note_left);
			note.css('top', note_top);
			note.show();		
			
			//loadImg($('#visual-img'),current_li.children('a.small').attr('img'));		
		}
		else {			
			$('#menu2 li').removeClass('current');
			//current_li.addClass('current');		
			note.html( current_li.children('div.visual-note').html() );		
			note.css('width', note_width);
			note.animate({left: note_left},200,"linear", function(){ 			
				current_li.addClass('current');	
				//note.find('a.icon').hide().show();													
			});		
		}	
				
		currentSlide = index;
		loadImg($('#visual-img'),current_li.children('a.small').attr('img'));
		//current_li.find('div.box').hide();
	}
	return false;	
}

function loadImg(obj, url) {
	img = obj.find("img");
	if (url == img.attr('src'))
		return false; 	
	if (loaded[url] != 1)
		obj.fadeTo("fast", 0.5);
	i = new Image();
	i.onload = function() {
		obj.fadeTo("fast", 1);
		img.attr("src", url);
		loaded[url] = 1;
	};	
	i.src = url;
}

function positions()
{
	return;
}

function positionsIE()
{
	return;
}

function toggleAttr(obj, attr, value1, value2)
{
	if (obj.attr(attr) == value1) obj.attr(attr,value2);
	else if (obj.attr(attr) == value2) obj.attr(attr,value1);
	return;
}

function setCookie(name, value) {
	eval("document.cookie = \"" + name + "=" + value + "; path=/; expires=Wed, 1 Jan 2050 00:00:00 GMT;\"");
}

function preloadImage(src) {
   var img=new Image();
   img.src = src;
}

function _debug(object) {
	var allF = "";
	for (superVar in object)
	{
		allF += superVar + ";<br>\n ";
	}
	qqq = window.open();
	qqq.document.write(allF);
}

function get_ending(number, endings) {	
	endings = endings.split(",");
	decimal = 0;
		
	if (endings.length != 3)
	{
		return;
	}
	
	// for 11-21 return 3-d form
	if (number > 10 && number < 21)
	{
		return endings[2];
	}
		
	if (number > 9)
	{
		//TODO: translate next two lines to JS
		decimal = floor((number % 100) / 10);
		number  = number % 10;
	}
	
	// for ..1 return 1-st form
	if (number == 1)
	{
		return endings[0];
	}	
	// for ..2-..4 return 2-st form
	else 
	{
		if (number > 1 && number < 5 && decimal != 1)
		{
			return endings[1];
		}
		// for ..5-..0 return 3-st form
		else
		{
			return endings[2];
		}
	}						
}
