function showPhotosOnMainPage(category){
	var table= document.getElementById('bottable');
	table.deleteRow(1);
	var newRow=table.insertRow(1);
	var tmp = photospercat[category];
	for (index in tmp){
		var newCell=newRow.insertCell(0);
		newCell.innerHTML="<a  href=\"http://dvrom.eu/"+sel_lang+"/"+images_array[tmp[index]][post_name]+"/\" ><img style=\"-moz-box-shadow: 5px 5px 5px #000000;-webkit-box-shadow: 5px 5px 5px #000000;box-shadow: 5px 5px 5px #000000;\" title=\""+images_array[tmp[index]][post_title]+"\" alt=\""+images_array[tmp[index]][post_title]+"\" src=\""+images_array[tmp[index]][photo_bot_th_url]+"\" style=\"border:2px solid white\" /></a>";
		newCell.setAttribute('align', 'center');
	}
}

function addPhotoToMap(in_js_key){
	var latBox = 0.0193729;
	var longBox = 0.0241577;
	var longdec=images_array[in_js_key][photo_long_dec];
	var latdec=images_array[in_js_key][photo_lat_dec];
	var pre_url=images_array[in_js_key][photo_pre_th_url];
	var pointSW = new GLatLng(latdec-latBox ,longdec-longBox);
	var pointNE = new GLatLng(latdec+latBox ,longdec+longBox);
	var groundOverlay = new GGroundOverlay(pre_url,new GLatLngBounds(pointSW, pointNE));
        addBorderToPhoto(pointNE,pointSW,in_js_key);
	map.addOverlay(groundOverlay);
}

function addBorderToPhoto(pointNE,pointSW,in_js_key){
	var topLat = pointNE.lat(); 
	var rightLng = pointNE.lng(); 
	var botLat = pointSW.lat(); 
	var leftLng = pointSW.lng(); 
	var boxPoints = []; 
	boxPoints.push(pointSW); 
	boxPoints.push(new GLatLng(topLat,leftLng)); 
	boxPoints.push(pointNE); 
	boxPoints.push(new GLatLng(botLat,rightLng)); 
	boxPoints.push(pointSW);
	var linewidth = 2;
	var polygon = new GPolygon(boxPoints, "#ffffff", linewidth , 1, "#ffffff", 0);
	GEvent.addListener(polygon, "mouseover", function() {
		highlightPhoto(in_js_key);
	});	
	GEvent.addListener(polygon, "mouseout", function() {
		unHighlightPhoto(in_js_key);
	});
	GEvent.addListener(polygon, "click", function() {
		goToPhoto(in_js_key);
	});
	map.addOverlay(polygon);
        polygons[in_js_key] = polygon;
}

function goTo(Category){
     centerAndZoom(new GLatLng(center_and_zoom_per_cat[Category][0],center_and_zoom_per_cat[Category][1]),center_and_zoom_per_cat[Category][2]);
}

function goToPhoto(in_js_key){
	var longdec=images_array[in_js_key][photo_long_dec];
	var latdec=images_array[in_js_key][photo_lat_dec];
	var my_point = new GLatLng(latdec,longdec);
    centerAndZoom(my_point,9);
    //window.open(images_array[in_js_key][guid], images_array[in_js_key][post_title]);
}


function centerAndZoom(inGLatLng,inZoom){
	map.setCenter(inGLatLng,inZoom);
}

function highlightPhoto(image_key){
        showRedBorderMarker(image_key);
        showRedBorderBot(image_key);
        setPhoto(image_key);
        setPhotoTitle(image_key);
        //setPhotoCategory(image_key);
}

function showRedBorderMarker(image_key){
	polygons[image_key].setStrokeStyle({color:"#ff0000"});
}

function showRedBorderBot(image_key){
	var element = document.getElementById(image_key);
	if (element){
		element.style.border="2px solid red";
	}	
}

function setPhoto(image_key){
	var cell = document.getElementById('prev_image');
        if (sel_lang=='es/'){
           var textdes = "Ver Detalles";
        } else if (sel_lang=='ca/'){
           var textdes = "Veure Detalls";
        } else {
          var textdes = "View Details";
        }
	var text="<a  href=\"http://dvrom.eu/"+sel_lang+images_array[image_key][post_name]+"/\" target=\"_blank\"><img title=\""+images_array[image_key][post_title]+"\" alt=\""+images_array[image_key][post_title]+"\" src=\""+images_array[image_key][photo_pre_th_url]+"\" onmouseout=\"javascript:showWhiteBorderMarker('"+image_key+"');showWhiteBorderBot('"+image_key+"');\" onmouseover=\"javascript:showRedBorderMarker('"+image_key+"');showRedBorderBot('"+image_key+"');\"/></a><br><br><a  href=\"http://dvrom.eu/"+sel_lang+images_array[image_key][post_name]+"/\" target=\"_blank\">"+textdes+"</a>";
        cell.innerHTML=text;
}

function setPhotoTitle(image_key){
        var cell_desc_cat = document.getElementById('image_description');
	cell_desc_cat.innerHTML="<h1>"+images_array[image_key][post_title]+"</h1>";
}


function setPhotoCategory(image_key){
        var cell_desc_cat = document.getElementById('image_category');
	cell_desc_cat.innerHTML="<h1>"+images_array[image_key][photo_category]+"</h1>";
}

function showPhotos(category){
	var table= document.getElementById('bottable');
	table.deleteRow(0);
	var newRow=table.insertRow(0);
	var tmp = photospercat[category];
	for (index in tmp){
		var newCell=newRow.insertCell(0);
		newCell.innerHTML="<a href=\"javascript:onmouseclick=goToPhoto(\'"+tmp[index]+"\');\"><img title=\""+images_array[tmp[index]][post_title]+"\" alt=\""+images_array[tmp[index]][post_title]+"\" src=\""+images_array[tmp[index]][photo_bot_th_url]+"\" id=\""+tmp[index]+"\" onmouseover=\"highlightPhoto(\'"+tmp[index]+"\');\" onmouseout=\"unHighlightPhoto(\'"+tmp[index]+"\');\" style=\"border:2px solid white\" /></a>";
	}
        setPhoto(tmp[index]);
        setPhotoTitle(tmp[index]);
        //setPhotoCategory(tmp[index]);
}


function unHighlightPhoto(photoname){
       showWhiteBorderMarker(photoname);
       showWhiteBorderBot(photoname);
}

function showWhiteBorderBot(photoname){
	var element = document.getElementById(photoname);
	if (element){
		element.style.border="2px solid white";
	}
}


function showWhiteBorderMarker(photoname){
	polygons[photoname].setStrokeStyle({color:"#ffffff"});
}
