var cTab=0;
var runSlide=1;
var hpnowDate = new Date();
var hpbeforeDate = new Date();
$(document).ready(function() {

  $('._listZile a').mouseover(function(){
      if (($(this).attr('href')!="") && ($(this).attr('href')!=undefined))
        $(this).prev('div').show();
  })
  $('._listZile a').mouseout(function(){
      if (($(this).attr('href')!="") && ($(this).attr('href')!=undefined))
        $(this).prev('div').hide();
  })
  
  $('.bntl').click(function() {
    runSlide=0;
		$('._selTab').removeClass('curent');
		cTab--;
		if (cTab<0)
			cTab=0;
		i=cTab;		
		$($('._selTab')[cTab]).addClass('curent');
		$('#sliders').animate({left: -i*649});
	  
   })
  $('.bntr').click(function() {
	    runSlide=0;
		cTab++;
		if ($('._selTab').length<=cTab)
		  	cTab=0;
		
		$('._selTab').removeClass('curent');
		i=cTab;		
		$($('._selTab')[cTab]).addClass('curent');
		$('#sliders').animate({left: -i*649});
	  
   })
  
  
  autoChangeTab = function() {
    if (!runSlide)
      return;
	  hpnowDate = new Date();
	  var elapsedTime = (hpnowDate.getTime() - hpbeforeDate.getTime());
	  if(elapsedTime > 4500) 
      cTab+=Math.floor(1*(elapsedTime/4500));
	  else
      cTab++;
	  if ($('._selTab').length<=cTab)
	  	cTab=0;
 		$('._selTab').removeClass('curent');
		i=$('._selTab').index($($('._selTab')[cTab]));		
		$($('._selTab')[cTab]).addClass('curent');
		
		$('#sliders').animate({left: -i*649},500,function(){ setTimeout("autoChangeTab()",4500); });
    hpbeforeDate = new Date();  
  }
  
	setTimeout("autoChangeTab()",4500);
	
	$('._selTab').click(function(){
    runSlide=0;
		$('._selTab').removeClass('curent');
		i=$('._selTab').index($(this));		
		cTab=i;
		$(this).addClass('curent');
		$('#sliders').animate({left: -i*649});
	});

	$('#_rightMonth').click(function(){
		nextMonth=parseInt($('#_curentMonth').data('month'))+1;
		year=parseInt($('#_curentMonth').data('year'));
		if (nextMonth>12) {
			nextMonth=1;
			year=parseInt($('#_curentMonth').data('year'))+1;
		}
		$.loadMonth(year,nextMonth);		
	})
	
	$('#_leftMonth').click(function(){
		nextMonth=parseInt($('#_curentMonth').data('month'))-1;
		year=parseInt($('#_curentMonth').data('year'));
		if (nextMonth<1) {
			nextMonth=12;
			year=parseInt($('#_curentMonth').data('year'))-1;
		}
		$.loadMonth(year,nextMonth);
		
	})
	$.loadMonth=function(year,month) {
		$('#_curentMonth').data('year',year);
		$('#_curentMonth').data('month',month);
		$.getJSON($('#_curentMonth').data('url')+year+'/'+month+'/',function(data){
			$('._listZile a').removeClass('radius3').removeClass('m').removeClass('red').removeAttr('href');
			$('#_curentMonth').html(data.month);
			$.each(data.list,function(key,val){
				val.zi--;
				if (val.red=='true')
					$($('._listZile a')[val.zi]).addClass('radius3').addClass('red');
				else
					$($('._listZile a')[val.zi]).addClass('radius3').addClass('m');
				$($('._listZile a')[val.zi]).attr('href',val.link);
				console.log($($('._listZile a')[val.zi]).prev('div'));
				$($('._listZile a')[val.zi]).prev('div').find('.c_title').html(val.title);
				$($('._listZile a')[val.zi]).prev('div').find('.c_desc').html(val.desc);
				
			})
		
		})	
	}
	$.loadMonth(parseInt($('#_curentMonth').data('year')),parseInt($('#_curentMonth').data('month')));
});

