/*  
  JavaScript ShowYT v 1.0 (PKOV)
  
  vatváření náhledu videa YouTube
  
  odkaz na video YT př.:  
    <a href="http://www.youtube.com/e/4jh-xcKazSk?enablejsapi=0&version=3&autoplay=1" class="yt">odkaz</a>
    (enablejsapi=0 - NUTNÉ !!! ,  jinak nefunguje v IE)
    
  inicializaci provést nakonci dokumentu:
    showyt.init(videoClass, frameWidth, frameHeight);  
    
        videoClass  - třída anchoru (př. <a ... class="yt">) který odkazuje na zobrazované video
        frameWidth  - šířka framu pro přehrávání videa
        frameHeight - výška framu pro přehrávání videa
        
    př.: showyt.init("yt", 640, 480);    
*/

var showyt = new function ShowYT()
{
  var self = this;
  
  this.vClass;
  this.shadow;
  this.fw;
  this.fh

// pomocné rutiny
  
  String.prototype.trim = function()
  {
    return this.replace(/(^\s*)|(\s*$)/g, "")
  };
  
  this.inArray = function(value, array) 
  {
  	for(var i in array) if(array[i] == value) return true;
  	return false;
  };
  
  this.getClasses = function(tag)
  {
    if(!tag.className) return new Array();
    var str = tag.className.trim();
    var rel=/[\s]+/;
    return str.split(rel); 
  }
  
  this.isClass = function(element, cName)
  {
    var classes = this.getClasses(element);
    return (self.inArray(cName, classes));
  }
  
  this.setListener = function(obj, event, method)
  {
    if (obj.addEventListener) 
    { 
      obj.addEventListener(event, method, false); 
    }    
    else 
    { 
      if (obj.attachEvent) 
      {
        var regexp = /^on[\w]+$/; 
        if(!regexp.test(event)) event = 'on' + event;  
        obj.attachEvent(event, method); 
      } 
    }
  }
  
  this.elementPos = function(e)
  { 
    var left = 0; 
    var top  = 0;  
    while (e.offsetParent)
    { 
        left += e.offsetLeft; 
        top  += e.offsetTop; 
        e     = e.offsetParent; 
    } 
 
    left += e.offsetLeft; 
    top  += e.offsetTop;  
    return {x:left, y:top}; 
  }
  
  this.elementCenter = function(e)
  {
    var p = self.elementPos(e);
    var horizontal = p.x + (e.offsetWidth / 2);
    var vertical = p.y + (e.offsetHeight / 2);
    return {x:horizontal, y:vertical};
  } 
  
  this.getTarget = function(event)
  {
    var targ;
    if (!event) event = window.event;       
    if (event.target) targ = event.target
  	else if (event.srcElement) targ = event.srcElement;
    return targ;
  }
  
  this.checkFlash = function()
  {
    var flash = false; 
    try { var ao = new ActiveXObject('ShockwaveFlash.ShockwaveFlash'); if (ao) flash = true; }
    catch(e) { if(navigator.mimeTypes ["application/x-shockwave-flash"] != undefined) flash = true; }
    return flash;
  }

// inicializace

  this.init = function(videoClass, frameWidth, frameHeight)
  {
    self.fw = frameWidth;
    self.fh = frameHeight;
    self.vClass = videoClass;
    var flash = self.checkFlash();
    var anchors = document.getElementsByTagName('a');
    for(var i in anchors) 
    {
      if(anchors[i].className == videoClass)
      {
        //if(flash)
        //{
          anchors[i].setAttribute('onclick', 'return false;');
        //}
        //else
        //{
        //  anchors[i].innerHTML = '<img border="0" src="/img/ytneedflash.png" />';
        //}
      }
    } 
  }

// zobrazení videa (pořadí jednotlivých kroků je nutno dodržet, jinak problémy se zobrazneím v IE)
  
  this.showVideo = function(href, desc)
  {
    // var sel = self.getTarget(evt);
    // while(!self.isClass(sel, self.vClass)) sel = sel.parentNode;
    // vytvoření masky pozadí (shadow) a iframu pro přehrání videa 
    self.shadow = document.createElement('div');
    self.shadow.setAttribute("style", " position: fixed; top: 0px; left: 0px; width: 100%; height: 100%; background: url(/images/black-70.png); z-index: 10000; text-align: center;");
    self.shadow.className="YTclose";
    var frame = document.createElement('div');
    frame.setAttribute("style", "background-color: #000; color: white; font-size: 11px; font-weight: bold; border: solid thin black; position: absolute; padding: 10px 10px 35px 10px;");
    var title = document.createElement('div');
    title.setAttribute("style", "background-color: #000; border: solid thin black; position: absolute; left: 10px; bottom: 10px;");
    var close = document.createElement('div');
    close.setAttribute("style", "background: url(/images/close.png) transparent; position: absolute; right: -12px; top: -12px; width: 24px; height: 24px; cursor: pointer;");
    close.setAttribute("title", "Zavřít");
    close.className="YTclose";
    var video = document.createElement('iframe');
    video.setAttribute("style", "background-color: #ddd; border: solid thin black;");
    // zatmaví se pozadí a na zatmavení se navěcí událost skrytí videa
    document.body.appendChild(self.shadow);
    self.setListener(close, 'click', self.hideVideo);
    self.setListener(self.shadow, 'click', self.hideVideo);
    // vystředí se iframe videa 
    var sc = self.elementCenter(self.shadow);
    var left = sc.x - (self.fw / 2);
    var top = sc.y - (self.fh / 2);
    video.style.width = self.fw + 'px';
    video.style.height = self.fh + 'px';
    frame.style.top = top + 'px';
    frame.style.left = left + 'px';
    // vloží odkaz na video a zobrazí
    video.src = href;
    title.innerHTML = desc;
    frame.appendChild(video);
    frame.appendChild(title);
    frame.appendChild(close);
    self.shadow.appendChild(frame); 
  }
  
// skrytí přehrávaného videa  
  
  this.hideVideo = function(evt)
  {
    var sel = self.getTarget(evt);
    if(sel.className != "YTclose") return;
    document.body.removeChild(self.shadow);    
  }
  

}
