/* 
	Main Javascript-File powered by JQuery for the Deutsche Bank Stiftung
	url: http://www.deutsche-bank-stiftung.de
	author: Robert Weber for closingtag 2009
	email: info@closingtag.de
*/


/*  sIFR for Keyvisual Heading
if($("#dbs #keyvisual").length){
  if(typeof sIFR == "function"){
    if($("#dbs.start").length){
      sIFR.replaceElement(named({sSelector:"#keyvisual h2", sFlashSrc:"/layout/universbold.swf", sColor: "#ffffff", sWmode: "transparent"}));        
    } else {
      if($("#dbs").hasClass("keyvisual")){
        sIFR.replaceElement(named({sSelector:"h2#title", sFlashSrc:"/layout/universbold.swf", sColor: "#ffffff", sWmode: "transparent"}));        
      } else {
        sIFR.replaceElement(named({sSelector:"h2#title", sFlashSrc:"/layout/universbold.swf", sColor: "#003399", sWmode: "transparent"}));        
      }
    }
  }
}*/

/* Cufon for Keyvisual Heading */

Cufon.replace('#keyvisual h2');

$(document).ready(function() {


      /* bind the disclaimer to every link with external href */
      //$("a[href~=http://]").bind("click",disclaimer);
      $("a[href$=\".pdf\"]").attr("target","_blank");

      /* Special treatments for IE6 and IE7 */
      if(navigator.appVersion.indexOf("MSIE 6.") != -1)     {
        		$("#dbs.page #content #main .columnwide + .column").css("display","none");
        		$("#dbs.page #content #main .column + .column").css("padding","0");
        		$("#dbs.page #content #main .col + .col + .col").css("margin","0");
        		$("#dbs #content #main .col2-3 .col + .col").css("margin-right","0");
        		$("#dbs #content #main .col2-3 + .col").css("margin-right","0");
        		$("#dbs.page #content #main.dates .col + .col + .col").css("margin","0 0 34px 0");
        		$("#dbs .search-form input[type=text]").attr("class","text");
        		$("#dbs .search-form input[type=image]").attr("class","image");
        		$("#dbs #navigation li:last-child a").css("border","none");
        		$("#dbs #metanavigation li:last-child a").css("border","none");
        		$("#dbs.page #content #main .publication + h5").attr("class","clear");
        		$("#dbs.page #content #main .publication + div").each(function(){
                          if($(this).attr("class") != "publication") {
                                $(this).attr("class",$(this).attr("class") + " clear");
                          }
                    });
        		$("#dbs.page #supertopic + #main").css("margin","30px 12px 0 0");			
        		$("#dbs.page #supertopic dt:first-child").css("margin","0");
          	$("#dbs.page #content #main .column .textbox p:last-child").css("padding","0");
        		$("#dbs.page #content #main.page ul#contactnavigation li:last-child").css("border","0");
        		$("#dbs.page #content #main.dates ul#datenavigation li:last-child ").css("border","0");
        		$("#dbs.page #content #main .columnwide div.line2:first-child").css("margin-top","0");
        		$("#dbs.page #content #main.supertopic .column .imagebox:first-child + .linklist").attr("class","linklist-first-child");
        		$("#dbs #content #main .column .textbox p + ul").css("margin","-1.0em 0 1.8em 0");
            if($("#supertopic").length)	{
                $("#dbs #top.tools").css("top","125px");
            }
      }
      else if(navigator.appVersion.indexOf("MSIE 7.") != -1)     {
      	$("#dbs.page #supertopic dt:first-child").css("margin","0");
      	$("#dbs.page #content #main .column .textbox p:last-child").css("padding","0");
		    $("#dbs #navigation li:last-child a").css("border","none");
		    $("#dbs #metanavigation li:last-child a").css("border","none");
		    $("#dbs .search-form input[type=text]").attr("class","text");
		    $("#dbs .search-form input[type=image]").attr("class","image");
        $("#dbs.page #content #main.page ul#contactnavigation li:last-child").css("border","0");
        $("#dbs.page #content #main.dates ul#datenavigation li:last-child ").css("border","0");
        if($("#supertopic").length)	{
            $("#dbs #top.tools").css("top","125px");
        }
      }

    	/* Add a special class to the body for Safari */
    	if(navigator.appVersion.indexOf('Safari') != -1){
    		$('#dbs').addClass('safari');
    	}

      /* Margin before Linklist */
      $("#dbs.page #content #main .column div.textbox:last").css("margin-bottom","2.8em"); 

      /* Images Galery Clearging */
      if($("#imagegalery").length){
      	var i = 1;
    		$("#imagegalery li").each(function(){
    			if(i % 3  == 0){
    				$(this).css("padding","0 0 32px 0");
    				$(this).next("li").css("clear","both");
    			}
    			i++;
    		});      
      }

      /* remove empty p-tags*/
      $("#main p").each(function(){
        if($(this).text() == "") $(this).remove();
      });

      /* Publications Clearing */
      var i = 1;
      $("div.publication").each(function(){
          if(i % 3  == 0 ){
          $(this).css("margin","0 0 44px 0");
          if(navigator.appVersion.indexOf("MSIE 7.") != -1 || navigator.appVersion.indexOf("MSIE 6.") != -1)     {
              $(this).after("<p></p>");
              $(this).next("p").css("clear","both");
              $(this).next("p").css("width","100%");
              $(this).next("p").css("line-height","0");
          } else {
              $(this).next("div.publication").css("clear","both");
          }
          }
          if($(this).next().attr("class") != "publication"){
              i = 1;
          } else {
              i++;
          }
      });

      /* Remove empty uls from Navigation */

      $("#dbs #navigation #subnavigation ul").each(function(){
      	if($(this).children().length == 0) $(this).remove();
      });

      /* Bind accessibility Controls */
      $("#accessibility li a").bind("click",accessibilityControl);
      
      /* Search Form Handling */
	    if($("#form-db").length){
      		$("#form-db input[type=text]").bind('focus',function(){
      			$(this).attr("value","");
      		});
	    }
      /* Big Search Form Handling */
	    if($("#form-dbs-full").length) {
	      $("#form-dbs-full").bind("submit", formSearch);
	    }

      /* Fix double line headings in contact detail list */
      if($("dl.contactdetail").length){
            $(window).load(function(){
                var height;
                $("dl.contactdetail dt").each(function(){
                    height = this.clientHeight - 3;
                    if(height == 32) {
                        $(this).next("dd").addClass("high");
                    } else if(height == 48) {
                        $(this).next("dd").addClass("realhigh");
                    }
                });
                $("dl.contactdetail dd").each(function(){
                    if($(this).find("p.desc").length && (this.clientHeight < $(this).find("p.desc")[0].clientHeight)) $(this).css("height",$(this).find("p.desc:first")[0].clientHeight+"px");
                });
            });
      }



      /* Alumni list handling*/
      if($('.alumni:first').length)    {

          $('.alumni:first').find('ul li p').each(function(){
            if($(this).text() == "") $(this).remove(); 
          })
    
          $('.alumni:first').find('ul').each(function(){
              var childlength = $(this).children().length;
              var cols = 0;
              if(childlength % 3 == 0){
                  cols = childlength / 3;
              }
              else    {
                  cols = ((childlength-(childlength % 3)) / 3) + 1;
              
              }
              var i = 1;
              var col = '';
              $(this).find('li').each(function(){
                  if(i == 1){ 
                      col += '<ul><li>' + $(this).html() + '</li>';
                  }
                  else if(i % cols == 0 && i >= cols && i != childlength)    {
                      col += '<li>' + $(this).html() + '</li></ul><ul>';
                  }
                  else if(i % cols == 0 && i >= cols && i != childlength)    {
                      col += '<li>' + $(this).html() + '</li></ul><ul>';
                  }
                  else if(i == childlength)    {
                      col += '<li>' + $(this).html() + '</li></ul>';
                  }
                  else    {
                      col += '<li>' + $(this).html() + '</li>';
                  }
                  i = i + 1;
              });
              $(this).parent().html(col);
          });
          $('.alumnilist').find('ul:last').css('margin','0');        
          $('.alumnilist').css('display','none');
          $('.alumni:first h2').bind('click',toggleList);
          $('.alumni:first').find('h2:last').css('border','0');
          $('.alumni:first h2.active + .alumnilist').css('display','block');
          $('.alumnilist ul li p').bind('click', toggleAlumni);
          $('.alumnilist ul li p + p').hide();
          var alumniheight = $('.alumni').find('div:first').height() + 35;
          if (navigator.appVersion.indexOf("MSIE 6.") != -1) {
              $('.alumni:first').css('height',alumniheight + 'px');
          }
          else    {
              $('.alumni:first').css('min-height',alumniheight + 'px');
          }
      }

      /* Toggle list */
      if($(".togglelist").length){
        $(".togglelist dt").bind("click",toggleDList);
        $(".togglelist dd p a.close").bind("click",toggleDList);
        $(".togglelist dd").hide();
      }

      /* Link target handling within marginal column */
      $("#marginal a.blank").each(function(){
        this.target = "_blank";
      });

      $("#marginal a.popup").click(function(){
        window.open(this.href, "PopUp", "width=700,height=400,left=100,top=200,scrollbars=no,resizeable=no");
        return false;
      });
});
// End of document ready

