
var HmEnvironment = {
  isLoading : false,
  
  content_detail: function(){
    return $('content_detail')
  },
    
  unload_dev_support: function(){
    $('developer_container').empty().set('html', '&nbsp;');
  },
    
  picture_switch: function(image_name){
    result = image_name + '.ru.png';
    if (HmFix.isIe6) {
      result = image_name + '_ie6.ru.gif';
    }
    return result;
  },
    
  url_for_picture: function(image_name){
    return 'url(' + HmEnvironment.picture_switch(image_name) + ')';
  },
    
  load_banner : function(images,imagebase) {
    if(HmEnvironment.isLoading) {
      nf('is loading');
      return;
    } else {
      HmEnvironment.isLoading=true;
      $j('#banner_image_message').empty();
      $j('<img />').attr('src','./loading.gif').attr('alt','').appendTo($j('#banner_image_message'));
    }
    // images get preloaded before render. preload_images array
    // is filled, but content is not used. images get rendered
    // in separate onComplete - taken from browser cache
    var _preload_images = [];
    $j(images).each(function(i, j){
      _preload_images.push(imagebase + 't' + j);
      _preload_images.push(imagebase + 'i' + j);
    });
    var _preloaded_images = new Asset.images(_preload_images, {
      onComplete: function(event){
        nf('images preloaded to cache');
        var columns = 3;
        var rows = 0;
        var rowlimit = 1;
        var nop = false;
        $j('#banner_image_container').empty();
        nf('banner image container emtied');
        $j(images).each(function(i, j){
          // nop - no processing after rowlimit is reached
          if (!nop) {
            // build img-tag, add style, add hover-routine
            k = $j('<img />').attr('src', imagebase + 't' + j).addClass('banner_item');
            k.hoverIntent(function(){
              l = $j('<img />').attr('src', imagebase + 'i' + j).addClass('banner_spot_item');
              l.click(function(){
                $j('#banner_image_spot').hide();
                $j('#content_detail').show();
              });
              $j('#banner_image_spot').empty().append(l);
              $j('#banner_image_spot').append($j('<p/>').html('click on image to close').css('background-color','orange').css('color','#222'));
              $j('#banner_image_spot').show();
              $j('#content_detail').hide();
              //$j('#banner_image_message').text(this.src);
              // non ie6 this.toggleClass('imghover');
            }, function(){
              // non ie6 this.toggleClass('imghover');
              // lass es stehen $j('#banner_image_spot').empty();
            });
            // add picture to banner container
            k.appendTo($j('#banner_image_container'));
            // check for column and row limits
            if (i % columns == (columns - 1)) {
              rows = rows + 1;
              if (rows >= rowlimit) {
                nop = true;
              }
              else {
                $j('<br />').addClass('clr').appendTo($j('#banner_image_container'));
              }
            }
          }
          else {
            // log message: no more rows after rowlimit
            lg('  nop!');
          }
        });
        nf('images loaded');
        HmEnvironment.isLoading=false;
        $j('#banner_image_message').empty();
      }
    });
        
  },
  
  load_gallery: function(images, imagebase){
    if(HmEnvironment.isLoading) {
      nf('is loading');
      return;
    } else {
      HmEnvironment.isLoading=true;
           $j('#gallery_image_message').empty();
      $j('<img />').attr('src','./loading.gif').attr('alt','').appendTo($j('#gallery_image_message'));
    }
    // images get preloaded before render. preload_images array
    // is filled, but content is not used. images get rendered
    // in separate onComplete - taken from browser cache
    var _preload_images = [];
    $j(images).each(function(i, j){
      if(j!='colr.gif' && j!='trans.gif'){
        _preload_images.push(imagebase + 't' + j);
        _preload_images.push(imagebase + 'i' + j);
      }
    });
    var _preloaded_images = new Asset.images(_preload_images, {
      onComplete: function(event){
        nf('images preloaded to cache');
        var columns = 4;
        var rows = 0;
        var rowlimit = 4;
        var nop = false;
        $j(images).each(function(i, j){
          // nop - no processing after rowlimit is reached
          if (!nop) {
            if(j=='colr.gif' || j=='trans.gif') {
              k = $j('<img />').attr('src', imagebase + j).addClass('gallery_item_colr');
            } else {
              k = $j('<img />').attr('src', imagebase + 't' + j).addClass('gallery_item');
              k.hoverIntent(function(){
                l = $j('<img />').attr('src', imagebase + 'i' + j).addClass('gallery_spot_item');
                l.click(function(){
                  $j('#gallery_image_spot').hide();
                  $j('#content_detail').show();
                });
                $j('#gallery_image_spot').empty().append(l);
                $j('#gallery_image_spot').append($j('<p/>').html('click on image to close').css('background-color','orange').css('color','#222'));
                $j('#gallery_image_spot').show();
                $j('#content_detail').hide();
                //$j('#gallery_image_message').text(this.src);
                // non ie6 this.toggleClass('imghover');
              }, function(){
                // non ie6 this.toggleClass('imghover');
                // lass es stehen $j('#gallery_image_spot').empty();
              });         
            }
     
            // add picture to gallery container
            k.appendTo($j('#gallery_image_container'));
                        
            // check for column and row limits
            if (i % columns == (columns - 1)) {
              rows = rows + 1;
              if (rows >= rowlimit) {
                nop = true;
              }
              else {
                $j('<br />').addClass('clr').appendTo($j('#gallery_image_container'));
              }
            }
          }
          else {
            // log message: no more rows after rowlimit
            lg('  nop!');
          }
        });
        HmEnvironment.isLoading=false;
        nf('images loaded');
        $j('#gallery_image_message').empty();
      }
    });
        
  },
    
    
  putz_banner: function(){
    $j('#banner_container').empty();
    $j('#banner_image_spot').hide();
    $j('#content_detail').show();
  },
    
  putz_gallery: function(){
    $j('#gallery_container').empty();
    $j('#gallery_image_spot').hide();
    $j('#content_detail').show();
  },
    
  putz: function(){
    HmEnvironment.putz_banner();
    HmEnvironment.putz_gallery();
  }
    
}

