//global vars
var cf_campid;	
var cf_icon_definitions;

var emptystar = "https://www.campingdreams.com/img/icons_stern_112016-41.png";
var fullstar = "https://www.campingdreams.com/img/icons_stern_112016-42.png";
//var ajax_url = "https://blogmanagement.custombay.de/ajaxcore/campsites/campsites.php";
var ajax_url = GlobalJSBasePath +"ajaxcore/campsites/campsites.php";
//https://beta.campingdreams.com/ajaxcore/campsites/campsites.php";

var $ = $.noConflict();
$(document).ready(function() 
{	
    BindCountrySelect();
    BindOpenerBtns();
	CreateResponsivePaging();
	BindSumbmitbtns();
	BindAccoSelect();
	BindSearchButtons();
	BindSearchChars();
	BindIconHovers();
	BindFilterSwitch();
	BindPremiumImageSwitch();
	BindRadiusFields();
});

function geolocFail(){
	console.log("log fail");
}

function BindPremiumImageSwitch() 
{
	
	$(".cf_crop_detailsub").click(function() {
		$(".cf_crop_details").attr("style",$(this).attr("style"));

		$("#copyright").text ("(c) " + $(this).attr("copy"));
	});
}
function BindFilterSwitch() 
{
	$(".SwitchButton").click(function() {
		$("#cf_SideSearch").toggle();
		if($("#cf_SideSearch").is(":visible")) {
			var str = $("#SwitchButton").val();
			var res = str.replace("+", "-");
			$("#SwitchButton").val(res);
		}else {
			var str = $("#SwitchButton").val();
			var res = str.replace("-", "+");
			$("#SwitchButton").val(res);
		}
	});
}

function BindIconHovers() 
{

	if(!isJSON(cf_icon_definitions)) {
		LoadDefinitions();

	}	
	$(".CF_Icons").hover(
	  function(e) {
	  	if($(this).hasClass("StartIcon")) {

	  	}else {
		  	var KeyID ="";
		  	KeyID =  $(this).attr("rel");
		  	var Type ="";
		  	Type= $(this).attr("type");
		    if(KeyID!="" && Type!="") 
		    {
		    	if(Type=="acco") {
		    		$(".toolTipBox").html(cf_icon_definitions.acco[KeyID]);		
		    	}
		    	else {
		    		$(".toolTipBox").html(cf_icon_definitions.chars[KeyID]);		
		    	}

		    	$(".toolTipBox").css({position:"absolute", left:e.pageX,top:e.pageY+30});
		    	$(".toolTipBox").show();
		    	$(".toolTipBox").fadeOut( "slow" ).delay(2000);

		    }
		}
	  }, function() {
	    //$(".toolTipBox").hide();
	  }
	);
}

function LoadDefinitions() {
	$.ajax({
		url : ajax_url,
		type : 'post',
		data : {
			action : 'get_iconinfos',
		},
		success : function( data ) 
		{
			var obj = $.parseJSON(data);
		 	cf_icon_definitions= obj;
		 	
		}
	});	
}

function isJSON (something) {
    if (typeof something != 'string')
        something = JSON.stringify(something);

    try {
        JSON.parse(something);
        return true;
    } catch (e) {
        return false;
    }
}


function BindSearchButtons() {
	//btnReset
	
	LoadCharValues();
	$("#btnSearch2").click(function() {
		$("#cf_searchform").submit();
	});
	$("#btnReset").click(function() {
		$("#acco_type").val("");
		$("#searchchars").val("");
		$("#searchstars").val("");
		$("#searchprovider").val("");
		$("#search_radius_limit").val("");
		$("#search_radius_start").val("");
		//$("#cf_searchform").submit();
		window.location.href="/?page=searchpage";
	});
}

function LoadCharValues() 
{
	
	var AccoList ="";
	var CharList ="";
	var StarList ="";
	$(".NavBox ").each(function() 
	{
		if($(this).hasClass("activIcon"))
		{
			if($(this).attr("type")=="char") 
			{
				CharList= CharList+","+ $(this).attr("rel");
			}
			if($(this).attr("type")=="acco") 
			{
				AccoList= AccoList+","+ $(this).attr("rel");	
			}
			if($(this).attr("type")=="star") 
			{
				StarList= StarList+","+ $(this).attr("rel");	
			}
		}
	});

	$("#acco_type").val(AccoList);
	$("#searchchars").val(CharList);
	$("#searchstars").val(StarList);
	


	return false;
}

function BindSearchChars()
{
	
	$(".NavBox").click(function() 
	{
		if($(this).hasClass("activIcon"))
		{
			$(this).removeClass("activIcon");
			if($(this).hasClass("StartIcon"))
			{
				$(this).attr("src",emptystar);
			}
		}else {

			$(this).addClass("activIcon");
			if($(this).hasClass("StartIcon"))
			{
				$(this).attr("src",fullstar);
			}
		}		
		LoadCharValues();
	});
}


