
jQuery.noConflict();

jQuery(document).ready(function()
{
    site_url = "http://www.marschner.com/";

    loadWesJQueryExtensions();
    loadFancyboxHandler();
    loadTransformerTooltips();

    jQuery('.tx-wesmarschnerconfig-pi1 .record_row_hasFile td').click(function() {
        window.open(jQuery(this).parent().find('td.orderno a').attr('href'));
    });

});

// Handler ------------------------------------------------------------------- 

function loadTransformerTooltips()
{
    jQuery('.tx-wesmarschnerconfig-pi1 .record_row td').hover(function() {
        tooltip.show(jQuery(this).parent().find('.more_info').html());
    },
    function() {
        tooltip.hide();
    });

    jQuery('.tx-wesmarschnerconfig-pi1 .col2 label').hover(function() {
        tooltip.show(jQuery(this).find('.image').html());
    },
    function() {
        tooltip.hide();
    });
}


function loadHomeNewsHandler()
{
  jQuery('.tab_bg:first').show(0);
  
  var news_tabs = document.getElementById('news_tabs').getElementsByTagName('li');
  ClassNames = new Array();
  for(i = 0; i < news_tabs.length; i++) {
    // Remove all links from Tab-Buttons if JS is available
    var link_construction = 'javascript:void(0)';
    news_tabs[i].getElementsByTagName('a')[0].setAttribute('href', link_construction);
    ClassNames[i] = news_tabs[i].className;
  }

  // Slide the arrow to position 1
  jQuery('#news_tabs li:eq(0)').click(
    function(){
      if(!jQuery(this).hasClass('active')){
        jQuery('.tab_arrow').stop(true, true).animate({left: "6.25em", color: "blue"}, {duration: 600, easing: 'easeOutBack'});
      }
    }
  );
  
  // Slide the arrow to position 2
  jQuery('#news_tabs li:eq(1)').click(
    function(){
      if(!jQuery(this).hasClass('active')){
        jQuery('.tab_arrow').stop(true, true).animate({left: "20.9em"}, {duration: 600, easing: 'easeOutBack'});
      }
    }
  );
  
  // Slide the arrow to position 3
  jQuery('#news_tabs li:eq(2)').click(
    function(){
      if(!jQuery(this).hasClass('active')){
        jQuery('.tab_arrow').stop(true, true).animate({left: "33.55em"}, {duration: 600, easing: 'easeOutBack'});
      }
    }
  );
  
  jQuery('#news_tabs li').click(
    function() {
      for(i = 0; i < news_tabs.length; i++){
        if(jQuery('#news_tab_content .tab_content:eq('+i+')').hasClass(jQuery(this).attr('class'))) {
          if(!jQuery(this).hasClass('active')){
            // Abort last animation, if new animation starts
            jQuery('#news_tabs li').stop(true, true);
            jQuery('#news_tab_content .tab_content').stop(true, true);
            
            // Hide the background of non-active Tab-Buttons
            jQuery('.tab_bg').fadeOut(500);
            
            jQuery('#news_tabs li').removeClass('active');
            // Slide up (hide) non-active Tab-Contents
            //jQuery('#news_tab_content>div').slideUp('slow').removeClass('active');
            jQuery('#news_tab_content .tab_content').css('position', 'absolute').fadeOut('slow',
              function() {
                // IE7 Bugfix: fadeOut doesn't set "display: none" to IE7
                jQuery(this).removeClass('active').css('display', 'none');
              }
            );
            
            // Slide down the active Tab-Content
            //jQuery('#news_tab_content>div:eq('+i+')').slideDown('slow').addClass('active');
            jQuery('#news_tab_content .tab_content:eq('+i+')').css('position', 'relative').fadeIn('slow').addClass('active');
            
            jQuery(this).show('fast').addClass('active');
            
            // Fade the Color of the non-active Tab-Button to grey
            //jQuery('#news_tabs li').filter('li:not(.active)').children('a').children('span').animate({ color: '#4c4c4c' }, 300);
            jQuery('#news_tabs li').filter('li:not(.active)').children('a').children('span');
                        
            // Fade the color of the active Tab-Button to white
            //jQuery(this).children('a').removeClass('hover').children('span').animate({ color: '#ffffff' }, 300);
            jQuery(this).children('a').removeClass('hover').children('span');

            // Background of the active Tab-Button
            jQuery(this).children('.tab_bg').stop(true, true).fadeIn(300);
            
          }
        }
      }
  });
  
  /* change color on tab hover */
  jQuery('#news_tabs ul li').hover(
    function() {
      if(!jQuery(this).hasClass('active')) {
        jQuery(this).stop(true, true).children('a').addClass('hover');
        //jQuery(this).children('a').children('span').stop(true, true).css('color', '#c42e00');
        jQuery(this).children('a').children('span').stop(true, true);
      }
    },
    function(){
      if(!jQuery(this).hasClass('active')) {
        //jQuery(this).children('a').removeClass('hover').children('span').stop(true, true).css('color', '#4c4c4c');
        jQuery(this).children('a').removeClass('hover').children('span').stop(true, true);
      }
    }
  );
  
  /* Hover-effect of the news fields */
  jQuery('.news-latest-item').hover(
    function(){
      jQuery(this).addClass('hover');
    },
    function(){
      jQuery(this).removeClass('hover');
    }
  );
  
  /* Link behaviour on click */
  jQuery('.news-latest-item').click(
    function(){
      window.location.href = jQuery(this).children('.news-latest-item-border').children('h3:first').children('a').attr('href');
    }
  );
}

