/*
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2006 Bharat Mediratta
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */

/**
 * This view shows the top most taged persons/objects/whatever.
 *
 * @package Imagetags
 * @author Björn Samvik <p04bs@student.lth.se>
 */

var viewing = true;
var tagging = false;

function el(element)
{
  return (typeof element == "string" ?
	  document.getElementById(element) : element);
}

function init_tagging() {
	$('img#IFid1').imgAreaSelect({ selectionColor: 'blue', onSelectEnd: endSelection }); 
	tagging = true;
};

function endSelection(img, selection) {
	if(selection.width > 20 && selection.height > 20) {

		el('tagX').value = selection.x1;
		el('tagY').value = selection.y1;
		el('tagWidth').value = selection.width;
		el('tagHeight').value = selection.height;

		if(!viewing) {
			data = findPos(el('IFid1'))
			el('addTag').style.top = (selection.y1+data.top+selection.height+10) +'px';
			el('addTag').style.left = (selection.x1+data.left) +'px';
			el('addTag').style.visibility = 'visible';
			el('addTag').style.zIndex = "20";
			el('tagTag').focus();
		}
	}
	else {
		el('addTag').style.visibility = 'hidden';
	}
}

function refreshPage(http) {
	window.location.reload( true );
}

function newItem(url) {
	var tagTag = document.getElementById('tagTag').value;
	var imgId = document.getElementById('imgId').value;
	var imgWidth = document.getElementById('imgWidth').value;
	var imgHeight = document.getElementById('imgHeight').value;
	var tagX = document.getElementById('tagX').value;
	var tagY = document.getElementById('tagY').value;
	var tagWidth = document.getElementById('tagWidth').value;
	var tagHeight = document.getElementById('tagHeight').value;

	

	url = url.replace("__TAG__", encodeURIComponent(tagTag));
	url = url.replace("__ID__", encodeURIComponent(imgId));
	url = url.replace("__IMG_WIDTH__", encodeURIComponent(imgWidth));
	url = url.replace("__IMG_HEIGHT__", encodeURIComponent(imgHeight));
	url = url.replace("__TAG_X__", encodeURIComponent(tagX));
	url = url.replace("__TAG_Y__", encodeURIComponent(tagY));
	url = url.replace("__TAG_WIDTH__", encodeURIComponent(tagWidth));
	url = url.replace("__TAG_HEIGHT__", encodeURIComponent(tagHeight));
	
	YAHOO.util.Connect.asyncRequest(
		'GET', url, {success: refreshPage, failure: null, scope: null}, null);
	
}

function removeItem(url) {
    YAHOO.util.Connect.asyncRequest(
        'GET', url, {success: refreshPage, failure: null, scope: null}, null);
}


function newTag(url) {
	var tagName = document.getElementById('name_form').value;
	var tagYear = document.getElementById('year_form').value;
	
	url = url.replace("__NAME__", encodeURIComponent(tagName));
	url = url.replace("__YEAR__", encodeURIComponent(tagYear))

	document.getElementById('name_form').value = "";
	document.getElementById('year_form').value = "";
	document.getElementById('tagTag').focus();
	document.getElementById('tagTag').value = tagName + ' ' + tagYear;

	YAHOO.util.Connect.asyncRequest(
 'GET', url, {success: null, failure: null, scope: null}, null);
}


function taggingMode() {
	viewing = false;

	if(!tagging)
		init_tagging();

	el('area').style.visibility = 'visible';
	el('border1').style.visibility = 'visible';
	el('border2').style.visibility = 'visible';
	el('area').style.zIndex = 10;
	el('border1').style.visibility = 10;
	el('border2').style.visibility = 10;
	
	hideFrame();

	el('IFid1').useMap="";
	el('IFid1').style.border = "3px solid red";
}

function viewingMode() {

	viewing = true;

	if(tagging) {
		el('area').style.visibility = 'hidden';
		el('border1').style.visibility = 'hidden';
		el('border2').style.visibility = 'hidden';
		el('area').style.zIndex = -10;
		el('border1').style.visibility = -10;
		el('border2').style.visibility = -10;
	
		el('addTag').style.visibility = 'hidden';
	}

	el('IFid1').useMap="map";
	el('IFid1').useMap="#map";
	el('IFid1').style.border = "0px solid red";
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
	while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return {left:curleft,top:curtop} //[curleft, curtop]
} 

function showFrame(msg, x0, y0, w, h) {
  
	data = findPos(el('IFid1'))

	rx = x0 + data.left
	ry = y0 + data.top
		
	document.getElementById("img_frame").style.top = ry + 'px'
	document.getElementById("img_frame").style.left = rx + 'px'
	document.getElementById("img_frame").style.width = w + 'px'
	document.getElementById("img_frame").style.height = h + 'px'

	document.getElementById("img_frame").style.visibility = "visible"
	document.getElementById("msg_frame").innerHTML = msg;
	  
}

function hideFrame() {
	document.getElementById("img_frame").style.visibility = "hidden"
}

function hideTagFrame() {
	el('addTag').style.visibility = "hidden";
}