
var $j = jQuery;

$j(document).ready(function(){
	/* audience selector */
	/* The audience selector has 2 versions. A JavaScript version and a non-JavaScript version
	 * Modernizr is not used, but we still want to know if JavaScript is enabled or not. So that is why the class js
	 * is added to the html tag. CSS now knows if the browser supports js or not.
	 * This is something you do not want to solve inside the html because of speed issues.
	 * In the long run it is important to strictly seperate JavaScript, CSS and HTML.
	 */
	$j('html').addClass('js');

	/* The audience switcher needs to have a dropdown onclick
	 * This event listeners checks if the correct element is clicked. If so add a classname 'active'
	 * to that audience selector
	 */
	 $j('.audienceSwitcher').click( function() {
		 $j(this).toggleClass('active');
	 });
	 /* FAQSearch
	  * If the faqsearch searchword input field is selected, it should get the class active directly
	  */
	 $j('.faqSearch input[type=text],.faqSearch input[type=submit]').focus( function() {
		 $j(this).parents('.faqSearch').addClass('selected');
	 }).blur( function() {

                /* Remove the selected class only if the search is not performed*/
                if(!$j(this).parents('.faqSearch').hasClass('searchDone')) {
                    $j(this).parents('.faqSearch').removeClass('selected');
                }		 
	 });

	
    /*******
	** mb - carrousel **
	*******/
	
    if ( $j('.scrollcarrousel').length > 0 ) {
		
        $j('.carrousel').addClass('jcarrousel');

        // to fix the problem that all images are loaded before the carousel is initialized
        // a class hidden is added to all rows except the first one. Remove the hidden classes here.
        $j('.carrouselpanes .item.hidden').removeClass('hidden');
        $j('.carrouseltabs').removeClass('hidden');
		
        var jqpanes = $j('.carrouselpanes')
        .scrollable({
            clickable: false,
            keyboard: false,
            size: 4
        })
        .circular()
        .autoscroll({
            interval: 5000
        })
        .navigator({
            navi: '.carrouseltabs',
            naviItem: 'a',
            activeClass: 'current',
            api: true
        });

        /*$j('ul.carrouseltabs a').click(function() {
			jqpanes.pause();
		});*/		
        $j('.carrouselpanes .item').hover(function() {
            jqpanes.pause();
        });
		
        if ( $j(this).find('.descriptionText p').length > 0 || ( $j(this).find('.descriptionText .metainfo').length > 0 ) ) {
				
            $j('.carrouselpanes .itemText').mouseenter(function() {
                $j(this).find('h2.fullName a').addClass('over').parent().removeClass('hidden');
                $j(this).find('h2.shortName').addClass('hidden');

                var titleHeight = $j(this).find('h2.shortName').height();
                var shortName = $j(this).find('h2.shortName a').html();
				
                var maxBodyHeight = 70;
                if(shortName && shortName.indexOf('...') > -1){
                    titleHeight = titleHeight * 2;
                    maxBodyHeight = 60;
                }
                var textHeight = $j(this).find('.descriptionText p').height();
                var sectorHeight = $j(this).find('.browseSector').height();
                var localName = $j(this).find('.geocode').height();
				
                var bodyHeight = textHeight + sectorHeight + localName;
                if(bodyHeight > maxBodyHeight){
                    bodyHeight = maxBodyHeight;
                }
				
                $j(this).find('.descriptionText').animate({
                    height:bodyHeight+'px'
                }, 300, function() {
                    // Animation complete.
                    });
                $j(this).find('h2.fullName').animate({
                    maxHeight: titleHeight+'px'
                }, 300, function() {
                    // Animation complete.
                    });
            });
            $j('.carrouselpanes .item').mouseleave(function() {
	
                $j(this).find('h2.fullName a').removeClass('over').parent().addClass('hidden');
                $j(this).find('h2.shortName').removeClass('hidden');

                $j(this).find('.descriptionText').animate({
                    height:'0px'
                }, 300, function() {
                    // Animation complete.
                    });
                $j(this).find('h2.shortName').animate({
                    maxHeight:'17px'
                }, 300, function() {
                    // Animation complete.
                    });
            });
        }
		
    }
	
    if ( $j('.mgggSearch .carrousel').length > 0 ) {
        $j('.carrousel').parent().parent('.mgggProjectView').addClass('mgggProjectViewAcc');
    }

	/*******
	** mb - Tabs maxwidth - add submenu **
	*******/
	
	if ( $j('.tabs').length > 0 ) {	
		var heightTabs = $j('.tabs').height();		
		
		if ( heightTabs > 40 ) {								
			var positionFirstTab = $j('.tabs ul li:first').position().top;				
			$j('.tabs ul li').each(function() {
				var positionTab = $j(this).position().top;	
				if ( positionTab > positionFirstTab ) {
					$j(this).addClass('plusItem');									
				}				
			});
						
			$j(document.createElement('li')).attr('class','plus').appendTo('.tabs ul');				
			var moreText = typeof translate === 'function' ? translate("resourcebundle", "tabs.more.label") : 'More...';
			$j('.tabs ul li.plus').append( $('<a href="/"><span>'+moreText+'</span></a>'));
			$j(document.createElement('ul')).appendTo('.tabs ul li.plus');				
			$j('.tabs ul li.plus ul').append( $('.tabs ul li.plusItem'));
			
			var positionPlusTab = $j('.tabs ul li.plus').position().top;
			if ( positionPlusTab > positionFirstTab ) {
				$j('.tabs ul li.plus').prev('li').addClass('plusItem');	
				$j('.tabs ul li.plus ul').append( $('.tabs ul li.plusItem'));							
			}	
		}			
	}
	
    /*******
	** Tablesortercall - 0.0.1 **
	*******/

    /*******
	** vp - first table link click - (c) - 0.0.1 **
	*******/
    if ( $j('table tbody th').length > 0 ) {
        $j('table tbody th').click(function(event){
            var el = $j(this).find(":nth-child(1)");
            if (el.attr('href') ) {
                document.location = el.attr('href');
            }
			
        });
	
    }
		
    /*******
	** mb - Toggler filteroptions **
	*******/
	
    if ( $j('.filterOptionsToggler').length > 0 ) {
        if ( !$j('.filterOptions').hasClass('selected')){
            $j('.filterOptions').hide();
        }
        else {
            $j('.filterOptionsToggler').addClass('less');
        }
        if ( !$j('.filterOptions1').hasClass('selected')){
            $j('.filterOptions1').hide();
            $j('.filterOptionsToggler1').removeClass('less');
        }
        $j('.filterOptionsToggler').click(function(event){
			
            if ( $j(this).hasClass('less') ) {
                $j(this).next('.filterOptions').animate({
                    height: 'toggle'
                }, 500, function() {
                    // Animation complete.
                    });
                $j(this).removeClass('less');
            }
            else {
                $j(this).next('.filterOptions').animate({
                    height: 'toggle'

                }, 500, function() {
                    // Animation complete.
                    //Fix for IE7
                    $j('.browseList li').addClass('position');
                });
                $j(this).addClass('less');
            }
            event.preventDefault();
        }); 
    }
	
    /*******
	** mb - Toggler browseoptions list **
	*******/
	
    if ( $j('.moreOptionsLink').length > 0 ) {
        if ( !$j('.moreOptions').hasClass('selected')){
        /*$j('.moreOptions').hide();			*/
        }
        else {
            $j('.moreOptionsLink').addClass('lessOptionsLink');
        }
        $j('.moreOptionsLink').click(function(event){
            $j(this).next('.moreOptions').addClass('positionOptions');
            if ( $j(this).hasClass('lessOptionsLink') ) {
                $j(this).next('.moreOptions').animate({
                    height: 'toggle'
                }, 300, function() {
                    // Animation complete.
                    });
                $j(this).removeClass('lessOptionsLink');
            }
            else {
                // BL: could not it working with height toggle in ie8 so therefore the calculation.
                var children = $j(this).next('.moreOptions').children('ul:first').children().length;
                var height = $j(this).outerHeight();
                $j(this).siblings('.moreOptions').animate({
                    height: ((children+1)*height)+'px'
                //height: 'toggle'
                }, 300, function() {
                    // Animation complete.
                    });
                $j(this).addClass('lessOptionsLink');
            }
            event.preventDefault();
        });
		
    }
	
    /*******
	** mb - Toggler How to search **
	*******/
	
    if ( $j('.howToSearchLink').length > 0 ) {
        $j('.itemInfo').hide();
		
        $j('.howToSearchLink').click(function(event){
            $j(this).next('.itemInfo').addClass('itemInfo1');
            $j(this).next('.itemInfo').animate({
                height: 'toggle'
            }, 300, function() {
                // Animation complete.
                });
            event.preventDefault();
        });
		
        $j('.closeLink').click(function(event){
            $j('.itemInfo').animate({
                height: 'hide'
            }, 300, function() {
                // Animation complete.
                });
            event.preventDefault();
        });
    }

 	/*******
	** mb - add email friend **
	*******/
	
    if ( $j('.addFriend').length ) {
	    $j('.extraEmail').hide();
        $j('.addFriend').click(function(event){
			if( $('.extraEmail:eq(0)').is(':visible') ) {
				$j('.extraEmail:eq(1)').slideDown(200);	
				$j(this).hide();					
			}
			else {
				if( $('.extraEmail').find(':visible').length > 2 ) {
					$j(this).hide(200);					
				}				
				$j('.extraEmail:eq(0)').slideDown(200);	
			}        
			event.preventDefault();
        });
		 $j('.form .closeLink').click(function(event){		
			$j(this).parent().hide(200);
		 	$(this).prev('input').attr("value","");
			$j('.addFriend').slideDown(200);
			event.preventDefault();
        });
    }
	
    /*******
	** vp - Accordion - (c) - 0.0.2 **
	*******/
    if ( $j('.accordion .item').length > 0 ) {
        $j('.accordion .item h2.accordionHeader').each(function() {
            $j(this).find('a').click(function(event){
                if ( $j(this).hasClass('selected') ) {
                    $j(this).removeClass('selected');
                    $j(this).parent().next().animate({
                        height: 'toggle'
                    }, 500, function() {
                        // Animation complete.
                        });
                }
                else {
                    // close all
                    $j(this).parent().parent().parent().children().each(function(){
                        if ( $j(this).find('h2.accordionHeader a').hasClass('selected')) {
                            $j(this).find('h2.accordionHeader a').removeClass('selected');
                            $j(this).find('.edgedContainer').animate({
                                height: 'toggle'
                            }, 500, function() {
                                // Animation complete.
                                $j(this).parent().removeClass('selected');
                            });
                        }
                    });
					
                    $j(this).addClass('selected');
                    $j(this).parent().next().animate({
                        height: 'toggle'
                    }, 500, function() {
                        // Animation complete.
                        });
                }
                event.preventDefault();
            });
        });
    }

    /*******
	** vp - Facts and Figures - (c) - 0.0.2 **
	*******/
    if ( $j('.factsAndFigures .item').length > 0 ) {
        $j('.factsAndFigures .item').each(function(i) {
            if ( $j(this).attr('class').indexOf('pos') != -1 ) {
                var orgClass = $j(this).attr('class');
                var firstPos = orgClass.slice(orgClass.indexOf('pos'));
                var cleanPosClass;
                if (firstPos.indexOf('selected') != -1 ){
                    cleanPosClass = firstPos.substr(0, firstPos.indexOf('selected') -1);
                }
                else {
                    cleanPosClass = firstPos;
                }
                var posClass = cleanPosClass.substr(3, cleanPosClass.length);
                var posLeft = posClass.substr(0, posClass.indexOf('x'));
                var posTop = posClass.substr(posClass.indexOf('x') + 1, posClass.length);
                $j(this).css({
                    left: posLeft + 'px',
                    top: posTop + 'px'
                });
            }
            $j(this).find('a').click(function(event){
                if ( $j(this).parent().hasClass('selected') ) {
                    $j(this).parent().find('.description').animate({
                        height: 'toggle'
                    }, 250, function() {
                        // Animation complete.
                        $j(this).parent().removeClass('selected');
                    });
                }
                else {
                    // close all
                    $j(this).parent().parent().children().each(function(){
                        if ( $j(this).hasClass('selected')) {
                            $j(this).find('.description').animate({
                                height: 'toggle'
                            }, 250, function() {
                                // Animation complete.
                                $j(this).parent().removeClass('selected');
                            });
                        }
                    });
					
                    $j(this).parent().addClass('selected');
                    //$j(this).parent().find('.description').fadeIn();
                    $j(this).parent().find('.description').animate({
                        height: 'toggle'
                    }, 250, function() {
                        // Animation complete.
                        });
                }
                event.preventDefault();
            });
        });
    }

    // if the rates slider exist on the page set the timeouts so it rotates.
    if($j('.rates ul li').length > 0){
        $j('.rates ul li').each(function(){
            $j(this).addClass('notvisible');
        });
        setTimeout('rateSlider()', 7000);
    }


    /*******
	** vp - search box text swapper - (c) - 0.0.1 **
	*******/
    $j('#querytext').focus(function() {
        var title = $j(this).attr('title');
        if ( $j(this).val() == title ) {
            $j(this).val('');
        }
    });
    $j('#querytext').blur(function() {
        var title = $j(this).attr('title');
        if ( $j(this).val() == '' ) {
            $j(this).val(title);
        }
    });


    /*******
	** vp - Second level navigation - (c) - 0.0.1 **
	*******/
    $j(".secondLevel ul li:has(ul)").addClass('hashChilds');
    $j(".secondLevel ul.nav li:nth-child(7)").addClass('seven');

    $j(".secondLevel ul li.hashChilds").hover(function(){
    
        $j(this).addClass("hover");
        $j('ul:first',this).css('display', 'block');
    
    }, function(){
    
        $j(this).removeClass("hover");
        $j('ul:first',this).css('display', 'none');
    
    });
	

    /*******
	** vp - hoverImage - (c) - 0.0.1 **
	*******/
    $j('.hoverImage').hover(function (e) {
        this.src = this.src.replace("-normal","-hover");
    },
    function() {
        this.src = this.src.replace("-hover","-normal");
    });


    /*******
	** vp - Brandbox - (c) - 0.0.1 **
	*******/
    if ( $j('.brandBox').length > 0 && jQuery().cycle ) {
        $j('#navBrandBox').empty();
        $j('.brandBoxWrapper')
        .cycle({
            fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
            timeout: 10000,
            speed: 1000,
            fastOnEvent: 300,
            pause: false,
            pauseOnPagerHover: true,
            pagerEvent:	  'mouseover',
            allowPagerClickBubble: true,
            pager: $j('#navBrandBox'),
            pagerAnchorBuilder: function(index, DOMelement) {
                var href = $j(DOMelement).find(".bbLink").eq(0).attr("href");
                var linkText = $j(DOMelement).find(".linkText").eq(0).text();
                var retValue = "";
                if (index == 0) {
                    retValue = '<li class="first-child">';
                }
                else if (index == 5) {
                    retValue = '<li class="last-child">';
                }
                else {
                    retValue = '<li>';
                }
                if(href === "" || href === undefined){
                    retValue += '<span>' + linkText + '</span>';
                }
                else {
                    retValue += '<a href="' + href +'">' + linkText + '</a>';
                }
                retValue += "</li>";
                return retValue;
            }
        });
        
        $j('.pause').click(function(event) {
            brandboxChangeState('pause');
            event.preventDefault();
        });

        $j('.resume').click(function(event) {
            brandboxChangeState('resume');
            event.preventDefault();
        });
    }
	
});

