/**
* Opens Report Broken Listing window
*
* @param int id listing id
*/
fav_pressed = 0;
function actionFavorites(listing_id, account_id, source_id, action, refresh, remove_phrase, add_phrase)
{
	if (!fav_pressed) {
		fav_pressed = 1;
		if (action == 'add') {
			$('#favbutton_on' + listing_id).html('<span title="Stirea a fost adaugata la favorite"></span>');
		}
		var msg = ('add' == action) ? add_phrase : remove_phrase;
		$.post("favorites.php", {
			account_id: account_id,
			listing_id: listing_id,
			source_id: source_id,
			action: action,
			refresh: refresh
		}, function(data){
			fav_pressed = 0;
			if (action == 'add') {
				text = 'Stirea a fost marcata ca favorita.';
			}
			else {
				text = 'Stirea a fost stearsa din lista de favorite.';
				$('#table' + listing_id).remove();
			}
			$('#__msg').text();
			$('#__msg').html('<div class="notification"><li>' + text + '</li></div>');
		});
		
		return false;
	}
}

var FavCats = new function() {
	
	this.add = function() {
		values = this.getValues('catList');
		if(values == null) {
			return;
		};
		newvalues = this.exists(values);
		if(newvalues != false) {
			texts  = this.getTexts('catList', newvalues);
			for(i = 0; i < newvalues.length; i++) {
				option = '<option value="' + newvalues[i] + '">' + texts[i] + '</option';
				$('#favcats').append(option);
			}
		}
	}
	
	this.rem = function() {
		values = this.getValues('favcats');
		if(values == null) {
			return;
		}
		$.each($('#favcats > option'), function(i, item){
			if($.inArray($(item).attr('value'), values) != -1) {
				$(item).remove();
			}
		})
	}
	
	this.exists = function(values) {
		new_values = new Array();
		options = $('#favcats > option');
		if (options.length != 0) {
			selectedvalues = new Array();
			for(i = 0; i < options.length; i++) {
				selectedvalues[i] = $(options[i]).val();
			}
			for(i = 0; i < values.length; i++) {
				if($.inArray(values[i], selectedvalues) == -1) {
					new_values[new_values.length] = values[i];
				}
			}
			if(new_values.length == 0) {
				return false;
			};
			return new_values;
		} else {
			return values;
		}
	}
	
	this.getValues = function(id) {
		return $('#'+id).val();
	}
	
	this.getTexts = function(id, values) {
		texts = new Array();
		$.each($('#' + id + '> option'), function(i, item) {
			if($.inArray($(item).attr('value'), values) != -1) {
				text = $(item).html();
				text = text.replace(/(&.*;)/, '');
				texts[texts.length] = text;
			}
		})
		if(texts.length == 0) {
			return false;
		};
		return texts;
	}
	
	this.addInline = function(catid, _url) {
		$.post("favorites.php", {
			action: 'addfavcat',
			category_id: catid,
			url: _url
		}, function(data){
			$('#__msg').text();
			$('#__msg').html('<div class="notification"><li>Categoria a fost adaugata la favorite</li></div>');
			$('#addFavCat').remove();
		});
	}
	
}

function changeWeather() {
	loc = $('#selectWeather').val();
	$.post('getWeather.php?inline=1&region='+loc, function(data) {
		if(data.length) {
			$('#weatherReport').html(data);
		}
	});
}