function loadHomeNewsHandler_v2()
{
  jQuery('.tab_bg:first').show(0);

  jQuery('.news-latest-items h3.headline:first a span.more').hide();
  jQuery('.news-latest-items h3.headline a').click(
    function() {
      if(!jQuery(this).parent().hasClass('selected'))
        jQuery('.news-latest-items h3').filter('h3:not(this)').children('a').children('span.more').fadeIn(300);

        jQuery(this).children('span.more').fadeOut(300);
    }
  );

  var news_tabs = document.getElementById('news_tabs').getElementsByTagName('li');
  
  ClassNames = new Array();
  for(i = 0; i < news_tabs.length; i++) {
    // Remove all links from Tab-Buttons if JS is available
    var link_construction = 'javascript:void(0)';
    news_tabs[i].getElementsByTagName('a')[0].setAttribute('href', link_construction);
    ClassNames[i] = news_tabs[i].className;
  }

  jQuery('#news_tabs li').click(
    function() {
      for(i = 0; i < news_tabs.length; i++){
        if(jQuery('#news_tab_content .tab_content:eq('+i+')').hasClass(jQuery(this).attr('class'))) {
          if(!jQuery(this).hasClass('active')){
            // Abort last animation, if new animation starts
            jQuery('#news_tabs li').stop(true, true);
            jQuery('#news_tab_content .tab_content').stop(true, true);
            
            // Hide the background of non-active Tab-Buttons
            jQuery('.tab_bg').fadeOut(500);
            
            jQuery('#news_tabs li').removeClass('active');
            
            jQuery('#news_tab_content .active').css({
              'position' : 'absolute',
              'z-index' : '1'
            }).fadeOut('slow',
              function() {
                // IE7 Bugfix: fadeOut doesn't set "display: none" to IE7
                jQuery(this).removeClass('active').css('display', 'none');
              }
            );

            jQuery('#news_tab_content .tab_content:eq('+i+')').css({
              'display' : 'none',
              'position' : 'relative',
              'visibility' : 'visible',
              'z-index' : '10'
            }).fadeIn('slow').addClass('active');
            
            jQuery(this).show('fast').addClass('active');
            jQuery('#news_tabs li').filter('li:not(.active)').children('a').children('span');
            jQuery(this).children('a').removeClass('hover').children('span');

            // Background of the active Tab-Button
            jQuery(this).children('.tab_bg').stop(true, true).fadeIn(300);
            
          }
        }
      }
  });
  
  /* change color on tab hover */
  jQuery('#news_tabs ul li').hover(
    function() {
      if(!jQuery(this).hasClass('active')) {
        jQuery(this).stop(true, true).children('a').addClass('hover');
        //jQuery(this).children('a').children('span').stop(true, true).css('color', '#c42e00');
        jQuery(this).children('a').children('span').stop(true, true);
      }
    },
    function(){
      if(!jQuery(this).hasClass('active')) {
        //jQuery(this).children('a').removeClass('hover').children('span').stop(true, true).css('color', '#4c4c4c');
        jQuery(this).children('a').removeClass('hover').children('span').stop(true, true);
      }
    }
  );
  
  /* Hover-effect of the news fields */
  /*
  jQuery('.news-latest-item').hover(
    function(){
      jQuery(this).addClass('hover');
    },
    function(){
      jQuery(this).removeClass('hover');
    }
  );
  */
  /* Link behaviour on click */
  jQuery('.news-latest-item').click(
    function(){
      //window.location.href = jQuery(this).children('.news-latest-item-border').children('h3:first').children('a').attr('href');
    }
  );
}