function brandboxChangeState(state){
    if(state === 'resume'){
        $j('.brandBoxWrapper').cycle('resume');
	$j('.resume').addClass('hidden');
	$j('.pause').removeClass('hidden');
    }
    else if(state === 'pause'){
        $j('.brandBoxWrapper').cycle('pause');
        $j('.pause').addClass('hidden');
        $j('.resume').removeClass('hidden');
    }
}

function rateSlider(){
    if($j('.rates ul li.selected').next('li').length === 0){
        $j('.rates ul li.selected').removeClass('selected').siblings(':first').addClass('selected');
    }
    else {
        $j('.rates ul li.selected').removeClass('selected').next('li').addClass('selected');
    }
    setTimeout('rateSlider()', 7000);
}

/*******
** mb - keep imagemap visible when scrolling **
*******/
function sticky_relocate() {
    if($j('.sticky-top').length > 0 && $j('.pagingBttm').length > 0) {
        var window_top = $j(window).scrollTop();
        var map_top = $j('.sticky-top').offset().top;
        var paging_top = $j('.pagingBttm').offset().top;
        var paging_bottom = paging_top - 100;
        var map_bottom = $j('.sticky-bottom').offset().top;
		
        if (map_bottom > paging_bottom) {
            $j('#mgggMap').removeClass('stick');
            $j('#mgggMap').addClass('stickBottom');
        }
        else {
            $j('#mgggMap').removeClass('stickBottom');
        }
        if (window_top > map_top) {
            $j('#mgggMap').addClass('stick');
        }
        else {
            $j('#mgggMap').removeClass('stick');
            $j('#mgggMap').removeClass('stickBottom');
        }
    }
}

/*******
** mb - tooltip **
*******/
function toolTip() {
    function showTooltip() {
        $j(this).find('.itemInfo').slideToggle(50, function() {
            // Animation complete.
            });
    }
    if($j('.tooltip').length > 0) {
        $j('.tooltip').hoverIntent({
            interval: 10, // milliseconds delay before onMouseOver
            over: showTooltip,
            sensitivity: 4,
            timeout: 10, // milliseconds delay before onMouseOut
            out: showTooltip
        });
    }
}

$j(function() {
    $j(window).scroll(sticky_relocate);
    sticky_relocate();
    toolTip();
});