var HmAdvertising = {
  /*****************************************************************
   * advertising menu
   *****************************************************************/
  ad_content: function(what){
    $('content_detail').set('html', $('ad_content_ad0').get('html'));
    HmEnvironment.putz();

    if (what == 'ci') {
      _imgbase = 'img.d/ad_ci/';
      _imgs = [ 
        "ci_01.jpg",
        "trans.gif",
        "trans.gif",
        "trans.gif",
        "ci_03.jpg",
        "ci_04.jpg",
        "ci_05.jpg",
        "trans.gif",
        "ci_02.jpg",
        "trans.gif",
        "trans.gif",
        "trans.gif"

      ];
      HmEnvironment.load_gallery(_imgs, _imgbase);
      $('gallery_container').set('html', $('gallery_storage').get('html'));
    }
    if (what == 'event') {
      _imgbase = 'img.d/ad_event/';
      _imgs = [ 
        "fp_04.jpg",
        "scen_02.jpg",
        "scen_16.jpg"

      ];
      HmEnvironment.load_banner(_imgs, _imgbase);
      $('banner_container').set('html', $('banner_storage').get('html'));
    }
    if (what == 'mass') {
      _imgbase = 'img.d/ad_mass/';
      _imgs = [ 
        "mm_04.jpg",
        "mm_05.jpg",
        "mm_06.jpg",
        "mm_10.jpg",
        "mm_07.jpg",
        "mm_01.jpg",
        "mm_08.jpg",
        "mm_03.jpg",
        "mm_09.jpg",
        "mm_11.jpg",
        "mm_02.jpg",
        "mm_12.jpg"

      ];
      HmEnvironment.load_gallery(_imgs, _imgbase);
      $('gallery_container').set('html', $('gallery_storage').get('html'));
    }
    if (what == 'outdoor') {
      _imgbase = 'img.d/ad_outdoor/';
      _imgs = [ 
        "trans.gif",
        "ou_01.jpg",
        "ou_02.jpg",
        "trans.gif",
        "ou_04.jpg",
        "colr.gif",
        "ou_05.jpg",
        "colr.gif",
        "colr.gif",
        "ou_03.jpg",
        "trans.gif",
        "trans.gif"

      ];
      HmEnvironment.load_gallery(_imgs, _imgbase);
      $('gallery_container').set('html', $('gallery_storage').get('html'));
    }
    if (what == 'prod') {
      _imgbase = 'img.d/ad_prod/';
      _imgs = [ 
        "pro_01.jpg",
        "colr.gif",
        "trans.gif",
        "trans.gif",
        "pro_02.jpg",
        "pro_03.jpg",
        "pro_04.jpg",
        "trans.gif",
        "pro_06.jpg",
        "pro_05.jpg"
        
      ];
      HmEnvironment.load_gallery(_imgs, _imgbase);
      $('gallery_container').set('html', $('gallery_storage').get('html'));
    }
    if (what == 'promo') {
      _imgbase = 'img.d/ad_promo/';
      _imgs = [ 
        "btl_01.jpg",
        "btl_10.jpg",
        "btl_03.jpg",
        "btl_04.jpg",
        "btl_05.jpg",
        "btl_02.jpg",
        "btl_11.jpg",
        "btl_12.jpg",
        "btl_13.jpg",
        "colr.gif"
      ];
      HmEnvironment.load_gallery(_imgs, _imgbase);
      $('gallery_container').set('html', $('gallery_storage').get('html'));
    }
  },
    
  ad_init_menu: function(){
    ['ci', 'event', 'mass', 'outdoor', 'promo', 'prod'].each(function(j){
      $("ad_" + j).addEvent('mouseenter', function(event){
        $("ad_" + j).setStyle('background-position', 'bottom left');
        $('adm_detail').setStyle('background-image', HmEnvironment.url_for_picture('add_' + j));
      });
      $("ad_" + j).addEvent('mouseleave', function(event){
        $("ad_" + j).setStyle('background-position', 'top left');
        $('adm_detail').setStyle('background-image', '');
      });
      $("ad_" + j).addEvent('click', function(event){
        HmAdvertising.ad_content(j);
      });
    });
  },
    
  load_ad_0: function(){
    $('content_detail').set('html', $('ad_content_ad0').get('html'));
    
    var _imgbase = 'img.d/ad_landing/';
    var _imgs = ['mm_02.jpg', 'mm_04.jpg', 'pro_01.jpg'];        
    HmEnvironment.load_banner(_imgs,_imgbase);
    $('banner_container').set('html', $('banner_storage').get('html'));
  }
    
}

