var googleMap;

var props = new Array('page');

var markerPos = new Array();

var position = new Array();

var markersOpen = new Array();

var object = new Array();

var gMapBounds = new google.maps.LatLngBounds();

var G_MIN_ZOOM = 18;

$(document).ready(function(){  

    

    $("span#filtreBtn").live('click', function(){

        /*$(this).stop(true,true).animate({'height':'0'}, 300, function(){

            $("div#filtreDisplay").fadeIn('slow');

        });*/

        $("#mapButtons .active").removeClass('active');

        $(this).addClass('active');

        $("div#filtreDisplay").fadeIn('slow');

    })

    

    $("#renuntaBtn").live('click', function(){

        /*$("span#filtreBtn").stop(true,true).animate({'height':'25px'}, 300, function(){

            $("div#filtreDisplay").fadeOut('slow');

            //$("#filtreDisplayFrom input").attr('checked', false);

        });*/

        $("span#filtreBtn").removeClass('active');

        $("#mapButtons #showMap").addClass('active');

        $("div#filtreDisplay").fadeOut('slow');

    })

        

    $("#submitFiltre").live('click', function(){

		$("#filtreDisplayFrom").validate({

		      submitHandler: function(form) {

                var cat = new Array;

                $('.selectCheckboxes input:checked').each(function(){

                    cat.push($(this).val());

                });

                if(cat != ''){
                    
                    if (1==1){
                        
                        var sUrl = 'http://centrulvechi.oneimage.eu/search/index/page:1/search:/searchType:3/locationAddress:/locationType:'+cat;
                        //alert(sUrl);
			 $('<img src="/img/frontEnd/ajax-loader.gif" id="loadingGifAjax">').insertAfter('#submitFiltre');

                        window.location = sUrl;
                        
                    }else{

                    $('<img src="/img/frontEnd/ajax-loader.gif" id="loadingGifAjax">').insertAfter('#submitFiltre');

                    $.ajax({

                        async: true,

                        type: "POST",

                        url: base + "/locations/getMap/filters:true",

                        data: "type="+cat,

                        dataType: "html",

                        success: function(data){   
                            
                            if ($("#markersMap").html() == null ) { $('body').append('<div id="markersMap"></div>'); }
                            
                            $("#markersMap").html(data);
                            
                            $(function() {

                                var latlng = new google.maps.LatLng(latGoogle, longGoogle);

                                var myOptions = {

                                  zoom: 16,

                                  center: latlng,

                                  mapTypeControl: false,

                                  mapTypeId: google.maps.MapTypeId.ROADMAP,

                                  scrollwheel:false

                                };

                                googleMap = new google.maps.Map(document.getElementById("googleMapBig"), myOptions);

                                $.each($("#markersMap .markers"), function(i, value){

                                    var id = $(value).attr('id');
												
												var markerLatLng = new google.maps.LatLng($('span.latitude', value).html(), $('span.longitude', value).html());
												 
                                    var marker = new google.maps.Marker({

                                        position: markerLatLng, 

                                        icon: url + '/files/media/images/listingImg/' + $('span.gPin', value).html(),

                                        map: googleMap, 

                                        visible: true,

                                        title: $('div.wrapperInfoMap .infoTextWrapper h2', "#"+id).html()

                                    });

                                    gMapBounds.extend(markerLatLng);

                                    var boxText = document.createElement("div");

                                    boxText.innerHTML = $('div.wrapperInfoMap', "#"+id).html();

                                    var newId = id.replace('marker-', '');

                                    

                                    var infowindow = new google.maps.InfoWindow({

                                        content: '<div class="contentInfo lessMargin noMargin">'+$('div.wrapperInfoMap .contentInfo', "#"+id).html()+'</div>',

                                        maxWidth: '129'

                                    });

                                    

                                    google.maps.event.addListener(marker, 'click', function() {

                                        infowindow.open(googleMap,marker);

                                    });

                                    $('div.wrapperInfoMap .contentInfo', "#"+id).remove();

                                });

                            })

                            $("#markersMap").remove();

                            $("span#filtreBtn").stop(true,true).animate({'height':'25px'}, 300, function(){

                                $("div#filtreDisplay").fadeOut('slow');

                            });

                            $("#loadingGifAjax").remove();
									 
									 //fixBounds(latlng);
									 
                        }

                    });
                    
                    }

                }else{

                    var message = $("#noCategoryMessage").val();

                    alert(message);

                }

                   

            }

        });

    })

    

    

    var cat;

    var subcat;

    if($("#cat").val() != ''){

        cat = $("#cat").val();

        $("#filtru_"+cat).attr('checked', 'checked');

    }else{

        cat = '';

    }

    if($("#subcat").val() != ''){

        subcat = $("#subcat").val();

    }else{

        subcat = '';

    }

    

    $.ajax({

        async: true,

        type: "POST",

        url: base + "/locations/getMap/type:"+cat+"/subtype:"+subcat,

        dataType: "html",

        success: function(data){      

            $("#insertMap").html(data);

            

            $.post(base + "/generals/getCalendar", function(response){

            $("#calendarHome").html(response);

    				  });

    

            $("#customCalendar").live('click', function(){

                $("#calendarHome").toggle();

            });

            

            $("#prevMonth").live('click', function(){

                var time = $("#prevMonthTime").val();

                $.post(base + "/generals/getCalendar/time:"+time, function(response){

                    $("#calendarHome").html(response);

            				  });

            });

            

            $("#nextMonth").live('click', function(){

                var time = $("#nextMonthTime").val();

                $.post(base + "/generals/getCalendar/time:"+time, function(response){

                    $("#calendarHome").html(response);

            				  });

            });

            

            $("#nextMonth").live('click', function(){

                var time = $("#nextMonthTime").val();

                $.post(base + "/generals/getCalendar/time:"+time, function(response){

                    $("#calendarHome").html(response);

            				  });

            });



            if(cat != ''){

                $("#filtru_"+cat).attr('checked', true);

            }

            $(function() {

            	$("#fullDate").datepicker({

            		showOn: 'button',

            		buttonImage: base + '/img/frontEnd/calendarIco.png',

                    dateFormat: 'dd-mm-yy',

            		buttonImageOnly: false

            	});

            });

            

            $("#fullDate").change(function(){

                $(this).parent().parent().submit();

            })

            

            $("#showHideInput").live('change', function(){

                $("#mapButtons").toggle();

                $("#googleMapBig").toggle();

                if($(this).attr('checked') == true) {                    

                    google.maps.event.trigger(googleMap, 'resize');

                    setCookie('showMap', 1, 1);

                }

                else if($(this).attr('checked') == false) setCookie('showMap', 0, 1);

            })

            

            $("#showMap").click(function(){

                googleMap.setMapTypeId(google.maps.MapTypeId.ROADMAP);

                $("#mapButtons .active").removeClass('active');

                $(this).addClass('active');

            })

            

            $("#showSatelite").click(function(){

                googleMap.setMapTypeId(google.maps.MapTypeId.SATELLITE);

                $("#mapButtons .active").removeClass('active');

                $(this).addClass('active');

            })

            

            $("#showTerrain").click(function(){

                googleMap.setMapTypeId(google.maps.MapTypeId.TERRAIN);

                $("#mapButtons .active").removeClass('active');

                $(this).addClass('active');

            })

            

            $("#showHybrid").click(function(){

                googleMap.setMapTypeId(google.maps.MapTypeId.HYBRID);

                $("#mapButtons .active").removeClass('active');

                $(this).addClass('active');

            })



            $(function() {

                var latlng = new google.maps.LatLng(latGoogle, longGoogle);

                var myOptions = {

                  zoom: 16,

                  center: latlng,

                  mapTypeControl: false,

                  mapTypeId: google.maps.MapTypeId.ROADMAP,

                  scrollwheel:false

                };

                googleMap = new google.maps.Map(document.getElementById("googleMapBig"), myOptions);

                    /*google.maps.event.addListener(googleMap, 'click', function(event) {

  console.log(event.latLng);

});*/

                $.each($("#markersMap .markers"), function(i, value){

                    var id = $(value).attr('id');
						  
						   var markerLatLng = new google.maps.LatLng($('span.latitude', value).html(), $('span.longitude', value).html());
						  
                    var marker = new google.maps.Marker({

                        position: markerLatLng, 

                        icon: url + '/files/media/images/listingImg/' + $('span.gPin', value).html(),

                        map: googleMap, 

                        visible: true,

                        title: $('div.wrapperInfoMap .infoTextWrapper h2', "#"+id).html()

                    });

                    gMapBounds.extend(markerLatLng);

                    var boxText = document.createElement("div");

                    boxText.innerHTML = $('div.wrapperInfoMap', "#"+id).html();

                    var newId = id.replace('marker-', '');

                    

                    var infowindow = new google.maps.InfoWindow({

                        content: '<div class="contentInfo lessMargin noMargin">'+$('div.wrapperInfoMap .contentInfo', "#"+id).html()+'</div>',

                        maxWidth: '200'

                    });

                    

                    google.maps.event.addListener(marker, 'click', function() {

                        infowindow.open(googleMap,marker);

                    });

                    $('div.wrapperInfoMap .contentInfo', "#"+id).remove();

                    

                });

                

                $(".displayNone").fadeOut('fast');

                $("#markersMap").remove();
					 
					 //fixBounds(latlng);
					 

            })

        }

    });

})



