/**
// Copyright (C) 2008 - 2010 Mike Feng Jinglong
//
// This program is a commercial software; you cannot redistribute it
// and/or modify it without the explicit consent from the tagged.
//
// All title and copyright in and to the Product is owned by Mike Feng
// and is protected by international copyright laws and treaty provisions.
// Mike Feng retains all ownership rights in the Product.
// The copyright notice must be reproduced and included on any copy
// that is expressly permitted under this Agreement.
**/
function startTagging() {
	if (cropping) cancelCropping();
	tagging = true;
	$('currentphoto').style.cursor = 'crosshair';
	$('photo-container').getElements('div[name=frame]').addClass('photo_tag_frame');
	$('photo-container').getElements('div[name=frame]').getElement('div').addClass('photo_tag_frame_inside');
	$('tag-info').toggle();
	$('tag-info').getElement('span').set('html', $('tag-info').getElement('input[type=hidden]').get('value'));
	new Fx.Scroll(window).toElement($('photo-scroll-top'));
	$('tag-info').getElement('input[type=button]').removeEvents('click');
	$('tag-info').getElement('input[type=button]').addEvent('click', function() { doneTagging(); });
}

function addTag(x, y, pic_id) {
	if (document.getElements('.pn-tagging-loading').length > 0) return;
	var tag = new PhotoNote('photo-container', {
		'point': {
			'x': x,
			'y': y
		},
		'size': tagDimensions,
		'info_line_1': SG_TAG_INFO_LINE1,
		'info_line_2': SG_TAG_INFO_LINE2,
		'tag_btn': SG_TAG_BTN,
		'cancel_btn': SG_CANCEL_BTN,
		'onShow': function(tag) {
			tag.nameInput.addEvent('keyup', function(e) {
				if (e.key == 'enter' && (tag.nameInput.get('value').trim() != '' || tag.Selector.getElements(':checked').length>0)) {
					tag.Save(tag);
					return;
				}
				loadFriendsList(tag, pic_id);								   
			});
			loadFriendsList(tag, pic_id);	
		},
		'onSave': function(tag) {
			saveTag(tag, pic_id);
		}
	});
}

function loadFriendsList(tag, id) {
	var name = tag.nameInput.get('value');
	var url = sgurlraw+'&func=tagcompleter&id='+id;
	var myReq = new Request({
		'url': url,
		'method': 'post',
		'data': {
			'value': name
		},
		onRequest: function() {
			tag.nameInput.addClass('autocompleter-loading');
		},
		onSuccess: function(responseText) {
			tag.nameInput.removeClass('autocompleter-loading');
			$('pn-userlist').empty();
			new Element('div', {'html': responseText}).getChildren().inject('pn-userlist');
			var users = $('pn-userlist').getElements('label');
			if (users.length == 1)
				users[0].getElement('input[type=checkbox]').checked = true;
			else if (users.length > 1) {
				users.each(function(el) {
					el.addEvent('click', function(e) {
						e.stop();
						$('pn-userlist').getElements('input[type=checkbox]').set('checked', false);
						el.getElement('input[type=checkbox]').checked = true;
						tag.Save(tag);
					});
				});
			}
		},
		onFailure: function(){
			tag.nameInput.removeClass('autocompleter-loading');
			Sexy.error("<strong>"+SG_AJAX_ERROR+"</strong>");
		}
	}).send();
}

function saveTag(tag, id) {
	var url = sgurlraw+"&func=savetag&id="+id;
	new Request.JSON({
		'url': url,
		'method': 'post',
		'data': {
			'tagArray': tag
		},
		onRequest: function() {
			tag.Frame.addClass('pn-tagging-loading');
		},
		onSuccess: function(responseJSON, responseText) {
			tag.Nametag.fade('out');
			tag.Frame.removeClass('pn-tagging-loading');
			if (responseJSON.error == 1) {
				tag.Cancel(tag);
				Sexy.error(responseJSON.message);
				return;
			}
			
			tag.Frame.set('frame', responseJSON.tagID);
			$('tags-list').set('html', responseJSON.tagsList);
			initTagsListEvents()
			$('tag-info').getElement('span').set('html', responseJSON.message);
			new Fx.Scroll(window).toElement($('photo-scroll-top'));
		},
		onFailure: function(){
			tag.Frame.removeClass('pn-tagging-loading');
			Sexy.error("<strong>"+SG_AJAX_ERROR+"</strong>");
			tag.Cancel();
		}
	}).send();
}

function drawTags(photo, hashes) {
	hashes.each(function(tag) {
		var note = new PhotoNote('photo-container', {
			'tagId': tag.tagid,
			'name': tag.nametag,
			'point': {
				'x': tag.tagArray.left,
				'y': tag.tagArray.top
			},
			'size': tag.tagArray.height
		});
	});
	//make the tags themselves taggable as they're blocking the photo
	$('photo-container').getElements('div[name=frame]').cloneEvents('currentphoto');
}

function redrawTags(hashes) {
	hashes.each(function(tag) {
		var thisTag = $('photo-container').getElement('div[frame='+tag.tagid+']');
		thisTag.setStyles({
			'top': tag.tagArray.top+'px',
			'left': tag.tagArray.left+'px'
		});
	});
}

function initTagsListEvents() {
	$('tags-list').getChildren('span.tags-list-item').addEvents({
		'mouseenter': function() {
			if (tagging) return;
			$('photo-container').getElements('div[frame='+this.get('frame')+']').addClass('photo_tag_frame');
			$('photo-container').getElements('div[frame='+this.get('frame')+']').getElement('div').addClass('photo_tag_frame_inside');
		},
		'mouseleave': function() {
			if (tagging) return;
			$('photo-container').getElements('div[frame='+this.get('frame')+']').removeClass('photo_tag_frame');
			$('photo-container').getElements('div[frame='+this.get('frame')+']').getElement('div').removeClass('photo_tag_frame_inside');
		}
	});
}

function doneTagging() {
	tagging = false;
	$('tag-info').toggle();
	$('currentphoto').style.cursor = 'pointer';
	$('photo-container').getElements('div[frame=-1]').destroy();
	$('photo-container').getElements('div[id=pn-tag-selector]').destroy();

	$('photo-container').getElements('div[name=frame]').removeClass('photo_tag_frame');
	$('photo-container').getElements('div[name=frame]').getElement('div').removeClass('photo_tag_frame_inside');
}

function deleteTag(tag_id) {
	var url = sgurlraw+"&func=deletetag&tag_id="+tag_id;
	new Request.JSON({
		'url': url,
		'method': 'get',
		onSuccess: function(responseJSON, responseText) {
			if (!responseJSON.error) {
				$('photo-container').getElement('div[name=frame][frame='+tag_id+']').destroy();
				$('tags-list').getElement('span[frame='+tag_id+']').destroy();
				if ($('tags-list').getChildren('span.tags-list-item').length == 0)
					$('tags-list').empty();
			}
			else
				Sexy.error(responseJSON.message);
		},
		onFailure: function(){
			Sexy.error("<strong>"+SG_AJAX_ERROR+"</strong>");
		}
	}).send();
}
