
var selectedArticle = ".homepage";
var selectedId = "";
var currentShownPhoto = 0;
var reinstated = false;
var wrapperLayoutShown = false;
var subpageLayoutShown = false;
var currentMenu = -1;
var photoRun = 0;
var menuContentHeight = 0;
var lastSource = 0;

var storedView;

$(document).ready(function()
{
    createMenuHandlers();
    createHomepageHandlers();

    setTimeout(showPhotos, 1000 );

    //Store the view
    storeCurrentView();

    //Website was called by link
    if ( m_articleId != 0 )
    {
        showSubpageByArticle( m_articleId, m_category, true );
        //xajax_showMenuPage( m_category );
    }
});

$.history.callback = function ( reinstate, cursor ) {
    
    if ( reinstate != null ) {
        reinstated = true;
        reinstateView( reinstate );
    } 
};

function reinstateStoreView() {
    reinstateView( storedView );
}

function reinstateView( reinstate ) {
        if ( reinstate.view == ".homepage" ) {
            highlightMenu(".homepage");
            showHomepage();
        } else if ( reinstate.view.substr(0,1) == "." ) {
            highlightMenu(reinstate.view );
            showSubpageByMenu( reinstate.view );
        } else if ( !IsNumeric( reinstate.view ) ) {
            highlightMenu(reinstate.menu );
            showSpecialArticle( reinstate.view, reinstate.attributes );
        } else {
            highlightMenu(reinstate.menu );
            showArticleById( reinstate.view, true );
        }
}

function storeCurrentView( attributes ) {
    if ( !reinstated ) {
        if ( attributes == null ) $.history( {view:selectedArticle,menu:selectedMenuItem} );
        else $.history( {view:selectedArticle,attributes:attributes,menu:selectedMenuItem});
    }
    reinstated = false;
}

function showPhotos() {
    
    if ( currentShownPhoto == 6 ) {
        $('#photoalbum-link').fadeIn('slow');
        $('#photoalbum-link').click(function() {
            showSpecialArticle('photo-album-list');
        });
        return;
    }

    $('#photo'+currentShownPhoto).fadeIn('slow');
    $("a#photo-qlink-"+currentShownPhoto).fancybox({
                                    'zoomOpacity'			: true,
                                    'overlayShow'			: false,
                                    'zoomSpeedIn'			: 500,
                                    'zoomSpeedOut'			: 500
                                });

    currentShownPhoto++;
    setTimeout(showPhotos, 100 );
}

function createHomepageHandlers() {
    // Events (fade in)
    //$('.event-item').mouseover( onEventMouseOver );

    // Events (fade out)
   // $('.event-item').mouseout( onEventMouseOut );

    // Events (click)
    //$('.event-item').click(function() {
        //handleMenuItemClick('.homepage' );
    //});
}

//Event handler functions
function onEventMouseOver( event ) {
    $('#event-text-'+ this.id).addClass('event-text-highlight');
    $('#event-title-'+ this.id).addClass('event-title-highlight');
    this.addClass('event-item-hightlight');
}

function onEventMouseOut( event ) {

    $('#event-text-'+ this.id).removeClass('event-text-highlight');
    $('#event-title-'+ this.id).removeClass('event-title-highlight');
}

function onArticleMouseHover( control, task ) {
    var id = '#as' + control.substring(2,3);
    
    //If this control matches the current article do nothing
    var articleIdAttribute = $("#"+control)[0].attributes.getNamedItem("aid").value;
    if ( articleIdAttribute == selectedArticle )
        return;

    //If the browser is IE don't fade
    jQuery.each(jQuery.browser, function(i, val) {
        if(i=="msie") {
            if ( task == 'fadeIn') {
                $(id).css("display","block");
            } else {
                $(id).css("display","none");
            }
            return;
        }
    });

    if ( !isFading(id) ) {
        startFading(id);
        
        if ( task == 'fadeIn') {
            $(id).fadeIn(fadeSpeed, function(){
                                      fadingComplete(id);
                                    });
        } else
            $(id ).fadeOut(fadeSpeed, function(){
                                      fadingComplete(id);
                                    });
    } else {
        sheduleTask(id, task );
    }
}

