$(document).ready(function(){
    
    // Disable caching of ajax requests
    $.ajaxSetup ({  
        cache: false
    });
    
    /*
        Clearing of default values in input fields of forms on focus
    */
    var input = document.getElementsByTagName('input');
    for (var a=input.length-1; a >= 0; a--) {
        if (input[a].type != 'text') { continue; } // skips non-text inputs 
        input[a].mask = input[a].value; 
        input[a].onfocus = function() {
            if (this.defaultValue==this.value) this.value = ""
        };
        input[a].onblur = function() {
            if (this.value == '') { this.value = this.mask; }
        };
    }

    
	    
    
    // Javascript to ajaxify latest blog posts on frontpage
    $("#frontpage #latest_blog_posts .nav li a").click(function (e) {
    
        // Get post id from the anchors rel attribute
        id = $(this).attr("rel");
        // Do jquery ajax load to retrieve the new blog post and display it
        // in the div with classname post
        $("#frontpage #latest_blog_posts .post").load("/internal/ajax/get/post/", { post_id: id });
        // Update navigation link with selected item
        $(this).parent().siblings().removeClass('selected');
        $(this).parent().addClass('selected');
        
        // Disable default anchor action
        e.preventDefault();
        return false;
    });
    
    
    
    // Javascript to ajaxify projects on frontpage
    $("#frontpage #projects .nav li > a").click(function (e) {
        
        // Do something to change view area
        id = $(this).attr("rel");   // Get project id from the rel attribute
        $("#frontpage #projects .project").load("/internal/ajax/get/project/", { project_id: id });     // Do jquery ajax load and replace
        
        // Update navigation link with selected item
        var speed = 425;
        var listElement = $(this).parent();     // Get the list element that is clicked
        $("span", listElement.siblings()).slideUp(speed);      // Hide all open ones : could this be done more effective?
        $("span", listElement).slideDown(speed);       // Open up the clicked item
        $("span", listElement).css({'display':'block'});        // Set display type to block, this fixes a flickering bug

        $(this).parent().siblings().removeClass('selected');    // Remove css class
        $(this).parent().addClass('selected');      // Set css class
        
        // Disable default anchor action
        e.preventDefault();
        return false;
        
    });
    
    
    // Disable links
    $("#attachment-viewer .attachments li a").click(function (e){        
        e.preventDefault();
        return false;
    });
    

    // Javascript to ajaxify project viewer
    $("#attachment-viewer .attachments").easySlider({
		auto: false,
		continuous: true,
		numeric: true,
		controlsShow : false,
		speed: 350
	});
	
	
	
	
	
    /* ADD NAVIGATION BUTTONS TO THE ATTACHMENT VIEWER NAVIGATION */
    var navContainer = $("#attachment-viewer .nav");
    var listHolder = $("ul", navContainer);
    if (listHolder.children().length > 7 ) {
    
        var navLeft = $("<p><a id='navLeft' href='#'>Venstre</a></p>");
        var navRight = $("<p><a id='navRight' href='#'>Høyre</a></p>");
        var pos = 0;
        var step = 81;
        var visibleSteps = 7;
        
        navLeft.click(function (e) {
            if (pos < 0) {

                var travel = pos + step;
                pos = travel;
                listHolder.animate({'marginLeft': travel});
            }
            
            e.preventDefault();
            return false;
        });
        navRight.click(function (e) {
        
            if (pos > (step*((listHolder.children().length)-visibleSteps))*-1 ) {
                var travel = pos - step;
                pos = travel;
                listHolder.animate({'marginLeft': travel});
            }
            e.preventDefault();
            return false;
        });
        
        navContainer.parent().append(navLeft);
        navContainer.parent().append(navRight);
    }
    

    /* ADD AJAX TO CATEGORY MENU */
    $("a", "#projects #content #categories ul.main").click(function (e) {
        var categoryMenu = $("#projects #content #categories .navwrapper");

        id = $(this).attr("rel");   // Get project id from the rel attribute
        $.post(
            "/internal/ajax/get/projects/",
            { category_id: id },
            function(data) {
                categoryMenu.append(data);
                categoryMenu.animate({'marginLeft': '-320px'});
            },
            "html"
        );
        

        // Swap title
        var header = $("h2", "#projects #content #categories .header");
        header.html('Velg prosjekt');

        e.preventDefault();
        return false;        
    });
    
    $("a", "#projects #content #categories .breadcrumb").click(function (e) {
        var categoryMenu = $("#projects #content #categories .navwrapper");
        
        categoryMenu.animate({'marginLeft': '0'}, function() {$('#projects #content #categories ul.sub').remove()});
        
        // Swap title
        var header = $("h2", "#projects #content #categories .header");
        header.html('Velg kategori');

        e.preventDefault();
        return false;        
    });
    


    // Embedd flash objects inside attachment-viewer
    $("#attachment-viewer .attachments a.flash").each(function() {
        var path = $(this).attr("href");
        $(this).flash(
            {
                swf: path,
                params: {
					wmode: 'opaque'
				},
                height: 345,
                width: 625
            }
        );
    });
    

    // If there is a link with class movie, use flowplayer to embedd video
    if ($('a.movie').length !== 0) {
        // If the video is inside attachment-viewer, embed with controls
        if ($('#attachment-viewer').length !== 0) {
            flowplayer("a.movie", {src: '/static/swf/flowplayer-3.2.4.swf', wmode: 'opaque'}, {
                wmode: 'transparent',
                clip: {
                    autoPlay: false,
                    autoBuffering: true,
                    scaling: 'fit'
                },
                plugins: {
                    controls: {
                        tooltipTextColor: '#ffffff',
                        buttonColor: '#6b6b6b',
                        backgroundColor: '#3b3b3b',
                        sliderGradient: 'none',
                        buttonOverColor: '#828282',
                        progressGradient: 'medium',
                        volumeSliderColor: '#000000',
                        tooltipColor: '#5F747C',
                        borderRadius: '5',
                        bufferGradient: 'none',
                        timeBgColor: '#555555',
                        progressColor: '#ffffff',
                        volumeSliderGradient: 'none',
                        backgroundGradient: 'none',
                        durationColor: '#adadad',
                        bufferColor: '#000000',
                        sliderColor: '#000000',
                        timeColor: '#ffffff',
                        height: 24,
                        opacity: 1.0
                    }
                }
            });
        }
        // Else embedd without controls
        else {
            flowplayer("a.movie", {src: '/static/swf/flowplayer-3.2.4.swf', wmode: 'opaque'}, {
                clip: {
                    autoPlay: false,
                    autoBuffering: true,
                    scaling: 'fit'
                },
                plugins: {
                    controls: null
                }
            });
        }
    }

});
