var business;
var idx;
var counter;
var marker;
var infowindow;
/**
 * Expands the label row item.
 * @param row The row to expand.
 */
function expandRow(row){
	$("#"+row).addClass('auto_height');
	$("#"+row +" .labels").show();
	$("#"+row +" .expand").hide();
}

/**
 * Contracts the label row item.
 * @param row The row to contract.
 */
function contractRow(row){
	$("#"+row).removeClass('auto_height');
	$("#"+row +" .labels").hide();
	$("#"+row +" .expand").show();
}

/**
 * Invokes the new result page width the new sorting option.
 * @param url URL with the correct sort value.
 */
function sortResult(url){
	window.location = url.value + 'asc';
}

/**
 * Removes all pipes "|" that were added by the paginator helper.
 */
function removePipes(){
	page = $("#pagination").html()
	$("#pagination").html(replaceAll(page, " | ", ""));
}

/**
 * Replace all chars or text from a String.
 * This process is similar to Javascript's replace() method, this one replaces all matches.
 * @param The new String with text replaced.
 */
function replaceAll(text, search, replace){
	while (text.toString().indexOf(search) != -1)
	{
		text = text.toString().replace(search, replace);
	}
	
	return text;
}

/**
 * Selects the first item from the listings page.
 */
var maxWordSize = 24;
function selectFirst(){
	$('#item_0').addClass("current");
	$('#item_0 .title').html("All");
	label = $('#item_0 .label').html().trim();
	if (label.length > maxWordSize)
	{
		label = label.substring(0, maxWordSize) + "...";
	}
	$('#label_info').html(label);
}

/**
 * Callback function for the click event on LI elements in the listings page.
 * @param LI element clicked.
 */
function selectList(li){
	$('#label_list li').removeClass("current");
	$('#label_list .title').html("Some");
	$(li).addClass("current");
	$("#" + li.id + " .title").html("All");
	label = $("#" + li.id + " .label").html().trim();
	if (label.length > maxWordSize)
	{
		label = label.substring(0,maxWordSize) + "...";
	}
	$('#label_info').html(label);
}

/* maps functions*/
var map;

// Default map location
var latitude = 40.7067971;
var longitude = -73.9903808;

/**
 * Initialize google maps api
 */
function initialize(_zoom) {
	
    var latlng = new google.maps.LatLng(latitude, longitude);
    var myOptions = {
      zoom: _zoom,
     center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
 }

/**
 * Adds news marks to map to represent the businesses locations.
 * @param lat Business latitude.
 * @param long Business longitude.
 * @param name Businnes name.
 */
function addMark(lat, long, name,idx){
	var myLatlng = new google.maps.LatLng(lat, long);
	var infowindow = new google.maps.InfoWindow({
	    content: name,
	    
	});
	marker = new google.maps.Marker({
	      map: map,
	      draggable: true,	      
	      position: myLatlng,
	      title:name,
	      zIndex: idx,
	      optimized: true,
	      raiseOnDrag: true
	    });
	google.maps.event.addListener(marker, 'click', function() {
		
		infowindow.open(map, marker);
	});
	
	infowindow.open(map, marker);
}
/**
 * 
 * add the listener to add a 
 */
function markListener()
{
	google.maps.event.addListener(marker, 'dragend', function() {
		var point = marker.getPosition();
		$("#latitude").val(point.lat());
		$("#longitude").val(point.lng());
		console.log(point.lat() + ',' + point.lng());
	});
}
/**
 * Adds news marks to map to represent the businesses locations.
 * @param lat Business latitude.
 * @param long Business longitude.
 * @param name Businnes name.
 */
function addMarkLabel(lat, long, name,label,idx){
	label = "<h3>" + name + "</h3> <span>" + label + "</span>";
	var myLatlng = new google.maps.LatLng(lat, long);
	infowindow = new google.maps.InfoWindow();
	infowindow.setContent(label);
	infowindow.setPosition(myLatlng);
	marker = new google.maps.Marker({
	      map: map,
	      draggable: true,	      
	      position: myLatlng,
	      title:name,
	      zIndex: idx,
	      clickable: true
	      
	    });
	 infowindow.open(map, marker);
	google.maps.event.addListener(marker, 'click', function() {
		 
		 infowindow.open(map, marker);
		});
}

/**
 * Adds a list of marks to the map.
 * @param marks Marks array.
 */
function addMarks(marks){
	for (i = 0; i < marks.length; i++)
	{
		if (marks[i].lat != '' && marks[i].long != '')
		{
			addMark(marks[i].lat, marks[i].long, marks[i].name, i);
		}
	}
}
/**
 * add the businessess to an array to manage them on the map
 * @param latitude
 * @param longitude
 * @param name
 * @param labels
 */
function addResultMarks(latitude, longitude,name, labels)
{
	var biz = new Array();
	biz['lat'] = latitude;
	biz['lng'] = longitude;
	biz['name'] = name;
	biz['labels'] = labels;
	business[counter] = biz;
	counter ++;
}
/**
 * 
 * increment in 1 the idx
 */
function incIdx()
{
	idx ++;
	idx = idx == business.length ? 0 : idx; 
	showMark(idx);
}
/**
 * 
 * decrement un 1 the idx
 */
function decIdx()
{
	idx --;
	idx = idx == -1 ? (business.length - 1)  : idx;
	showMark(idx);
}
/**
 * add the mark from the indicate index 
 * @param i
 */
function showMark(i)
{
	if(marker != null)
	{
		marker.setMap(null);
	}
	var latlng = new google.maps.LatLng(business[i].lat, business[i].lng);
	map.setCenter(latlng);
	addMarkLabel(business[i].lat, business[i].lng,business[i].name,business[i].labels,1);
}
/**
 * Marker click event handler. Shows the name of the business.
 */
function toggleBounce(){
	if (marker.getAnimation() != null) 
	{
		marker.setAnimation(null);
	} 
	else 
	{
		marker.setAnimation(google.maps.Animation.BOUNCE);
	}
}

/**cookies*/

function setCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function deleteCookie(name) {
    setCookie(name,"",-1);
}

