// Copyright opiniac.com
// version 1.10, 5.02.2010
function _Opiniac()
{
  if (arguments.length < 1)
    return null;
    
  this.opiniac_hash = arguments[0];  
  this.add_params = new Object();
  this.link_text = '';
  this.pictogram_location = '/opiniac/opiniac_pictogram.gif';
  this.config = {
    block: {
	  background: null,
	  border: null
    },
    remove_after_click: true
  };

  
  //--------------------------------------------------------------------
  this.addBackground = function()
  {
	this.setOptions({background: true});
	return this;
  }

  
  //--------------------------------------------------------------------
  this.addBorder = function()
  {
	this.setOptions({border: true});
	return this;
  }

  
  //--------------------------------------------------------------------
  this.setOptions = function(_options)
  {
	for (var i in _options) {
		switch (i) {
			case 'background':
				if (_options[i] == true)
					this.config.block.background = '#ffffff';
				else
					this.config.block.background = _options[i];
				break;
				
			case 'border':
				if (_options[i] == true)
					this.config.block.border = '1px solid #777777';
				else
					this.config.block.border = _options[i];
				break;
		}
	}
	return this;
  }

  
  //--------------------------------------------------------------------
  this.getLinkText = function(_escape)
  {
  	if (_escape)
  		return this.link_text.replace(/"/g,'&quot;');
  				
    return this.link_text + '';
  }

  
  //--------------------------------------------------------------------
  this.setLinkText = function(txt)
  {
	this.link_text = txt;
	return this;
  }

  
  //--------------------------------------------------------------------
  this.segment = function(segment)
  {
    this.addParam('segment', segment);
    return this;
  } 


  //--------------------------------------------------------------------
  this.addParam = function(key, val)
  {
    if (key in {'s':'', 'c':'', 'r':'', 'u':'', 't':''})
      return this;
      
    this.add_params[key] = val;
    return this;
  } 
  
  
  //--------------------------------------------------------------------
  this.onclick = function()
  {
    var p = '';
    
    if (window.screen) {
      p += "/s/"+window.screen.width+"x"+window.screen.height;
      if (!isNaN(screen.pixelDepth) && (screen.pixelDepth) != 0) {
        p += "/c/"+screen.pixelDepth;
      } else {
        p += "/c/"+screen.colorDepth;
      }
    }
    if (document.referrer)
      p += "/r/"+encodeURIComponent((document.referrer).substring(0,299));
    if (document.URL)
      p += "/u/"+encodeURIComponent((document.URL).substring(0,299));
    p += "/t/"+(new Date()).getTime();

    for (var i in this.add_params) {
      p+= "/" + i + "/" + encodeURIComponent((this.add_params[i]).substring(0,128));
    }

    window.open('http://polls.opiniac.com/s/' + this.opiniac_hash+p+'/t2/'+(new Date()).getTime(), 'opiniac', 'width=700,height=600,innerWidth=700,innerHeight=600,titlebar=0,scrollbars=1,status=0,location=0,toolbar=0,menubar=0');
    
    if (this.config.remove_after_click)
    	document.getElementById('opiniac_blk').style.display = 'none';
    
  	return false;
  }
  
  
  //--------------------------------------------------------------------
  this.run = function(mode)
  {
    var that = this, s = '', b_add = '', opiniac_blk_add = '';
    mode = mode || 'standard';
    
    if (mode == 'standard') {
    	if (this.config.block.background)
    		opiniac_blk_add += 'background-color: '+ this.config.block.background +';';	
    	if (this.config.block.border)
    		opiniac_blk_add += 'border: '+ this.config.block.border +';';
    	
    	s = "<style type=\"text/css\">\n";
    	s += "#opiniac_blk { position: absolute; right: 10px; bottom: 10px; z-index: 16777270; text-align:center;padding:3px 7px 8px 7px; color:#000000; font-size: 13px; "+ opiniac_blk_add +" }\n";
    	s += "div > div#opiniac_blk { position: fixed; }\n";
    	s += "div#opiniac_blk a { text-decoration: underline; color:#000000; }\n";
    	s += "div#opiniac_blk a:hover, div#opiniac_blk a:active { text-decoration: none; }\n";
    	s += "div#opiniac_blk img { display: inline; }\n";
    	s += "</style>\n";
    	s += "<!--[if gte IE 5.5]><![if lt IE 7]>\n";
    	s += "<style type=\"text/css\">\n";
    	s += "div#opiniac_blk {\n";
    	s += "right: auto; bottom: auto;\n";
    	s += "left: expression( ( -10 - opiniac_blk.offsetWidth + ( document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth ) + ( ignoreMe2 = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ) ) + 'px' );\n";
    	s += "top: expression( ( -10 - opiniac_blk.offsetHeight + ( document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight ) + ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) ) + 'px' );\n";
    	s += "}\n";
    	s += "</style>\n";
    	s += "<![endif]><![endif]-->";
    	document.writeln(s);
    } else if (mode == 'link') {
    	s = "<style type=\"text/css\">\n";
    	s += "div#opiniac_blk { display: inline; }\n";
    	s += "div#opiniac_blk a { text-decoration: underline; color:#000000; }\n";
    	s += "div#opiniac_blk a:hover, div#opiniac_blk a:active { text-decoration: none; }\n";
    	s += "div#opiniac_blk img { display: inline; }\n";
    	s += "</style>\n";
    	b_add = ' style="display: inline;"'
    	document.writeln(s);
    }
    
    var a = '<a href="#" onclick="return false;" title="'+ this.getLinkText(true) +'"';
    var b = '<div'+ b_add +'><div id="opiniac_blk">' + a + '>' + this.getLinkText(false) + '</a>&nbsp;' + a + 'style="position: relative; top: 4px;"><img src="'+ this.pictogram_location +'" alt="'+ this.getLinkText(true) +'" style="border: none;" /></a></div></div>';
    document.writeln(b);
    
    var tab = document.getElementById('opiniac_blk').getElementsByTagName('a');
    for (var i = 0; i < tab.length; i++) {
      tab[i].onclick = function () { that.onclick(); return false; }
    }
  }
  
  return this;
}
