$(document).ready(function () {
    initFlash();

    $('#gallery a').lightBox();

    initMenu();

    $('.slider .images').easySlider();

    if ($('#googlemap').length != 0) {
        initGoogleMaps();
    }
    if ($('#pagination_content').length != 0) {
        initPagination(5);
    }
    if ($('#language').length != 0) {
        $('#language > li').bind('mouseover', language_open);
        $('#language > li').bind('mouseout', language_close);
    }

    if ($('#organigram').length > 0) {
        $('#organigram .organigramSub').hide();

        $('#organigram .organigramTitle').click(function () {
            $(this).next('div').toggle(); //$(this).next('#organigram .organigramSub').toggle();
            if ($(this).children('img').attr('src').indexOf('-minus') > -1) {
                $(this).children('img').attr('src', $(this).children('img').attr('src').replace('-minus', '-plus'));
            }
            else {
                $(this).children('img').attr('src', $(this).children('img').attr('src').replace('-plus', '-minus'));
            }
        });
    }

//    //tijdelijke code
//    (function ($) {
//        $.fn.blink = function (options) {
//            var defaults = { delay: 1500, fade_in: 250, fade_out: 250 };
//            var options = $.extend(defaults, options);

//            return this.each(function () {
//                var obj = $(this);
//                setInterval(function () {
//                    $(obj).fadeOut(options.fade_out).fadeIn(options.fade_in);
//                }, options.delay);
//            });
//        }
//    } (jQuery))
//    $('#temptext').blink({ delay: 2500, fade_in: 500, fade_out: 500 });
});

function language_open() {
    $('#language ul').css('visibility', 'visible');
}
function language_close()
{
    $('#language ul').css('visibility', 'hidden');    
}

function initGoogleMaps() {
    //Settings
    var adress = 'Waregemstraat 200 8540 Deerlijk';
    var image = siteUrl + 'images/logo_small.png';
    var latlng = new google.maps.LatLng(50.8573695, 3.371787); // Used as back up if adress is not found
    var zoom = 14;
    
    //Code
    var myOptions = {
        zoom: zoom,
        center: latlng,
        disableDefaultUI: true,
        //MapType
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        //Navigation Control
        navigationControl: true,
        navigationControlOptions: {style: google.maps.NavigationControlStyle.ZOOM_PAN},
        //MapType Control
        mapTypeControl: true,
        mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}
    };
    
    var map = new google.maps.Map(document.getElementById('googlemap'), myOptions);

    var geocoder = new google.maps.Geocoder();
    if (geocoder) {
        geocoder.geocode({ 'address': adress }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                latlng = results[0].geometry.location;
            }
        });
        map.setCenter(latlng);
        var marker = new google.maps.Marker({
            map: map,
            position: latlng,
            icon: image
        });
    }
    
    $('#calculateroute').click(function() {
        var start = $('#routefrom').val();
       
        var end = adress;
        directionsDisplay = new google.maps.DirectionsRenderer();
        directionsDisplay.setMap(map);
        directionsDisplay.setPanel(document.getElementById('directions'));

        calculateRoute(start, end, directionsDisplay);

    });
}
function calculateRoute(start, end, directionsDisplay)
{

    var directionsService = new google.maps.DirectionsService();

    var request = {
        origin:start, 
        destination:end,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    directionsService.route(request, function(result, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(result);
        }
    });
}

function initFlash() {
    if ($('#noFlash').length > 0) {
        var flashvars = {};
        flashvars.siteUrl = siteUrl;
        flashvars.photoUrl = siteUrl + 'fotos.xml';
        flashvars.configUrl = siteUrl + 'config.xml';
        flashvars.sort = 0;
        var params = {};
        params.wmode = 'transparent';
        var attributes = {};
        swfobject.embedSWF('' + siteUrl + 'transitions.swf?t=' + new Date().getTime(), 'noFlash', '745', '178', '9.0.0', false, flashvars, params, attributes);
    }
}

function initMenu() {
    $('#menurealisations ul').hide();
    $('#menurealisations ul:first').show();
    $('#menurealisations li a').click(
    function() {
        var checkElement = $(this).next();
        if ((checkElement.is('ul')) && (checkElement.is(':visible'))) {
            return false;
        }
        if ((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
            $('#menurealisations ul:visible').slideUp('normal');
            checkElement.slideDown('normal');
            return false;
        }
    }
    );
}
