(function($){
    function uipopup(){
        $('.screenhome, .screenevent').click(function(e) {
            e.preventDefault();
            var id = '.window';
            if($(this).hasClass('screenhome')){
                $('body').append("<div class='window clearfix'>"
                    +"<span class='close'><a href=''>close</a>PROJECT NINETY MESSAGE</span>"
                    +"<div>"+$(this).parent().parent().children('.fulltext').html()+"</div></div>"
                    +"<div id='mask'></div>"
                );
            }else{
                $('body').append("<div class='window clearfix' style='width: 800px;'><span class='close'><a href=''>close</a>Project Ninety Event Gallery</span>"
                    +"<div id='slider-event'></div>"
                    +"<div class='picdesc'></div>"
                    +"</div>"
                    +"<div id='mask'></div>"
                );

                $(this).next('ul').clone().appendTo($('#slider-event')).css('display','block');

                $('#slider-event').easySlider({
                    continuous: true
                });

                $('#nextBtn').appendTo('.picdesc');
                $('#prevBtn').appendTo('.picdesc');
                $('#nextBtn, #prevBtn').css({
                    'float':'right',
                    'margin':'0 0 0 5px'
                });
            }

            //Get the screen height and width
            var maskHeight = $(document).height();
            var maskWidth = $(window).width();

            //Set heigth and width to mask to fill up the whole screen
            $('#mask').css({'width':maskWidth,'height':maskHeight});

            //transition effect
            $('#mask').fadeIn(1000);
            $('#mask').fadeTo("slow",0.8);

            //Get the window height and width
            var winH = $(window).height();
            var winW = $(window).width();

            //Set the popup window to center
            //$(id).css('top',  winH/2-$(id).height()/2);
            //$(id).css('left', winW/2-$(id).width()/2);
            centerWin();

            //transition effect
            $(id).fadeIn(2000);

            uiclosepopup();
        });

        function uiclosepopup(){
            //if close button is clicked
            $('.window .close a').click(function(e) {
                //Cancel the link behavior
                e.preventDefault();
                $('#mask').remove();
                $('.window').remove();
            });
            //if mask is clicked
            $('#mask').click(function() {
                $(this).remove();
                $('.window').remove();
            });
        }

        //centering popup
        function getScrollXY() {
            var scrOfX = 0, scrOfY = 0;
            if( typeof( window.pageYOffset ) == 'number' ) {
                //Netscape compliant
                scrOfY = window.pageYOffset;
                scrOfX = window.pageXOffset;
            } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
                //DOM compliant
                scrOfY = document.body.scrollTop;
                scrOfX = document.body.scrollLeft;
            } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
                //IE6 standards compliant mode
                scrOfY = document.documentElement.scrollTop;
                scrOfX = document.documentElement.scrollLeft;
            }
            return {X:scrOfX, Y:scrOfY};
        }

        function getWindowSize() {
            var myWidth = 0, myHeight = 0;
            if( typeof( window.innerWidth ) == 'number' ) {
                //Non-IE
                myWidth = window.innerWidth;
                myHeight = window.innerHeight;
            } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
                //IE 6+ in 'standards compliant mode'
                myWidth = document.documentElement.clientWidth;
                myHeight = document.documentElement.clientHeight;
            }
            return{X:myWidth, Y:myHeight}
        }

        //centering popup
        function centerWin(){
            //request data for centering
            var windowDim = getWindowSize();
            var popupHeight = $(".window").height();
            var popupWidth = $(".window").width();
            var scroll = getScrollXY();
            //centering
            $(".window").css({
            "position": "absolute",
            "top": windowDim.Y/2-popupHeight/2 + scroll.Y-15,
            "left": windowDim.X/2-popupWidth/2 + scroll.X-15
            });
            //only need force for IE6
            //window.alert("\npopup.Height="+popupHeight+"\nInner Height="+windowDim.Y);
        }

    }

    function uisameheight(){
        $('.home-news').height($('.home-text').height());
    }

    function uitabs(tabshown) {
        $('.tab-panel').hide();
        $('.tabs .'+tabshown).addClass('active');
        $('.text .tab-panel')
            .eq($('.tabs a').index($('.'+tabshown)))
            .show();

        $('.tabs a').click(function(e) {
            e.preventDefault();
            $('.tabs a').removeClass('active');
            $('.tab-panel').hide();
            $(this).addClass('active');
            $('.text .tab-panel')
                .eq($('.tabs a').index(this))
                .show();
        });
    }

    function uiceotabs() {
        $('.hpic, .htext').hide();
        $('.hnav a:first').addClass('active');
        $('#ceo-a > img:first, #ceo-a > div:first').show();

        $('.hnav a').click(function(e) {
            e.preventDefault();
            if($(this).hasClass('active') == false){
                $('.hnav a').removeClass('active');
                $(this).addClass('active');
                $('.hpic, .htext').hide();
                $('.hpic')
                    .eq($('.hnav a').index(this))
                    .fadeIn();
                $('.htext')
                    .eq($('.hnav a').index(this))
                    .fadeIn();
            }
        });

        var min = 0;
        var max = 7;
        var maxelem = 8;
        var ctr;
        var limit = false;

        $('.hnav a').hide();
        if($('.hnav a').size() > maxelem){
            $('.hnav a:first').addClass('more-than');
        }else{
            $('.hnav span').remove();
        }
        
        for(ctr=min;ctr<=max;ctr++){
            $('.hnav a').eq(ctr).show();
        }

        $('.hnav span').click(function() {

            if($(this).hasClass('next')){
                if(max < $('.hnav a').size()-1){
                    $('.hnav a').removeClass('more-than');
                    $('.hnav a').eq(min).hide();
                    min++;max++;
                    $('.hnav a').eq(min).addClass('more-than');
                    $('.hnav a').eq(max).show();
                    if(max == $('.hnav a').size()-1){
                        limit = true;
                    }else{
                        limit = false;
                    }
                }else{
                    limit = true;
                }
            }else{
                if(min != 0){
                    $('.hnav a').removeClass('more-than');
                    $('.hnav a').eq(max).hide();
                    min--;max--;
                    $('.hnav a').eq(min).addClass('more-than');
                    $('.hnav a').eq(min).show();
                    if(min == 0){
                        limit = true;
                    }else{
                        limit = false;
                    }
                }else{
                    limit = true;
                }
            }

            if(limit) {
                $(this).addClass('limit');
            }else{
                $('.hnav span').removeClass('limit');
            }
        });
    }

    function uicontact() {
        $('.contact #Name, .contact #Email, .contact #Subject, .contact #Message')
            .focus(function(){
                if(!$(this).hasClass('focused')){
                    $(this).val('');
                }
                $(this).addClass('focused');
            })
            .focusout(function(){
                if($(this).val() == ''){
                    $(this).val($(this).attr('data-value')).removeClass('focused');
                }
            });
    }

    // donate
    function donate(){
        $("a[class~=dNow]").click(function(event){
            $("#donateNow").submit();
            event.preventDefault();
        });
    }

    function flash(){
        $('#ceo-b div').hide();
        $('#ceo-b img').click(function(){
            $(this).fadeOut(1000);
            $('#ceo-b div').show();
            $('#ceo-b object').show();
            $('#ceo-b div').delay(143000).hide(0);

            $('#ceo-b img').fadeIn(1000);
        });

        $('#ceo-b div, #ceo-b object').click(function(){
            $(this).hide();
            $('#ceo-b img').fadeIn(1000);
        });
    }

    $(function() {
        // donate
        donate();
        uipopup();
        uisameheight();
        if($('.tabs').length > 0){
            uitabs(tabshown);
        }
        uiceotabs();
        uicontact();
        flash();


        // Temporary function
        /*
        setInterval(
            function slideSwitch() {
                var $active = $('#ceo-b img.active');

                if ( $active.length == 0 ) $active = $('#ceo-b img:last');

                var $next =  $active.next().length ? $active.next()
                    : $('#ceo-b img:first');

                $active.addClass('last-active');

                $next.css({opacity: 0.0})
                    .addClass('active')
                    .animate({opacity: 1.0}, 1000, function() {
                        $active.removeClass('active last-active');
                });
            },
        5000 );
        */
        // END Temporary function

    });
})(jQuery);