var config_mixer = {
  headerclass: "mixer", //Shared CSS class name of headers group
  contentclass: "mixer_item", //Shared CSS class name of contents group
  revealtype: "click", //Reveal content when user clicks or onmouseover the header? Valid value: "click" or "mouseover"
  mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
  collapseprev: false, //Collapse previous content (so only one open at any time)? true/false
  defaultexpanded: [], //index of content(s) open by default [index1, index2, etc]. [] denotes no content.
  onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)
  animatedefault: false, //Should contents open by default be animated into view?
  persiststate: false, //persist state of opened contents within browser session?
  toggleclass: ["", ""], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
  togglehtml: ["suffix", "<img src='img/basket/mixer_closed.gif' /> ", "<img src='img/basket/mixer_open.gif' /> "], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
  animatespeed: "fast", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
  oninit:function(expandedindices){ //custom code to run when headers have initalized
  //do nothing
  },
  onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
  //do nothing
  }
}


var config_filter = {
  headerclass: "filter_item", //Shared CSS class name of headers group
  contentclass: "filter", //Shared CSS class name of contents group
  revealtype: "click", //Reveal content when user clicks or onmouseover the header? Valid value: "click" or "mouseover"
  mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
  collapseprev: false, //Collapse previous content (so only one open at any time)? true/false
  defaultexpanded: [0,1,2,3], //index of content(s) open by default [index1, index2, etc]. [] denotes no content.
  onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)
  animatedefault: false, //Should contents open by default be animated into view?
  persiststate: false, //persist state of opened contents within browser session?
  toggleclass: ["closed", "open"], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
  togglehtml: ["prefix", "<img src='img/basket/arrow_closed.gif' /> ", "<img src='img/basket/arrow_open.gif' /> "], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
  animatespeed: "fast", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
  oninit:function(expandedindices){ //custom code to run when headers have initalized
  //do nothing
  },
  onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
  //do nothing
  }
}
var config_beteiligungen = {
  headerclass: "head", //Shared CSS class name of headers group
  contentclass: "details", //Shared CSS class name of contents group
  revealtype: "click", //Reveal content when user clicks or onmouseover the header? Valid value: "click" or "mouseover"
  mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
  collapseprev: true, //Collapse previous content (so only one open at any time)? true/false
  defaultexpanded: [], //index of content(s) open by default [index1, index2, etc]. [] denotes no content.
  onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)
  animatedefault: false, //Should contents open by default be animated into view?
  persiststate: false, //persist state of opened contents within browser session?
  toggleclass: ["default", "selected"], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
  togglehtml: ["", "", ""], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
  animatespeed: "fast", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
  oninit:function(expandedindices){ //custom code to run when headers have initalized
  //do nothing
  },
  onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
  //do nothing
  }
}
function addProducttoCart(prodId,menge){
  //result=$.get('http://tash_b2b.local/de/beteiligungsmoeglichkeiten?action=addProducttoCart&prodId='+prodId, '');
  //alert(result);
  if(prodId > 0 && prodId != ""){
    $.ajax({
      type: 'POST',
      url: 'http://'+window.location.host+'/de/index',
      data: 'action=addProducttoCart&prodId='+prodId+'',
      success: function(html){
        $("#basket .content").replaceWith(html);
        $("#metanavi .basket a").html('Warenkorb ('+$(".cartitems").html()+')');
        $("#tools .basket a").html('Warenkorb anzeigen ('+$(".cartitems").html()+')');
      }
    });
  }

}

function removeProductonCart(itemId,teaser){

  $.ajax({
    type: 'POST',
    url: 'http://'+window.location.host+'/de/index',
    data: 'action=removeProductonCart&itemId='+itemId+'&ajax='+teaser,
    success: function(html){
      if(teaser){
        $("#basket .content").replaceWith(html);
      }else{
        $("#content").replaceWith(html);
      }
      $("#metanavi .basket a").html('Warenkorb ('+$(".cartitems").html()+')');
      $("#tools .basket a").html('Warenkorb anzeigen ('+$(".cartitems").html()+')');
    }
  });

}

