/* 

	Main Javascript-File powered by JQuery for the Alfred Herrhausen Gesellschaft 

	url: http://www.alfred-herrhausen-gesellschaft.de

	author: Robert Weber for closingtag 2008

	email: info@closingtag.de

*/

/* preloading Images */
if(navigator.appVersion.indexOf("MSIE") != -1)     {

      $(window).bind('load', function() {
          var preload = new Array();
          $("#claim-list li img").each(function() {
              s = $(this).attr("src");
              preload.push(s)
          });
          var img = document.createElement('img');
          $(img).bind('load', function() {
              if(preload[0]) {
                  this.src = preload.shift();
              }
          }).trigger('load');
      });
}
/* Global Vars */

var animation = true;

var activeListItem;



$(document).ready(function() {
      if(navigator.appVersion.indexOf("MSIE") != -1)     {
            window.setTimeout("prepareClaimList()", 750);
      }
      else {
			/*$('#claim-list li img:last-child').load(function(){
                  prepareClaimList();
            });*/
			window.setTimeout("prepareClaimList()", 750);
      }

      /* bind the disclaimer to every link with external href */

      $("a[href~=http://]").bind("click",disclaimer);



      /* Animation should not start in IE6 due to low JS Performance also some CSS Selectors IE 6 doesnt understand */

      if(navigator.appVersion.indexOf("MSIE 6.") != -1)     {

            animation = false;

		$("#ahg.page #content #main .columnwide + .column").css("display","none");

		$("#ahg.page #content #main .column + .column").css("padding","0");

		$("#ahg.page #content #main .col + .col + .col").css("margin","0");

		$("#ahg.page #content #main.dates .col + .col + .col").css("margin","0 0 34px 0");

		$("#ahg .search-form input[type=text]").attr("class","text");

		$("#ahg .search-form input[type=image]").attr("class","image");

		$("#ahg #navigation li:last-child a").css("border","none");

		$("#ahg #metanavigation li:last-child a").css("border","none");

		//$("#ahg.start #content #topics .teaser:last-child").attr("class","last-child");

		$("#ahg.page #content #main .publication + h5").attr("class","clear");

		$("#ahg.page #content #main .publication + div").each(function(){

                  if($(this).attr("class") != "publication") {

                        $(this).attr("class",$(this).attr("class") + " clear");

                  }

            });

		$("#ahg.page #supertopic + #main").css("margin","30px 12px 0 0");			

		$("#ahg.page #supertopic dt:first-child").css("margin","0");

		//$("#ahg #content #main .column .mediathek ul li:first-child").css("margin","0 0 0 0");

		//$("#ahg #content #main .column .mediathek ul li:last-child").css("border","0");

		$("#ahg.page #content #main.page ul#contactnavigation li:last-child").css("border","0");

		$("#ahg.page #content #main.dates ul#datenavigation li:last-child ").css("border","0");
		
		$("#ahg.page #content #main.supertopic .column .imagebox:first-child + .linklist").attr("class","linklist-first-child");
		$("#ahg.page #content #main.supertopic .column .noborder + .linklist").attr("class","linklist-first-child");

            if($("#supertopic").length)	{

                  $("#ahg #top.tools").css("top","125px");

            }

      }



      else if(navigator.appVersion.indexOf("MSIE 7.") != -1)     {



      	$("#ahg.page #supertopic dt:first-child").css("margin","0");

		$("#ahg #navigation li:last-child a").css("border","none");

		$("#ahg #metanavigation li:last-child a").css("border","none");

		$("#ahg .search-form input[type=text]").attr("class","text");

		$("#ahg .search-form input[type=image]").attr("class","image");

		//$("#ahg #content #main .column .mediathek ul li:first-child").css("margin","0 0 0.75em 0");

		//$("#ahg #content #main .column .mediathek ul li:last-child").css("border","0");

            $("#ahg.page #content #main.page ul#contactnavigation li:last-child").css("border","0");

            $("#ahg.page #content #main.dates ul#datenavigation li:last-child ").css("border","0");

            if($("#supertopic").length)	{

                  $("#ahg #top.tools").css("top","125px");

            }

      }

      

	/* Add a special class to the body for Safari */

	if(navigator.appVersion.indexOf('Safari') != -1){

		$('#ahg').addClass('safari');

	}



	/* supertopic effects */

	if($("#supertopic").length)	{

		$("#supertopic dt").bind('click',superTopicAni);

	}





	/* Show all button */

	if($("#showall").length)	{

		var $active;

		$("#showall").hover(function(){

			if($("#main.overview").length){

				$(".map dt").addClass("hover");

			}

			else {

				if($(".map dt.active").length) {

                              $active = $(".map dt.active");

                        }

                        $(".map dt").addClass("active");

			}

		},function(){

                  if($("#main.overview").length){

				$(".map dt").removeClass("hover");

			}

			else {

				$(".map dt").removeClass("active");

                        if($active.length){

                              $active.addClass("active");

                              $active = null;

                        }

			}

		});

	}

	/* Click behaviour for House-Icons on supertopicpage projects */



	if($("dl.map").length) {

            $("dl.map dt").bind("click",showDd);

            $("dl.map dt").hover(function(){

                  $("dl.map dt").removeClass("hover");

                  $(this).addClass("hover");

            },function(){

                  $(this).removeClass("hover");

            });

      }



      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++;

		});      

      }



      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++;

            }

      });



      $("#ahg #navigation #subnavigation ul").each(function(){

      	if($(this).children().length == 0){

      		$(this).remove();

      	}

      });



      $("#accessibility li a").bind("click",accessibilityControl);

	  

	if($("#form-db").length){

		$("#form-db input[type=text]").bind('focus',function(){

			$(this).attr("value","");

		});

	}

	if($("#form-ahg-full").length) {
	      var pattern = /(.*)(querytext=)(.+?)(&)(.*)/;
            var result = pattern.exec(window.location.href);
            if(result != null) {
                var query = result[3];
                query = decodeURI(query);
                $("#querytext").val(query);
		    $("#form-ahg-full").bind("submit", formSearch);
		    $("#form-ahg-full").submit();
            }
            else {
                $("#form-ahg-full").bind("submit", formSearch);
            }
	}

	/* Animation control for claim-list End */

  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");
            });
        });
  }



});
// 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;

}



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'){

			$("#ahg").attr("style","");

			$(this).parent().addClass("active");

		}

		else if($(this).parent().attr("id") == 'normalfont'){

			$("#ahg").css("font-size","70%");

			$(this).parent().addClass("active");

		}

		else if($(this).parent().attr("id") == 'largefont'){

			$("#ahg").css("font-size","80%");

			$(this).parent().addClass("active");

		}

	}

}