function formSearch() {
	var $form = $(this);
	var queryString = $form.attr("action") + "?";
	var i = 1;

	$form.find("input[type!=submit]").each(function(){
		queryString += $(this).attr("name") + "=" + escape($(this).attr("value"));
		if($form.find("input[type!=submit]").length != i) {
			queryString += "&amp;";
		}
		i++;
	});
  //queryString = escape(queryString);

	if($("#search-results").length){
	  $("#search-results").remove();
	}
	$form.after("<div id=\"search-results\"><iframe src=\""+queryString+"\" marginheight=\"0\" marginwidth=\"0\" frameborder=\"0\" scrolling=\"no\"></iframe></div>");
	return false;
}


/* Accessibility Control Handling*/

function accessibilityControl() {
	if($(this).parent().attr("id") == 'contrast'){
		if($(this).parent().attr("class") == 'active'){
			$("#content").removeClass("contrast");
			$("#metanavigation").removeClass("contrast");
			$("#navigation").removeClass("contrast");
			$(this).parent().removeClass("active");
		} else {
      $("#content").addClass("contrast");
			$("#metanavigation").addClass("contrast");
			$("#navigation").addClass("contrast");
			$(this).parent().addClass("active");
		}
	} else {
		$(this).parent().parent().find("li").each(function(){
			if($(this).attr("id") != 'contrast') $(this).removeClass("active");
		});
		if($(this).parent().attr("id") == 'smallfont'){
			$("#dbs").attr("style","");
			$(this).parent().addClass("active");
		} else if($(this).parent().attr("id") == 'normalfont'){
			$("#dbs").css("font-size","70%");
			$(this).parent().addClass("active");
		} else if($(this).parent().attr("id") == 'largefont'){
			$("#dbs").css("font-size","80%");
			$(this).parent().addClass("active");
		}
	}
}

