$(document).ready(function() {

	/* ~~ menu ~~ */
	var currentId;
	
	function SetHover($e) {
		$(".menu ul li").filter("#"+$e).find('.subMenu').stop(true, true)
			 .animate({ height: 'toggle' },300,'easeOutQuad');
		$(".menu ul li").not("#"+$e).stop(true)
			 .animate({ opacity: 0.3 },300);
	}
	
	function ResetHover($e) {
		$(".menu ul li").filter("#"+$e).find('.subMenu').stop(true, true)
			 .animate({ height: 'toggle' },300,'easeInQuad');
		$(".menu ul li").not("#"+$e).stop(true)
			 .animate({ opacity: 1 },300);
	}

	$(".menu ul li").hover(function(){	
		currentId = $(this).attr('id');	  
		SetHover(currentId);
	}, function(){		  
		ResetHover(currentId);
	});
	
	$(".subMenu p").hover(function(){
		$(this).stop(true).css('background-color', '#5e5552');
	},  function(){		  
		$(this).stop(true).css('background-color', '#362f2d');
	});
	
	/* ~~ etkinlik takvimi ~~ */ 
	var m = new Date();
	var eventId = m.getMonth()+1;
	var yearId = m.getFullYear();
	var saveId = eventId;
	InitEvents(eventId,yearId);
	$('#holder').hide();
	
	$('#prevEvent').click(function(){
		
				
		if ( eventId == 1 ) {
			
		eventId = 12;
		saveId = 12;
		yearId--
		InitEvents(eventId,yearId);
		
		} else { 
		
		eventId--;
		InitEvents(eventId,yearId);
		if(eventId == saveId-2) {
			$(this).hide();
		} else {
			$(this).parent().parent().find('#nextEvent').show();
			$(this).parent().parent().find('#holder').hide();
		}
		}
	});
	
	$('#nextEvent').click(function(){
		
		if ( eventId == 12 ) {
			
		eventId = 1;
		saveId = 1;
		yearId++
		InitEvents(eventId,yearId);
		
		} else { 
		
		eventId++;
		InitEvents(eventId,yearId);
		if(eventId == saveId+2) {
			$(this).hide();
			$(this).parent().parent().find('#holder').show();
		} else {
			$(this).parent().parent().find('#prevEvent').show();
		}
		}
	});
	
	$('#nextEvent').hover(function(){	
		$(this).stop(true).animate({ opacity: 0.3 },200);
	}, function(){		  
		$(this).stop(true).animate({ opacity: 1 },200);
	});
	
	$('#prevEvent').hover(function(){	
		$(this).stop(true).animate({ opacity: 0.3 },200);
	}, function(){		  
		$(this).stop(true).animate({ opacity: 1 },200);
	});
	
	function InitEvents($e,$d) {
		var topicset;
		$.getJSON("data/getdb.asp?ay="+$e+"&yil="+$d, function(data) {
			$(".takvim .ay label").text(data.isim + " Etkinlikleri");
			
			gelentarih = data.isim.split(" ");
			fonksiyontarih = data.ay_id+"/01/"+gelentarih[1];
			
		    dtm = new Date(fonksiyontarih);
			ilk_gun = dtm.getDayName(true);
			
			ktm = new Date(fonksiyontarih);
			toplam_gun = ktm.getDaysInMonth();
		
			InitCalender(ilk_gun, toplam_gun);
			
			$(".takvim .detaylar h3").stop(true, true).hide(1000, function() { $(this).remove()});
			$(".takvim .detaylar p").stop(true, true).hide(1000, function() { $(this).remove()});
			
			$.each(data.etkinlik, function(i,item){
				
				if(item.id == 0) {
					
				$("<h3 style=\"display:none; background-color:#CCC;\"></h3><p style=\"display:none;\"><a href=\"#\">Etkinlik bulunamad&#305;.</a> L&#252;tfen yeniden se&#231;im yap&#305;n&#305;z.</p>").appendTo(".takvim .detaylar");
					
				} else {
				
				$(".takvim ul li[value='" + item.gun_id + "']").css('background-color', item.renk);
				
				if (topicset != item.oyun_id) {
					$("<h3 style=\"display:none; background-color:" + item.renk + "\">" + 
					item.oyun + "</h3>").appendTo(".takvim .detaylar");
					topicset = item.oyun_id;
				};
				
				$("<p style=\"display:none;\"><a href=\"data/events.asp?id=" +
			    item.id + "\" class=\"fancy_box\">" + item.tarih + "</a>" + 
				item.yer + "</p>").appendTo(".takvim .detaylar");
				
				}
			});
			ResetEvents();
		}); 
	};
	
	function ResetEvents() {
		KufonReplace();
		$(".takvim .detaylar h3").each( function(i) {
				$(this).delay(i*200).show(200);
		});  
		$(".takvim .detaylar p").each( function(i) {
				$(this).delay(i*100).show(200);
		});  
		$(".fancy_box").fancybox({
			overlayOpacity : 0.5,
			overlayColor: '#333'
		});
		$(".takvim .detaylar p").hover(function(){	
			$(this).css("background-color", "#EEE");
		}, function(){		  
			$(this).css("background-color", "#FFF");
		});
	}
	
	function InitCalender($a,$b) {
		var day = 0;
		var firstDay = $a;
		var lastDay = $b;
		$('.takvim ul li').each(function(i,item){	
				$(this).text(" ");
				$(this).val(100);
				$(this).not(".hs").removeClass();
				$(this).css("background-color", "transparent");
				$(this).filter(".hs").css("background-color", "#EEE");
			if( i < firstDay-1 || day > lastDay-1 ) {
				return;
			} else {
				day++;
				$(this).text(day);
				$(this).val(day);
				$(this).not(".hs").css("background-color", "#FFF");
				$(this).filter(".hs").css("background-color", "#EEE");
			};
		});		
	}
	
	/* ~~ font embed ~~ */
	
	KufonReplace();
	
	function KufonReplace() {
		Cufon.replace('.subMenu p a', {
			color: '#a4a09e',
			fontSize: '13px'
		});
		Cufon.replace('h4', {
			color: '#000',
			fontSize: '14px'
		});
		Cufon.replace('.altsayfa h3', {
			color: '#FFF',
			fontSize: '15px'
		});
		Cufon.replace('.takvim .ay', {
			color: '#FFF',
			fontSize: '14px'
		});
		Cufon.replace('.takvim .detaylar h3', {
			color: '#000',
			fontSize: '14px'
		});
		Cufon.replace('.linkler h3', {
			color: '#FFF',
			fontSize: '12px'
		});
		Cufon.replace('.linkler h5', {
			color: '#333',
			fontSize: '13px'
		});
	}
	
	var slidetime = setInterval(ChangePic,8000);
	var currentIndex = 0;	
	var totalIndex = $(".linkler ul li").size();
	SpotChange(currentIndex)
	
	function ChangePic() {
		if (currentIndex + 1 == totalIndex) { 
			currentIndex = 0;
			SpotChange(currentIndex);

		} else { 
			currentIndex++;
			SpotChange(currentIndex);
		}
	}

	function SpotChange($e) { 
		$(".linkler ul li:not("+$e+")").fadeOut(800);	
		$(".linkler ul li:eq("+$e+")").delay(810).fadeIn(800);			
	}
	
	
	/* ~~ haber listesi ~~ */
	
	$('#epk').focus(function(){
		$(this).val('');
	});
	
});
