var curImage = 0;

$(function() {

		months = new Array("январь", "февраль", "март", "апрель", "май", "июнь", "июль", "август", "сентябрь", "октябрь", "ноябрь", "декабрь");
		months_skl = new Array("января", "февраля", "марта", "апреля", "мая", "июня", "июля", "августа", "сентября", "октября", "ноября", "декабря");

//		$("div#coursesmenu").css("display", "none");

		latest_seminars = new Array();  		
		monthcounter = new Array();     
		now = new Date();



		$("span[id^='month']").each(function(index, value) { 
			$(this).text(months[now.getMonth()+index <= 11 ? now.getMonth()+index : now.getMonth()+index - 12]); 
		});
		
		$("body").append("<div id='raspshortsb'></div>");



		$("div#raspshortsb").load("/raspshortsb.php div#raspshortsb", function() {				


			$("div[name='seminarsb']").each(
			function()
			{       
				currentdiv = $(this);						
				currentdiv.find("span.startdata").each(
				function()
				{ 
					if (found = $(this).text().match(/(\d\d)\.(\d\d)\.(\d\d\d\d)/))
					{
	                        		seminar_day   = parseFloat(found[1]);
	                        		seminar_month = parseFloat(found[2]);
						seminar_year  = parseFloat(found[3]);

						seminar_date = new Date(found[3], found[2]-1, found[1]);		

						if (! monthcounter[parseFloat(seminar_month)]) monthcounter[parseFloat(seminar_month)] = 0;
						var ai = monthDiff(now, seminar_date)+1;

						if (ai>=0 && ai<=4 && (seminar_date - now) >0 && monthcounter[seminar_month]++ <=6) {
							$("div.box").eq(Math.abs(ai)).append($('<div class="one-event"><div class="date"><div class="num">' + seminar_day + '</div><span class="month">' + months_skl[seminar_month-1] + '</span></div><div class="desc"><a href="' + currentdiv.find("a").attr("href") + '">' + currentdiv.find("a").text() + '</a></div>'));
						}
					}
				});
			});




		var emptyTabs = [];
		$("span[id^='month']").filter(function(index) {	
			if ($("div.box").eq(index).not(":has('a')").length) emptyTabs.push (index);
		}); 
		
		$.each(emptyTabs.sort(function(a,b){return b - a}), function(key, value){
			$("span[id='month" + parseFloat(value+1) + "']").parent().remove();
			$("div.box").eq(value).remove();
		});
		
		$("div#schedule").css("display", "block");
		$("span[id^='month']").eq(0).click();
		$("div#raspshortsb").remove();

		});

		

		$("div.rightperenos1").each(function() { $("td#rasptd2").append($(this)); });




		$("div[name='level4'] li[name!='centr-znanie.ru']").filter("li.level4").css("display", "none");
		$("div[name='level3'] li[name!='centr-znanie.ru']").filter("li.level3").css("display", "none");
//		$("div[name='level2'] li[name!='centr-znanie.ru']").filter("li.level2").remove();
//		$("div[name='level2'] li[name!='centr-znanie.ru']").filter("li.level2").remove();
		$("div[name='level2'] li:contains('1уровень')").filter("li.level2").css("display", "none");

           	$("div[name='level1'] li[name!='centr-znanie.ru']").filter("li.level1").css("display", "none");

		$("div[name='level4'] li.level4").not(":has(div)").css("display", "none");
		$("div[name='level3'] li.level3").not(":has(div)").css("display", "none");
//        	$("div[name='level2'] li.level2").not(":has(div)").remove();                                                                

//        	$("div[name='level1'] li.level1").not(":has(div)").remove();                                                              



		$("div#content-inside tr td div:contains('2уровень')").not(':has(div[name="seminarsb"])').remove();
		$("div#content-inside tr td div:contains('1уровень')").not(':has(div[name="seminarsb"])').remove();



		$("div#coursesmenu").css("display", "block");











//$("div.main_image").css("background-color", "black");
$(".image_thumb ul li:first").addClass('active'); //Add the active class (highlights the very first list item by default)
$(".image_thumb ul li").live('click', function(){
	changeImg($(this));
	curImage = $(this).parent().find('>' + this.tagName).index(this);
}) .hover(function(){ //Hover effects on list-item 
    $(this).addClass('hover'); //Add class "hover" on hover 
    }, function() {
    $(this).removeClass('hover'); //Remove class "hover" on hover out
});


$(".main_image a").attr("href", $(".image_thumb ul li div.block span[name='url']").eq(0).text()); //Switch the main image (URL + alt tag)
$(".image_thumb ul li").eq(0).trigger('click');


$("a.collapse").hover(function(){
    $(".main_banner .block").slideToggle(); //Toggle the description (slide up and down)
    $("a.collapse").toggleClass("show"); //Toggle the class name of "show" (the hide/show tab)
});

function changeImg (elem){

    //Set Variables
    var imgAlt = elem.find('img').attr("alt"); //Get Alt Tag of Image
    var imgTitle = elem.find('a').attr("href"); //Get Main Image URL
    var imgDesc = elem.find('.block').html();  //Get HTML of the "block" container
    var imgDescHeight = $(".main_image").find('.block').height(); //Find the height of the "block"
    var imgHref = elem.find('.block span[name="url"]').text();




//    if (0 ||  $(this).is(".active")) {  //If the list item is active/selected, then...
//        return false; // Don't click through - Prevents repetitive animations on active/selected list-item
//    } 
//else 

//{ //If not active then...
        //Animate the Description
//        $(".main_image .block").animate({ opacity: 0, marginBottom: -imgDescHeight }, 150 , function() { //Pull the block down (negative bottom margin of its own height)
//            $(".main_image .block").html(imgDesc).animate({ opacity: 0.85,  marginBottom: "0" }, 150 ); //swap the html of the block, then pull the block container back up and set opacity

//	    console.log ("animate!");

/*
            $(".main_image img").fadeOut(0, function() {

		console.log ("fadeout");

		$(this).attr('src', imgTitle);
		$(this).fadeIn(150);       
		});


*/

//		$("div.main_image").css("background-image", "url("+imgTitle+")");
//		$(".main_image img").fadeOut(300);
//		setTimeout (function() { 



			$(".main_image img").attr('src', imgTitle); 
//			$(".main_image img").fadeIn(300);  }, 600);

//		$(".main_image img").attr('src', imgTitle);





//            $(".main_image img").attr({ src: imgTitle , alt: imgAlt}); //Switch the main image (URL + alt tag)
	    $(".main_image a").attr("href", imgHref); //Switch the main image (URL + alt tag)
//        });
//    }
    //Show active list-item
    $(".image_thumb ul li").removeClass('active'); //Remove class of 'active' on all list-items
    elem.addClass('active');  //Add class of 'active' on the selected list
    return false; 

}



function nextImage() {	
	if (curImage >= $(".image_thumb ul li").length) curImage = 0;	
	if (typeof(c1) == 'undefined' || c1== $(".image_thumb ul li").length) c1 = typeof(firstLaunch) == 'undefined' ? 1 : 0;
	changeImg($(".image_thumb ul li").eq(curImage++));
//	console.log (curImage);
	firstLaunch = 1;
}                          

nextImage();

$(document).everyTime(5000, 'rotate', function() { 
	nextImage();
}
);

$(window).blur(function(e) {    
$(document).stopTime('rotate');
});

$(window).focus(function(e) {
$(document).stopTime('rotate');
$(document).everyTime(5000, 'rotate', function() { nextImage();});
});


});	



function monthDiff(d1, d2) {
    var months;
    months = (d2.getFullYear() - d1.getFullYear()) * 12;
    months -= d1.getMonth() + 1;
    months += d2.getMonth();
    return months;
}