/* Show and hide Disclaimer */

function disclaimer()   {
      var href = $(this).attr("href");

      if(href.search(/.+www.urban-age.net.*/) != -1 || href.search(/.+www.foresightproject.net.*/) != -1 || href.search(/.+www.db.com.*/) != -1 || href.search(/.+www.deutsche-bank.de.*/) != -1) return true;
      var offset = $(this).offset();
      if($("#dbs a#de").length) {
            $("#dbs").append("<div id=\"disclaimer\"><p>&Uuml;ber diesen Link verlassen Sie die Informationsseiten der Deutsche Bank Gruppe und erreichen die Web-Page eines anderen Anbieters. Die dort eingestellten Informationen sind ohne Mitwirkung der Deutsche Bank Gruppe erstellt worden und unterliegen der ausschlie&szlig;lichen Verantwortung des anderen Anbieters. Die Deutsche Bank Gruppe &uuml;bernimmt keinerlei Haftung f&uuml;r die Richtigkeit, Vollst&auml;ndigkeit und Aktualit&auml;t dieser Informationen. Wenn Sie die vorstehenden Hinweise akzeptieren, best&auml;tigen Sie dies mit dem Button einverstanden.</p><p><a href=\""+ href +"\">einverstanden</a> <a id=\"reject\" href=\"#\">ablehnen</a></p>")
      } else if($("#dbs a#en").length) {
            $("#dbs").append("<div id=\"disclaimer\"><p>When you access this link, you are leaving the Deutsche Bank website. The information provided on any websites accessed through this link has been produced by independent providers and Deutsche Bank does not endorse or accept any responsibility for information provided on any such sites. Any opinions or recommendations expressed on such other websites are solely those of the independent providers and are not the opinions or recommendations of Deutsche Bank. The existence of a link from this Website to any other such websites does not constitute a recommendation or other approval by Deutsche Bank of such websites or any provider thereof.</p><p><a href=\""+ href +"\">accept</a> <a id=\"reject\" href=\"#\">reject</a></p>")      
      }

      if($("#dbs #disclaimer").length){
            if(offset.left > 484){
                  var right = 726 - offset.left;
                  $("#dbs #disclaimer").css("right",right + "px");
            } else {
                  $("#dbs #disclaimer").css("left",offset.left + "px");            
            }
            $("#dbs #disclaimer").css("top",offset.top + "px");
            $("#dbs #disclaimer #reject").bind("click",reject);
      }
      return false;
}