var HmDigitalMedia = {
  /*****************************************************************
   * digital media menu
   *****************************************************************/
  dm_content: function(what){
    $('content_detail').set('html', $('dm_content_dm0').get('html'));
    HmEnvironment.putz();
      
    if (what == 'shoot') {
      
      _imgbase = 'img.d/dm_shoot/';
      _imgs = [ 
        'sh_01.jpg', 'trans.gif', 'trans.gif', 'trans.gif',
        'sh_02.jpg', 'sh_03.jpg', 'sh_04.jpg', 'trans.gif',
        'sh_05.jpg'
      ];
      HmEnvironment.load_gallery(_imgs, _imgbase);
      $('gallery_container').set('html', $('gallery_storage').get('html'));
    }
    
    if (what == 'reso') {
      
      _imgbase = 'img.d/dm_reso/';
      _imgs = [
        'pp_03.jpg', 'pp_05.jpg', 'colr.gif', 'trans.gif',
        'w_01.jpg', 're_02.jpg', 'w_03.jpg', 'w_02.jpg',
        'w_04.jpg', 'colr.gif', 're_01.jpg'
      ];
      HmEnvironment.load_gallery(_imgs, _imgbase);
      $('gallery_container').set('html', $('gallery_storage').get('html')); 
    }
    
    if (what == 'post') {
      _imgbase = 'img.d/dm_post/';
      _imgs =[
        "pp_01.jpg","pp_02.jpg","pp_04.jpg","pp_06.jpg",
        "pp_08.jpg","pp_10.jpg","pp_15.jpg","pp_16.jpg",
        "pp_23.jpg","pp_24.jpg","pp_25.jpg","pp_26.jpg"
      ];
      HmEnvironment.load_gallery(_imgs, _imgbase);
      $('gallery_container').set('html', $('gallery_storage').get('html')); 
    }
    
    if (what == 'audio') {

    }
  },
    
  dm_init_menu: function(){
    ['shoot', 'reso', 'post', 'audio'].each(function(j){
      $("dm_" + j).addEvent('mouseenter', function(event){
        $("dm_" + j).setStyle('background-position', 'bottom left');
        $('dmm_detail').setStyle('background-image', HmEnvironment.url_for_picture("dmd_" + j));
      });
      $("dm_" + j).addEvent('mouseleave', function(event){
        $("dm_" + j).setStyle('background-position', 'top left');
        $('dmm_detail').setStyle('background-image', '');
      });
      $("dm_" + j).addEvent('click', function(event){
        HmDigitalMedia.dm_content(j);
      });
    });
  },

  load_dm_0: function(){
    
    $('content_detail').set('html', $('dm_content_dm0').get('html'));
    
    var _imgbase = 'img.d/dm_landing/';
    var _imgs = ['pp_01.jpg', 'pro_03.jpg', 're_01.jpg'];        
    HmEnvironment.load_banner(_imgs,_imgbase);
    $('banner_container').set('html', $('banner_storage').get('html'));
  }
}

