$(document).ready( function(){

// Cycle stuff

    // Enable cycle
    
    // Add a pager (Why here? Fixes overlapping on IEs)
    $("#happenings").prepend('<div id="cycle-handler"></div>');
    $("#zone-top").cycle({
        timeout : 7500,
        pager: '#cycle-handler'
    });
    // Calculate some new height values
    var shortest = 0;
    $("#zone-top .token").each( function(){
        var newHeight = $(this).find("img").height();
        if (newHeight > shortest) {
            shortest = newHeight+'px';
        }
    });    
    // Set the content area's height to same as the shortest image (so the fade will fit in nicely)
    $("#carousel").css({
        height: shortest
    });
    
// Search manipulation 

    $(".q").focus( function(){
        $("#search").css({'background-position':'left bottom'});
    })    
    .blur( function(){
        $("#search").css({'background-position':'left top'});
    });

    
// Menu manipulation                          
        
    $(".topmenu li.parent").hover( function(){
        $(this).addClass('hovering').find("ul:first").slideDown("fast");
    },
    function(){
        $(this).removeClass('hovering').find("ul:first").slideUp("fast");
    });     
    
    

// Event manipulation
    if ( !$("body").hasClass("editmode") ) {
    
        // Stylize the container 
        $("#happenings").css({
            'position': 'relative',
            'z-index': '60',
            'height': '260px',
            'margin': '-130px 0 0 0',
            'padding': '50px 0 0 40px',
            'background': 'url("/res/default/img/bgEvents.png") no-repeat center top transparent'            
        });
        $("#happenings .listlink").css({
            'position': 'absolute',
            'right': '60px',
            'bottom': '40px'        
        });          
        // Add the needed containers
        $("#happenings").append('<div id="events-navi"></div><div id="events-container"></div>');
        // Hide the "panel" in event mode
        $(".editmode #happenings").css({'background':'none', 'height':'auto'});
        // Move the content into the panel
        var headerRow = 0;
        $("#zone-ajankohtaista .token").each( function(){
            // Header
            var header = $(this).find("h2").text();
            // Add the header
            $("#events-navi").append('<a href="#" id="tab-'+headerRow+'">'+header+'</a>');
            // Corresponding content to the header
            var content = $(this).find("div:first").html();
            // Add the content
            $("#events-container").append('<div class="event-data-container" id="content-'+headerRow+'">'+content+'</div>');
            // Remove the old header from the content area
            $("#events-container h2:first").remove();
            // Remove the copied container
            $(this).remove();
            headerRow++;        
        });
        // Shape the content divs a bit
        $(".event-data-container").each( function(){
            $(this).find(".row:first").css({'padding-left':'0'});
        });    
        // Add function to the navigation links
        $("#events-navi a").click( function(e){
            e.preventDefault();
            // Get the id, so we know what content area to open on click
            var id = $(this).attr("id").split("-");
            id = id[1];
            // Handle the "active" classes
            $("#events-navi a").removeClass("active").find(".event-arrow").remove();
            $(this).addClass("active").append('<span class="event-arrow">&nbsp;></span>');
            // Hide the current visible content
            $("#events-container .event-data-container:visible").toggle();
            // Find the correct content and open it
            $('#events-container #content-'+id).toggle();
        });      
        // Active the first tab
        $("#events-navi a:first").click();
        // Check if the event/news has an image
        $("#events-container .row")
            .each( function(){
                var img = $(this).find("img").length;
                if (img) {
                    var imgHeight = ($(this).find("img").height())+'px';
                    $(this).find(".image-and-ingress img").after('<div class="event-image-fader"></div>');                    
                    $(this).addClass("ingress-with-an-image");
                    $(this).find(".ingressi").css({'height':imgHeight, 'bottom': 0});
                    $(this).find(".event-image-fader").css({'bottom': 0});                    
                }
            })
            .hover( 
                function(){
                    var img = $(this).find("img").length;
                    if (img) {                
                        var imgHeight = ($(this).find("img").height())+'px';
                        $(this).find("img").animate({'opacity':'0'}, 300);
                    }   
                    $(this).find("a").css({'text-decoration':'underline'});                 
                },
                function(){
                    var img = $(this).find("img").length;
                    if (img) {                
                        $(this).find("img").animate({'opacity':'1'}, 300);
                    }     
                    $(this).find("a").css({'text-decoration':'none'});  
            })
            .click( function(){
                window.location = $(this).find("a").attr("href");
            });
        
        
        
        
    } // End body class check
    
    
// Add a new link to the event navi
    $("#events-navi").append('<a href="/kuntainfo/tapahtumakalenteri/">Tapahtumakalenteri</a>');
        
    
// Contacts manipulation
    $(".member_data tr").hover( 
        function(){
            $(this).addClass("row-hovering");    
        },
        function(){
            $(this).removeClass("row-hovering");
    });
    
// News and events read view manipulation
    $("#token-25832 .julkaisu, #token-31140 .julkaisu, #token-28465 .julkaisu, #token-28469 .julkaisu").remove();
    $("#token-25832 .liitteet h3, #token-31140 .liitteet h3, #token-28465 .liitteet h3, #token-28469 .liitteet h3").html('Kuulutus (PDF-muodossa)');
    
});
