$(document).ready(function() {

	// Test
	$("#sendComment").click(function(){
		alert('aaa');
		// var name = $('input[name="name"]').val();
		// var comment = $('input[name="comment"]').val();
		// var id = window.location.hash.substring(1);
		// $.getJSON("http://mojukus.rs/coda/mobile/comments?id="+id+"&comment="+comment+"&name="+name+"&jsoncallback=?", function(data){

		// });
	});

	/* Open & close Options window */
	$(".options").click(function(){
		$("#wrappAll").addClass("aniLeft");
	});
	$("#optionsBox .close").click(function(){
		$("#wrappAll").removeClass("aniLeft");
	});


	/* Open & close Search window */
	$(".search").click(function(){
		$("#wrappAll").addClass("aniRight");
	});
	$("#searchBox .close").click(function(){
		$("#wrappAll").removeClass("aniRight");
	});

	/* Populate search input */
	$("#searchBox a").click(function(){
		var href = $(this).attr("href");
		$("#searchInput").val(href);
	});

	/* Add remove favorites */
	$("a.star").click(function(){

		var fav = $(this).parent();

		if(fav.hasClass("off")){
			fav.removeClass("off");
		} else {
			fav.addClass("off");
		}
	});


	
	/* Choose picture again */
	$(".photo .close").click(function(){
		$(".photo").addClass("displayNone");
		$(".browseBg").removeClass("displayNone");
	});

	/* CAMERA FUNCTION */
	function openCamera(){
		if(window.MozActivity) {
		    var activity = new MozActivity({
		        name: 'pick',
		        data: {
		            type: 'image/jpeg'
		        }
		    });

		    activity.onsuccess = function() {
		        var picture = this.result;
		        var blob = picture.blob; // just a standard blob
		        var img = document.createElement('img');
		        img.src = window.URL.createObjectURL(blob);
	//	        img.setAttribute('class', 'cappi');
	//            jsonova(img);
		        
		        $(".photo").removeClass("displayNone");

		        /* Delete current and add new img */
		        $(".photoWrapp").empty();
		        $(".photoWrapp").html(img);

				$(".browseBg").addClass("displayNone");
		    }

		    activity.onerror = function() {
		        // oh no!
		    }
		}
	}

	/* Open ffos camera&browse api */
    $(".imgBg").click(function(){
		openCamera();
	});

	/* This needs to be on the bottom */
	$("a.star, .options, #optionsBox .close, .search, .photo .close, #searchBox a").click(function(e){
		e.preventDefault();
		e.stopPropagation();
		e.stopImmediatePropagation();
	});
});

	function singleImage(id){
		$.getJSON("http://mojukus.rs/coda/mobile/singleReqest?id="+id+"&jsoncallback=?", function(data){
			var jajson = data.single;
	        for(x in jajson){
	        	var locations = jajson[x];
	        	var img = new Image();
				img.src = "http:\/\/mojukus.rs\/coda\/uploadz\/"+locations.image;
				$('.complaint').prepend(img);
				$('.location').html(locations.name);
				$('.description').html(locations.description);
				$.getJSON("http://mojukus.rs/coda/mobile/comments?id="+id+"&jsoncallback=?", function(data){
					var comblocks = data.comments;
					console.log(comblocks);
					var even = 1;
					for(y in comblocks){
						var comblock = comblocks[y];
						var classPosted;
						if(even%2 == 0){classPosted='posted';}else{classPosted='posted even';}
						var posted = $('<div>').addClass(classPosted);
						var user = '<span>'+comblock.user+'</span>';
						var userImg = '<img src="images/user/user.jpg">';
						var pblock = '<p>'+user+comblock.comment+'</p>';
						var ptime = '<p class="time">'+comblock.date+'</p>';
						posted.append(userImg);
						posted.append(pblock);
						posted.append(ptime);
						$(".comments").append(posted);
						even++;
					}
				});
	        }	        
		});
	    
	}
	//json getting data from server
	function getImages(){
		$.getJSON("http://mojukus.rs/coda/mobile/getLocations?jsoncallback=?", function(data){
			var jajson = data.dblocations;
	        qloc = jajson;
	        locations(qloc);
		});
	    
	}
	function locations( qloc ){     
	    var locations = qloc;
	    for(x in locations){
	        var location = locations[x];

	        var complaint = $('<a>').addClass('kurac')
	        				.attr('href', 'complaint.html#'+location.cordinate_id)
	        				.attr('data-reportid', location.cordinate_id);
	        var title = '<p class="title">'+location.loc+'</p>';
	        var loc = '<p>'+location.name+'</p>';

	        /* This creats image */
	        var img = new Image();
	        img.src = "http:\/\/mojukus.rs\/coda\/uploadz\/"+location.image;
	        img.setAttribute('class', location.cordinate_id);
	        
	        complaint.append(img);
	        complaint.append(title);
	        complaint.append(loc);

	        $('.list').append(complaint); 
	    }
	}	
	//maps geolocation
	function findMyCurrentLocation(){
		var geoService = navigator.geolocation;
		if (geoService) {
			navigator.geolocation.getCurrentPosition(showCurrentLocation,errorHandler);
		} else {
			$("#searchResults").html("Your Browser does not support GeoLocation.");
		}
	}

	function showCurrentLocation(position){
		$("#searchResults").html("Your location details --> Current Latitude : " + position.coords.latitude + " , Longitude : " + position.coords.longitude);
		// return position.coords.latitude;
	}

	function errorHandler(error){
		  $("#searchResults").html("Error while retrieving current position. Error code: " + error.code + ",Message: " + error.message);
	}