// Copyright opiniac.com
// version 1.16, 18.05.2011
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,
    fill_cookie_block: false
  };

  
  //--------------------------------------------------------------------
  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;
			    
			  default:
			    this.config[i] = _options[i];
      }
    }
    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.setCookie = function(name, value, expires, path, domain, secure)
  {
    var cs = name + '=' + escape (value);

    if (expires) {
      var d = new Date();
      var expir = new Date(d.getTime() + (expires*1000*60));
      cs += "; expires=" + expir.toGMTString();
    }
    if (path) cs += "; path=" + escape (path);
    if (domain) cs += "; domain=" + escape (domain);
    if (secure) cs += "; secure";
    document.cookie = cs;
    
    return this;
  }

  
  //--------------------------------------------------------------------
  this.getCookie = function(cookie)
  {
    var results = document.cookie.match ( '(^|;) ?' + cookie + '=([^;]*)(;|$)' );

    if  (results) return (unescape(results[2]));
    else return null;
  }
  
  
  //--------------------------------------------------------------------
  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';
    
    if (this.config.fill_cookie_block) {
      var future = 60 * 24 * this.config.fill_cookie_block;
      this.setCookie('opiniac_std_time', 1, future, '/');
    }
    
  	return false;
  }
  
  
  //--------------------------------------------------------------------
  this.run = function(mode)
  {
    var that = this, s = '', b_add = '', opiniac_blk_add = '';
    var opiniac_std_time = this.getCookie('opiniac_std_time');
    mode = mode || 'standard';

    if (opiniac_std_time)
      return;
    
    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";
    	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);
    } else if (mode == 'right') {
      s = "<style type=\"text/css\">\n";
      s += "#opiniac_blk { position: absolute; right: 0px; top: 40%; z-index: 16777270; text-align:center;padding:3px 0px 8px 7px; 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";
      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;
}

