// Copyright opiniac.com
// version 1.9, 10.07.2009
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.remove_after_click = true;

  
  //--------------------------------------------------------------------
  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;
  }

  
  //--------------------------------------------------------------------
  this.segment = function(segment)
  {
    this.addParam('segment', segment);
  } 


  //--------------------------------------------------------------------
  this.addParam = function(key, val)
  {
    if (key in {'s':'', 'c':'', 'r':'', 'u':'', 't':''})
      return;
      
    this.add_params[key] = val;
  } 
  
  
  //--------------------------------------------------------------------
  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.remove_after_click)
    	document.getElementById('opiniac_blk').style.display = 'none';
    
  	return false;
  }
  
  
  //--------------------------------------------------------------------
  this.run = function(mode)
  {
    var that = this, s = '', b_add = '';
    mode = mode || 'standard';
    
    if (mode == 'standard') {
    	s = "<style type=\"text/css\">\n";
    	s += "#opiniac_blk { position: absolute; right: 10px; bottom: 10px; text-align:center;padding:0px 10px 14px 0px; color:#000000; font-size: 13px; }\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;
}
