/*
 * Site Functions
 * All Javascript functions necesssary to run this site.
 * Author: Mustafa Tunc Firmasoft Tech
 */

 
/*Initializes all event links*/		
function initEventLinks(){
	$("a.eventLinks").fancybox({'overlayColor'		: '#000'});	
}				
/*Initializes gallery links*/
function initGallery(){
	$("a.galleryLinks").fancybox({'overlayColor'		: '#000'});
}	
/*Initializes tab structure*/
function initTabs(defaultTab){
	if(defaultTab == null || defaultTab == ''){
		defaultTab = 0;
	}
	$( "#tabs" ).tabs({selected:defaultTab});
	$( "#tabs ul li").click(function(){
		$(this).find("a").click();
	});	
	$(".tab_content").fadeIn('slow');
}
/*Inititializes the Reservation Form*/
function initResForm(){
	$(function() {		
		var day = new Date();		
		$( "#res_date" ).datepicker({
			dateFormat: 'dd.mm.yy',
			altFormat: 'yy-mm-dd',
			altField:"#res_date_alt",
			minDate: day,
			monthNames: ['Ocak','Şubat','Mart','Nisan','Mayıs','Haziran','Temmuz','Ağustos','Eylül','Ekim','Kasım','Aralık']	,
			dayNamesMin: ['Pa', 'Pt', 'Sa', 'Ça', 'Pe', 'Cu', 'Ct']	,
			firstDay: 1		
		});
		
	});				
	$( "#res_submit" ).button();
}

/*Initializes the background slideshow*/
function initBackgrounds(){
	//Image List				
	var images = new Array();
		images[0] = 'style/images/backgrounds/1.jpg';
		images[1] = 'style/images/backgrounds/2.jpg';
		images[2] = 'style/images/backgrounds/3.jpg';
		images[3] = 'style/images/backgrounds/4.jpg';
		images[4] = 'style/images/backgrounds/5.jpg';
	
	var isFirst = true;
				
	$(images).each(function(index,value){
		// new image object
		var img = new Image();
		// image onload
		$(img).load(function () {					
			$("#backgrounds").append(img);											
		}).attr('src', value).hide();		
		
		if(index == 0){													
			$(img).fadeIn(1000).addClass('active');
			isFirst = false;
		}
	});	
	
	setInterval('swapImages()', 10000);	
}

/*Function to swap the backgrounds*/
var swapActive = true;										
function swapImages(){
	if(!swapActive)
		return false;
		
	var $active = $('#backgrounds .active');
	var $next = ($('#backgrounds .active').next().length > 0) ? $('#backgrounds .active').next() : $('#backgrounds img:first');			  
	if($active.attr('src') != $next.attr('src') && $next.attr('src') != ''){
		$next.fadeIn(1000,function(){
			$active.removeClass('active');
			$active.fadeOut(1000);
		}).addClass('active');
	}			  	
}	


/*
 *Google Maps
 *initializes the google map on contact page
 */
var map;		
var mapInited = false;	
function initialize_map() {					
	if(!mapInited){					
		var latlng = new google.maps.LatLng(40.96391589345453, 29.07369656078263);
		var myOptions = {
			zoom: 13,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
		
		marker = new google.maps.Marker({
			position: latlng, 
			map: map			   
		});
		var contentString = '<div class="mapContent">'+
								'<strong>Nook Cafe & Restaurant</strong><br><br>'+
								'<div><strong>Adres:</strong>'+$("#nook_address").html()+'</div>'+
							'</div>';
		
		infowindow = new google.maps.InfoWindow({
			content: contentString
		});
		
		google.maps.event.addListener(marker, 'click', function() {
			infowindow.open(map,marker);
		});
		
		infowindow.open(map,marker);						 	
		mapInited = true;	
	}				
}


/*Draw Popup Functions*/
function initDrawPop(){
	$("a#drawPopUpLink").fancybox({
		'overlayColor'			: '#000',
		'autoDimensions'		: false,
		'autoScale'				: false,
		'width'         		: 485,
		'height'        		: 240,
		'onClosed'				: function(){cleanDrawPopup()},
		'onStart'				: function(){swapActive = false;},
		'centerOnScroll' 		: false
	});
	
	$("#draw_submit").button();
	$("#draw_back").button();				
	$("#draw_close").button();
	
	$("#draw_close").click(function(){
		$.fancybox.close();					
	});				
	$("#draw_submit").click(function(){sendDrawForm();});
	$("#draw_back").click(function(){
		$("#drawFail").fadeOut("fast");
		$("#drawSuccess").fadeOut("fast");		
		$("#drawForm").fadeIn("fast");		
	});
}

/*Hide Everything and show form*/
function cleanDrawPopup(){
	$("#drawFail").hide();
	$("#drawSuccess").hide();
	$("#draw_name").val("") ;
	$("#draw_email").val("") ;
	$("#draw_phone").val("");
	$("#success_name").html("");
	$("#drawForm").show();
	swapActive = true;
}

/*Sends the form*/
function sendDrawForm(){
	
	$("#drawForm").fadeOut("fast");
	$("#drawLoader").fadeIn("fast");
	
	$.ajax({
		url: "includes/functions/db.functions.php",
		data: "command=addToDraw&name="+$("#draw_name").val() + '&email=' + $("#draw_email").val() + '&phone='+$("#draw_phone").val(),
		success: function(result) {
			$("#drawLoader").fadeOut("fast");
			if(result == "1"){
				$("#success_name").html($("#draw_name").val());
				$("#drawSuccess").fadeIn("fast");
			}	
			else{							
				$("#drawFailMessage").html("Formunuz kaydedilirken bir hata oluştu lütfen daha sonra tekrar deneyiniz.");
				if(result != "")
					$("#drawFailMessage").html(result);
				$("#drawFail").fadeIn("fast");
			}
				
		}
	});				
}	


function homePopUpInit(){
	$("a#homePopup").fancybox({'overlayColor'			: '#000'});
	$("a#homePopup").click();
}