function editCart(){
  var f = $("#warenkorbform");
  var serializedData = f.serialize();
  $.ajax({
    type: 'POST',
    url: 'http://'+window.location.host+'/de/index',
    data: 'action=editCart&'+serializedData,
    success: function(html){
      $("#content").replaceWith(html);
      $("#metanavi .basket a").html('Warenkorb ('+$(".cartitems").html()+')');
      $("#tools .basket a").html('Warenkorb anzeigen ('+$(".cartitems").html()+')');
    }
  });
}

function searchMarketingMixer(){
  var f = $("#marketing_mixer_form");
  var serializedData = f.serialize();
	
  ajaxhtml= '<tbody><tr class="default"><td colspan="3" class="wait">Bitte einen Augenblick Geduld, wir aktualisieren die Maßnahmen passend zu Ihren Wünschen...<br /><img src="img/objects/ajax-loader.gif" alt="" /></td></tr></tbody>';
  $("#beteiligung tbody").replaceWith(ajaxhtml);
	
  $.ajax({
    type: 'POST',
    url: 'http://'+window.location.host+'/de/index',
    data: serializedData+'&action=marketingmixer&ajax=true',
	
    success: function(html){
      $("#content").replaceWith(html);
		
      // Reinitialisieren der Ajax-Funktionen
      re_init();
								
    }
  });
   
}

function removeMarketingMixerFilter(filter){
	
  ajaxhtml= '<tbody><tr class="default"><td colspan="2" class="wait">Bitte einen Augenblick Geduld, wir aktualisieren die Maßnahmen passend zu Ihren Wünschen...<br /><img src="img/objects/ajax-loader.gif" alt="" /></td></tr></tbody>';
  $("#beteiligung tbody").replaceWith(ajaxhtml);
	
  $.ajax({
    type: 'POST',
    url: 'http://'+window.location.host+'/de/index',
    data: '&action=marketingmixer&ajax=true&remFilter='+filter,
	
    success: function(html){
      $("#content").replaceWith(html);
		 
      // Reinitialisieren der Ajax-Funktionen
      re_init();
    			
    			
								
    }
  });
   
}

function re_init(){
  $(".filter_item").unbind();
  $(".mixer").unbind();
  $(".head").unbind();
  ddaccordion.init(config_mixer)
  ddaccordion.init(config_filter)
  ddaccordion.init(config_beteiligungen)
	
  $("a.basket").click(function(){
    if(!$(this).hasClass('selected')){
      $("img.plus").fadeIn(600).fadeOut(1000);
      addProducttoCart($(this).children("span").html(),1);
      $(this).addClass('selected').html("Im Warenkorb");
    }
  });
  // re_init_sort();
  var Sort_Table=JB_getElementsByClass_TagName("table","sortable");
  for(var i=0;i<Sort_Table.length;i++) new JB_Table(Sort_Table[i]);
	
}


function login(){
  var f = $("#login");
  var serializedData = f.serialize();

  $.ajax({
    type: 'POST',
    url: 'http://'+window.location.host+'/de/index',
    data: serializedData+'&action=login&ajax=true&setlogindata=true',
	
    success: function(html){
      if($('#logout',html).is(":contains('Logout')")){
        $("#navwrap").replaceWith(html);
        $.ajax({
          type: 'POST',
          url: 'http://'+window.location.host+'/de/index',
          data: serializedData+'&action=warenkorb&ajax=true',

          success: function(html){
            $("#basket .content").replaceWith(html);
            if($(".cartitems").html() > 0){
              window.location.href='http://'+window.location.host+'/de/index?action=warenkorb';
            }else{
              window.location.href='http://'+window.location.host+'/de/index?action=registrieren&state=edit';
            }
          }
        });
      }else{
        window.location.href='http://'+window.location.host+'/de/index?action=login&error=userorpass';
      }
    }
  });
   
}