function BindRadiusFields() 
{
	
	$("input[name=slc_search_radius_start]").unbind().on("change",function() {
		$("#search_radius_start").val($("input[name=slc_search_radius_start]").val());

	});

	$("select[name=slc_search_radius_limit]").unbind().on('change', function() {
  		var CurrValue = this.value;
		$("#search_radius_limit").val(CurrValue);  		
	});	
}

function BindAccoSelect() 
{
	$("select[name=accotype]").unbind().on('change', function() {
  		var CurrValue = this.value;
  		GetProviderTable(cf_campid,CurrValue) 
	});	

	$("select[name=slc_provider]").unbind().on('change', function() {
  		var CurrValue = this.value;
		$("#searchprovider").val(CurrValue);  		
	});	

	
	
}


function BindSumbmitbtns() 
{
	
	$("#cf_map_submit").click(function(){
		LoadCharValues();
		$("#cf_searchform").attr("action", $("#hidden_map_action").val()+"#results");
		$("#cf_searchform").submit();
	});
	$("#cf_normal_submit").click(function(){
		LoadCharValues();
		$("#cf_searchform").attr("action", $("#hidden_normal_action").val() +"#results");
		$("#cf_searchform").submit();
	});

	$("#btnMap").click(function(){
	//	window.location.href=CF_Ajax.mappage;
	});
}


function GetProviderTable(campid,acco) 
{
	cf_campid=campid;
	$.ajax({
		url : ajax_url,
		type : 'post',
		data : {
			action : 'get_providers',
			cf_detail_id: campid,
			acco_type: acco
		},
		success : function( data ) 
		{
		 	$("#ProviTable").html(data);
		}
	});
	BindAccoSelect();
}


function LoadMapDetails(searchword,country,region,campid) 
{
	$.ajax({
		url : ajax_url,
		type : 'post',
		data : {
			action : 'get_mapdetails',
			searchword: searchword,
			campsite_country: country,
			campsite_region: region,
			cf_detail_id: campid

		},
		success : function( data ) {
		 	var obj = $.parseJSON(data);
			LoadMap(obj.data);
		}
	});
}

function LoadMap(Itemlist) {
	var Items = Itemlist;
    $('#world-map').vectorMap({
		  map: 'europe_mill',
		  markers: Items.map(function(h){ return {name: h.campiste_name, latLng: h.campsite_latlong, id: h.campsite_id} }),
		    labels: {
		        markers: {
		          render: function(index){
		            //return plants[index].name;
		          }
		        }
		    },
		    onMarkerClick: function(events, index, label) {
                    var CurrID = Items[index].campsite_id;
                    var Details = GetCurrentDetails(CurrID);
                   
                   },
                  onRegionClick:function(events,code) 
                  {
                  		$.ajax({
							url : ajax_url,
							type : 'get',
							data : {
								action : 'get_country_id',
								campsite_country: code		
							},
							success : function( data ) 
							{		 	
								var obj = $.parseJSON(data);
								window.location.href=obj.url;
							}
						});
                  },
		  backgroundColor: '#C0C0C0',
		  hoverColor: '#999999',
		  color: '#dddddd'
	});
}

function GetCurrentDetails(campid) 
{
	$.ajax({
		url : ajax_url,
		type : 'get',
		data : {
			action : 'get_map_shortdetails'	,
			cf_detail_id: campid		
		},
		success : function( data ) 
		{		 	
		 $("#campsite_short_info").html(data);
		}
	});
}

function BindOpenerBtns() 
{	
	$(".btnOpener").click(function(){
		if($(this).hasClass("closed")) {
			$(this).removeClass("closed").addClass("opened");	
			$(this).next(".DetailText").show();
			$(this).html("Weniger Details");
		}else {
			$(this).removeClass("opened").addClass("closed");
			$(this).next(".DetailText").hide();
			$(this).html("Mehr Details");
		}
	});
}

function CreateResponsivePaging() {
	
	$("div.cf_pagination select").change(function() {
	  window.location = $(this).find("option:selected").val();
	});
}



function BindCountrySelect() {
	 $("select[name='campsite_country']").change(function(){
        var country_idx =$(this).val();
		$.ajax({
				url : ajax_url,
				type : 'post',
				data : {
					action : 'get_regions',
					country_id : country_idx
				},
				success : function( data ) {
				 	$("select[name='campsite_region']").empty();
	                var obj = $.parseJSON(data);
	                $.each(obj.data, function(key, item) 
	                {   
	                   $("select[name='campsite_region']").append("<option value='"+ item.country_region_id +"'>"+item.country_region_value +"</option>")
	                });
				}
			});
    });
}
