/*                                                                             .___.__        
*     ____________  ______             ____   ______  _  __   _____   ____   __| _/|__|____   
*    /     \_  __ \/  ___/   ______   /    \_/ __ \ \/ \/ /  /     \_/ __ \ / __ | |  \__  \  
*   |  Y Y  \  | \/\___ \   /_____/  |   |  \  ___/\     /  |  Y Y  \  ___// /_/ | |  |/ __ \_
*   |__|_|  /__|  /____  >           |___|  /\___  >\/\_/   |__|_|  /\___  >____ | |__(____  /
*         \/           \/                 \/     \/               \/     \/     \/         \/
*
*  copyright @ 2011, mayr record scan
*  customer: tanner.ag
*  devision: new media
*  teamleader: stefan hagspiel
*

*  HELPER FUNCTIONS OF TANNER.AG NEWSROOM.
*  DONT BE A DICK. DONT COPY.
*
*/

document.observe("dom:loaded", function() {  
  
  newsroom_start_engine();
  
  
});

function newsroom_start_engine() {
  
  /* init auto_wrap */
  init_auto_wrap();
  
  /* init mouse-over functions */
  init_mo();
  
  /* update SWF OBJECTS */
  init_issuu_swf();
  
  /* just if IE -> otherwise we'll use text-shadow css*/
  if (Prototype.Browser.IE)
    initDropShadow();
  
}

function init_auto_wrap() {
  
  $$('#newsroom .header .title, #newsroom .content-row h2').each(function(title) {
  
   var previous = title.previous();
  
   if(previous == null || !previous.hasClassName('thumbnailLeft'))
         return;
         
    var t = title.innerHTML;
    
    title.update(t.wordWrap(22, '<br>', 1));
    
  });
  
}

function init_mo() {
  
  var links = $$('.social_channel_icons img');
  
  links.each(function(link) {
    
    var org = link.src;
    var extension = org.substring(org.lastIndexOf('.'),org.length)
        var ro_image = org.replace(extension,'-mo' + extension);
    /* preload it */
    (new Image()).src = ro_image;
    
    link.observe('mouseover', function(event) {
      link.setAttribute('src',ro_image);
    })
      
      link.observe('mouseout', function(event) {
      link.setAttribute('src',org);
  })
    
    });
  
}

function init_issuu_swf() {
  
    var file_id = $('issuu_doc_id').getValue();
    var attributes =     {id: "issuuViewer"};  
    var params =         {allowfullscreen: "true", menu: "false", wmode: "transparent" };
    var flashvars =     {jsAPIClientDomain: "www.tanner.de",  mode: "mini",  documentId: file_id};
    
    swfobject.embedSWF("http://static.issuu.com/webembed/viewers/style1/v2/IssuuReader.swf", "issuu_doc_container", "320", "190", "10.0.0.0","/include/javascript/swfobject/expressInstall.swf", flashvars, params, attributes);
  
  
}

function initDropShadow() {
  
  $$('.dropshadow').each(function(e){
    DS_process(e);
  });
}

function DS_process(e) {
  
  var nel = e.cloneNode(1);
  
  nel.className = " shadowed ";
  nel.className += e.className.replace('dropshadow','');
  
  e.style.color = '#000000';
  e.parentNode.insertBefore(nel,e);
  
  var p = e.positionedOffset();
  nel.setStyle({'left' : p[0] - 1 + 'px', 'top' : p[1] - 1 + 'px'});
  
}

/* HACKS */

String.prototype.wordWrap = function(m, b, c){
  var i, j, l, s, r;
  if(m < 1)
    return this;
  for(i = -1, l = (r = this.split("\n")).length; ++i < l; r[i] += s)
    for(s = r[i], r[i] = ""; s.length > m; r[i] += s.slice(0, j) + ((s = s.slice(j)).length ? b : ""))
      j = c == 2 || (j = s.slice(0, m + 1).match(/\S*(\s)?$/))[1] ? m : j.input.length - j[0].length
      || c == 1 && m || j.input.length + (j = s.slice(m).match(/^\S*/)).input.length;
  return r.join("\n");
};