function loadHomeNewsTagHeightHandler()
{  
  jQuery('.tx-calendar-pi1 .news-latest-item:first').addClass('first');
  /* Set start-height of the news-content div */
  var highest_news_tab_height = jQuery('#news_tab_content .tab_content:first .news-latest-items').height();
  
      // Show all boxes to calculate the highest box
        jQuery('#news_tab_content .tab_content').filter(':not(.active)').css('display', 'block');
        
        for(var i = 0; i < jQuery('#news_tab_content .tab_content').length; i++) {
          if(highest_news_tab_height < jQuery('#news_tab_content .tab_content:eq('+i+') .news-latest-items').height())
            var highest_news_tab_height = jQuery('#news_tab_content .tab_content:eq('+i+') .news-latest-items').height();
        }
        
      // hide all not visible boxes
        jQuery('#news_tab_content .tab_content').filter(':not(.active)').css('display', 'none');

  // IE6 uses high instead of min-height
  if(window.navigator.userAgent.indexOf("MSIE 6") > -1){
    jQuery('#news_tab_content .tab_content .news-latest-items').css('height', highest_news_tab_height);
    
    /* If other news-content boxes are higher, set new height */
    jQuery('#news_tabs li').click(
      function() {
        if(highest_news_tab_height < jQuery('#news_tab_content .tab_content:visible .news-latest-items').height()) {
          highest_news_tab_height = jQuery('#news_tab_content .tab_content:visible .news-latest-items').height();
          jQuery('#news_tab_content .tab_content .news-latest-items').css('height', highest_news_tab_height);
        }
    });
  } else {
    jQuery('#news_tab_content .tab_content .news-latest-items').css('min-height', highest_news_tab_height);
    
    /* If other news-content boxes are higher, set new height */
    jQuery('#news_tabs li').click(
      function() {
        if(highest_news_tab_height < jQuery('#news_tab_content .tab_content:visible .news-latest-items').height()) {
          highest_news_tab_height = jQuery('#news_tab_content .tab_content:visible .news-latest-items').height();
          jQuery('#news_tab_content .tab_content .news-latest-items').css('min-height', highest_news_tab_height);
        }
    });
  }
}

function loadHomeNewsTagHeightHandler_v2()
{
  jQuery('.tx-calendar-pi1 .news-latest-item:first').addClass('first');
  /* Set start-height of the news-content div */
  var highest_news_tab_height = jQuery('#news_tab_content .tab_content:first').height();
        
        for(var i = 0; i < jQuery('#news_tab_content .tab_content').length; i++) {
          if(highest_news_tab_height < jQuery('#news_tab_content .tab_content:eq('+i+')').height())
            var highest_news_tab_height = jQuery('#news_tab_content .tab_content:eq('+i+')').height();
        }

  // IE6 uses high instead of min-height
  if(window.navigator.userAgent.indexOf("MSIE 6") > -1){
    jQuery('#news_tab_content').css('height', highest_news_tab_height);

    jQuery('#news_tabs li').click(
      setNewHeight('height', highest_news_tab_height)
    );
  } else {
    jQuery('#news_tab_content').css('min-height', highest_news_tab_height);
    
    /* If other news-content boxes are higher, set new height */
    jQuery('#news_tabs li').click(
      setNewHeight('min-height', highest_news_tab_height)
    );
  }
}