var HmOtherServices = {
  /*****************************************************************
   * other services menu
   *****************************************************************/
  os_content: function(what){
    $('content_detail').set('html', $('os_content_os0').get('html'));
    HmEnvironment.putz();
    
    if (what == 'art') {
      _imgbase = 'img.d/os_art/';
      _imgs =[
        "bo_01.jpg",
        "bo_02.jpg",
        "bo_03.jpg",
        "trans.gif",
        "bo_04.jpg",
        "bo_05.jpg",
        "trans.gif",
        "trans.gif",
        "bo_06.jpg"

      ];
      HmEnvironment.load_gallery(_imgs, _imgbase);
      $('gallery_container').set('html', $('gallery_storage').get('html')); 
      
    }
    
    if (what == 'event') {
      _imgbase = 'img.d/os_event/';
      _imgs =[
        "ev_01.jpg",
        "ev_02.jpg",
        "ev_03.jpg",
        "colr.gif",
        "ev_04.jpg",
        "colr.gif",
        "ev_05.jpg",
        "ev_06.jpg",
        "ev_07.jpg",
        "ev_08.jpg",
        "colr.gif",
        "ev_09.jpg"

      ];
      HmEnvironment.load_gallery(_imgs, _imgbase);
      $('gallery_container').set('html', $('gallery_storage').get('html')); 
      
    }
    
    if (what == 'scene') {
      _imgbase = 'img.d/os_scene/';
      _imgs =[
        "scen_01.jpg",
        "scen_02.jpg",
        "scen_03.jpg",
        "scen_04.jpg",
        "scen_05.jpg",
        "scen_06.jpg",
        "scen_07.jpg",
        "scen_08.jpg",
        "scen_09.jpg",
        "scen_10.jpg",
        "scen_14.jpg",
        "scen_17.jpg"

      ];
      HmEnvironment.load_gallery(_imgs, _imgbase);
      $('gallery_container').set('html', $('gallery_storage').get('html')); 
      
    }
  },
    
  os_init_menu: function(){
    ['art', 'event', 'scene'].each(function(j){
      $("os_" + j).addEvent('mouseenter', function(event){
        $("os_" + j).setStyle('background-position', 'bottom left');
        $('osm_detail').setStyle('background-image', HmEnvironment.url_for_picture('osd_' + j));
      });
      $("os_" + j).addEvent('mouseleave', function(event){
        $("os_" + j).setStyle('background-position', 'top left');
        $('osm_detail').setStyle('background-image', '');
      });
      $("os_" + j).addEvent('click', function(event){
        HmOtherServices.os_content(j);
      });
    });
  },
    
  load_os_0: function(){
    $('content_detail').set('html', $('os_content_os0').get('html'));
    
    var _imgbase = 'img.d/os_landing/';
    var _imgs = ['bo_02.jpg', 'ev_01.jpg', 'scen_02.jpg'];        
    HmEnvironment.load_banner(_imgs,_imgbase);
    $('banner_container').set('html', $('banner_storage').get('html'));
  }
}

var HmReferences = {
  /*****************************************************************
   * references menu
   *****************************************************************/
  load_re_0: function(){
    
    $('content_detail').set('html', $('re_content_re0').get('html'));
    HmEnvironment.putz();
  }
}

