﻿$(document).ready(function(){
	var list = "#slide-show";
	var time = 10000;
	var currentImg = 0;
	var maxImg = $(list+" li").length - 1;
	var lastImg = maxImg;

	$(list+" li").eq(currentImg).show();

	start_SlideShow();

	function setImg(id) {
		if (id > maxImg) {
			next = 0;
		}
		else
			next = id;

		$(list+" li").eq(lastImg).fadeOut('slow');
		$(list+" li").eq(next).fadeIn('slow');
		lastImg = next;
	}

	function nextImg() {
		if (currentImg < maxImg)
			currentImg++;
		else
			currentImg = 0;

		setImg(currentImg);
	}

	function start_SlideShow() {
		$(list).everyTime(time, 'timer', nextImg);
	}

	function showMsg(data, type) {

		var text = '';

		for (i=0; i<data.length; i++) {
			text += data[i]+'<br />';
		}

		$('#g-infobox').find('.infobox').html(text).removeClass('error ok info').addClass(type);
		var h = $('#g-infobox').height()+1;

		if (type=='ok' || type=='info') {
			$('#g-infobox').css('top', '-'+h+'px').show();
			$('#g-infobox').animate({'top':'0px'}, 500);

			$('#g-infobox').oneTime("5s", function() {
				$(this).animate({'top': '-'+h+'px'}, 500);
			});
		}
		else {
			$('#g-infobox').css('top', '-'+h+'px').show();
			$('#g-infobox').animate({'top':'0px'}, 500);
		}
	};

	$('.teachers .title').click(function() {
		$(this).children('.toggle').toggleClass('toggle_open');
		$(this).next('div').slideToggle();
	});

/*********************************************************/	
/*	$("#flag").click(function() {
		$(this).toggleClass("flag_en");
		$(".lang").toggle();
	});
*/
	$(".ajaxform").ajaxForm({
		url: '?ajax',
		dataType: 'json',
		beforeSubmit: function(arr, $form, options) {
		},
		success: function(data) {
			if (data.error.length) {
				showMsg(data.error, 'error');
			}
			else if (data.msg.length){
				showMsg(data.msg, 'ok');
			}
			else {
				showMsg('Ошибка передачи данных', 'error');
			}
		}
	});
/*********************************************************/	
	$("#nav li").hover(function() {
//		alert($(this).children('ul').is('ul'));
		if ($(this).children('ul').is('ul')) {
			$(this).children('a').stop().animate({paddingTop:"6px", paddingBottom:"69px"}, 500);
//			$(this).children('ul').delay(200).animate({width:"180px"}, 400);
			$(this).children('ul').stop().animate({top:"30px"}, 500);
		}
//		$(this).children('ul').slideDown('fast');
	},
	function() {
		if ($(this).children('ul').is('ul')) {
			$(this).children('a').stop().animate({paddingTop:"66px", paddingBottom:"9px"}, 500);
//			$(this).children('ul').animate({width:"0px"}, 400).hide('slow');
			$(this).children('ul').stop().animate({top:"140px"}, 500);
		}
//		$(this).children('ul').slideUp('fast');
	}
	);
/*********************************************************/
//	var size_menu = $("#nav li").length;

//	$("#test").text($("#nav li").eq(5).offset().left);
	$("#header").prepend("<ul id=\"background_menu\">" + $("#nav").html() + "</ul>");
	$("#background_menu li a").text('');
	$("#nav li").hover(function() {
		var id = $("#nav li").index(this);
		$("#background_menu li").eq(id).addClass('hover');
		$("#test").text(id);
	},
	function() {
		var id = $("#nav li").index(this);
//		$("#background_menu").eq(id).fadeOut('normal', function() {
			$("#background_menu li").eq(id).removeClass('hover');
//		});
		$("#test").text(id);
	}
	);

/*	$("#nav li").each(function(i, n){
		$("#header").append("<div class=\"www\" style=\"left:"+menu[i].x1+"; width:"+menu[i].width+"px; height:100px; background:#DDD; top:0; position:absolute\"></div>");
	});
*/
//	$("#test").text(showProperties(menu[0], 'fdfd'));

	function showProperties(obj, objName) {
		var result = "The properties for the " + objName + " object:" + "\n";

		for (var i in obj) {result += i + " = " + obj[i] + "\n";}

		return result;
	}

});