function loadCalHandler(site_url)
{
  jQuery('.event_header_container').hover(
    function() {
      jQuery(this).parent().addClass('hover');
    },
    function() {
      jQuery(this).parent().removeClass('hover');
    }
  );


  jQuery('.event_list_item').click(
    function() {
      redirect_url = jQuery(this).children().children('.event_title').children('a').attr('href');
      //jQuery(window.location).attr('href', site_url + redirect_url);
      window.location = site_url + redirect_url;
    }
  );
}

function loadUserInteractHandler()
{
  var linked_opener_text = jQuery('#user_interact_opener a span').html();
  
  // checks cookies on reload and call last status of the interact area (openned/closed)
  interactCookieStatus(linked_opener_text);

  // Show login-box
  jQuery('#user_interact_opener a.icon').click(
    function() {
       interactBoxStatusChanger(linked_opener_text);
    }
  );
  
  jQuery('.ui_open').click(
    function() {
      interactBoxStatusChanger(linked_opener_text);
      /* deactivate links with the class ui_open */
      if(jQuery(this).attr('href')) {
        return false;
      }
    }
  );
 
  // Close User Interact, if click on content
  /*
  jQuery("html").click(function(event) {
    var target = jQuery(event.target);
    if (jQuery('#user_interact').is(':visible') && !target.is("#user_interact_container *") && !target.is('#user_interact_opener a') && !target.is('#user_interact_opener a span') && !target.is('.teaser')) {
      jQuery('#user_interact').slideUp(400);
      jQuery('#user_interact_opener a.icon').removeClass('opened').children('span').html(linked_opener_text);
    }
  });
  */
}

function loadFancyboxHandler() {
 
  // copy the rel-attr from <img> into <a>, because impossible in TypoScript
  jQuery('img[rel=image]').parent().parent('a').attr('rel', 'image');
  
  jQuery('a[rel=image]').fancybox({
    'titlePosition' : 'inside',
    'overlayColor'  : '#000000',
    'padding' : 20
  });
  
  // FE Editting iframe in fancybox
  jQuery('.edit_entry a.fe_admin').fancybox({
    'width'         : 770,
    'height'        : '75%', 
    'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'easingIn'      : 'easeOutBack',
		'easingOut'     : 'easeInBack',
		'overlayColor'  : '#000000',
		'autoScale'     : true, 
    'type'          : 'iframe' 
  });
  
  // 
  jQuery('a.fancy_iframe').fancybox({
    'width'         : 956,
    'height'        : 590, 
    'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'easingIn'      : 'easeOutBack',
		'easingOut'     : 'easeInBack',
		'overlayColor'  : '#000000',
		'autoScale'     : true,
		'titleShow'     : false,
    'type'          : 'iframe'
  });
  
}

function loadCatTreeHandler() {
  
  var loader = '<img class="loader_image" src="/fileadmin/images/default/loader.gif" alt="loader" />';
  jQuery('.category_tree_item li a').addClass('loader').append(loader);
  
  // add class="hasSub" on the cat-link if there are sub-categories
  jQuery('.category_tree_item_sub_container').prev().addClass('hasSub');
  
  // keep the second level openned on reload
  jQuery('.category_tree_item_sub .cur').parent().show().parent().parent().parent().addClass('active_sub');

  jQuery('.category_tree_item li a').click(
    function() {
      if(jQuery(this).next().hasClass('category_tree_item_sub_container')) {
        // if already visible, then hide it
        if(jQuery(this).next().is(':visible')) {
          jQuery('.category_tree_item_sub_container').hide().parent('li').removeClass('active_tree_sub');
          
          return false;
        }
        // hide other submenues
        jQuery('.category_tree_item_sub_container').hide().parent('li').removeClass('active_tree_sub');
        jQuery(this).next().show();
        jQuery(this).parent('li').addClass('active_tree_sub');
        return false;
      } else {
        jQuery('.loader_image').fadeOut('fast');
        jQuery(this).children('.loader_image').fadeIn('fast');
        
        // remove loader-image, when finished loading
        jQuery(window).unload(
          function() {
            jQuery('.loader_image').fadeOut('fast');
          }
        );  
      }
    }
  );
  jQuery("html").click(function(event) {
    var target = jQuery(event.target);
    if (!target.is('.category_tree_item') && !target.is('.category_tree_item *')) {
      jQuery('.category_tree_item_sub_container').hide().parent('li').removeClass('active_tree_sub');
    }
  });
  
}


