/**
  * @author mike
  */
 
// show content
function showForm(type, name) {
  // show shader
  var shader  = Ext.get('shader');
  var height  = getWindowHeight();
  shader.setHeight(height).show();
  shader.on('click', hideContent, this, {
    single: true,
    delay: 100
  });
  // show content
  var content = Ext.get('shader-content');
  // get code
  Ext.Ajax.request({
    url: 'plugin/'+type+'/'+name,
    method: 'get',
    success: function(o, s, r) {
      content.update(o.responseText);
      content.show();
      //console.log(content.getWidth());
    },
    failure: function() {
      hideContent();
      alert('Es ist ein Fehler aufgetreten!');
    },
    params: {id: id}
  });
} 

// post form
function postForm(form, url) {
  var f = Ext.get(form);
  // show content
  var content = Ext.get('shader-content');
  // get code
  Ext.Ajax.request({
    url: f.getAttribute('action'),
    success: function(o, s, r) {
      if(url) {
        document.location.href = url;
      } else {
        content.update(o.responseText);
      }
    },
    failure: function() {
      alert('Es ist ein Fehler aufgetreten!');
    },
    form: f
  });
} 

// show content
function showContent(id, view, h, w) {
  if(!h) h = '';
  if(!w) w = '';
  // show shader
  var shader  = Ext.get('shader');
  var height  = getWindowHeight();
  shader.setHeight(height).show();
  shader.on('click', hideContent, this, {
    single: true,
    delay: 100
  });
  // show content
  var content = Ext.get('shader-content');
  // get code
  Ext.Ajax.request({
    method: 'get',
    url: 'app/'+id+'/render?h='+h+'&w='+w+'&view='+view,
    success: function(o, s, r) {
      content.update(o.responseText);
      content.show();
    },
    failure: function() {
      hideContent();
      alert('Es ist ein Fehler aufgetreten!');
    }
  });
} 
// show content in window
function showWindow(id, h, w) {
  showContent(id, 'window', h, w);
}


function hideContent() {
  var shader = Ext.get('shader');
  shader.hide();
  var content = Ext.get('shader-content');
  content.update('');
  content.hide();
} 

// show video
function showVideo(id) {
  // show shader
  var shader  = Ext.get('shader');
  var height  = getWindowHeight();
  shader.setHeight(height).show();
  shader.on('click', hideVideo, this, {
    single: true,
    delay: 100
  });
  // show video
  var video = Ext.get('video');
  // get code
  Ext.Ajax.request({
    url: 'app/'+id+'/show',
    success: function(o, s, r) {
      video.update(o.responseText);
      video.show();
    },
    failure: function() {
      hideVideo();
      alert('Es ist ein Fehler aufgetreten!');
    },
    params: {id: id}
  });
} 

function hideVideo() {
  var shader = Ext.get('shader');
  shader.hide();
  var video = Ext.get('video');
  video.update('');
  video.hide();
} 


// show image
function showImage(a) {
  var a = Ext.get(a);
  var t = a.parent('div.magazine').child('a');
  var p = t.child('img');
  t.set({href: a.getAttribute('href')});
  p.dom.src = a.getAttribute('href');
  initLytebox();
  return false;
}


// recommend
function recommend() {
  el = Ext.get('recomend-container');
  //el.setLeft( (Ext.lib.Dom.getViewWidth()/2) +200 );
  if(!el.isDisplayed()) {
    el.slideIn('t', {
      easing: 'bounceOut',
      duration: 1
    });
  } else {
    el.slideOut('t', {
      duration: .5,
      remove: false,
      useDisplay: true
    });
  }
}

// sendRecommendForm
function sendRecommendForm() {
  var f = document.forms['recommend-form'];
  var p = {
    action:   'sendForm',
    snd_name: f.elements['snd[name]'].value,
    snd_mail: f.elements['snd[mail]'].value,
    rec_name: f.elements['rec[name]'].value,
    rec_mail: f.elements['rec[mail]'].value,
    url:      document.location.href,
    title:    document.title
  };
  Ext.Ajax.request({
    url: 'plugin/form/recommend',
    success: function(o, s, r) {
      Ext.get('recommend-container-state').update('<strong>Vielen Dank!</strong>');
      window.setTimeout(recommend, 2000);
    },
    failure: function() {
      alert('Es ist ein Fehler aufgetreten. Bitte füllen Sie alle Felder aus!');
    },
    params: p
  });
  return false;


}

// bookmark
function addNet(url,titel){
  if((typeof window.sidebar=="object") && (typeof window.sidebar.addPanel=="function")) {
    window.sidebar.addPanel(titel,url,"");
  } else {
    alert("Bitte fügen Sie die Seite manuell zu Ihren Lesezeichen hinzu!");
  }
}
function bookmark(){
  var url = window.location.href, t = document.title, ap = navigator.appName;
  if(ap=="Microsoft Internet Explorer") {
    window.external.AddFavorite(url,t);
  } else {
    if(ap=="Netscape") {
      addNet(url,t);
    } else {
      alert("Mit ihren Browser ist kein Lesezeichen über Javascript möglich.\nBitte legen sie das Lesezeichnen manuel an.\nURL:"+url);
    }
  }
}

// print
function printview() {
  window.print();
}


// open window
function openWindow(url, name, options) {
  window.open(url, name, options);
}

// get window height
function getWindowHeight() {  
  var yScroll;
  if (window.innerHeight && window.scrollMaxY) {
    yScroll = window.innerHeight + window.scrollMaxY + 1;
  } else if (document.body.scrollHeight > document.body.offsetHeight){
    yScroll = document.body.scrollHeight;
  } else {
    yScroll = document.getElementsByTagName("html").item(0).offsetHeight;
    yScroll = (yScroll < document.body.offsetHeight) ? document.body.offsetHeight : yScroll;
  }
  return yScroll;
}