/* Show dd element */

function showDd() {

      if($("#main.overview").length){

            $(this).parent().find("dd").hide();

            $(this).next("dd").toggle();

      }

      else {

            $(this).parent().find("dd").hide();

            

            $(this).next("dd").find("strong").remove();

            

            

            var spanText = $(this).find("span").text();

            $(this).next("dd").prepend("<strong>" + spanText + "</strong>");

            $(this).next("dd").toggle();

      }

}







/* Show hide topics in Definition list */

function superTopicAni()	{

      var $item = $(this);

      if($(this).next("dd").find(".teaser").css("display") != 'block')	{

		$('#supertopic dd.active .teaser').slideUp(500);

		$item.next("dd").find(".teaser").slideDown(500);

            $('#supertopic dd.active .claim').fadeOut(1000);

            $item.next("dd").find(".claim").fadeIn(1000);

            $('#supertopic dd.active').removeClass('active');

            $item.next('dd').addClass('active');

      }

}



/* Fade the Claims in and out */

function prepareClaimList() {
	if($('#claim-list').length)	{
            if($('#claim-list li img:last').length)	{
                  $('#claim-list li img:last').removeClass("hide");
                  $('#claim-list li.animation-control a').bind('click',ControlAnimation);
                  fadeInClaim($('#claim-list li img:last'));
            }
	}
}


function fadeInClaim(listitem)	{

	if(animation) {

		if(listitem.parent().prev().find('img').length){
                  listitem.parent().prev().find('img').attr("class","");
			listitem.fadeOut(5000, function(){
                        fadeInClaim(listitem.parent().prev().find('img'));
			});
		}
		else	{

			listitem.parent().parent().find('li img:last').fadeIn(5000, function(){

				listitem.parent().parent().find('li img').show();

				fadeInClaim(listitem.parent().parent().find('li img:last'));

			});

		}

	}

	else {

		activeListItem = listitem;

	}

}





/* Control Animation on click */

function ControlAnimation() {

	if($(this).attr('id') == 'start-animation') {

		animation = true;

		$('#stop-animation').css('display','block');

		$(this).css('display','none');

		fadeInClaim(activeListItem);

	}

	else {

		animation = false;

		$('#stop-animation').css('display','none');

		$('#start-animation').css('display','block');

	}

}



/* 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 || href.search(/.+www.denkichandeutschland.net.*/) != -1 || href.search(/.+www.alfred-herrhausen-gesellschaft.de.*/) != -1) return true;

      var offset = $(this).offset();

      

      if($("#ahg a#de").length) {

            $("#ahg").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($("#ahg a#en").length) {

            $("#ahg").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($("#ahg #disclaimer").length){

            if(offset.left > 484){

                  var right = 726 - offset.left;

                  $("#ahg #disclaimer").css("right",right + "px");

            }

            else {

                  $("#ahg #disclaimer").css("left",offset.left + "px");            

            }

            $("#ahg #disclaimer").css("top",offset.top + "px");

            $("#ahg #disclaimer #reject").bind("click",reject);

      }

      return false;

}



function reject(){

      if($("#ahg #disclaimer").length){

            $("#ahg #disclaimer").remove();

      }

}