function loadNavHandler()
{
  jQuery('#mainnav li.level1').hover(
    function(){
      jQuery(this).addClass('hover');
    },
    function(){
      jQuery(this).removeClass('hover');
    }
  );
}

// Helper Functions ------------------------------------------------------------
function setNewHeight(heightType, highest_news_tab_height)
{
  if(highest_news_tab_height < jQuery('#news_tab_content').height()) {
    highest_news_tab_height = jQuery('#news_tab_content').height();
    jQuery('#news_tab_content').css(heightType, highest_news_tab_height);
  }
}

function setTeaserHeight(teaser_height, teaser_number) {
  if(teaser_number >= 1) {
    for(i = 0; i < teaser_number; i++) {
      if(teaser_height < jQuery('.small_teaser_container:eq('+i+')').height()) {
        teaser_height = jQuery('.small_teaser_container:eq('+i+')').height();
      }
      if(window.navigator.userAgent.indexOf("MSIE 6") > -1){
        jQuery('.small_teaser').css('height', teaser_height);
      } else {
        jQuery('.small_teaser').css('min-height', teaser_height);
      }
    }
  }
}


function interactBoxStatusChanger(linked_opener_text){
  if(jQuery('body').hasClass('fe_login')) {
    if(jQuery('#user_interact_body').is(':hidden')) {
      jQuery.cookie("wes_interact", 1, { path: '/'});
      jQuery('#user_interact_body').slideDown(400);
      jQuery('#top').scrollTo(600);
      jQuery('#user_interact_opener a.icon').addClass('opened').children('span').html('Schliessen');
    } else {
      jQuery.cookie("wes_interact", 0, { path: '/'});
      jQuery('#user_interact_body').slideUp(400);
      jQuery('#user_interact_opener a.icon').removeClass('opened').children('span').html(linked_opener_text);
    }
  } else {
    if(jQuery('#user_interact').is(':hidden')) {
      jQuery.cookie("wes_interact", 1, { path: '/'});
      jQuery('#user_interact').slideDown(400);
      jQuery('#top').scrollTo(600);
      jQuery('#user_interact_opener a.icon').addClass('opened').children('span').html('Schliessen');
    } else {
      jQuery.cookie("wes_interact", 0, { path: '/'});
      jQuery('#user_interact').slideUp(400);
      jQuery('#user_interact_opener a.icon').removeClass('opened').children('span').html(linked_opener_text);
    }
  }
}

function interactCookieStatus(linked_opener_text){
  if(jQuery('body').hasClass('fe_login')) {
    if(jQuery.cookie("wes_interact") == 1) {
      jQuery('#user_interact_body').show();
      jQuery('#user_interact_opener a.icon').addClass('opened').children('span').html('Schliessen');
    } else {
      jQuery('#user_interact_body').hide();
      jQuery('#user_interact_opener a.icon').removeClass('opened').children('span').html(linked_opener_text);
    }
  } else {
    if(jQuery.cookie("wes_interact") == 1) {
      jQuery('#user_interact').show();
      jQuery('#user_interact_opener a.icon').addClass('opened').children('span').html('Schliessen');
    } else {
      jQuery('#user_interact').hide();
      jQuery('#user_interact_opener a.icon').removeClass('opened').children('span').html(linked_opener_text);
    }
  }
}

