Ext.namespace('TGD');

TGD.app = function() {
   var switchContainer;
   
   
    var initFeatureTables = function() {
        var featureText = Ext.select('.feature-more-icon');
        featureText.on('mouseover', function(){
            Ext.get(this).addClass('hover');
        });
        featureText.on('mouseout', function(){
            Ext.get(this).removeClass('hover');
        });
    }
    var initMainMenu = function() {
        if (Ext.isIE6)
        {
            var links = Ext.select('#nav-main li.l1');
            links.on('mouseover', function(){
                Ext.get(this).addClass('hover');
            });
            links.on('mouseout', function(){
                Ext.get(this).removeClass('hover');
            });
            /*var sublinks = Ext.select('#nav-main li.l1 div.navi-top-sub');
            sublinks.on('mouseover', function(){
                Ext.get(this).addClass('hover');
            });
            sublinks.on('mouseout', function(){
                Ext.get(this).removeClass('hover');
            });
            var sublinks2 = Ext.select('#nav-main li.l1 div.navi-top-sub li');
            sublinks2.on('mouseover', function(){
                Ext.get(this).addClass('hover');
            });
            sublinks2.on('mouseout', function(){
                Ext.get(this).removeClass('hover');
            });*/
        }
    }
   
   var initBoxLinks = function() {
      var moreLinks = Ext.select('div.m-click');
      moreLinks.on('click', openMoreText, this);
      var lessLinks = Ext.select('div.l-click')
      lessLinks.on('click', closeMoreText, this);
      var toggleLinks = Ext.select('div.t-click');
      toggleLinks.on('click', toggleMoreText, this);
      var toggleLinks = Ext.select('div.t-click');
      toggleLinks.on('click', toggleMoreText, this);
      switchContainer = Ext.select('div.vertical-switcher');
      switchContainer.on('click', switchVerticalText, this);
   }
   
   var initScreenShots = function() {
        var screens = Ext.select("a[@rel='lightbox']");
        screens.addClass('auto-screenshot');
        for (var i = 0; i < screens.elements.length; i++)
        {
            var link = Ext.get(screens.elements[i]);
            link.insertHtml('beforeEnd', '<span class="title">'+link.dom.title+'</span>');
            var img = link.first();
            if (img.getAttribute('width')) link.setStyle('width', img.getAttribute('width')+'px');
        }
   }
   
   var initSwfDemos = function() {
        var swfs = Ext.select("div.epaper-autoplayswf");
        var swf,width,height,link,href,title;
        for (var i = 0; i < swfs.elements.length; i++)
        {
            swf = Ext.get(swfs.elements[i]);
            width = swf.getAttribute('width');
            height = swf.getAttribute('height');
            link = swf.first();
            href = link.getAttribute('href');
            title = link.getAttribute('title');
            var a = link.dom.innerHTML.split("|");
            if (a[1] && swfobject.getFlashPlayerVersion().major < 6) {
                link.dom.innerHTML = '<img src="'+a[1]+'" alt="" border="0" width="'+width+'"/>';
            } else {
                if (href && width && height) {
                    swfobject.embedSWF(
                        a[0], swf.id, width, height, "9.0.115", "swf/expressInstall.swf",
                        {
                            clickAction: "Shadowbox.open({content:'"+href+"',player:'iframe',title:'"+title+"',height:2000,width:2000})"
                        },
                        {id : swf.id}
                    );
                }
            }
        }
   }
   
   var initFaqLists = function() {
        var lists = Ext.select("dl.faq-list");
        lists.addClass('clickable');
        var lists = Ext.select("dl.faq-list dt");
        lists.on('click', toggleDD, this);
        var dds = Ext.select("dl.faq-list dd");
        dds.setStyle('height', '0px');
        dds.setStyle('overflow', 'hidden');
    }
   
   var initCodeBoxes = function() {
        var boxes = Ext.select("div.code");
        Ext.select("div.code").wrap({tag: 'div', cls:'code-wrapper'});
        /*Ext.select("div.code-wrapper").insertFirst({tag:'div',cls:'mark-code'}).on('click', function(ev,el){
            var x = 1;
        });*/
        //boxes.insertHTML('beforeEnd', '<div class="copy-code"></div>');
   }
   
    var toggleDD = function(ev) {
        var dt = ev.getTarget('dt');
        var dtEl = Ext.get(dt);
        var ddEl = dtEl.next();
        if (ddEl) {
            ddEl.stopFx();
            var innerDiv = ddEl.first();
            if (ddEl.getHeight() > 0)
            {
                ddEl.setHeight(0, true);
                dtEl.removeClass('open');
            } else {
                ddEl.setHeight(innerDiv.getHeight(), true);
                dtEl.addClass('open');
            }
            
                //ddEl.addClass('sliding');
                /*if (!ddEl.isVisible()) {
                    ddEl.slideIn('t',
                        {
                            useDisplay: true
                        }
                    );
                } else {
                    ddEl.slideOut('t',
                        {
                            useDisplay: true
                        }
                    );
                }*/
        }
    }
   
   var openScreenShot = function(ev)
   {
        var img = ev.getTarget('img')
        Shadowbox.open(
        {
            content:    img.src,
            player:     "img",
            title:      img.title
        });
   }
   
   var openMoreText = function(ev)
   {
      var el;
      if(el = ev.getTarget('div.m-click'))
      {
         var id;
         if (id = el.className.match(/content-click-(\d+)/)) {
            var text = Ext.get('teaser-text-'+id[1]);
            if (text) {
               Ext.get('teaser-hl-'+id[1]).removeClass('close').addClass('open');
               text.stopFx();
               text.removeClass('collapsed');
               text.setDisplayed(true);
               if(Ext.isIE6) {
               } else {
                  text.slideIn('t',
                  {
                     callback: function()
                     {
                        
                     }
                  });
               }
            }
         }
      }
      ev.stopEvent();
   }
   
   var closeMoreText = function(ev)
   {
      var el;
      if(el = ev.getTarget('div.l-click'))
      {
         var id;
         if (id = el.className.match(/content-click-(\d+)/)) {
            var text = Ext.get('teaser-text-'+id[1]);
            if (text) {
               Ext.get('teaser-hl-'+id[1]).removeClass('open').addClass('close');
               if(Ext.isIE6) {
                  text.addClass('collapsed');
                  text.setDisplayed(false);
               } else {
                  text.stopFx();
                  text.slideOut('t',
                  {
                     callback : function(){
                        text.addClass('collapsed');
                        text.setDisplayed(false);
                     }
                  });
               }
            }
         }
      }
      ev.stopEvent();
   }
   
   var toggleMoreText = function(ev)
   {
      var el, oel;
      if(el = ev.getTarget('div.t-click'))
      {
         if (el.parentNode)
         {
            oel = Ext.get(el.id);
            var id = el.parentNode.id.match(/teaser-hl-(\d+)/)[1];
            var text = Ext.get('teaser-text-'+id);
            if (text) {
               var content_id = el.className.match(/toggle\-content\-(\d+)/);
               if (content_id)
               {
                  Ext.select('.toggle-text-'+content_id[1]).setDisplayed(false);
                  var checkOpened = false;
                  if (oel.hasClass('t-click-open')) {
                     checkOpened = true;
                  }
                  Ext.select('.toggle-content-'+content_id[1]).removeClass('t-click-open');
                  if (checkOpened)
                  {
                     oel.addClass('t-click-open');
                     text.setDisplayed(true);
                  }
               }
               
               text.stopFx();
               if (oel.hasClass('t-click-open'))
               {
                  oel.removeClass('t-click-open');
                  if(Ext.isIE6) {
                     text.addClass('collapsed');
                     text.setDisplayed(false);
                  } else {
                     text.slideOut('t',
                     {
                        callback: function()
                        {
                           text.addClass('collapsed');
                           text.setDisplayed(false);
                        }
                     });
                  }
               } else {
                  oel.addClass('t-click-open');
                  text.removeClass('collapsed');
                  text.setDisplayed(true);
                  if(Ext.isIE6) {
                  } else {
                     text.slideIn('t',
                     {
                        callback: function()
                        {
                        }
                     });
                  }
               }
            }
         }
      }
      ev.stopEvent();
   }
   
   var switchVerticalText = function(ev, element)
   {
      var el;
      if(el = ev.getTarget('div.click-text'))
      {
         showSwitchText(el);
      }
   }
   var showSwitchText = function(element)
   {
      var movex = 714;
      var cardsize = 33;
      var duration = 0.5;
      var el = element.parentNode;
      if (el) {
         var contentNr = el.className.match(/content-nr-(\d+)/);
         if (contentNr) {
            var nr = contentNr[1];
            var cont, oldx;
            var xstart = el.parentNode.offsetLeft;
            if (el.offsetLeft < 0)
            {
               //Alle nachfolger und selbst verschieben
               for (var i = 3; i >= nr; i--)
               {
                  cont = switchContainer.el.select('.content-nr-'+i).first();
                  cont.stopFx();
                  var newx = xstart + ((i-1) * cardsize);
                  cont.el.setX(newx,{duration : duration});
               }
            } else {
               //Alle vorgaenger verschieben
               for (var i = 1; i < nr; i++)
               {
                  cont = switchContainer.el.select('.content-nr-'+i).first();
                  cont.stopFx();
                  var newx = xstart - movex + (i * cardsize);
                  cont.el.setX(newx,{duration : duration});
               }
            }
         }
      }
   }
   
    return {
        init: function() {
            var isFrame = false;
            if (top.location!=document.location) {
                Ext.getBody().addClass('is-iframe');
                isFrame = true;
            }
            /*if(window.top && window.top.location.href != document.location.href) {
                Ext.getBody().addClass('is-iframe');
            }*/
            initMainMenu();
            initBoxLinks();
            initScreenShots();
            initFaqLists();
            initSwfDemos();
            initCodeBoxes();
            initFeatureTables();
            if (isFrame && Ext.get('logo-back-link'))
            {
                Ext.get('logo-back-link').on('click', function(ev) {
                    parent.Shadowbox.close();
                });
            }
        },
        playFLV: function(url,title,width,height) {
            Shadowbox.open({
                content:    url,
                player:     "flv",
                title:      title,
                width:      width,
                height:     height

            });
        },
        showFLV: function(url,id,width,height,config)
        {
            if (!config) 
            {
                config = {autostart: true};
            }
            swfobject.embedSWF(
                'js/shadowbox/libraries/mediaplayer/player.swf', id, width, height, "9.0.115", "swf/expressInstall.swf",
            {
                file: url,
                height: height,
                width: width,
                autostart: config.autostart ? true:false,
                controlbar: "bottom",
                backcolor: "0x000000",
                frontcolor: "0xCCCCCC",
                lightcolor: "0x557722"
            },{
                allowFullScreen: true
            });
        }
    }
}();

Ext.onReady(TGD.app.init);
