function pk_array_index_of(obj, search) {
	for(var i=0; i<obj.length; i++){
		if(obj[i]==search) {
			return i;
		}
	}
	return -1;
}

function pk_validar_name_space(name) {
	var msg = null;
	var res = true;

	if (name.indexOf(' ')>=0) {
		msg = 'nao pode conter "espacos"';
		res = false;
	};

	if (name.indexOf('-')>=0) {
		msg = 'nao pode conter "hifens"';
		res = false;
	};

	if (!res) {
		pk_write_error('Namespace invalido: ' + name + ' (' + msg + ')');
	}

	return res;
}


function pk_show_wdg(parametros) {
	var referer_params = document.location.search;

	var referer_params_by_ns = {};
	if (referer_params) {
		referer_params = referer_params.substring(1).split('&');
		for (var i=0; i<referer_params.length; i++) {
			var name_value = referer_params[i].split('=');			
			var name = name_value[0];
			var value = name_value[1];
			
			var name_ns = name.split('-');
			if (name_ns.length>1) {
				var ns = name_ns[0];
				var name = name_ns[1];			
				if (!referer_params_by_ns[ns]) 
					referer_params_by_ns[ns] = {}
				referer_params_by_ns[ns][name] = value;
			}
		}
	}

	if (!parametros.cliente) {
		pk_write_error('Cliente nao informado');
		return;
	}
	
	if (!parametros.wdgs) {
		pk_write_error('Widget(s) nao informado(s)');
		return;
	}
	

	var qs_wgs_props = ''
	var ids = '';
	var list_name_spaces = [];
	var list_ids = [];
	for (var windex=0; windex<parametros.wdgs.length; ++windex) {
		var widget = parametros.wdgs[windex];
		var name_space = widget.ns.toLowerCase();
		var id = 'w' + windex;

		if (!name_space) {
			pk_write_error('Namespace nao definido');
			continue;
		}

		if (!pk_validar_name_space(name_space)) 
			continue;

		if (pk_array_index_of(list_name_spaces, name_space)<0)
			list_name_spaces[list_name_spaces.length] = name_space;		
		
		list_ids[list_ids.length] = id;
	
		if (ids)
			ids+= '+';
		ids+= id;
	
		for (var prop in widget) {
			
			if (qs_wgs_props)
				qs_wgs_props+= '&';
			
			var value = widget[prop];

			// se for uma lista
			if (typeof(value)=='object' && value instanceof Array)
				value = value.join(',');
			
			qs_wgs_props+= id + '-' + prop + '=' + value;
		}
		
	}

	// montando query string com os namespaces/values (somente que forem usados pelos widgets)
	var qs_name_spaces = '';
	var name_spaces = '';
	if (referer_params_by_ns)
		for (var i=0; i<list_name_spaces.length; ++i) {
			var name_space = list_name_spaces[i];
			if (name_space in referer_params_by_ns) {
				if (name_spaces)
					name_spaces+= '+';
				name_spaces+= name_space;
				for (var param in referer_params_by_ns[name_space]) {
					if (qs_name_spaces)
						qs_name_spaces+= '&';
					qs_name_spaces+= name_space + '-' + param + '=' + referer_params_by_ns[name_space][param];
				}
			}
		}
	if (qs_name_spaces) qs_name_spaces = '&' + qs_name_spaces;
	if (qs_wgs_props) qs_wgs_props = '&' + qs_wgs_props;

	if (!parametros.output) parametros.output = '';
	qs = '?cliente=' + parametros.cliente + '&' + 'output=' + parametros.output + '&ids=' + ids + '&nss=' + name_spaces + qs_name_spaces + qs_wgs_props;

	var script = '<script type="text/javascript" src="http://widgets.presskit.com.br/show_widget.php' + qs + '"><\/script>';
	document.writeln(script);
}

function pk_write(str) {
	document.writeln(str);
}

function pk_write_error(str) {
	document.writeln('<span style="display: block">');
	document.writeln(str);
	document.writeln('</span>');
}

function pk_parent(parent, str) {
	p = document.getElementById(parent);
	if (p)
		p.innerHTML = str;
}


function PresskitIterator(list) {

	this.list = list;
	this.index = -1;

	this.__current__ = function() {
		var r = this.list[this.index];
		r.__index__ = this.index;
		return r;
	}

	this.is_first = function () {
		return (this.index==0);
	}

	this.is_last = function () {
		return (this.index==this.count()-1);
	}

	this.next = function() {
		if (this.is_last())
			return false;
		else {
			++this.index;
			return this.__current__();								
		}
	}

	this.prior = function() {			
		if (this.is_first())
			return false;
		else {
			--this.index;
			return this.__current__();
		}
	}

	this.first = function() {
		this.index = 0;
		var r = this.__current__();
		this.index = -1;
		return r;
	}

	this.last = function() {
		if (this.count()>0) {
			this.index = this.count()-1;
			var r = this.__current__();
			this.index = this.count();
			return r;
		}
	}

	this.count = function() {
		return this.list.length;
	}

	this.get_by_id = function (id) {

		for (var i=0; i<this.list.length; ++i)
			if (this.list[i].id==id) {
				this.index = i;
				return this.__current__();
			}

		return false;
	}
}