// Scroll to window position extension
function loadWesJQueryExtensions() {
  jQuery.fn.extend({
    scrollTo : function(speed, easing) {
      return this.each(function() {
        var targetOffset = jQuery(this).offset().top;
        jQuery('html,body').animate({scrollTop: targetOffset}, speed, easing);
      });
    }
  });
}

function ieSelectBoxWidthFix() {
  //alert(window.navigator.userAgent);
  
  if(window.navigator.userAgent.indexOf("MSIE") > -1){
    jQuery('select').hover(
      function() {
        jQuery(this).addClass('ie_width_fix_hover');
        //jQuery(this).children('option:first').focus();
        jQuery(this).css('width', 'auto');
      }, function() {
        jQuery(this).removeClass('ie_width_fix_hover');
        jQuery(this).removeAttr('style');
      }
    );
    
    jQuery('select').focus(
      function() {
        jQuery(this).addClass('ie_width_fix_focus');
        jQuery(this).css('width', 'auto');
        //jQuery(this).children('option:first').focus();
      }
    );
    jQuery('select').blur(
      function() {
        jQuery(this).removeClass('ie_width_fix_focus');
        jQuery(this).removeAttr('style');
      }
    );
  }
}

// Functions

var tooltip=function(){
	var id = 'tt';
	var top = 3;
	var left = 3;
	var maxw = 185;
	var speed = 10;
	var timer = 20;
	var endalpha = 100;
	var alpha = 0;
	var tt,t,c,b,h;
	var ie = document.all ? true : false;

  return{
		show:function(v,w){
			if(tt == null){
				tt = document.createElement('div');
				tt.setAttribute('id',id);
				right = document.createElement('div');
				right.setAttribute('id',id + 'rgt');
				c = document.createElement('div');
				c.setAttribute('id',id + 'cont');
				b = document.createElement('div');
				b.setAttribute('id',id + 'bot');
				tt.appendChild(right);
				tt.appendChild(c);
				tt.appendChild(b);
				document.body.appendChild(tt);
				tt.style.opacity = 0;
				tt.style.filter = 'alpha(opacity=0)';
				document.onmousemove = this.pos;
			}
			tt.style.display = 'block';
			c.innerHTML = v;
			/*
      //tt.style.width = w ? w + 'px' : '';
      if(!w && ie){
				t.style.display = 'none';
				b.style.display = 'none';
				//tt.style.width = tt.offsetWidth;
				t.style.display = 'block';
				b.style.display = 'block';
			}
			//if(tt.offsetWidth > maxw){tt.style.width = maxw + 'px'}
			//tt.style.width = 210 + 'px';
			*/
			h = parseInt(tt.offsetHeight) + top;
			clearInterval(tt.timer);
			tt.timer = setInterval(function(){tooltip.fade(1)},timer);
		},
		pos:function(e){
			var u = ie ? event.clientY + document.documentElement.scrollTop : e.pageY;
			var l = ie ? event.clientX + document.documentElement.scrollLeft : e.pageX;
			//var x=getposOffset(obj, "left");
			//var y=getposOffset(obj, "top")+verticaloffset
			tt.style.top = (u - h) + 'px';
			tt.style.left = (l + left) + 'px';

			//alert(document.documentElement.scrollTop);

		},
		fade:function(d){
			var a = alpha;
			if((a != endalpha && d == 1) || (a != 0 && d == -1)){
				var i = speed;
				if(endalpha - a < speed && d == 1){
					i = endalpha - a;
				}else if(alpha < speed && d == -1){
					i = a;
				}
				alpha = a + (i * d);
				tt.style.opacity = alpha * .01;
				tt.style.filter = 'alpha(opacity=' + alpha + ')';
			}else{
				clearInterval(tt.timer);
				if(d == -1){tt.style.display = 'none'}
			}
		},
		hide:function(){
			clearInterval(tt.timer);
			tt.timer = setInterval(function(){tooltip.fade(-1)},timer);
		}
	};
}();

