$(document).ready(function(){
	$("#selectedevent").hide();
	//this part of the code is for the fade in fade out effect -> social icons + the tooltip in <strong></strong>
	$("#socialjs li").each(function() {
		$("a strong", this).css("opacity", "0");
	});
	
	$("#socialjs li").hover(function() {
		$(this).stop().fadeTo(500, 1).siblings().stop().fadeTo(500, 0.2);
			
		$("a strong", this).stop().animate({
			opacity: 1,
			top: "-10px"
		}, 300);
		
	},
	function(){
		$(this).stop().fadeTo(500, 1).siblings().stop().fadeTo(500, 1);
			
		$("a strong", this).stop().animate({
			opacity: 0,
			top: "-1px"
		}, 300);
	});
	//
	
	//the plugin for the slider (nivoslider)	
	$('#slider').nivoSlider({
        effect:'random',
		animSpeed: 500,
		pauseTime: 6000
    });
	//
	
	//the flip wall effect for the links
	$('.sponsorflip').bind("click",function(){
		var elem = $(this);
		
		if(elem.data('flipped')){
			elem.revertFlip();
			elem.data('flipped',false);
		}
		else{
			elem.flip({
				direction:'lr',
				speed: 350,
				onBefore: function (){
					elem.html(elem.siblings('.sponsordata').html());
				}
			});
			elem.data('flipped',true);
		}
	});	
	//

		
	var myurl = $(location).attr('href');
	var filename = myurl.substr(myurl.lastIndexOf("/") + 1);
	var mymonth;
	//the datepicker is inside the Ajax code!
	//problem of sync between the loop beforeshowday and the ajax request.
	//u need to do 1) the ajax request then 2) when u have the result from all events, add them with the loop beforeshowDay
	//Php returns a string with the echo function, the string format is "["yyyy-mm-dd","yyyy-mm-dd",...,"yyyy-mm-dd"]"
	// eval function change it into code.
	$.ajax({
		url : "script/calendaronload.php",
		complete:function(xhr,result){
			if(result!="success") return;
			var events = eval(xhr.responseText); 
			
			//all english files start with "en_"
			if(filename[0]=="e" && filename[1]=="n" && filename[2]=="_"){
				mymonth = ["january","february","march","april","may","june","july","august","september","october","november","december"];
			}
			else{
				mymonth = ["januar","februar","mart","april","maj","juni","juli","avgust","septembar","oktobar","novembar","decembar"];
			}
			
			$("#datepicker").datepicker({
				dateFormat: 'dd-mm-yy',
				nextText : 'sledeci',
				prevText : 'prethodni',
				monthNames : mymonth,
				beforeShowDay: function(dateText){
					var d = dateText.getDate();
					var m = dateText.getMonth()+1;
					var yyyy = dateText.getFullYear();
					if(m<10) m = "0"+m;
					if(d<10) d = "0"+d;			
					var ymd = yyyy + '-' + m + '-'+ d;
					
					//look inside events for ymd if not return [true,'']
					//else return [true,'highlight']
					var highlight;
					for(var i=0,highlight=false;i<events.length;i++){
						if(events[i]==ymd) highlight=true;
					}
					if(highlight==false)return[true,''];
					else return [true,'highlight'];					
				},
				onSelect:function(dateText){
					var dd = dateText.substring(0,2);
					var mm = dateText.substring(3,5);
					var yyyy = dateText.substring(6,10); 
					var dateTextrevert = yyyy + '-' + mm + '-' + dd;
					var data = {date : dateTextrevert};
					$.ajax({
						url : "script/calendarclick.php",
						data : data,
						complete:function(xhr,result){
							if(result!="success") return;
							var response = xhr.responseText;
							if(response){
								$("#selectedevent").show();
								$("#selectedevent").html(response);
							}
							else{
								$("#selectedevent").hide();
							}
						}
					});
				}
			});
		}		
	});
	//	
	if(filename[0]=="e" && filename[1]=="n" && filename[2]=="_"){
		var lang = "en";		
	}
	else{
		var lang = "srb";
	}
	var data = {lang : lang};
	//load next event
	$.ajax({	
		url : "script/loadnextevent.php",
		data : data,
		complete:function(xhr,result){
			if(result!="success") return;
			var response = xhr.responseText;
			$("#event").html(response);
		}
	});
	//
 });