function showHomepage() {
    subpageLayoutShown = false;
    //Store the view
    selectedArticle = ".homepage";
    storeCurrentView();

    $("#body-filler").animate( { height:"187px" }, 500, null, function() {
                                            $("#homepage-body").fadeIn('normal', function () {
                                            document.getElementById("subpagecontent").innerHTML = '';
                                            document.getElementById("articlemenucontainer").innerHTML = '';
                                            $("#articlemenucontainer").fadeOut('fast');
                                            $("#linkcontainer").fadeOut('fast');
                                        });
                               });     
}

function showSubpageByMenu( menu ) {
    $("#linkcontainer").fadeOut('fast');
    $("#articlemenucontainer").fadeOut('fast');
    hideWrapperLayout();
    showSubpageLayout();
    selectedArticle = menu;
    xajax_showMenuPage( menu );

    //Store the current view to intercept the back button
    storeCurrentView();
}

function showSpecialArticle( article, attributes ) {
    //Create a back option
    if ( IsNumeric( selectedArticle ) || selectedArticle.substr(0,1) == "." ) {
        storedView = $.history.stack[$.history.cursor];
    }

    selectedArticle = article;

    //Store the view
    storeCurrentView( attributes );

    //Select the correct menu
    switch ( article ) {
        case "event-calendar": 
            showSubpageLayout();
            highlightMenu( ".evenementen" );
            selectedId = "#as0"; break;
        case "news":
            showSubpageLayout();
            highlightMenu( ".vereniging" );
            selectedId = "#as1"; break;
        case "photo-album-list":
            showWrapperLayout();
            $("#wrappercontent").fadeOut('fast');
            highlightMenu( ".vereniging" ); selectedId = "#as1"; break;
        case "photo-album":
            $("#wrappercontent").fadeOut('fast');
            showWrapperLayout();
            highlightMenu( ".vereniging" ); selectedId = "#as1";
            break;
    }

    xajax_showArticle( article, true, attributes );
}

function showSpecialArticleFromSubpage( article ) {
    selectedArticle = article;
    $("#linkcontainer").fadeOut('fast');
    //Store the view
    storeCurrentView( );
    xajax_showArticle( article, false );
}

function showSubpageByArticle( articleId, categoryName, updateMenu ) {
    showSubpageLayout();

    //Higlight the selected menu
    highlightMenu( "." + categoryName );

    selectedArticle = articleId;
    showArticleById( articleId, updateMenu );
}

function showSubpageLayout() {
    subpageLayoutShown = true;
    $("#subpage-menu-background").fadeIn('fast');
    document.getElementById("body_header").className = "subpage";
    document.getElementById("body-filler").className = "filler subpage-filler";
    document.getElementById("footer").className = "footer_sub";
    $("#linkcontainer").fadeOut('fast');
    $("#homepage-body").fadeOut('fast');
    $("#contentloader").fadeIn('fast');
}

function showWrapperLayout() {
    wrapperLayoutShown = true;
    document.getElementById("body_header").className = "wrapper";
    document.getElementById("footer").className = "wrapper-footer";
    document.getElementById("body-filler").className = "filler wrapper-filler";
    $("#subpagecontent").fadeOut('fast');
    $("#homepage-body").fadeOut('fast');
    $("#linkcontainer").fadeOut('fast');
    $("#articlemenucontainer").fadeOut('fast');
    $("#subpage-menu-background").fadeOut('fast');
}

function hideWrapperLayout() {
    if ( wrapperLayoutShown && subpageLayoutShown ) {
        wrapperLayoutShown = false;
        var contentHeight = $('#subpagecontent').height();
        contentHeight -= 350;

        if ( contentHeight < 0 ) contentHeight = 0;

        document.getElementById("body_header").className = "subpage";
        document.getElementById("body-filler").className = "filler subpage-filler";
        document.getElementById("footer").className = "footer_sub";

        $("#wrappercontent").fadeOut('fast');
        $("#body-filler").animate( { height:contentHeight + "px" }, 500, null, function() {
                                                                $("#subpagecontent").fadeIn('fast');
                                                                $("#linkcontainer").fadeOut('fast');
                                                                $("#articlemenucontainer").fadeIn('fast');
                                                          });
    } else if ( wrapperLayoutShown )
    {
        wrapperLayoutShown = false;
        $("#wrappercontent").fadeOut('fast');
        showHomepage();
    }
}

