/* Copyright (c) 2008 Astronim* (astronim.com) */


$(document).ready(
	function() { 
		var monthCurrentOpened;
		var tim;

		/*  Раскраска таблицы */
		$("#content tr:nth-child(even)").addClass("chet");
		$("#content tr > td:nth-child(1)").addClass("first")

		 // ifixpng для всех картинок с класом png 
		$('img[src$=.png]').ifixpng(); 

		//$.sifr({
		//		path: _root+'public/i/',
		//		save: true,
		//		font: 'helioscondbold'
		//	});
		//$('#month h2').sifr({fontSize: '32'});
		//$('#index #month h2').sifr({fontSize: '52'});
		//$('#month h2').sifr();
		//$('#afisha .item h2').sifr();
		//$('#newsline .item h3').sifr({fontSize: '15'});
		//$('#newsline .dateline h2').sifr();
/*
		$('#afisha .item h2').sifr();		
		$('#subscribe h2').sifr({fontSize: '26'});
		$('#contacts h4').sifr();
		$('h1#contentheader').sifr();
		$('#searchNews .header h2').sifr();
		
		
		$('#content h2').sifr();
		$('#content h3').sifr();
		$('#content h4').sifr();
		$('#content h5').sifr();
		$('#content h6').sifr();
		
		$('#content #news h2').sifr({fontSize: '24'});
*/
/*
		$("#itemsOuter .date").each( function() {
			$(this).flash(
				{ 
				  src: _root+'public/i/instpol_date_32x195_1.swf',
				  width: 32,
				  height: 195,
				  wmode: 'transparent',
				  flashvars: { date: $(this).html() }
				},
				{ expressInstall: true }
			);			
		});
*/
		

		var InitLinks = function ( month ) {
			$('#rubricator .loader').hide();

			$('#rubricator a.buttonLeft').click(function () {
				rubHeight = $('#rubricator').height();
				$('#rubricator .loader').show();
				monthCurrentOpened = $(this).attr("m");
				//$('#rubricator').slideUp();
				$.ajax({
					type: "POST",
					url: $(this).attr("href"),
					data: "action=outEventsOnMain",
					success: function(msg){
						$('#rubricator').html( msg );
						InitLinks( monthCurrentOpened );
						$('#rubricator .loader').hide();
					}
				 });
	
				$('#rubricator').height( rubHeight );
				return false;
			});
			
			$('#rubricator a.buttonRight').click(function () {
				rubHeight = $('#rubricator').height();
				$('#rubricator .loader').show();
				monthCurrentOpened = $(this).attr("m");
				//$('#rubricator').slideUp();
				$.ajax({
					type: "POST",
					url: $(this).attr("href"),
					data: "action=outEventsOnMain",
					success: function(msg){
						$('#rubricator').html( msg );
						InitLinks( monthCurrentOpened );
						$('#rubricator .loader').hide();
					}
				 });
	
				$('#rubricator').height( rubHeight );
				return false;
			});

			$('.days_events').hover(function () {
				clearTimeout( tim );
			},
			function () {
				$( ".dateline .days_events" ).hide();
			});


			$('#newsline .dateline ul li').hover(function () {
					
					if ( tim ) {
						clearTimeout( tim );
					}
					
					$( "#newsline .dateline .days_events" ).hide();

					if ( $("span span", this).text() ) {
						var _thisSpan = $("span span", this);
						var offset = _thisSpan.offset();
						var rubOffset = $( "#newsline" ).offset();
						$( "#newsline .dateline .d"+_thisSpan.text() ).show();
						var left = offset.left - rubOffset.left - 110;
						if ( left < 0 ) {
							left = 0;
						}
						else if ( left > 354 && !$("body#index").length ) {
							left = 354;
						}
						$( "#newsline .dateline .d"+_thisSpan.text() ).css("left", left );
					}
					else {
						var _thisSpan = $("span", this);
						var offset = _thisSpan.offset();
						var rubOffset = $( "#newsline" ).offset();
						$( "#newsline .dateline .d"+_thisSpan.text() ).show();
						var left = offset.left - rubOffset.left - 110;
						if ( left < 0 ) {
							left = 0;
						}
						else if ( left > 354 && !$("body#index").length ) {
							left = 354;
						}
						$( "#newsline .dateline .d"+_thisSpan.text() ).css("left", left );
					}

				},
				function () {
					tim = setTimeout( function(){
						$( "#newsline .dateline .days_events" ).hide();
					}, 500 );
				});

			(function ReInitRubricatorSifrAndFlash () {
		
				//$('#month h2').sifr();
				//$('#newsline .item h3').sifr({fontSize: '15'});
				//$('#newsline .dateline h2').sifr();
								
				$("#itemsOuter .date").each( function() {
					$(this).flash(
						{ 
						  src: _root+'public/i/instpol_date_32x195_2.swf',
						  width: 32,
						  height: 195,
						  wmode: 'transparent',
						  flashvars: { date: $(this).html() }
						},
						{ expressInstall: true }
					);			
				});

				//$("#itemsOuter").css( 'left', '-291px' );
	
			})();
			
			slide.Init();

			var date = new Date();
			currentDate = date.getDate();
			currentMonth = date.getMonth();

			if ( ( currentMonth+1 ) == month || !month ) {
				if ( $( "#da" + currentDate ).length > 0 ) {
					slide.GoToCurrentDay( currentDate );
				}
				else {
					var slideLast;
					for ( var j=currentDate; j<=31; j++ ) {
						if ( $("#da"+j ).length > 0 ) {
							slideLast = 0;
							slide.GoToCurrentDay( j );
							break;
						}
						else {
							slideLast = 1;
						}
					}
					if ( slideLast == 1 ) {
						for ( var j=currentDate; j>=1; j-- ) {
							if ( $("#da"+j ).length > 0 ) {
								slide.GoToCurrentDay( j );
								break;
							}
						}
					}
				}
			}

		}

		InitLinks( monthCurrentOpened );

	}
);

