var isLandingPage = false;

if(window.surveyURL){
	$.ajax({
		url: window.surveyURL,
		dataType: 'json',
		success: function(data){
			if(data.showSurvey){
				$(function(){
					var elem = $('#surveypopupcontainer');
					if(!elem[0]){return;}
					var className = elem.attr('className') || '';
					tb_show("", "TB_inline?height=flex&amp;width=flex&amp;inlineId=surveypopupcontainer", "", className);
					$('#TB_ajaxContent').css('height', '');
					tb_position();
					$('#bospopupform input').click(tb_remove);
				});
			}
		}
	});
}
$('html').addClass('js js-on');
function checkCookie(cookieId, trueElementId, falseElementId){
    if ($.cookies.get(cookieId)) {
        $(falseElementId).hide();
        $(trueElementId).show();
    }
}

function getLocationParams(){
    var locationParams = "";
    if (window.location.search != "") {
        var deeplinkparameter = String(window.location).split("?");
        if (deeplinkparameter[1] != "") {
            var args = deeplinkparameter[1].split("&");
            for (i = 0; i < args.length; i++) {
                var a = args[i].split("=");
                if (a[0] == "PartnerID" | a[0] == "ref" | a[0] == "zanpid") {
                    locationParams = locationParams + "PartnerID=" + a[1] + "&";
                }
                else {
                    locationParams = locationParams + a[0] + "=" + a[1] + "&";
                }
            }
            locationParams = locationParams.substr(0, locationParams.length - 1);
        }
    }
    if (locationParams != "") {
        locationParams = "?" + locationParams;
    }
    return locationParams
}

