var map;
var arrMarkers = [];
var arrInfoWindows = [];
var bStores = [];


$(document).ready(function(){
	$("#markers a").live("click", function(){
		var i = $(this).attr("rel");
		// this next line closes all open infowindows before opening the selected one
		//for(x=0; x < arrInfoWindows.length; x++){ arrInfoWindows[x].close(); }
		arrInfoWindows[i].open(map, arrMarkers[i]);
	});
		
		

//"https://graph.facebook.com/273338471131/home?access_token=171283726259346|2.C5yY2y4jAzQ6mVZvSpk0gA__.3600.1303747200.1-100002136107384|1TsrBNYomDKh_snN4zkuYwmZPm0"
		
		//initMiniMap
		arCountries();
		
		$.post("/process", { 'city' : '', 'country' : 'Türkiye' }, 
		function(data){
			arCities(data.cities);
		},"json");

		getStores('', 'Türkiye');
		
		function arCountries(){
			$("#mCountry").html('');
			$("#mCountry").append("<option value='Türkiye'>Türkiye</option>");
			$("#mCountry").append("<option value='United Kingdom' >İngiltere</option>");
		}
		
		//get cities
		function arCities(cities){
			$("#mCity").html('');
			
			if (cities[0].country == 'Türkiye'){
				$("#mCity").append("<option value=''>Şehir Seçin</option>");
			}
			for(var i = 0; i < cities.length; i++){
				$("#mCity").append("<option value='"+cities[i].slug+"'>"+cities[i].city+"</option>");
			}
			if (cities[0].country == 'United Kingdom'){
				getStores(cities[0].slug, 'United Kingdom');
				$("#textArea").css("display","none");
				if($("#gMap").hasClass("cont")){
					$("#gMap").css("height","257px");
				}else{
					$("#gMap").css("height","172px");
				}
			}
			else{
				getStores('', 'Türkiye');
			}
		}
		
		$('#mCountry').live("change",
			function(){
				$("#textArea").css("display","none");
				if($("#gMap").hasClass("cont")){
					$("#gMap").css("height","257px");
				}else{
					$("#gMap").css("height","172px");
				}
				$("#mStore").attr("disabled","disabled");
				$("#mStore").html("<option value=''>Mağaza Seçin</option>");
				$.post("/process", { 'city' : '', 'country' : $(this).val() },
				function(data){
					arCities(data.cities);
				},"json");
			}
		);
		
		$('#mCity').live("change",
			function(){
				getStores($(this).val(), $('#mCountry').val());
				$("#textArea").css("display","none");
				if($("#gMap").hasClass("cont")){
					$("#gMap").css("height","257px");
				}else{
					$("#gMap").css("height","172px");
				}
				//changeMap($(this).val());
			}
		);

		$('#mStore').live("change",
			function(){
				var $ind = $(this).val().substring($(this).val().lastIndexOf("-")+1);
				changeMapPro($ind);
			}
		);
		
		function getStores(city, country){
			$.post("/process", { 'city' : city ,  'country' : country }, 
			function(data){
				arStores(data.stores, city); 
				changeMap(city, data.stores);
			},"json");
		
		}

		function arStores(stores, city){
			if (city != 'london'){
				var mStoreStr = "<option value=''>Mağaza Seçin</option>";
			}
			
			for(var i = 0; i < stores.length; i++){
				if (stores[i].title == 'Kahve Dünyası London'){
					stores[i].title = 'Kahve Dünyası Londra';
				}
				mStoreStr += "<option value='"+stores[i].slug+"-"+i+"'>"+stores[i].title+"</option>";
			}
			
			$("#mStore").html(mStoreStr);
			if(city != ""){
				$("#mStore").removeAttr("disabled");
			}
			else{
				$("#mStore").attr("disabled","disabled");
			}
			if (stores.length == 1){
				setTimeout("changeMapPro(0);",200);
			}
		}
		
		


		$(".zoomIn").click(function(){
			map.setZoom(map.zoom + 1); 
		});

		$(".zoomOut").click(function(){
			map.setZoom(map.zoom - 1); 
		});

		$(".zTitle").click(function(){
			if($('#mCity').val() != ""){
				$countrySlug = 'turkiye';
				if ($('#mCountry').val() == "İngiltere"){
					$countrySlug = 'ingiltere';
				}
				location.href = "http://www.kahvedunyasi.com/magazalar/" + $countrySlug + "/" + $('#mCity').val() + "/" + $("#mStore").val().substring(0,$("#mStore").val().lastIndexOf("-"));
			}
			else{
				location.href = "http://www.kahvedunyasi.com/magazalar";
			}
		});

		function changeMap($city, $stores){

			$cLat = 38.8;
			$cLong = 30;
			$zoom = 5;
			
			if ($city == 'adapazari'){$cLat = 40.77352187640244; $cLong = 30.399856567382812; $zoom = 11; }		
			if ($city == 'ankara'){$cLat = 39.91237576385938; $cLong = 32.8346142578125; $zoom = 11; }
			if ($city == 'bursa'){$cLat = 40.19874040614999; $cLong = 29.052143096923828; $zoom = 11; }
			if ($city == 'denizli'){ $cLat = 37.78034830365159; $cLong = 29.06930923461914; $zoom = 11;	}
			if ($city == 'eskisehir'){ $cLat = 39.773186271304056; $cLong = 30.512466430664062; $zoom = 11; }
			if ($city == 'istanbul-anadolu'){ $cLat = 40.96927050641019; $cLong = 29.198619689941406; $zoom = 9; }
			if ($city == 'istanbul-avrupa'){ $cLat = 41.04440429915501; $cLong = 28.85258819580078; $zoom = 9; }
			if ($city == 'izmir'){ $cLat = 38.42014444555175; $cLong = 27.080050659179688; $zoom = 11; }
			if ($city == 'kocaeli'){ $cLat = 40.77742172100596; $cLong = 29.6630859375; $zoom = 8; }
			if ($city == 'mugla'){ $cLat = 37.02612854150744; $cLong = 27.362136840820312; $zoom = 10;}
			if ($city == 'london'){ $cLat = 51.5092499517704; $cLong = -0.132308006286621; $zoom = 10;}
			if ($city == 'antalya'){ $cLat = 36.877008041817906; $cLong = 30.706540129184723; $zoom = 10;}
			
			var centerCoord = new google.maps.LatLng($cLat, $cLong);
			var mapOptions = {
				zoom: $zoom,
				center: centerCoord,
				disableDefaultUI: true,
				mapTypeId: google.maps.MapTypeId.ROADMAP
			};
			map = new google.maps.Map(document.getElementById("gMap"), mapOptions);

			for(var $i = 0; $i < $stores.length; $i++){
				$("#markers").append("<li><a href='#' rel='" + $i + "'>" + $stores[$i].title + "</a></li>");
				var marker = new google.maps.Marker({
					position: new google.maps.LatLng($stores[$i].latitude, $stores[$i].longitude),
					map: map,
					title: $stores[$i].title,
					adr:$stores[$i].address,
					slug: $stores[$i].slug,
					url: '/magazalar/' + $stores[$i].countrySlug + '/' + $stores[$i].citySlug + '/' + $stores[$i].slug
				});
				arrMarkers[$i] = marker;
				google.maps.event.addListener(marker, 'click', function() {
					document.location.href = $(this).attr('url');					
				});
			}


		}


	
});

function changeMapPro($z){
	var centerCoord = arrMarkers[$z].position;
	$("#textArea").html(arrMarkers[$z].adr);
	$("#textArea").css("display","block");
	if($("#gMap").hasClass("cont")){
		$("#gMap").css("height","217px");
	}else{
		$("#gMap").css("height","160px");
	}
	map.setCenter(centerCoord);
	map.setZoom(15);
}