function reject(){
      if($("#dbs #disclaimer").length){
            $("#dbs #disclaimer").remove();
      }
}


function toggleList()    {
    $('.alumni:first h2').attr('class','');
    $('.alumnilist').css('display','none');
    $(this).attr('class','active');
    $(this).next().css('display','block');
    var alumniheight = $(this).next().height() + 35;
    $('.alumni:first').css('min-height',alumniheight + 'px');
}

function toggleAlumni()    {
    if($(this).next().css('display') == 'none')    {    
        $('.alumnilist ul li p + p').hide();
        $('.alumnilist ul li').attr('style','');
        $(this).next().show();
//        $(this).next().bind('mouseover', keepState);
        var alumniheight = $(this).parent().parent().height() + 35;
        if (navigator.appVersion.indexOf("MSIE 6.") != -1) {
            $('.alumni:first').css('height',alumniheight + 'px');
        }
        else    {
            $('.alumni:first').css('min-height',alumniheight + 'px');
        }
        var thisheight = $(this).parent().height();
        thisheight = thisheight + 10 + 9 + 1;
        if(thisheight % 36 != 0 )    {
            thisheight = (((((thisheight - (thisheight % 36)) / 36) + 1) * 36) - thisheight);
            $(this).next().css('padding-bottom',thisheight+'px');
        }
    }
    else {
        $('.alumnilist ul li p + p').hide();
        $(this).parent().attr('style','');
    }
}

function keepState() {
    $(this).show(); 
    var thisheight = $(this).parent().height();
    thisheight = thisheight + 10 + 9 + 1;
    if(thisheight % 36 != 0 )    {
        thisheight = (((((thisheight - (thisheight % 36)) / 36) + 1) * 36) - thisheight);
        $(this).css('padding-bottom',thisheight+'px');
    }  
}

function toggleDList(){
  if($(this).next("dd").length) {
    $(this).next("dd").toggle();
  } else {
    $(this).parent().parent().toggle();
  }
  return false;
}