function setMarker(id, value){

    var x = markersOpen[id];

    if(typeof(x)=='undefined') markersOpen[id] = value;

    else {

        if(markersOpen[id]==1 && value == 0) { return false; }

        else markersOpen[id] = value;

    }

}



function checkMarker(id){

    var x = markersOpen[id];

    if(typeof(x)=='undefined'){ return false; }

    else {

        if(markersOpen[id]==0) { $("#wrapperInfoMap-"+id).hide(); }

        else return false;

    }

}



function register(e)

{

	if (!e) e = window.event;

	var printstring = '';

    

	for (var i=0;i<props.length;i++)

	{

		position[0] = e[props[i]+'X'];

		position[1] = e[props[i]+'Y'];

	}

    if(typeof(position[1]) == 'undefined'){

		position[0] = e['clientX'];

        position[1] = $("html").scrollTop();

        if(position[1] == '0') position[1] = e['clientY'];

        else {

            var pos = e['screenY'];

            pos = parseInt(position[1]) + parseInt(pos) - 70;

            position[1] = pos;

        }

    }

    return position;

}

function fixBounds(latLng){
	
	if (gMapBounds.isEmpty()) { gMapBounds = new google.maps.LatLngBounds(latLng, latLng); }
	
	googleMap.fitBounds(gMapBounds);
	
	google.maps.event.addListenerOnce(googleMap, 'bounds_changed', function(event) {
		if (this.getZoom() > G_MIN_ZOOM ){	this.setZoom(G_MIN_ZOOM);  }
	});
	
}


