function PVLogin(id,cfg){
    this.elementId = id;
    this.init(cfg);
}

PVLogin.prototype.add_javascript = function(jsname,pos) {
    var th = document.getElementsByTagName(pos)[0];
    var s = document.createElement('script');
    s.setAttribute('type','text/javascript');
    s.setAttribute('src',jsname);
    th.appendChild(s);
};

PVLogin.prototype._proccess_json = function(events){
	for(var i = 0; i < events.length; i++){
		var data = events[i];
		if(data.event == 4){
			this.iframe.attr("width",data.json_content.width+"px").attr("height",data.json_content.height+"px");
			this.onChangeSize.call(this,data.json_content.width,data.json_content.height);
		} else if(data.event == 3) {
			this.onLogin.call(this,data.json_content);
		}
	}
};

PVLogin.prototype._check_message = function(params){
	$.ajax({
		url: "http://auth.provida.org.br/novo/getevent.php",
		context: this,
		dataType: "jsonp",
	    type: "post",
	    data: params,
	    success: function(data,textStatus){
	    	this._proccess_json(data);
	    },
		error: function(){
		},
		complete: function(){
		}
	});			        	
};

PVLogin.prototype.init = function(cfg){
	this.configurations = {
		lang: null
	};
	
	for(i in cfg){
		this.configurations[i] = cfg[i];
	}
	
    this.add_javascript("http://auth.provida.org.br/novo/js/json2.js","head");
    this.add_javascript("http://auth.provida.org.br/novo/js/jquery.min.js","head");
    var self = this;
    
    this.wait_for_jquery(function(){
        this.element = jQuery(this.elementId); 
        $.ajax({
        	url: "http://auth.provida.org.br/novo/vars.php?r="+Math.random(),
        	context: self,
        	dataType: "jsonp",
        	type: "get",
        	cache: false,
        	success: function(data, textStatus){
        		var url = document.location.href;
		        this.iframe = jQuery("<iframe width=\"470\" height=\"345\" scrolling=\"no\" frameborder=\"0\"></iframe>");
   				this.onChangeSize.call(this,470,345);
   				
   				var str_lang = "";
   				if(this.configurations["lang"]){
   					str_lang = "&lang="+this.configurations["lang"];
   				}
   				
		        this.iframe.attr("src","http://auth.provida.org.br/novo/form.php?uid="+data.uid+"&ip="+data.ip+str_lang+"&url="+url);
		        this.element.append(this.iframe);
		        
		        var self = this;
		        var obj = {
		        	uid: data.uid,
		        	ip: data.ip
		        };
		        
		        var params = jQuery.param(obj);
		        
		        // Para os navegadores antigos sem suporte a HTML5
		        if(window.postMessage){
		        	// Para navegadores novos, html5, com suporte a comunicação
		        	// entre janelas
		        	var fn = function(evt){
		        		if(evt.originalEvent.origin === "http://auth.provida.org.br"){
		        			if(evt.originalEvent.data == "changed"){
		        				self._check_message(params);
		        			}
		        		}
		        	};
		        	$(window).bind("message",fn);
		        	$(window).bind("onmessage",fn);
		        } else {
			        setInterval(function(){
			        	var hash_part = document.location.hash;
		    	    	if(hash_part.indexOf("changed") > 0){
			    	    	document.location.hash = "";
		        			self._check_message(params);
				    	}
		        	},500);
		        }	
        	}
        });
    });
};

PVLogin.prototype.wait_for_jquery = function(callback){
    var self = this;
    var timeout = setInterval(function(){
        if(typeof jQuery != "undefined" && typeof JSON != "undefined"){
            clearInterval(timeout);
            callback.call(self);
        }
    },500);
};

PVLogin.prototype.onLogin = function(user){
};

PVLogin.prototype.onChangeSize = function(width, height){
};

