/* © 2010 by OOP SOLUTIONS GmbH & Co. KG, Author: Martin Teufel */

var oop = oop ||
  {
      $: jQuery.noConflict(true),			// releasing $
      debug: false,						// Enable debuging
      data: {},
      copyright: '©  2010, Author: OOP SOLUTIONS GmbH & Co. KG'
  };

oop.$(document).ready(function(){
 
  oop.cmp.navigation.init();
  oop.cmp.filter.init();
  oop.cmp.banner.init();
  if(typeof wInitBrowseEdit == 'function'){
    wInitBrowseEdit();
  }
});

/*
	© 2010 by Martin Teufel, OOP SOLUTIONS GmbH & Co. KG
*/

(function($) {
  oop.cmp = oop.cmp || {};
  var menu_width = 0;
  $.extend(oop.cmp, {
    banner: {
      /** JS for banner detection  **/
      init: function(c) {

        var carousel = $('div.mds-cmp-banner > ul ').cycle({     
          speed:  'normal', 
          timeout: 0, 
          height: 454,
          next: 'div.mds-cmp-banner > ul',
          slideExpr: '> li',     
          onPrevNextEvent: oop.cmp.banner.next
        }); 
        
        //$('div.mds-cmp-banner').height($('div.mds-cmp-banner ul li img').height());
 
        $("div.mds-cmp-banner dd ul li a").click(function(event) {
          event.preventDefault(); 
          $("div.mds-cmp-banner dd ul li").removeClass('ui-state-active');
          $(this).parent('li').addClass('ui-state-active');
          carousel.cycle(parseInt($(this).attr('rel'))-1); 
        }); 
        
        if($.browser.msie)
          $('#layer > div:last-child').addClass('lastChild');
      },
      next: function(is,i,el){
        var active = $('img',el).attr('rel');
        $('div.mds-cmp-banner > dl li.ui-state-active').removeClass('ui-state-active');
        $('div.mds-cmp-banner > dl a[rel="'+active+'"]').parent('li').addClass('ui-state-active');       
      }
    },
    navigation: {
      /** JS for navigation detection  **/
      navigation_width: 0,
      init: function(c) {
        if ($.browser.msie && (parseFloat($.browser.version) < 7)) {
          $('div.mds-cmp-1stlevelnavigation').width($('div.mds-cmp-1stlevelnavigation').width() - 10);
        }
		  
        $('div.mds-cmp-1stlevelnavigation > ul > li').hover(
          function(){
            //oop.cmp.navigation.set_flyout_padding($('> ul', $(this)));
            //oop.cmp.navigation.set_position($(this));
            $(this).addClass('ui-state-active');
            $(this).parent('ul').addClass('ui-state-open');
            $(this).siblings('.active').addClass('ui-state-inactive');
          },
          function(){
            $(this).removeClass('ui-state-active');
            $(this).siblings('.active').removeClass('ui-state-inactive');
            if($(this).parent('ul').hasClass('ui-state-open'))
              $(this).parent('ul').removeClass('ui-state-open');
          }
        );
        
        $('div.mds-cmp-1stlevelnavigation > ul > li > ul > li').hover(
          function(){
            //oop.cmp.navigation.set_flyout_padding($('> ul', $(this)));
            //oop.cmp.navigation.set_position($(this));
            $(this).addClass('ui-state-active');
            $(this).parent('ul').addClass('ui-state-open');
            $(this).siblings('.active').addClass('ui-state-inactive');
          },
          function(){
            $(this).removeClass('ui-state-active');
            $(this).siblings('.active').removeClass('ui-state-inactive');
            if($(this).parent('ul').hasClass('ui-state-open'))
              $(this).parent('ul').removeClass('ui-state-open');
          }
        );        
        
        this.navigation_width = $('div.mds-cmp-1stlevelnavigation > ul').width();
        
        $('div.mds-cmp-1stlevelnavigation > ul').each(function(i, el){
          oop.cmp.navigation.set_flyout_padding($(this));
        });
        $('div.mds-cmp-1stlevelnavigation > ul > li > ul').each(function(i, el){
          oop.cmp.navigation.set_flyout_padding($(this));
        });
        
      },
      set_flyout_padding: function(obj){
        var that = oop.cmp.navigation;
        var flyout_width = 0;
        
        if($(obj).data('flyout_padding') === undefined){
          var el = $('> li', obj);
          el.each(function(){
              flyout_width = flyout_width + parseInt($(this).outerWidth(true));
          });  
          if ($.browser.msie && (parseFloat($.browser.version) < 7)) {
            $(obj).width(flyout_width);
          }
          
          if(flyout_width < that.navigation_width){ 
            flyout_padding = that.navigation_width - 7 - flyout_width;
            if($.browser.msie && (parseFloat($.browser.version) == 7)){} 
            else{
              obj.css('paddingLeft', flyout_padding);  
            }
          }
          else{
            obj.css('width', flyout_width);
          }
            

          $(obj).data('flyout_padding', 1);
          
        }
      }      
    },
    filter: {
      /** JS for FILTER detection  **/
      filter_param: '',
      init: function(c) {
       window.oop.data.list = '';
        var that = this;
        
        this._bind();

        $('div.mds-cmp-tsll').css({width: $('div.mds-cmp-tsll').parent().width(), position: 'relative'});
      },
      _unbind: function(){
        var filter_links = $('div.mds-cmp-filter > dl > dd ul li a');
        filter_links.unbind();
        filter_links.bind('click', function(e){e.preventDefault();});
      },
      _bind: function(){
        $('div.mds-cmp-filter > dl > dd a').unbind('click hover');
        var has_view = ($('div.mds-cmp-filter > dl').hasClass('with_view')) ? true : false;
 
        if(has_view){
          $('div.mds-cmp-filter > dl > dd:eq(0) ul li a').bind('click', $.proxy(this._change_thumbnails, this) );
          $('div.mds-cmp-filter > dl > dd').not(':eq(0)').find('ul li a').bind('click', $.proxy(this._filter_list, this) );
          $('div.mds-cmp-filter > dl > dd').not(':eq(0)').find('ul li a').hover(
            function(){
              $(this).parents('ul').addClass('ui-state-open');
            },
            function(){
              var parent_ul = $(this).parents('ul');        
             (parent_ul.children('.ui-state-active').length == 0) ? parent_ul.removeClass('ui-state-open') : (!parent_ul.hasClass('ui-state-open')) ? parent_ul.addClass('ui-state-open') : '';        
            }
          );
        }
        else{
          $('div.mds-cmp-filter > dl > dd').find('ul li a').bind('click', {view: has_view}, $.proxy(this._filter_list, this) );
          $('div.mds-cmp-filter > dl > dd').find('ul li a').hover(
            function(){
              $(this).parents('ul').addClass('ui-state-open');
            },
            function(){
              if(!$('div.mds-cmp-filter > dl > dd').find('ul li').hasClass('ui-state-active')){
                $(this).parents('ul').removeClass('ui-state-open');
              }
            }
          );
        }
      },
      _filter_list: function(event){
        event.preventDefault();
        var target    = $(event.target);
        var parent    = target.parent();
        var parent_ul = target.parents('ul');
        
        (!parent.hasClass('ui-state-active')) ? parent.addClass('ui-state-active') : parent.removeClass('ui-state-active');        
        (target.parents('ul').children('.ui-state-active').length == 0) ? parent_ul.removeClass('ui-state-open') : (!parent_ul.hasClass('ui-state-open')) ? parent_ul.addClass('ui-state-open') : '';
        
        var options = target.attr('rel').split('|');
        var view = options[0];
        var mode = options[1];
        //parent.siblings().removeClass('ui-state-active').addClass('ui-state-inactive');
        //parent.removeClass('ui-state-inactive').addClass('ui-state-active');

        oop.cmp.filter._prepare_filter_param(event);
        oop.cmp.filter._load_list(target);
      },
      _prepare_filter_param: function(event){
        var that = oop.cmp.filter;
        var param = view = '';
        var item = null;
        var items = (event.has_view) ? $('div.mds-cmp-filter > dl > dd').not(':eq(0)').find('ul li.ui-state-active') : items = $('div.mds-cmp-filter > dl > dd ul li.ui-state-active');
        var count_items = items.length;
        
        items.each(function(i, obj){
         param += ($('> a', obj).attr('rel').indexOf('view') == 0) ? $('> a', obj).attr('rel').replace('|', '=') : $('> a', obj).attr('rel').replace('|', '[]=');
          if(i < count_items-1) param += '&';
        });
        that.filter_param = param;
      },
      _load_list: function(link){
        var that = oop.cmp.filter;
        var query = window.location.search.substring(1).replace('reset=1', '');
        //window.location.search = query;
        var jqxhr = $.ajax({url: '', data: query+'&'+that.filter_param+'&mode=ajax', dataType: 'html', method: 'GET', async: false, beforeSend : function() {that._unbind();}})
            .success(function() {})
            .error(function() {alert("error");})
            .complete(function(data) {          
              
              var content = ($(data.responseText).children('div.mds-cmp-tsll').find('> ul').length == 1) ? $(data.responseText).children('div.mds-cmp-tsll').find('> ul') : $(data.responseText).children('div.mds-cmp-tsll').find('> p');
              content.css({
                opacity: 0,
                position: 'absolute',
                zIndex: -1                
              });

              $('div.mds-cmp-tsll').append(content);

              var min_height = parseInt($('#container > div[id|="mds-tpl"]').css('min-height'));
              var height = (content.outerHeight() < min_height) ? min_height : content.outerHeight();
              var insert_element = content.get(0).tagName;
              
              $('div.mds-cmp-tsll').animate({height: height}, 1000);
              $('div.mds-cmp-tsll > ul:first, div.mds-cmp-tsll > p:first').animate({opacity: 0}, 1000, function(e){
                if(insert_element == 'P' || $('div.mds-cmp-tsll > ul').length == 2)
                  $('div.mds-cmp-tsll > ul:first').eq(0).remove();
                if(insert_element == 'UL' || $('div.mds-cmp-tsll > p').length > 1)
                  $('div.mds-cmp-tsll > p:first').remove();
                $('div.mds-cmp-tsll > ul, div.mds-cmp-tsll > p').animate({opacity: 1}, 1000, function(e){                  
                  $(this).removeAttr('style');   
                  that._bind();
                });
              });
              
              
            });

      },
      _change_thumbnails: function(event){
        event.preventDefault();
       
        var target = $(event.target);
        var parent = target.parent();
        if(!parent.hasClass('ui-state-active')){
        
        var options = target.attr('rel').split('|');
        var view = options[0];
        var mode = options[1];
        parent.siblings().removeClass('ui-state-active').addClass('ui-state-inactive');
        parent.removeClass('ui-state-inactive').addClass('ui-state-active');
       
        if(mode == 'plot') $('div.mds-cmp-tsll > ul').addClass('images_only');
        else $('div.mds-cmp-tsll > ul').removeClass('images_only');
       
        $('div.mds-cmp-tsll > ul > li').each(function(i, obj){
          var src = $('a img', this).attr(mode+'_src');          
          var img = $('a img', this).clone().attr('src', '').css({position: "absolute", display: "none"})
                                      .bind("load", function(e) {                                          
                                          var self = $(this);
                                          imgRatio = $(e.target).width() / $(e.target).height();
    
                                      }).insertAfter($('a img', obj));
          $('a img:first-child', obj).fadeOut(1000, function(){             
          });
          $('a img:last-child', obj).fadeIn(800, function(){       
            $('a img:first-child', obj).remove();
          });
       
          img.attr("src", src); // Hack for IE img onload event          
        });
      }
      }
    }
  });
})(oop.$);