function logout(){
  var f = $("#login");
  var serializedData = f.serialize();
		
  $.ajax({
    type: 'POST',
    url: 'http://'+window.location.host+'/de/index',
    data: serializedData+'&action=logout&ajax=true',
	
    success: function(html){
      $("#navwrap").replaceWith(html);
				
      //Slidereffekt für die Login
      $("#sprachwahl").accordion("10","v");
      $("#sprachwahl").bind("mouseenter", function(){
        if($("#sprachwahl").height() == "10"){
          $("#sprachwahl").accordion("200","v");
        }
      }).bind("mouseleave", function(){
        $("#sprachwahl").accordion("10","v");
      });
				
      $.ajax({
        type: 'POST',
        url: 'http://'+window.location.host+'/de/index',
        data: serializedData+'&action=warenkorb&ajax=true',
				
        success: function(html){
          $("#basket .content").replaceWith(html);
        }
      });
      window.location.href='http://'+window.location.host+'/de/index';
    }
  });
    
   
}
$(document).ready(function(){
  $("#textsearch input.buttonstyle").click(function(){
    $("#textsearch").submit();
  });
  $('A[rel="external"]').click( function() {
    window.open( $(this).attr('href') , "Zweitfenster", "width=700,height=700,left=100,top=200,scrollbars=yes");

    return false;
  });
  $('A[rel="internal"]').click( function() {
    window.open( $(this).attr('href'));

    return false;
  });

  getLanguageLabel($('#langid').val());

   
});

function getLanguageLabel(language){
  switch(language){
    case 'en':
      langhtml='<span class="gb">English</span>';
      $('#start-date-presse').dpSetSelected(new Date().addMonths(-3).asString());
      addRemoveLanguage('gb');
      break;
    case 'nl':
      langhtml='<span class="nl">Nederlands</span>';
      $('#start-date-presse').dpSetSelected(new Date().addMonths(-3).asString());
      addRemoveLanguage('nl');
      break;
    case 'se':
      langhtml='<span class="se">Svenska</span>';
      $('#start-date-presse').dpSetSelected(new Date().addMonths(-3).asString());
      addRemoveLanguage('se');
      break;
    case 'da':
      langhtml='<span class="dk">Dansk</span>';
      $('#start-date-presse').dpSetSelected(new Date().addMonths(-3).asString());
      addRemoveLanguage('dk');
      break;
    case 'no':
      langhtml='<span class="no">Norsk</span>';
      $('#start-date-presse').dpSetSelected(new Date().addMonths(-3).asString());
      addRemoveLanguage('no');
      break;
    case 'at':
      langhtml='<span class="de">A/CH</span>';
      $('#start-date-presse').dpSetSelected(new Date().addMonths(-3).asString());
      addRemoveLanguage('de');

      break;
    default:
      langhtml='<span class="de">International</span>';
      $('#start-date-presse').dpSetSelected(new Date().addMonths(-1).asString());
      break;
  }

  jQuery("#langContainer h3").html(langhtml);
  $('#langid').val(language);

}

function switchLanguage(language){
 getLanguageLabel(language);
  $('#langid').val(language);
  getPressArticle(language);
}

function switchLanguageAndHeadline(language){
 switchLanguage(language);
 switchHeadline(language);
}

function switchHeadline(language){
 switch(language){
   case 'en':
      langhtml='News in English';
      break;
    case 'nl':
      langhtml='Nieuws in het Nederlands ';
      break;
    case 'se':
      langhtml='Nyheter på Svenska ';
      break;
    case 'da':
      langhtml='Nyheder på Dansk ';
      break;
    case 'no':
      langhtml='Nyheter på Norsk';
      break;
    case 'at':
      langhtml='Nachrichten auf deutsch für Österreich und die Schweiz ';
      break;
    default:
      langhtml='Nachrichten';
      break;
  }
  $("h1").html(langhtml);
}

function addRemoveLanguage(lang){
  if($("#lang li[class=de]").html() == null){
    $('<li class=\'de\'><a href=\'javascript:switchLanguage("de");\'>International</a></li>').appendTo('#lang');
  }
}

function getPressArticle(language){

  sort = $('#sort').val();
  var f = $("#pressForm");
  var serializedData = f.serialize();
  
  jQuery.ajax({
    type: 'POST',
    url: 'http://'+window.location.host+ window.location.pathname,
    data: serializedData+'&ajax=true'+'&sort='+sort+'&showlanguage='+language,
    success: function(html){
      listhtml=jQuery('#list',html).html();
      jQuery("#list").html(listhtml);
      $('.searchResult strong:first').html($('.pageflip strong span').html());

    }
  });

}

function getLongPressArticle(boollong){

  jQuery.ajax({
    type: 'POST',
    url: 'http://'+window.location.host+ window.location.pathname,
    data: '&ajax=true'+'&long='+boollong,
    success: function(html){
      articlehtml=jQuery('.fliesstext',html).html();
      jQuery(".fliesstext").html(articlehtml);

    }
  });

}