var slide = {
	
	_block_id : -1,
	_bannersCells : null,
	_timer : null,
	_finished : false,
	_numItems : null,

	Init : function () {

		var context = this;

		this._bannersCells = $('#items .item');
		this._numItems = this.GetNumBanners();

		var numItems = this._numItems;
		
		$('#buttonLeft').click(function () {
			if ( numItems < 3 ) {
				return false;
			}
			$('.dateline.index li').removeClass("current");
			context.stopAnimate();
			
			context.SlideNext.call(context);
			return false;
		});
		
		$('#buttonRight').click(function () {
			if ( numItems < 3 ) {
				return false;
			}
			$('.dateline.index li').removeClass("current");
			context.stopAnimate();

			context.SlidePrev.call(context);
			return false;
		});

		$('.dateline.index li a').click(function () {
			if ( numItems < 3 ) {
				$('.dateline.index li').removeClass("current");
				$(this).parent().addClass("current");
				return false;
			}
			$('.dateline.index li').removeClass("current");
			$(this).parent().addClass("current");
			context.GoToCurrentDay( $(this).attr("name") );
			return false;
		});
				

		this.ResetSlide();
		
		var width = 0;
		this._bannersCells.each(function () {
			width += this.offsetWidth;
		});
		
		$('#itemsOuter').width(width);

	},
/*	
	startAnimate : function () {
		this.__timer = setInterval(this.Automatic.bind(this), 10000);
	},
*/	
	stopAnimate : function () {
		clearInterval(this.__timer);
	},
/*	
	Automatic : function () {
		
		if (this._finished == false) {
			this.SlideNext();
		}
		else {
			this.SlidePrev();
		}
		
	},
*/	
	SetBlock : function (block) {
		
		this._block_id = block;
	},

	GetRandomBanners : function () {
		
		var banner_block = banners.getBlock(this._block_id);
		return banner_block;
		
	},
	
	SetBlockRand : function () {

		var banners = this.GetRandomBanners();
		var context = this;
		
		this._bannersCells.parent().fadeOut(500, function () {
			
			var items = $('a', this);
			items.each(function () {
				$(this).hide();
			});

			for (var i = 0, length = banners.length; i < length; i++) {
			
				$(items[i]).css('background-image', 'url(' +banners[i].image + ')');
				$(items[i]).css('display', 'block');
				
			}

			$(this).fadeIn(500, function () {
				context.ResetSlide();
			});
			
		});

	},
	
	_current_left : 0,
	
	GetNumBanners : function() {
		
		var length = 0;
		this._bannersCells.each(function (i) {
			length++;
		});
		return length;
		
	},
	
	ToggleStr : function () {

		this.ShowRightStr();
		this.ShowLeftStr();
		
		if (this._current_left == 0 || this._numItems <= 3) {
			this.HideLeftStr();
		}
		
		if (this._current_left >= this._numItems - 3) {
			this.HideRightStr();
		}
		
	},
	
	HideRightStr : function () {
		$('#buttonRight').addClass('disabled');
	},
	
	ShowRightStr : function () {
		$('#buttonRight').removeClass('disabled');
	},
	
	
	HideLeftStr : function () {
		$('#buttonLeft').addClass('disabled');
	},
	
	ShowLeftStr : function () {
		$('#buttonLeft').removeClass('disabled');
	},

	GoToCurrentDay : function( day ) {

		this._current_left = $("#da" + day ).attr('num') - 1;
		
		if (this._current_left >= this._numItems - 3) {
			this._current_left = this._numItems - 3;
			this._finished = true;
		}
	
		this.ToggleStr();
		
		left = $('#items').width() * this._current_left / 3;

		$('#itemsOuter').animate({left : -left}, 1000, 'swing')
		/*
		this._current_left = 3;
		
		if (this._current_left >= this._numItems - 3) {
			this._current_left = this._numItems - 3;
			this._finished = true;
		}
	
		this.ToggleStr();
		
		return $('#items').width() * this._current_left / 3;
		*/
	},	

	GetNextLeft : function() {
		
		this._current_left += 3;
		
		if (this._current_left >= this._numItems - 3) {
			this._current_left = this._numItems - 3;
			this._finished = true;
		}
	
		this.ToggleStr();
		
		return $('#items').width() * this._current_left / 3;
		
	},
	
	ResetSlide : function () {
		
		$('#itemsOuter').css('left', 0);
		this._current_left = 0;
		
		this.ToggleStr();
		
	},
	
	GetPrevLeft : function() {
		
		this._current_left -= 3;
		
		if (this._current_left <= 0) {
			this._current_left = 0;
			this._finished = false;
		}
		
		this.ToggleStr();
		
		return $('#items').width() * this._current_left / 3;
		
	},
	
	SlideNext : function () {
		
		var left = this.GetPrevLeft();
		$('#itemsOuter').animate({left : -left}, 1000, 'swing')
		
	},

	SlidePrev : function () {
		var left = this.GetNextLeft();
		$('#itemsOuter').animate({left : -left}, 1000, 'swing')
	}
	
};

function SubmitSearchForm( ) {

	if ( document.searchForm.searchtext.value == 'пошук па сайту' || document.searchForm.searchtext.value.length == 0 ) {
		return false;
	}
	document.searchForm.submit( );

}

function SubmitSubscribeForm( ) {

	if ( document.subscribeForm.subscribemail.value == 'Адрас эл. пошты' || document.subscribeForm.subscribemail.value.length == 0 ) {
		return false;
	}
	document.subscribeForm.submit( );

}

function PostDelivery(f) {
	
	if (f.email.value && f.charset.value) {
			w=window.open('', 'feedback','height=160,width=220,maximize=0,minimize=0,center=1,resizable=0,status=0,top=200,left=300');
			w.focus();
	}
	else{
		alert('Калі ласка, запоўніце адрас электроннай пошты');
		return false;
	}

	return true;
	
}
function fbs_click() {u=location.href;t=document.title;window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');return false;}