var HmShow = {
  /*****************************************************************
   * show menu
   *****************************************************************/
  sh_content: function(what){
    $('content_detail').set('html', $('sh_content_sh0').get('html'));
    HmEnvironment.putz();
    
    if (what == 'audio') {
      //$('content_detail').set('html', 'audio audio');
      _imgbase = 'img.d/sh_audio/';
      _imgs =[
        "au_01.jpg" 
        
      ];
      HmEnvironment.load_banner(_imgs, _imgbase);
      $('banner_container').set('html', $('banner_storage').get('html')); 
    }
    if (what == 'full') {
      //$('content_detail').set('html', 'OPTICAL MATRIX HE!');    
      _imgbase = 'img.d/sh_full/';
      _imgs =[
        "fp_01.jpg",
        "fp_03.jpg",
        "fp_04.jpg",
        "trans.gif",
        "fp_05.jpg",
        "fp_06.jpg",
        "fp_07.jpg",
        "trans.gif",
        "fp_08.jpg",
        "fp_09.jpg",
        "fp_10.jpg"

      ];
      HmEnvironment.load_gallery(_imgs, _imgbase);
      $('gallery_container').set('html', $('gallery_storage').get('html')); 
    }
    if (what == 'prod') {
      //$('content_detail').set('html', 'prod prod');    
      _imgbase = 'img.d/sh_prod/';
      _imgs =[
        "se_01.jpg",
        "se_02.jpg",
        "se_03.jpg"

      ];
      HmEnvironment.load_banner(_imgs, _imgbase);
      $('banner_container').set('html', $('banner_storage').get('html')); 
    }
    if (what == 'custom') {
      //$('content_detail').set('html', 'custom custom');    
      _imgbase = 'img.d/sh_custom/';
      _imgs =[
        "cu_01.jpg",
        "cu_02.jpg",
        "cu_03.jpg"

      ];
      HmEnvironment.load_banner(_imgs, _imgbase);
      $('banner_container').set('html', $('banner_storage').get('html')); 
    }
  },
    
  sh_init_menu: function(){
    ['audio', 'full', 'prod', 'custom'].each(function(j){
      $("sh_" + j).addEvent('mouseenter', function(event){
        $("sh_" + j).setStyle('background-position', 'bottom left');
        $('shm_detail').setStyle('background-image', HmEnvironment.url_for_picture("shd_" + j));
      });
      $("sh_" + j).addEvent('mouseleave', function(event){
        $("sh_" + j).setStyle('background-position', 'top left');
        $('shm_detail').setStyle('background-image', '');
      });
      $("sh_" + j).addEvent('click', function(event){
        HmShow.sh_content(j);
      });
    });
  },
    
  load_sh_0: function(){
    $('content_detail').set('html', $('sh_content_sh0').get('html'));

    var _imgbase = 'img.d/sh_landing/';
    var _imgs = ['fp_01.jpg', 'se_01.jpg', 'vi_01.jpg'];        
    HmEnvironment.load_banner(_imgs,_imgbase);
    $('banner_container').set('html', $('banner_storage').get('html'));
  }
}

