/* Portfolio page listing functionality */
current_page = 0;
listing_type = "date";

function list_callback(data) {
    var projects = data.split(",");
    
    // The last two items in the projects array above will be the current page
    // being displayed and the total number of available pages.  Grab those
    // values first
    var total_pages = projects.pop();
    current_page = projects.pop();
    
    // Now reverse the array so that the values will pop off in the right
    // order.
    projects.reverse();
    
    var project = 0;
    while (projects.length > 0) {
        var title = projects.pop();
        var link = projects.pop();
        var thumbnail = projects.pop();
        
        // Hide the components of the project listing box
        jQuery("#project_thumbnail_"+project).hide();
        jQuery("#project_title_"+project).hide();
        
        // Switch out the image, title, and URL
        jQuery("#project_href_"+project).attr("href", link);
        jQuery("#project_title_"+project).text(title);
        jQuery("#project_thumbnail_"+project).attr("src", thumbnail);
        
        // Fade those elements back in
        jQuery("#project_thumbnail_"+project).fadeIn();
        jQuery("#project_title_"+project).fadeIn();
        
        project++;
    }
    
    // For the remainder of the boxes, just go ahead and clear/hide them.
    while (project < 9) {
        jQuery("#project_href_"+project).attr("href", "#");
        jQuery("#project_title_"+project).text("");
        jQuery("#project_thumbnail_"+project).hide();
        project++;
    }
    
    // Update the page count and previous/next links
    var display_page = Number(current_page) + 1;
    jQuery("#page_count").text("" + display_page + " / " + total_pages);
    if (current_page == 0) {
        jQuery("#previous_page").hide();
    } else {
    	jQuery("#previous_page").show();
   	}
    
    if (current_page == (total_pages-1)) {
    	jQuery("#next_page").hide();
    } else {
    	jQuery("#next_page").show();
    }
}

function update_listing(href) {
	var data = { bright_portfolio_listing: listing_type, bright_portfolio_page: current_page  }
	jQuery.post(href, data, list_callback);
}

/* End portfolio page functions */

/* Project page functions */
rotator_active_image = null;
rotator_images = null;

function project_rotator(images) {
    if (images.length == 0) return;
 
    // Set the first image in the sequence immediately; no need for it to fade in
    jQuery("#project-header-image-rotator").attr('src', images[0]);
 
    // Save the passed image array in a global variable
    rotator_images = images;
    rotator_active_image = 0;
    
    // Create a function to rotate through the rest of the images
    var rotate_func = function() {
        rotator_active_image++;
        if (rotator_active_image >= rotator_images.length) {
            rotator_active_image = 0;
        }
        
        var img = jQuery("#project-header-image-rotator");
        img.hide();
        img.attr('src', rotator_images[rotator_active_image]);
        img.fadeIn();
    }
    
    // And schedule the rotator to be called every 5 seconds
    setInterval(rotate_func, 5000);
}

function project_info_text(html) {
    // Replace the text to the right of the rotating image on project pages
    jQuery(document).ready( function() {
        jQuery("#project-header-content").html(html);
    });
}

/* End project page functions */

/* Main page header/rotation */
main_header_num_entries = 0;
main_header_active_entry = 0;
main_header_entries = new Array();
main_header_interval = 0;

function main_set_active_entry(entry_num, clicked) {
    if (entry_num >= 0 && entry_num < main_header_num_entries) {
        var base_id = "#" + main_header_entries[entry_num];
        var href = jQuery(base_id + "_href").attr("href");
        var image = jQuery(base_id + "_image").attr("src");
        var content = jQuery(base_id + "_content").html();
        var chooser = jQuery(base_id + "_chooser");
        
        // Hide the current image
        jQuery("#header_img").hide();
        
        // Switch out colors on the chooser on the left
        jQuery("#header_picker a").css("color", "#e96c64");
        chooser.css("color", "#ffffff");
        
        jQuery("#header_link").attr("href", href);
        jQuery("#header_img").attr("src", image);
        jQuery("#header_overlay").html(content);
        
        jQuery("#header_img").fadeIn();
        jQuery("#header_overlay").fadeIn();
        
        main_header_active_entry = entry_num;
        
        // If we're switching because of a mouse click, reset the
        // interval timer so that the image doesn't switch too fast
        if (clicked) {
            clearInterval(main_header_interval);
            main_header_interval = setInterval(main_header_rotator, 5000);
        }
    }
}

function main_header_rotator() {
    var which = main_header_active_entry + 1;
    if (which >= main_header_num_entries) {
        which = 0;
    }
    
    main_set_active_entry(which);
}

function main_page_init() {
    // Gather up any main_header_entry collections and    
    // save all of their IDs
    jQuery(".main_header_entry").each(function() {
        main_header_entries.push(jQuery(this).attr("id"));
    });
    
    // Set the first entry active
    main_header_num_entries = main_header_entries.length;
    main_set_active_entry(0);
    
    // Set up the numbers down the left of the header image
    var count = 1;
    var html = "<ul>";
    for (var e in main_header_entries) {
        html = html + "<li><a id=\"main_header_" + count + "_chooser\" onclick=\"main_set_active_entry(" + (count - 1) + ");\">" + count + "</a></li>";
        count = count + 1;
    }
    html = html + "</ul>"
    jQuery("#header_picker").html(html);
    
    // If there's more than one entry, schedule the rotator
    if (main_header_num_entries > 1) {
        main_header_interval = setInterval(main_header_rotator, 5000);
    }
}

/* End main page stuff */

jQuery(document).ready( function($) {
    $("#sort_by_date").click( function() {
    	listing_type = "date";
    	current_page = 0;
    	update_listing();
        return false;
    });
    
    $("#sort_by_client").click( function() {
        listing_type = "client";
        current_page = 0;
    	update_listing();
        return false;
    });
    
    $("#sort_by_project").click( function() {
        listing_type = "project";
        current_page = 0;
    	update_listing();
        return false;
    });
    
    $("#previous_page").click( function() {
    	if (current_page > 0) {
    		current_page--;
    	}
    	update_listing();
    	return false;
    });
    
    $("#next_page").click( function() {
    	current_page++;
    	update_listing();
    	return false;
    });
});