$(document).ready(function(){
			$(".cut").each(function(i) {
			   $(this).html($(this).html().split('').join('<wbr>'));
			});
			
			$('textarea[maxlength]').keyup(function(){
				 
		        var max = parseInt($(this).attr('maxlength'));
		        if($(this).val().length > max){
		       	  
		          $(this).val($(this).val().substr(0, max));
					
		        }
		});

	var addTeaserLeftRightClass = function(e, data){
		data.instance.atomElem.removeClass('left-view right-view')
			.eq(data.newIndex)
			.addClass('left-view')
			.end()
			.eq(data.newIndex + 4)
			.addClass('right-view')
		;
	};
	
	$('div.teaser-wrapper').scroller({ 
		prevLink: 'div.prev span', 
		nextLink: 'div.next span',
		init: addTeaserLeftRightClass,
		end: addTeaserLeftRightClass
	});
	
	$('#browserupdate')
		.browserupdate({
			devMode: false, //für live betrieb auf false oder browser kürzel ändern!!! true | msie | mozilla | opera | safari
			showupdate: function(e, ui){
				$('.close > a', ui.instance.element).bind('click', function(){
					ui.instance.element
						.animate(
							{
								opacity: 0,
								marginTop: ui.instance.element.outerHeight() * -1
							},
							{
								complete: function(){
									$(this).hide();
								}
							}
						)
					;
					return false;
				});
				setTimeout(function(){
					ui.instance.element
						.css({
							'marginTop': ui.instance.element.outerHeight() * -1,
							opacity: 0,
							display: 'block'
						})
						.animate({
							opacity: 1,
							marginTop: 0
						})
					;
				}, 300);
			}
		})
	;
	
    // open account button
    $('#openAccountDetails').appendTo('body');
	$('#auto-overlay').appendTo('body').each(function(){
		tb_show("", "TB_inline?height=flex&amp;width=flex&amp;inlineId=auto-overlay", "", $('> div', this)[0].className);
		$('#TB_ajaxContent').css('height', '');
		tb_position();
	});
	
	
	//allgemeingültiges css das die aktuellen Parameter immer anhängt.
	$("a.getParams").click(function(){		
		this.href = this.href + getLocationParams();
		return true;
	});
	
	
    $("a#buttonAccountOpen, a#buttonAccountOpen2, a#buttonAccountOpenLanding, a#buttonLogin").click(function(){
        var openDialog = $(this).hasClass('open-dialog');
        
        
        
        if (isLandingPage && !openDialog) {
            if (languageCode == "de") {
                var currentLocationParams = getLocationParams();
                var layerAccountLink = $(".contentOpenAccountDetails .detailsLeft a.buttonSmall");
                layerAccountLink.attr("href", layerAccountLink.attr("href") + currentLocationParams);
                this.href = this.href + currentLocationParams;
            }
            
            return true;
        }
        
        if (languageCode == "de" || openDialog || (languageCode == "nl" && $(this).is('#buttonLogin'))) {
            var currentLocationParams = getLocationParams();
            var layerAccountLink = $(".contentOpenAccountDetails .detailsLeft a.buttonSmall");
            layerAccountLink.attr("href", layerAccountLink.attr("href") + currentLocationParams);
            this.href = this.href + currentLocationParams;
            var userLoginedIn = $.cookies.get("bankCustomer");
            if (!userLoginedIn) {
                tb_show("", "TB_inline?height=flex&amp;width=617&amp;inlineId=openAccountDetails", "");
                return false;
            }
        }
        if (languageCode == "nl") {
            /*
             tb_show("","TB_inline?height=flex&amp;width=617&amp;inlineId=openAccountDetails","");
             return false;
             */
        }
        return true;
    });
    
    // add mousover to info icon
    
    $("a img.contentInfoIcon").hover(function(){
        $(this).attr("src", basicImagePath + "/icon_info-a.gif");
    }, function(){
        $(this).attr("src", basicImagePath + "/icon_info.gif");
    });
    
    if (typeof jsAjaxBase == "undefined") {
        // init contact form
        $("#kontaktForm").jForms();
        
        $("#mainSubject_fake_list li").each(function(i, e){
            $(e).click(function(){
                $("#subject1Container, #subject2Container, #subject3Container").hide();
                $("#subject" + i + "Container").show();
                $("#subject1_fake_label, #subject2_fake_label, #subject3_fake_label").css("display", "none");
                if (i != 0) {
                    $("#subject" + i + "_fake_label").css("display", "block");
                }
            });
        });
        
        // init content search
        $("#contentSearchForm").jForms();
    }
    
    $(".searchInput").mouseover(function(){
        $(this).hide();
        $("#searchButton").show();
    }).click(function(){
        $("#contentSearchForm").submit();
    });
    $("#searchButton").mouseout(function(){
        $(this).hide();
        $(".searchInput").show();
    }).click(function(){
        $("#contentSearchForm").submit();
    });
    
    $("#contentSearchValue").click(function(){
        $(this).select();
    });
    
    // build tab buttons
    $(".contentTabNavigation li a.active").before('<img src = "' + basicImagePath + '/tab_navi_left_active.gif" />').after('<img src = "' + basicImagePath + '/tab_navi_right_active.gif" />')
    $(".contentTabNavigation li a:not(.active)").before('<img src = "' + basicImagePath + '/tab_navi_left.gif" />').after('<img src = "' + basicImagePath + '/tab_navi_right.gif" />')
    $(".tabContent").after('<img class="tabBgBottom" src = "' + basicImagePath + '/tab_bg_bottom.gif" />')
    
    // add corners to main navi
    $(".mainNavigation table").before('<div class="bgMainnavigationLeft"></div>').after('<div class="bgMainnavigationRight"></div>');
    
    //add numbers to columns
    $("#contentTableLoginInfo .contentTableColumn img").each(function(i, e){
        $(this).before('<div class="contentTableColumnNumber' + (i + 1) + '"></div>');
        if (i == 3) {
            $(this).parent().addClass("last");
        }
    });
    $("#table3Columns .contentTableColumn img.columnImage").each(function(i, e){
        $(this).before('<div class="contentTableColumnNumber' + (i + 1) + '"></div>');
        if (i == 3) {
            $(this).parent().addClass("last");
        }
    });
    $(".contentTable4Columns .contentTableColumn img.columnImage").each(function(i, e){
        $(this).before('<div class="contentTableColumnNumber' + (i + 1) + '"></div>');
        if (i == 3) {
            $(this).parent().addClass("last");
        }
    });
    
    // add icon to pdf link
    $("#relatedContent, #sidebarRight").find("p.standard a.pdf").append('<div class="iconpdf"></div>');
    
    //show subnavigation
    var elementLeft = 18;
    $(".mainNavigation a").each(function(i, e){
    
        var elementWith = $(".mainNavigation table td:eq(" + i + ")").width();
        
        if (i == 0) {
            $("#subNavigation" + i).css("left", elementLeft - 6);
        }
        else 
            if (i == $(".mainNavigation table td").size() - 1) {
                $("#subNavigation" + i).css("right", 237);
                $("#subNavigation" + i).css("left", "auto");
            }
            else {
                $("#subNavigation" + i).css("left", elementLeft);
            }
        elementLeft += elementWith;
        
        $("#subNavigation" + i).show();
        if ($("#subNavigation" + i + " a").width() < elementWith) {
            if (i == 0) {
                $("#subNavigation" + i + " a").width(elementWith - 21);
            }
            else 
                if (i == $(".mainNavigation table td").size() - 1) {
                    $("#subNavigation" + i + " a").width(elementWith - 23);
                }
                else {
                    $("#subNavigation" + i + " a").width(elementWith - 28);
                }
        }
        else {
            $("#subNavigation" + i + " a").width($("#subNavigation" + i + " a").width());
        }
        $("#subNavigation" + i).hide();
        
        $(this).mouseover(function(){
            $("#subNavigation" + i).show();
            var tester = $("#subNavigation" + i).width();
            var tester2 = $("#subNavigation" + i).height();
            $("#subNavigation" + i).css("width", tester);
            $(".subNavigationBottomBG").css("width", tester - 22);
            $(".subNavigationLeftBG").css("height", tester2);
            $(".subNavigationRightBG").css("height", tester2);
            if (i == 0) {
                $(".bgMainnavigationLeft").addClass("bgMainnavigationLeftActive");
            }
            if (i == $(".mainNavigation table td").size() - 1) {
                $(".bgMainnavigationRight").addClass("bgMainnavigationRightActive");
            }
        }).mouseout(function(){
            $("#subNavigation" + i).hide();
            if (i == 0) {
                $(".bgMainnavigationLeft").removeClass("bgMainnavigationLeftActive");
            }
            if (i == $(".mainNavigation table td").size() - 1) {
                $(".bgMainnavigationRight").removeClass("bgMainnavigationRightActive");
            }
        });
    });
    
    $(".subNavigation").mouseenter(function(){
        $(this).show();
        if (this.id == "subNavigation0") {
            $(".bgMainnavigationLeft").addClass("bgMainnavigationLeftActive");
        }
        if (this.id == "subNavigation" + ($(".mainNavigation table td").size() - 1)) {
            $(".bgMainnavigationRight").addClass("bgMainnavigationRightActive");
        }
    }).mouseleave(function(e){
        $(this).hide();
        if (this.id == "subNavigation0") {
            $(".bgMainnavigationLeft").removeClass("bgMainnavigationLeftActive");
        }
        if (this.id == "subNavigation" + ($(".mainNavigation table td").size() - 1)) {
            $(".bgMainnavigationRight").removeClass("bgMainnavigationRightActive");
        }
    });
    
}); // document.ready

