Idaep = {}
Idaep.Controller = function()
{
   this.totalRotationTime = 5000;
   this.fadeTime = 3000;
   this.faded = Idaep_faded;
   this.fading = false;
   this.interval = 0;
   this.numImages = 0;
   this.imageIdx = 1;
   this.images = [];
   this.fpImages = [];
   this.fpCaptions = [];
   this.Init();
}

Idaep.Controller.prototype = {
   ListTemplate: "<li><a id='c__ID__' class='content-link' href='#'>__NAME__</a></li>",
   
   AccordionTemplate: "<li id='__ID__'><a id='__AID__' class='__ACLASS__' href='#'>__NAME__</a>__CONTENT__</li>",
   
   ContentPanel: '<div id="content-image-panel" style="z-index:__Z__">' +
                     '<table cellpadding="0" cellspacing="0" border="0">' +
                     '<tbody><tr><td class="center"/>' +
                     '<td class="center-content"><img class="main" src="default.png"/><div class="caption"></div></td>' +
                     '<td class="center"/></tr></tbody></table>' +
                     '<div id="filmstrip"></div>' +
                  '</div>',
                  
   FilmstripTemplate: "<a id='fsimg___IDX__' href='#'><img src='__SRC__'/></a>",
   
   Init: function()
   {
      var that = this;

      //count the number of images
      var cnt = 0;
      var current = $("div#intro-image-" + (cnt + 1) + " img");
      if (current.length > 0) 
      { 
         this.fpImages.push(current.attr("src")); 
         this.fpCaptions.push($("div#intro-image-" + (cnt + 1) + " div").html());
      }
      while(current.length > 0) 
      { 
         ++cnt; 
         current = $("div#intro-image-" + (cnt + 1) + " img");
         if (current.length > 0) 
         { 
            this.fpImages.push(current.attr("src")); 
            this.fpCaptions.push($("div#intro-image-" + (cnt + 1) + " div").html());
         }
      
      }
      this.numImages = cnt;
      
      if(Idaep_faded && this.numImages > 0)
      {
         this.fpImages[0] = Idaep_first_src;
         this.fpCaptions[0] = Idaep_first_caption;
      }
      
      if(this.numImages > 1 && !Idaep_faded) 
      { 
         this.StartImageRotation();
      }
      
      if(Idaep_faded)
      {
         if($("div#content-image-panel").length == 0)
         { 
            $("div.intro-image-cont").append($(that.ContentPanel.replace(/__Z__/g, "" + (this.numImages * 2 + 1)))); 
         }
      }
      
      $("a.intro-link").click(function(e) { that.LoadContentGroup(e); });
      $("a.data-link").click(function(e) { that.LoadContentGroup(e); });
      $("a.content-close-link").click(function(e) { that.CloseContent(e); });
      $("a.data-link-home").click(function(e) { that.ReverseFadeIntro(e); });
      
      $("a.content-link").click(function(evt) { that.LoadContent(evt); } );
      $("a.solution-link").click(function(evt) { that.LoadSolution(evt); });
      
      $("div.content-actual").jScrollPane({showArrows:true});
   },
   
   ChangeImage: function() 
   {
      var that = this;
      this.fading = true;
      $("div#intro-image-" + this.imageIdx + " img").animate({"opacity":0}, this.fadeTime, "linear", function() { that.RotateImages() });
      $("div#intro-image-" + this.imageIdx + " div").animate({"opacity":0}, this.fadeTime, "linear");
   },
   
   RotateImages: function()
   {
      this.fading = false;
      this.imageIdx += 1;
      if(this.imageIdx > this.numImages) { this.imageIdx = 1; }
      for(var i = 1; i <= this.numImages; ++i)
      {
         $("div#intro-image-" + i + " img").css("opacity", 1);
         $("div#intro-image-" + i + " img").css("-moz-opacity", 1);
         $("div#intro-image-" + i + " img").css("-khtml-opacity", 1);
         $("div#intro-image-" + i + " img").css("filter", "alpha(opacity=100)");
         $("div#intro-image-" + i + " div").css("opacity", 1);
         $("div#intro-image-" + i + " div").css("-moz-opacity", 1);
         $("div#intro-image-" + i + " div").css("-khtml-opacity", 1);
         $("div#intro-image-" + i + " div").css("filter", "alpha(opacity=100)");
         
         var idx1 = i - this.imageIdx;
         if(idx1 < 0) { idx1 = this.numImages + idx1; }
         //alert("index: " + this.imageIdx + " idx1: " + idx1 + " i: " + i);
         var zBase = this.numImages - idx1;
         $("div#intro-image-" + i).css("z-index", zBase);
         $("div#intro-image-" + i + " div").css("z-index", 2*zBase);
         $("div#intro-image-" + i + " img").css("z-index", 2*zBase - 1);
      }
   },
   
   StartImageRotation: function()
   {
      var that = this;
      this.interval = setInterval(function() { that.ChangeImage(); }, this.totalRotationTime);
   },
   
   StopImageRotation: function()
   {
      if(this.interval != 0)
      {
         clearInterval(this.interval);
      }
   },
   
   FadeIntro: function()
   {
      var that = this;
      var time = 500;
      this.StopImageRotation();
      //$("div.intro-image-cont").css("background-image", "url(" + $("div#intro-image-" + this.imageIdx + " img").attr("src") + ")");
      $("div.intro-content").css("border-left", "0px solid white");
      $("div.intro-content").css("border-right", "0px solid white");
      $("div.intro-banner, div.intro-banner-text, div.intro-banner-back").animate({"opacity":0}, time, "linear");
      $("div.intro-icon img").animate({"opacity":0}, time, "linear");
      $("div.intro-overlay, div.intro-image-cont, div.intro-image-inner").animate({"width":'385px'}, time, "swing", function() { 
         if($("div#content-image-panel").length == 0)
         { 
            $("div.intro-image-cont").append($(that.ContentPanel.replace(/__Z__/g, "" + (this.numImages * 2 + 1)))); 
         }
         //$("div.intro-image-cont").css("background-image", "");
      } );
      $("div.intro-content").animate({"left":'310px', "width":'385px'}, time, "swing");
      $("div.intro-content").animate({"borderLeftWidth":'5px', "borderRightWidth":'5px'}, time/5., "swing");
      
      $("div.intro-image-inner div").hide("fast", null);
      this.faded = true;
      
      
   },
   
   ReverseFadeIntro: function(e)
   {
      if (e) { e.preventDefault(); }
      if(this.contentOpen) { this.CloseContent(); }
      var that = this;
      var time = 500;
      if(!this.fading) { this.ChangeImage(); }
      this.RestoreFrontPageImages();
      $("div.intro-content").animate({"borderLeftWidth":'0px', "borderRightWidth":'0px'}, time/5., "swing");
      $("div.intro-content").animate({"left":'0px', "width":'720px'}, time, "swing");
      $("div.intro-overlay, div.intro-image-cont, div.intro-image-inner").animate({"width":'720px'}, time, "swing");
      
      $("div.intro-banner, div.intro-banner-text").animate({"opacity":1}, time, "linear");
	  $("div.intro-banner-back").animate({"opacity":0.5}, time, "linear");
      $("div.intro-icon img").animate({"opacity":1}, time, "linear");
      
      $("div.intro-image-inner div").show("fast", null);
      
      this.StartImageRotation();      
      this.faded = false;
   },
   
   RestoreFrontPageImages: function()
   {
      var that = this;
      var idx = this.imageIdx;
      for(var i = 0; i < this.fpImages.length; ++i)
      {
         if (i+1 != idx)
         {
            $("div#intro-image-" + (i + 1) + " img").attr("src", this.fpImages[i]);
            $("div#intro-image-" + (i + 1) + " div").html(this.fpCaptions[i]);
         }
      }
      setTimeout(function() {
            $("div#intro-image-" + idx + " img").attr("src", that.fpImages[idx - 1]);
            $("div#intro-image-" + idx + " div").html(that.fpCaptions[idx - 1]);
      
      }, 1.5 * this.fadeTime);
   },
   
   LoadContentGroup: function(evt)
   {
      evt.preventDefault();
      var that = this;
      var elem = evt.target;
      while(!elem.id) { elem = $(elem).parent()[0]; } 
      var id = elem.id.substring(1);
      var data = {};
      var url = "/load/ArchContentGroup/" + id;
      $.post(url, data, function(resp) { that.ChangeContentGroup(resp); }, "json");
   },
   
   ChangeContentGroup: function(data)
   {
      var that = this;
      if(data.success)
      {
         if(this.contentOpen) { this.CloseContent(null); }
      
         //update list
         var listHtml = "";
         for (var i in data.smgList)
         {
            listHtml += '<li class="tree"><ul>';
            break;
         }
         for (var i in data.smgList)
         {
            var smg = data.smgList[i];
            var smgHtml = this.AccordionTemplate.replace(/__NAME__/g, smg.name);
            var smgContent = '<ul>';
            for (var j in smg.sgList)
            {
               var sg = smg.sgList[j];
               var sgHtml = this.AccordionTemplate.replace(/__NAME__/g, sg.name);
               var sgContent = '<ul>';
               for (var k in sg.solutions)
               {
                  s = sg.solutions[k];
                  sgContent += this.AccordionTemplate
                   .replace(/__NAME__/g, s.name)
                   .replace(/__AID__/g, "sg" + sg.id + "idx" + k + "_" + s.id)
                   .replace(/__ID__/g, "sgli" + sg.id + "idx" + k + "_" + s.id)
                   .replace(/__ACLASS__/g, "solution-link")
                   .replace(/__CONTENT__/g, "");
               }
               sgContent += '</ul>';
               smgContent += sgHtml
                .replace(/__CONTENT__/g, sgContent)
                .replace(/__ACLASS__/g, '')
                .replace(/__ID__/g, "sg" + sg.id + "idx" + k)
                .replace(/__AID__/g, "");               
            }
            if(smg.solutions.length > 0)
            {
               for (var j in smg.solutions)
               {
                  s = smg.solutions[j];
                  smgContent += this.AccordionTemplate
                   .replace(/__NAME__/g, s.name)
                   .replace(/__AID__/g, "smg" + smg.id + "idx" + k + "_" + s.id)
                   .replace(/__ID__/g, "smgli" + smg.id + "idx" + k + "_" + s.id)
                   .replace(/__ACLASS__/g, "solution-link")
                   .replace(/__CONTENT__/g, "");
               }
            }
            smgContent += '</ul>';
            
            listHtml += smgHtml.replace(/__CONTENT__/g, smgContent)
             .replace(/__ACLASS__/g, '')
             .replace(/__AID__/g, '')
             .replace(/__ID__/g, "smgli" + smg.id + "idx" + k);
         }
         for (var i in data.smgList)
         {
            listHtml += '</ul></li>';
            //alert(listHtml);
            break;
         }
         for (var i in data.groupContent)
         {
            listHtml += this.ListTemplate.replace(/__NAME__/g, data.groupContent[i].name).replace(/__ID__/g, data.groupContent[i].id);
         }
         $("div.data-list-panel ul").html(listHtml);
         
         $("div.data-list-panel").jScrollPane({showArrows:true});
         
         //update color
         $("div.data-inner").css("background-color", data.color);
         if(data.gradient && '' != data.gradient)
         {
            $("div.data-inner").css("background-image", "url(" + data.gradient + ")");
         }
         else
         {
            $("div.data-inner").css("background-image", "none");
         }
         
         //update background image
         if(data.background && '' != data.background)
         {
            $("div.data-content-inner").css("background-image", "url(" + data.background + ")");
         }
         else
         {
            $("div.data-content-inner").css("background-image", "none");
         }
         
         //update right image
         var toUpdate = this.imageIdx + 1;
         if(toUpdate > this.numImages) { toUpdate = 1; }
         $("div#intro-image-" + toUpdate + " img").attr("src", data.foreground);
         
         if(!this.faded) { this.FadeIntro(); }
         
         if(!this.fading) { this.ChangeImage(); }

         //set accordions
         $("li.tree").tree({
            ui: { theme_name: "apple", dots:false },
            types: { icon: { image:true, position:false } },
            callback: { 
               onopen: function(node, tree) {  
                  $("div.data-list-panel").jScrollPane({showArrows:true});
               },
               onclose: function(node, tree) {  
                  $("div.data-list-panel").jScrollPane({showArrows:true});
               }
            }
         });
         
         $("a.content-link").click(function(evt) { that.LoadContent(evt); } );
         $("a.solution-link").click(function(evt) { that.LoadSolution(evt); });
         
      }
   },
   
   LoadContent: function(evt)
   {
      evt.preventDefault();
      var that = this;
      var elem = evt.target;
      while(!elem.id) { elem = $(elem).parent()[0]; } 
      var id = elem.id.substring(1);
      var data = {};
      var url = "/load/ArchContent/" + id;
      $.post(url, data, function(resp) { that.ChangeContent(resp); }, "json");
   },
   
   LoadSolution: function(evt)
   {
      evt.preventDefault();
      var that = this;
      var elem = evt.target;
      while(!elem.id) { elem = $(elem).parent()[0]; } 
      var id = elem.id.split('_')[1];
      var data = {};
      var url = "load/ArchSolution/" + id;
      $.post(url, data, function(resp) { that.ChangeContent(resp); }, "json");
   },
   
   ChangeContent: function(data)
   {
      if(data.success)
      {
         this.contentOpen = true;
         var time = 500;
         $("div#filmstrip").html("");
         $("div.content-top div.right").html(data.name);
         $("div.content-actual").empty().append($('<p></p>').html(data.content)).jScrollPane({showArrows:true});

         $("div#content-image-panel img.main").attr("src", data.image);
         $("div#content-image-panel div.caption").html(data.caption);
      
         $("div#content-image-panel").animate({"opacity":1}, time*2, "swing");
         $("div.data-content-panel").animate({"left": "-15px", "width":"290px"}, time, "swing");
         
         //add images, if present
         if(data.images != undefined && data.images != null && data.images.length > 0)
         {
            var imgContent = '';
            this.images = data.images;
            var that = this;
            for(var i in data.images)
            {
               //has to be done backwards, since these are floated right
               var idx = data.images.length - i - 1;
               imgContent += this.FilmstripTemplate.replace(/__SRC__/g, 
                data.images[idx].url).replace(/__IDX__/g, '' + idx);
            }
            $("div#filmstrip").html(imgContent);
            
            //need to re-route clicks from div.intro-icon
            $("div.intro-icon").click(function(evt)
            {
               var x = evt.pageX;
               var y = evt.pageY;
               $("div#filmstrip a").each( function()
                  {
                     var offset = $(this).offset();
                     var height = $(this).height();
                     var width = $(this).width();
                     //alert(offset.top + " " + y + " " + (offset.top + height));
                     //alert(offset.left + " " + x + " " + (offset.left + width));
                     if (offset.left <= x && offset.left + width >= x
                         && offset.top <= y && offset.top + height >= y)
                     {
                        $(this).click();
                     }
                  }
               );
            });
            $("div#filmstrip a").click(function(evt)
            {
               evt.preventDefault();
               var id = evt.target.id
               if (evt.target.tagName == 'IMG')
               {
                  //IE
                  id = $(evt.target).parent().attr("id");
               }
               var idx = id.split("_")[1];
               $("div#content-image-panel img.main").attr("src", that.images[idx].url);
               $("div#content-image-panel div.caption").html(that.images[idx].caption);
            });
         }
      }
   },
   
   CloseContent: function(evt)
   {
      if(evt) { evt.preventDefault(); }
      var time = 500;
      $("div#content-image-panel").animate({"opacity":0}, time*2, "swing");
      $("div.data-content-panel").animate({"left": "290px", "width":"0px"}, time, "swing");
      $("div#filmstrip").html('');
      this.contentOpen = false;
   }
}

idaep = null;
$(document).ready( function() {
   idaep = new Idaep.Controller();
});