var HmKwicks = {
  /*****************************************************************
   * zweite menue ebene
   *****************************************************************/
  hide_menues: function(){
    $("menu_a").setStyle('visibility', 'hidden');
    $("menu_b").setStyle('visibility', 'hidden');
    $("menu_c").setStyle('visibility', 'hidden');
    $("menu_d").setStyle('visibility', 'hidden');
    $("menu_e").setStyle('visibility', 'hidden');
    HmEnvironment.isLoading=false;
  },
    
  add_kwick_handlers: function(){
    $("kwick_a").addEvent("mouseenter", function(event){
      HmKwicks.hide_menues();
      $("menu_a").setStyle('visibility', 'visible');           
    })
    $("kwick_b").addEvent("mouseenter", function(event){
      HmKwicks.hide_menues();
      $("menu_b").setStyle('visibility', 'visible');
    })
    $("kwick_c").addEvent("mouseenter", function(event){
      HmKwicks.hide_menues();
      $("menu_c").setStyle('visibility', 'visible');
    })
    $("kwick_d").addEvent("mouseenter", function(event){
      HmKwicks.hide_menues();
      $("menu_d").setStyle('visibility', 'visible');
    })
    $("kwick_e").addEvent("mouseenter", function(event){
      HmKwicks.hide_menues();
      //$("menu_e").setStyle('visibility', 'visible');
    })
    $("menu_container").addEvent("mouseleave", function(event){
      HmKwicks.hide_menues();
    })
  },
    
  /*****************************************************************
   * kwicks
   *****************************************************************/
  init_kwicks: function(){
    var szNormal = 192, szSmall = 184, szFull = 224;
        
    var kwicks = $$("#kwicks .kwick");
    var fx = new Fx.Elements(kwicks, {
      wait: false,
      duration: 150,
      transition: Fx.Transitions.Back.easeOut
    });
        
    kwicks.each(function(kwick, i){
      kwick.addEvent("mouseenter", function(event){
        var o = {};
        o[i] = {
          width: [kwick.getStyle("width").toInt(), szFull]
        }
        kwicks.each(function(other, j){
          if (i != j) {
            var w = other.getStyle("width").toInt();
            if (w != szSmall) 
              o[j] = {
                width: [w, szSmall]
              };
          }
        });
        fx.start(o);
      });
    });
        
    $("kwicks").addEvent("mouseleave", function(event){
      var o = {};
      kwicks.each(function(kwick, i){
        o[i] = {
          width: [kwick.getStyle("width").toInt(), szNormal]
        }
      });
      fx.start(o);
    })
  },
    
  init_kwick_click_handlers: function(){
    $('kwick_a').addEvent('click', function(event){
      HmEnvironment.putz();
      HmDigitalMedia.load_dm_0();
    });
    $('kwick_b').addEvent('click', function(event){
      HmEnvironment.putz();
      HmShow.load_sh_0();
    });
    $('kwick_c').addEvent('click', function(event){
      HmEnvironment.putz();
      HmAdvertising.load_ad_0();
    });
    $('kwick_d').addEvent('click', function(event){
      HmEnvironment.putz();
      HmOtherServices.load_os_0();
    });
    $('kwick_e').addEvent('click', function(event){
      HmEnvironment.putz();
      HmReferences.load_re_0();
    });
        
  }
}

var HmThirdMenu = {
  /*****************************************************************
   * dritte menue ebene
   *****************************************************************/
  init_click_handlers: function() {
    ['legal','links','contact','videos','gallery'].each(function(i){
      a='tm_'+i
      lg(a)
      try {
        $(a).addEvent('click', function(event){
          HmEnvironment.putz();
          HmThirdMenu.tm_content(i);
        });
      } catch(e){
        nf(e)
      }
    });
  },
  
  tm_content : function(what) {
    HmEnvironment.putz();
    
    if(what=='legal'){$('content_detail').set('html', $('tm_content_legal').get('html'));nf('legal');}
    if(what=='links'){$('content_detail').set('html', $('tm_content_links').get('html'));nf('links');}
    if(what=='contact'){$('content_detail').set('html', $('tm_content_contact').get('html'));nf('contact');}
    
    if(what=='videos'){
      player = $j('<div />').attr('id','video_player').css('background-color','black').css('width',335).css('margin-left',80);
      $j('#tm_content_videos').empty().append(player);
      $('content_detail').set('html', $('tm_content_videos').get('html'));
      nf('videos');
    
      flashembed("video_player", 
	
      /* 
			first argument supplies standard Flash parameters. See full list:
			http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_12701
       */
      {
        src:'./FlowPlayerDark.swf',
        width: 335, 
        height: 262
      },
		
      /*
			second argument is Flowplayer specific configuration. See full list:
			http://flowplayer.org/player/configuration.html
       */
      {config: {   
          autoPlay: false,
          autoBuffering: true,
          controlBarBackgroundColor:'0x2e8860',
          initialScale: 'scale',
          //orig_videoFile: 'http://blip.tv/file/get/N8inpasadena-Flowers457.flv',
          videoFile: './media.d/HYPERMEDIA_small.flv'
        }} 
    );
    }
    
    if(what=='gallery'){$('content_detail').set('html', $('tm_content_gallery').get('html'));nf('gallery');}
  }
}
