
var dsgn_favorites = {};

dsgn_favorites.vars = {
	addString: 'Add to favorites',
	removeString: 'Remove from favorites',
	templatePath: 'design/widgets/favouriteswidget.tpl',
	addAction: 'DSGNAddFavorite',
	removeAction: 'DSGNRemoveFavorite',
	favoritesListContainer: 'userfavoritescontainer',
	systemFavoritesListContainer: 'systemfavoritescontainer',
	showAction: 'DSGNShowFavorite',
	systemTemplatePath: 'design/widgets/listingwidget.tpl'
}

dsgn_favorites.addFavorite = function(target, favoriteId) {
	dsgn_favorites.doAddFavorite(favoriteId, target);
	return false;
}

dsgn_favorites.setToAdd = function(target, favoriteId) {
	$(target).innerHTML = dsgn_favorites.vars.addString;
	$(target).onclick = dsgn_favorites.addFavorite.bind(window, [target, favoriteId]);
}

dsgn_favorites.doAddFavorite = function(favoriteId, target) {

	var postObject = {
		action: dsgn_favorites.vars.addAction,
		fid: favoriteId,
		templatePath:dsgn_favorites.vars.templatePath
	}
	
	var request = new Json.Remote('/sbeos/ajax/JsonCall.php?rnd=' + Math.random(), {onComplete:dsgn_favorites.onAddFavoriteComplete.bindAsEventListener(window, [target, favoriteId])});
	request.send(postObject);
	
}

dsgn_favorites.onAddFavoriteComplete = function(JsonResponse, target, favoriteId) {

	if(JsonResponse.success != true && JsonResponse.success != 'true') {
		alert(JsonResponse.message);
	} else {
		dsgn_favorites.setToRemove(target, favoriteId);
		if($chk(JsonResponse.template)) {
			dsgn_favorites.setFavoritesView(JsonResponse.template);
		}
	}
}

dsgn_favorites.removeFavorite = function(target, favoriteId) {
	dsgn_favorites.doRemoveFavorite(favoriteId, target);
	return false;
}

dsgn_favorites.setToRemove = function(target, favoriteId) {
	$(target).innerHTML = dsgn_favorites.vars.removeString;
	$(target).onclick = dsgn_favorites.removeFavorite.bind(window, [target, favoriteId]);
}

dsgn_favorites.doRemoveFavorite = function(favoriteId, target) {
	
	var postObject = {
		action: dsgn_favorites.vars.removeAction,
		fid: favoriteId,
		templatePath:dsgn_favorites.vars.templatePath
	}
	
	var request = new Json.Remote('/sbeos/ajax/JsonCall.php?rnd=' + Math.random(), {onComplete:dsgn_favorites.onRemoveFavoriteComplete.bindAsEventListener(window, [target, favoriteId])});
	request.send(postObject);
	
}

dsgn_favorites.onRemoveFavoriteComplete = function(JsonResponse, target, favoriteId) {
	
	if(JsonResponse.success != true && JsonResponse.success != 'true') {
		alert(JsonResponse.message);
	} else {
		dsgn_favorites.setToAdd(target, favoriteId);
		if($chk(JsonResponse.template)) {
			dsgn_favorites.setFavoritesView(JsonResponse.template);
		}
	}
}

dsgn_favorites.setFavoritesView = function(tplstring) {
	var oldContainer = $(dsgn_favorites.vars.favoritesListContainer);
	oldContainer.id = oldContainer.id+'_old';
	
	var tmpElm = new Element('div');
	tmpElm.innerHTML = tplstring;

	oldContainer.replaceWith(tmpElm.getFirst());
}

dsgn_favorites.viewSystemFavorites = function(target) {
	if (target.selectedIndex > 0) {
		var favoriteId = target[target.selectedIndex].value;
		if (favoriteId != "" && favoriteId != null)
		dsgn_favorites.doShowFavorite(favoriteId, target);
	}
	return false;
}

dsgn_favorites.doShowFavorite = function(favoriteId, target) {

	var postObject = {
		action: dsgn_favorites.vars.showAction,
		sysfav: favoriteId,
		templatePath:dsgn_favorites.vars.systemTemplatePath
	}
	
	var request = new Json.Remote('/sbeos/ajax/JsonCall.php?favorite=' + favoriteId + '&rnd=' + Math.random(), {onComplete:dsgn_favorites.onShowFavoriteComplete.bindAsEventListener(window, [target, favoriteId])});
	request.send(postObject);
	
}

dsgn_favorites.onShowFavoriteComplete = function(JsonResponse, target, favoriteId) {

	if(JsonResponse.success != true && JsonResponse.success != 'true') {
		alert(JsonResponse.message);
	} else {
		if($chk(JsonResponse.template)) {
			dsgn_favorites.setSystemFavoritesView(JsonResponse.template);
		}
	}
}
dsgn_favorites.setSystemFavoritesView = function(tplstring) {
	var oldContainer = $(dsgn_favorites.vars.systemFavoritesListContainer);
	oldContainer.id = oldContainer.id + '_old';
	
	var tmpElm = new Element('div');
	tmpElm.innerHTML = tplstring;
	oldContainer.replaceWith(tmpElm.getFirst());
}
