var ls_registry = new Array();

function Leadform() {
	
	this.ref =  document.referrer;
	this.params = window.location.search;
	
	this.return_page = function(url){
		this.ref = url;
	}

	this.set_params = function(p){
		this.params = p;
	}
	
	this.render = function(form_id){
	    var url = 'http://leadsale.ru/client/form.js' + this.params + '&form_id='+form_id+'&_ref='+encodeURI(this.ref) + '&_path=' + encodeURI(window.location.pathname);
		insertScriptTag(url);
	}

}

function Leadsource() {
    
	this.ref_id = document.referrer;
	
	this.site = function(s){
	  this.site_id = s;
	}

	this.section = function(s){
	  this.section_id = s;
	}

	this.variant = function(s){
	  this.variant_id = s;
	}
	
	this.referer = function(s){
	  this.ref_id = s;
	}
	
    this.record = function(){
       insertScriptTag('http://leadsale.ru/client/source.js',  {'_sr': encodeURIComponent(this.ref_id), '_sl': encodeURIComponent(window.location), '_si' : this.site_id, '_se' : this.section_id, '_va' : this.variant_id});   
	}
}

function Leadsale(quote){
		
		this.ls_index = ls_registry.push(this) - 1;		
	    this.quote = quote;
				
		this.form = function(form_id){
			this.form_id = form_id;			
		}

		this.render = function(ad_id){
			insertScriptTag('http://leadsale.ru/client/ads.js', {'_q': this.quote, 'ad_id': ad_id, 'clickable': 'ls_registry['+this.ls_index+']'});
		}
		
		this.createForm = function(){
			leadform = new Leadform();
			leadform.set_params(window.location.search + '&_q='+this.quote);
			leadform.return_page(window.location);
			return leadform;
		}

		
		this.offers = function(html_tag, classname, attribute){
		    			
	        var html_tags = document.getElementsByTagName(html_tag.toUpperCase());
			for(var i=0;i<html_tags.length;i++){
			  var html_class = html_tags[i].className;
			  var html_id = html_tags[i].getAttribute(attribute);
			  if(html_class!=null && html_class.indexOf(classname) >=0 && html_id!=null){
 			    this.decorate_offer(html_tags[i], html_id);
			  	html_tags[i].removeAttribute(attribute);
			  }
			}
		}
		
		this.decorate_offer = function(element, tag){
		  if(element.id==null || element.id==''){
			element.id = element.tagName + Math.floor(Math.random()*100000000);
		  }
		  this.decorate_element(element, element.id, tag);
		}
		
		this.decorate_element = function(element,id,tag){
		  if(element.tagName=='A'){
            ls = this;
		    element.onclick = function() { ls.offer(this,tag,id); return false; };
		  }
		  var children = element.childNodes;
		  for(var i=0;i<children.length;i++){
		    this.decorate_element(children[i], id, tag);
		  }
		}
		
		this.offer = function(clicked, tag, id){
		  var element = document.getElementById(id);
		  var text = this.convert_text(element);
		  var form = this.insertForm({'clicked_url' : clicked.href, 'clicked_text' : encodeURI(clicked.innerHTML), 'page_url' : document.location, 'selection' : encodeURI(text), 'tag' : tag, '_q' : this.quote, 'form_uri' : this.form_id});
		  form.submit();
		}
		
		this.insertForm = function(fields){
		  var form = document.createElement("form");
		  form.method = 'post';
		  form.action = 'http://leadsale.ru/client/post_offer';
		  for(field in fields){
		    var hidden = document.createElement("input");
			hidden.type = 'hidden';
			hidden.name = field;
			hidden.value = fields[field];
			form.appendChild(hidden);
		 }
		  
		  document.body.appendChild(form);
		  return form;
		}
		
		this.convert_text = function(element){
		  var text = '<' + element.tagName + '>';
		  text = text + element.innerHTML;
		  text = text + '</' + element.tagName + '>';
		  
		  if(element.tagName=='TR'){
		     if(element.parentNode.children[0]!=element && element.parentNode.children[0].tagName=='TR'){
  		      text = '<table><tr>' + element.parentNode.children[0].innerHTML + '</tr>' + text + '</table>';
			 }else{
  		      text = '<table>' + text + '</table>';
			 }
		  }
		  return text;
		}

		this.click = function(campaign){
		    var url = this.form_id +  (this.form_id.indexOf('?') > 0 ? '&' : '?' );
			url += '_q=' + this.quote + '&_c=' + campaign;
			window.location.assign(url);
		}
		


}


	    insertScriptTag = function(url, params) {
		
  		  var scripturl = url;
		  if(params !=null ){
		    var first = true;
			for (var key in params) {
			    if(typeof( params[key] ) != 'undefined'){
  			     scripturl += first ? '?' : '&';
				 first = false;
				 scripturl += key;
				 scripturl += '=';
				 scripturl += params[key];
				}
			}
		  }

         document.write('<script src="'+scripturl+'" type="text/javascript"></script>');
	   }
	


