var ie = !!document.getElementById && !window.getComputedStyle;
var ie6 = ie && navigator.appVersion.indexOf('MSIE 6') != -1;


var Tabs = {
	INITIALIZED: false,
	tabs: [],
	get: function(n){
		return this.tabs[n];
	},
	getType: function(elem){
		return elem.className.split(' ')[elem.className.split(' ').length - 1].split('-')[1];
	}
};

function calendar_select(type, date){
	var obj = calendar_events[type];
	if(obj[date]){
		document.location.href = obj[date].link;
	}
}
function calendar_hover(type, date){
	var obj = calendar_events[type];
	if(obj[date]){
		$('#calendar-tip').html(comment_to_html(obj[date].comment));
	}
}
function comment_to_html(comment){
	if(typeof comment == 'string'){
		return '<ul><li>' + comment + '</li></ul>';
	}else if(typeof comment == 'object'){
		var str = '<ul>';
		$(comment).each(function(){
			str += '<li>' + this + '</li>';
		});
		str += '</ul>';
		return str;
	}
}

var MainItems = {
	here_item: null,
	set_here: function(item){
		if(this.here_item){
			$(this.here_item).removeClass('here');
		}

		$('#cloud-title').html(item._title);
		$('#cloud-content').html(item._content);

		$(item).addClass('here');
		this.here_item = item;

	}
};

$(document).ready(function(){
	if(window.opera){
		$(document.body).addClass('opera');
	}
	if(ie6){
		$('#main-menu TD').hover(function(){
			$(this).addClass('hover');
		}, function(){
			$(this).removeClass('hover');		
		});


		$('#main-items TD IMG').each(function(){
			var src = this.src;
			this.src = this.src.replace(this.nameProp, 'blank.gif');
			this.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + src + '", sizingMethod="crop")';
		});

		$('#seminars .seminar').hover(function(){
			$(this).addClass('seminar-hover');
		}, function(){
			$(this).removeClass('seminar-hover');
		})
	}

	
	$('#main-items TD IMG').each(function(){
		var par = this.parentNode.parentNode;
		par._title = $('.title', par).html();
		par._content = $('.content', par).html();
		if($(par).hasClass('here')){
			MainItems.set_here(par);
		}
		$(this).click(function(){
			MainItems.set_here(this.parentNode.parentNode);
			return false;
		});

	});

	var t_counter = 0;
	$('#text-content TABLE TR').each(function(){
		if(!$('TH', this).size()){
			if(t_counter && t_counter % 2){
				$(this).addClass('zebra');
			}
			t_counter++;
		}
	});


	$('#calendar-1').calendarLite({
		dateFormat: '{%yyyy}-{%mm}-{%dd}',
		onHover: function(date){
			calendar_hover(1, date);
		},
		onSelect: function(date) {
			calendar_select(1, date);
			return false;                }
	});

	$('#calendar-2').calendarLite({
		dateFormat: '{%yyyy}-{%mm}-{%dd}',
		onHover: function(date){
			calendar_hover(2, date);
		},
		onSelect: function(date) {
			calendar_select(2, date);
			return false;                }
	});

	$('#calendar-3').calendarLite({
		dateFormat: '{%yyyy}-{%mm}-{%dd}',
		onHover: function(date){
			calendar_hover(3, date);
		},
		onSelect: function(date) {
			calendar_select(3, date);
			return false;                }
	});




	//tabs
	if($('#calendar .tabs .tab').size()){
		$('#calendar .tabs .tab').each(function(){
			this._type = Tabs.getType(this);
			Tabs.tabs[this._type] = this;
			if($(this).hasClass('tab-here')){
				Tabs.here_tab = this;
			}
			$(this).click(function(){
				if($(this).hasClass('tab-here')){
					return false;
				}
				if(Tabs.here_tab){
					$(Tabs.here_tab).removeClass('tab-here');
					$(Tabs.here_tab._content).removeClass('cnt-here');
				}
				$(this).addClass('tab-here');
				$(this._content).addClass('cnt-here');
				Tabs.here_tab = this;
			});
		});
		$('#calendar .tabs-content .cnt').each(function(){
			this._type = Tabs.getType(this);
			this._tab = Tabs.get(this._type);
			this._tab._content = this;
		});
	}
	
});
