$(function() {
    
    // let's build a data object that will be used later on for sorting/filtering
    createDataObject();

    /* Hide #About and #Contact sections */
    $('#About').hide();
    $('#Tickets').hide();
    $('#Venue').hide();
    $('#Bonus').hide();
    $('#Contact').hide();

    /* Set up Accordion */
    $('#Accordion').accordion({
        collapsible: true,
        autoHeight: false,
        active: false,
        changestart: onAccordionChangeStart
    }
    );

    /* Close Accordion */
    $('.section_close').click(function() {
        $('#Accordion').accordion({
            active: false
        }
        );
    });

    /* Show #About section  */
    $('#AboutLink').click(function() {
		$('#Tickets').hide();
		$('#Venue').hide();
	    $('#Bonus').hide();
	    $('#Contact').hide();
        $('#About').toggle();
        $('#Accordion').accordion({
            //active: false
        }
        );
    });

    /* Show #Tickets section  */
    $('#TicketsLink').click(function() {
		$('#About').hide();
		$('#Venue').hide();
	    $('#Bonus').hide();
	    $('#Contact').hide();
        $('#Tickets').toggle();
        $('#Accordion').accordion({
            //active: false
        }
        );
    });

    /* Show #Venue section  */
    $('#VenueLink').click(function() {
		$('#About').hide();
		$('#Tickets').hide();
	    $('#Bonus').hide();
	    $('#Contact').hide();
        $('#Venue').toggle();
        $('#Accordion').accordion({
            //active: false
        }
        );
    });

    /* Show #Bonus section  */
    $('#BonusLink').click(function() {
		$('#About').hide();
		$('#Tickets').hide();
	    $('#Venue').hide();
	    $('#Contact').hide();
        $('#Bonus').toggle();
        $('#Accordion').accordion({
            //active: false
        }
        );
    });

    /* Show #Contact section  */
    $('#ContactLink').click(function() {
		$('#About').hide();
		$('#Tickets').hide();
	    $('#Venue').hide();
	    $('#Bonus').hide();
        $('#Contact').toggle();
        $('#Accordion').accordion({
            //active: false
        }
        );
    });

    /* Collapse top sections when viewing portfolio*/
    $('#Accordion h2 a').click(function() {
	    $('#About').hide();
	    $('#Tickets').hide();
	    $('#Venue').hide();
	    $('#Bonus').hide();
	    $('#Contact').hide();
    });

    /* Open anchors with rel="external" in new window */
    $("a[rel*='external']").click(function() {
        this.target = "_blank";
    });

    /* Add class="last" to each section div */
    $('.section_close').prev().addClass('last');

    /* Add class="last" to each section paragraph */
    $('.block.details p:last-child').addClass('last');

    /* Cufon */
    //Cufon.replace('h1');
    //Cufon.now();

    // if a bookmark has been specified
    if (window.location.hash != "") {

        // search for it
        var __element = $('#' + window.location.hash.substring(1).split("/")[1])

        // if it exists
        if (__element != undefined) {

            // determine where the selected element is relative to its siblings in the accordion
            var __position = Math.floor(__element.index() / 2);

            // fire it
            $('#Accordion').accordion("activate", __position);
        }
    }

    
});


// called prior to the start of the animation
function onAccordionChangeStart(e, jUI) {
    
    // we need to scroll the document to align the new selection with the top of the viewport
    alignWindowByHeaderElement(jUI.newHeader);
    // set the hash to reflect this new selection, so that we can bookmark specific content
    if(jUI.newHeader != undefined && jUI.newHeader.attr("id") != undefined){
        window.location.hash = "#/" + jUI.newHeader.attr("id");
    }
    else{
        window.location.hash = "";
    }
}

function alignWindowByHeaderElement(headerElement) {

    // determine where the selected element is relative to its siblings
    var __position = Math.floor(headerElement.index() / 2);

    // determine where the first item in the accordion is positioned
    var __accordionOffset = $('#Accordion').offset().top - 100;

    // since all header elements are the same height, we don't need to invoke a loop to calculate
    var __headerElementHeight = headerElement.height();

    var __calculatedPosition = __accordionOffset + (__position * __headerElementHeight) + (__position * 3);

    $('html,body').animate({scrollTop: __calculatedPosition}, 300)

}

/* Everything below this point is IN-PROGRESS */

function createDataObject(){
    
    accordionItemData = [];
    
    $('#Accordion h2').each(
        function(){
            var __this = $(this);
            var __itemData = {
                id:__this.attr('id'),
                year:__this.next('section').find('.date').text(),

            };
            
            // because the print and web denoting is done via the assignment of a class (?)
            // we need to do an explicit check for each --- this is less than ideal, but it'll
            // work for a sample set as small as the elements on this page
            
            // check for web
            if(__this.attr('class').indexOf('web') != -1){
                __itemData.web = true;
            }
            else{
                __itemData.web = false;
            }
            
            // check for print
            if(__this.attr('class').indexOf('print') != -1){
                __itemData.print = true;
            }
            else{
                __itemData.print = false;
            }
            
            accordionItemData.push(__itemData)
        }
    );
}

function sortByYear(){
    
    $('#Accordion').accordion("activate", false);
    $('#Accordion').accordion("destroy");
    
    var __sorted = $.extend(true, [], accordionItemData);
    __sorted.sort(function(objA, objB){
        if(objA.year < objB.year){
            return 1;
        }
        else if(objA.year > objB.year){
            return -1;
        }
        else{
            return 0;
        }
    });
    
    // move elements around in the DOM
    for(i = 0; i < __sorted.length; i++){
        console.log(__sorted[i].id);
        $("#" + __sorted[i].id).parent().append($("#" + __sorted[i].id));
        $("#" + __sorted[i].id).parent().append($("#" + __sorted[i].id).next('section'));
    }
    
    // re-apply the accordion
    $('#Accordion').accordion({
        collapsible: true,
        autoHeight: false,
        active: false,
        changestart: onAccordionChangeStart
    }
    );
}

function sortByWeb(){
    
}

function sortByPrint(){
    
}