function hidePhotoAlbumList() {
    reinstateStoreView();
    hideWrapperLayout();
}

function showArticle( menuEvent ) {
    var id = '#as' + this.id.substring(2,3);  
    var articleIdAttribute = this.attributes.getNamedItem("aid");
    if ( IsNumeric( articleIdAttribute.value ) ) {
        showArticleById( articleIdAttribute.value );
    } else {
        showSpecialArticleFromSubpage( articleIdAttribute.value );
    }
    if ( selectedId != "" ){
        $(selectedId).fadeOut(fadeSpeed, function(){
                                              fadingComplete(id);
                                         });
        $(selectedId).removeClass("articleSelected");
    }
    
    $(id).addClass("articleSelected");

    selectedArticle = articleIdAttribute.value;
    selectedId = id;

    //Store the current view to intercept the back button
    storeCurrentView();
}

function showArticleById( articleId, updateMenu ) {
    $("#subpagecontent").fadeOut('fast');
    $("#linkcontainer").fadeOut('fast');
    $("#contentloader").fadeIn('fast');

    if ( updateMenu )$("#articlemenucontainer").fadeOut('fast');
    
    selectedArticle = articleId;
    xajax_showArticle( articleId, updateMenu );
}

//Xajax callback functions

function showArticleMenu( menuItemCount ) {

    var contentHeight = $('#articlemenucontainer').height();
    menuContentHeight = contentHeight;
    
    contentHeight-=157;

    if ( contentHeight < 30 ) contentHeight = 30;

    $("#subpage-menu-background").animate( { height:contentHeight + "px" }, 200, null, function() {
                                                $("#articlemenucontainer").fadeIn('fast');
                                          });
   

    for( var i=0;i<menuItemCount;i++ ) {
        // Events (mouseover)
        $('#am'+i).mouseover( function () { onArticleMouseHover(this.id, "fadeIn"); } );
        $('#at'+i).mouseover( function () { onArticleMouseHover(this.id, "fadeIn"); } );

        // Events (mouseout)
        $('#am'+i).mouseout( function () { onArticleMouseHover(this.id, "fadeOut"); } );
        $('#at'+i).mouseout( function () { onArticleMouseHover(this.id, "fadeOut"); } );

        // Events (mouseclick)
        $('#am'+i).click( showArticle );
        $('#at'+i).click( showArticle );
        
        //Highlight the selected article
        var menu = $('#am'+i);
        var aid = menu[0].attributes.getNamedItem("aid");
        if ( aid.value == selectedArticle ) {
            $('#as'+i).addClass("articleSelected");
            $('#as'+i).fadeIn('fast');
        }
    }    
}

function showContent( hasLinkedDocuments ) {
    var contentHeight = $('#subpagecontent').height();

    if ( contentHeight < menuContentHeight + 70 )
        contentHeight = menuContentHeight + 70; 
		
    contentHeight -= 440;
    if ( contentHeight < 0 ) contentHeight = 0;

    if ( !hasLinkedDocuments ) {
        $("#body-filler").animate( { height:contentHeight + "px" }, 500, null, function() {
                                                                    $("#subpagecontent").fadeIn('fast');
                                                                    $("#contentloader").fadeOut('fast');
                                                                    $("#articlemenucontainer").fadeIn('fast');
                                                                  });
    } else {
        contentHeight += 140;
        var containerHeight = menuContentHeight + 200;
        if ( containerHeight < 390 ) containerHeight = 390;
        $("#linkcontainer").css("top", containerHeight + "px" );
        $("#body-filler").animate( { height:contentHeight + "px" }, 500, null, function() {
                                                                    $("#subpagecontent").fadeIn('fast');
                                                                    $("#contentloader").fadeOut('fast');
                                                                    $("#linkcontainer").fadeIn('fast');
                                                                    $("#articlemenucontainer").fadeIn('fast');
                                                                  });
    }
}

