if($('#map').length) {
    google.load("maps", "2.x", {"callback" : mapsLoaded});
}

$(document).ready(function () {
    $('.NewsSummary').hover(function() {
        $(this).css('background-color','#530804');
        $(this).find('a').css('color','#FDB814');
        $(this).find('.NewsSummaryCategory a').css('color','#FFF')
    },function() {
        $(this).css('background-color','transparent');
        $(this).find('a').css('color','#FFF');
    });

    if($('#timeline').length) {
        $('.event-content').hide();
        $('.event-content').css('visibility','visible');
        $('.event-content').css('height','auto');

      	$('.event-header').click(function() {
      	    $(this).parent().siblings('li').removeClass('active');
      		$(this).next('div.event-content').slideToggle('fast').parent().siblings('li.event').children('div.event-content').slideUp('fast');

      		if($(this).parent().hasClass('active')) {
      			$(this).parent().removeClass('active');
      		} else {
      			$(this).parent().addClass('active');
      		}
      	});

      	$('.event-header').hover(function() {
      		// $(this).css('color','#FF0000');
      		$(this).css('background-color','#2B0000');
      	},function() {
      		$(this).css('color','#FDB814');
      		$(this).css('background-color','#3B0000');
      	});
    }

    /*
    if($('#timeline-years').length) {
        $('#timeline-years a').click(function() {
            var yearClass = '.year-' + $(this).text();
            $.scrollTo(yearClass,500);
            return false;
        });
    }
    */

    if($('#services-wrapper').length) {

    	$('.service-content').hide();
        $('.service-content').css({visibility:'visible',height:'auto'});
    	$('.service-header').click(function() {
    		$(this).parent().siblings('li').removeClass('active');

            $(this).next('div.service-content').slideToggle('fast').parent().siblings('li.service-item').children('div.service-content').slideUp('fast');
    		if($(this).parent().hasClass('active')) {
    			$(this).parent().removeClass('active');
    		} else {
    			$(this).parent().addClass('active');
    		}
    	});

    	$('.service-header').hover(function() {
    		// $(this).css('color','#FF0000');
    		$(this).css('background-color','#222');
    	},function() {
    		$(this).css('color','#FFF');
    		$(this).css('background-color','#000');
    	});

        /*
        // second level accordion

        $('#services-main .service-subhead').click(function() {
    	    $(this).parent().siblings('li').removeClass('active');
    		$(this).next('div.service-content').slideToggle('fast').parent().siblings('li.service-item').children('div.service-content').slideUp('fast');

    		if($(this).parent().hasClass('active')) {
    			$(this).parent().removeClass('active');
    		} else {
    			$(this).parent().addClass('active');
    		}
    	});

        // end second level accordion functionality
        */

        $('#e2e-start div.service-header').click(function() {
            $(this).siblings('.service-content').css('visibility','visible');
            $(this).siblings('.service-content').slideToggle('fast');
        });
        $('#e2e-end div.service-header').click(function() {
            $(this).siblings('.service-content').css('visibility','visible');
            $(this).siblings('.service-content').slideToggle('fast');
        });

    }

    /*
        show scroll to top link if content
        doesn't fit on pages that scroll
    */
    $(function() {
        $.fn.scrollToTop=function() {
            $(this).css("visibility","visible");
            $(this).hide().removeAttr("href");
            if($(window).scrollTop()!="0") {
                $(this).fadeIn("fast")
            }
            var scrollDiv=$(this);
            $(window).scroll(function(){
                if($(window).scrollTop()=="0") {
                    $(scrollDiv).fadeOut("fast")
                } else {
                    $(scrollDiv).fadeIn("fast")
                }
            });
            $(this).click(function() {
                $("html, body").animate({scrollTop:0},"fast")
            })
        }
    });

    $(function() {
        $("#toTop").scrollToTop();
    });

});

function mapsLoaded() {
    var map = new GMap2(document.getElementById("map"));
    map.setCenter(new GLatLng(-27.683528083787756, 134.6484375), 4);
    map.setUIToDefault();
    var markers = [];

    // Add Controls
    var c = new GMapTypeControl();
    map.addControl(c);
    //map.setMapType(G_NORMAL_MAP);  // sets default map to normal (G_SATELLITE, G_HYBRID)

    var geocoder = new GClientGeocoder();
    $('#addressList a').each(function(){

        $(this).click(function() {
            return false;
        });
        var addressTitle = $('.locationTitle',this).text();
        var address = $('.address',this).text();

        var _this = this;
        setTimeout(function() {
            geocoder.getLatLng(address, function(point) {
                if (point) {
                    var marker = new GMarker(point,'<span style="width:200px;background-color:blue">test</span>');
                    map.addOverlay(marker);

                    $(_this).click(function(e)  {
                        if (map.getZoom() <= 4) map.setZoom(15);
                        map.panTo(point);
                        marker.openInfoWindowHtml('<div class="info-window-copy"><span class="info-title">' + addressTitle + '</span>' + '<br />' + '<span class="info-address">' + address + '</span></div>');
                    });
                }
            });
        }, 500);
    });
}

$(function(){
    // $('.tip').removeAttr("title");
    // also remove alt attribute to prevent default tooltip in IE
    $('.tip img').attr('alt', '');

    $('.tip').tooltip({
        track: true,
        delay: 0,
        showURL: false,
        showBody: " - ",
        fade: 250
    });
    $('.tip').hover(function() {
        $(this).stop().fadeTo(250, 0.5);
    },function() {
        $(this).stop().fadeTo(250, 1);
    });
});