//////////////////////////////////////////////////////////////////
//
// @method void wOpenURL(url)
//
// @desc Öffnet eine Seite
//
// @return void
//
////////////////////////////////////////////////////////////////

function wOpenURL(url){
  if(location.pathname.indexOf('/weblication/grid5/') != 0){
    top.location.href = url;
  }
}

////////////////////////////////////////////////////////////////
//
// @method void wOpenPopupURL(url, width, height, name)
//
// @desc Öffnet eine Seite in einem Popup
//
// @return void
//
////////////////////////////////////////////////////////////////

function wOpenPopupURL(url, width, height, name){

  var name = name || '';
  
  if(width == '' || width == ' ' || typeof(width) == 'undefined'){
    width = 786;
  }
  if(height == '' || height == ' ' || typeof(height) == 'undefined'){
    height = 677;
  }
  
  url = url.replace(/&amp;/g, '&');
  options = ',location=no, menubar=no,toolbar=no,resizable=no,scrollbars=no,status=no';
  width   = parseInt(width);
  height  = parseInt(height);

  var posLeft = (screen.width / 2)  - (width  / 2);
  var posTop  = (screen.height / 2) - (height / 2);
  alert(name);
  window.open(url, name, 'top='+posTop+',left='+posLeft+',width='+width+',height='+height+',' + options);
}

////////////////////////////////////////////////////////////////
//
// @method void wSendMailNospam(data)
//
// @desc Öffnet eine E-Mail zum Versenden
//
// @return void
//
////////////////////////////////////////////////////////////////

function wSendMailNospam(data){
  location.href = 'mailto:' + data.replace('|', '@');
}