function showWrapperContent( ) {
    var contentHeight = $('#subpagecontent').height();
    var wrapperContentHeight = $('#wrappercontent').height();

    if ( wrapperContentHeight > contentHeight && selectedArticle != "photo-album" ) {
        contentHeight = wrapperContentHeight;
        contentHeight -= 370;
        if ( contentHeight < 0 ) {
			$("#wrappercontent").fadeIn('fast');
			return;
		}
        $("#body-filler").animate( { height:contentHeight + "px" }, 500, null, function() {
                                                                $("#wrappercontent").fadeIn('fast');
                                                            });
    } else {
       $("#wrappercontent").fadeIn('fast');
    }

    if ( selectedArticle == "photo-album" ) {
        photoRun = 0;
        $("#body-filler").animate( { height:"70px" }, 500, null, function() {
                                                    $("#wrappercontent").fadeIn('fast');
                                               });
        setTimeout(fadeInPhotoPage, 1000 );
    }
}

function enablePhotoAlbumList( noOfAlbums ) {
    for ( var i=0; i<noOfAlbums;i++ ) {
        // Homepage menu item (fade in)
        $('#album-'+i).mouseover(onAlbumListMouseOver);

        // Homepage menu item (fade out)
        $('#album-'+i).mouseout(onAlbumListMouseout);

        // Homepage menu item (fade out)
        $('#album-'+i).click(onAlbumListClick);
    }
}

function onAlbumListMouseOver( event ) {
    var indexAttribute = $("#"+this.id)[0].attributes.getNamedItem("albumIndex").value;
    $('#album-'+indexAttribute).addClass("album-selected");
    //$('#album-selected-'+indexAttribute).fadeIn('fast');
}

function onAlbumListMouseout( event ) {
    var indexAttribute = $("#"+this.id)[0].attributes.getNamedItem("albumIndex").value;
    $('#album-'+indexAttribute).removeClass("album-selected");
    //$('#album-selected-'+indexAttribute).fadeOut('fast');
}

function onAlbumListClick( event ) {
    var indexAttribute = $("#"+this.id)[0].attributes.getNamedItem("albumId").value;
    showSpecialArticle( "photo-album", {albumid:indexAttribute} );
}

function fadeInPhotoPage( ) {
    if ( photoRun == 11 ) return;

    if ( photoRun < 8 )  { 
        fancyBoxThumbnail(photoRun);
        $('#photo-thumb-'+photoRun).fadeIn('normal');
    }
    if ( photoRun >= 1 ) {
        fancyBoxThumbnail((photoRun+7));
        $('#photo-thumb-'+(photoRun+7)).fadeIn('normal');
    }
    if ( photoRun >= 2 ) {
        fancyBoxThumbnail((photoRun+14));
        $('#photo-thumb-'+(photoRun+14)).fadeIn('normal');
    }
    if ( photoRun >= 3 ) {
        fancyBoxThumbnail((photoRun+21));
        $('#photo-thumb-'+(photoRun+21)).fadeIn('normal');
    }

    photoRun++;
    setTimeout(fadeInPhotoPage, 100 );
}

function fancyBoxThumbnail( index ) {
    $("a#photo-link-"+index).fancybox({
                                    'zoomOpacity'			: true,
                                    'overlayShow'			: false,
                                    'zoomSpeedIn'			: 500,
                                    'zoomSpeedOut'			: 500
                                });
}

function gotoAlbumPage( page, id ) {
    xajax_gotoPhotoAlbumPage( page, id );
}

function showTournamentForm( filename, formname, formType, tournamentId ) {
    showWrapperLayout();
    xajax_showTournamentForm( filename, formType, tournamentId );
}

function IsNumeric(strString)
   //  check for valid numeric strings
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }

function showTemaApplicationForm()
{
     showSubpageByArticle(1585, 'vereniging', true );
}



