// this file contains all of the functions we're
// going to be using associated with our photos on
// the site

// some global vars for our photos...
var photo_album = new Array();      // this will store all of the photo objects we get from the db
var photo_idx   = -1;               // the current index in our photo_album
var prev_idx    =  0;               // the starting idx for this page
var page_max    =  9;               // the max number of photos per page
var photo_total =  0;               // the total number of photos in this album
var current_pg  =  0;               // the current page in the album

// get our photo album images from the db
function __search_photos()
{
    var args = __search_photos.arguments;

loading(true);
    // generate a new form id and submit it
    var id = generate_id();
    __submit("search_photos.php",function(){search_photos(id);},id);
}
function search_photos()
{
    var args = search_photos.arguments;
    var id   = args[0];
    var r    = _response[id];

    // sanity
    if( !r ) { loading(false); return; }

    // check the errorcode
    if( r.errorcode != 0 ) { display_message(r.error_message); }
    else
    {
        // we have to reset the album...
        reset_photo_album();
        // set the total number of photos retrieved
        photo_total = r.matches;
        // create our album
        if( r.matches == 1 ) { photo_album.push(r.data); }
        else
        {
            // set up our photo album
            for( var i=0; i<r.matches; i++ ) { photo_album.push(r.data[i]); }
        }
        // now show the first page of photos
        // are we in an admin page?
        if( dojo.byId("admin") && (dojo.byId("admin").value == 1) ) { display_admin_photos(); }
        else
        {
            // get the default page... if any
            current_pg = parseInt(dojo.byId("pgid").value);
            display_photos(current_pg);
        }
    }
}

// display 1 page worth of photos
function display_photos()
{
    var args  = display_photos.arguments;
    var pg    = (args[0]) ? args[0] : 1;
    var idx   = photo_idx;
    // photo vars
    var photo = {};
    var pic   = "";
    var html  = "";

loading(true);

    // set the photo album page
    set_album_page(pg);
    // load in our thumbnail display html
    dojo.io.bind({
        url:        "html/photos_thumbnail_display.html",
        load:       function( type, data )
                    {
                        // we don't need to eval anything, we're reading in html
                        // loop the photo_album for page_max photos and slap in the html
                        while( photo = next_photo() )
                        {
                            // copy the "data" html to another var
                            pic = data;
                            // replace the necessary strings and add it to our html
                            pic = pic.replace(/{THUMBNAIL}/g,photo.Thumbnail);
                            pic = pic.replace(/{NAME}/g,unescape(photo.FirstName));
                            pic = pic.replace(/{USERID}/g,photo.UserID);
                            pic = pic.replace(/{PHOTOID}/g,photo.ID);
                            pic = pic.replace(/{PAGE}/g,current_pg);
                            // add the "pic" html to our "html" var
                            html += pic;
                        }
                        photo_idx = idx;

                        // update the html in our gallery div
                        dojo.byId("gallery").innerHTML = html;
loading(false);
                    },
        mimetype:   "text/html"
    });
}
// the admin func to display photos
function display_admin_photos()
{
    var html   = "";
    var myopts = new Array("OK","PENDING","FEATURED");
    // load in our list display
    dojo.io.bind({
        url:        "html/photos_admin_list.html",
        load:       function( type, data )
                    {
                        // loop thru all photos and display them
                        for( var i=0; i<photo_total; i++ )
                        {
                            var photo = photo_album[i];
                            // copy our "data" html into another var
                            pic = data;
                  // create the select list for our status
                  var box_html  = "<select id={PHOTOID} onchange=\"__update_photo_status(this.value,this.id,{MEMBERID})\">";
                  for( var j=0; j<myopts.length; j++ )
                  {
                      box_html += "<option";
                      box_html += (photo.Status==myopts[j]) ? " SELECTED" : "";
                      box_html += ">"+myopts[j]+"</option>";
                  }
                      box_html += "</select>";
                            pic = pic.replace(/{STATUS}/g,box_html);
                            // replace the necessary strings and add it to our html
                            pic = pic.replace(/{ICON}/g,photo.Icon);
                            pic = pic.replace(/{NAME}/g,unescape(photo.FirstName));
                            pic = pic.replace(/{ADDED}/g,photo.DateAdded);
                            pic = pic.replace(/{PHOTOID}/g,photo.ID);
                            pic = pic.replace(/{MEMBERID}/g,photo.UserID);
                            // add the "pic" to our html
                            html += pic;
                        }
                        // set the gallery display
                        dojo.byId("gallery").innerHTML = html;
loading(false);
                    },
        mimetype:   "text/html"
    });
}

// get the next photo
function next_photo()
{
    // increment the idx we're gonna get
    photo_idx++;
    // sanity
    if( photo_idx >= photo_total ) { return false; }
    // make sure we're not going past our page max
    if( (photo_idx - (page_max * current_pg)) >= prev_idx ) { return false; }

    // return the photo object
    return photo_album[photo_idx];
}
// get the previous photo
function prev_photo()
{
    // sanity
    if( photo_idx < 1 ) { return false; }

    // decrement the photo_idx photo object
    photo_idx--;
    return photo_album[photo_idx];
}

// show photo... just send to photo page
function show_photo()
{
    var args = show_photo.arguments;
    var pid  = args[0];
    var pgid = args[1];

    location.href = "photos_view.php?pid="+pid+"&pgid="+pgid;
}

// load the "next" page
function next_page() { var p = current_pg+1; display_photos(p); }
function prev_page() { var p = current_pg-1; display_photos(p); }

// set the page of the photo album we're going to display
function set_album_page()
{
    var args   = set_album_page.arguments;
    var pg     = (args[0]) ? args[0] : 1;
    current_pg = pg;

    // set the current page display values
    dojo.byId("page_display").innerHTML = "Page "+pg;

    // take away one from the page...
    pg--;
    // now we'll set the photo_idx accordingly
    photo_idx = (page_max * pg) - 1;

    // check to see whether or not to display our
    // "next" and "prev" links
    var n = dojo.byId("link_next");
    var p = dojo.byId("link_prev");
    // next
    if( (photo_idx + page_max + 1) < photo_total ) { n.style.visibility = "visible"; }
    else { n.style.visibility = "hidden"; }
    // prev
    if( (photo_idx - page_max) < -1 ) { p.style.visibility = "hidden"; }
    else { p.style.visibility = "visible"; }
}

// reset the photo album
function reset_photo_album() { photo_album = new Array(); }

// get a specific photo's details from the db
function __get_photo_details()
{
    var args = __get_photo_details.arguments;

loading(true);
    // get it...
    var id = generate_id();
    __submit("details_photos.php",function(){get_photo_details(id);},id);
}
function get_photo_details()
{
    var args = get_photo_details.arguments;
    var id   = args[0];
    var r    = _response[id];
    // details...
    var pg_html = "";
    var html    = "";

    // sanity
    if( !r ) { loading(false); return; }

    // check errorcode and continue
    if( r.errorcode != 0 ) { display_message(r.error_message); }
    else
    {
        // read in our details html
        dojo.io.bind({
            url:        "html/photos_details.html",
            load:       function( type, data )
                        {
                            // we don't need to eval anything, we're getting
                            // straight html
                            pg_html = data;
                            pg_html = pg_html.replace(/{PGID}/g,parseInt(dojo.byId("pgid").value));
                            pg_html = pg_html.replace(/{FILEPATH}/g,r.photo.Filepath);
                            pg_html = pg_html.replace(/{TAG}/g,(r.photo.Tag)?unescape(r.photo.Tag):"");
                            html = pg_html;
                            dojo.byId("photo_details").innerHTML = html;

                        // do we have any comments on this photo yet?
/*
                        if( r.comments )
                        {
                            // create our list of comments
                            var comments_div = dojo.byId("photo_comments");
                            var comment      = {};
                            var div; var photo_div; var text_div; var temp_html;
                            // this is a "clear" div...
                            var clear_div = document.createElement("div");
                            clear_div.setAttribute("style","clear:both;");

                            // if we only have one comment, it is NOT returned as an array
                            var the_comments;
                            if( r.num_comments > 1 ) { the_comments = r.comments; }
                            else { the_comments = new Array(r.comments); }
                            for( var i=0; i<r.num_comments; i++ )
                            {
                                var c    = the_comments[i];
                                // create the photo div
                                photo_div = document.createElement("div");
                                photo_div.setAttribute("class","comment_photo");
                                photo_div.setAttribute("className","comment_photo");
                                temp_html  = "<a href=\"fanclub_member.php?id="+c.UserID+"\">";
                                temp_html += "<img src=\""+c.Icon+"\" border=0></a>";
                                photo_div.innerHTML = temp_html;
                                // create the text div
                                    var my_comment = c.Comments.replace(/\n/g,"<br>\n");
                                text_div = document.createElement("div");
                                text_div.setAttribute("class","a_comment");
                                text_div.setAttribute("className","a_comment");
                                temp_html  = unescape(my_comment);
                                temp_html += "<br><br><div class='fineprint' align='right'><span class=\"greytext\">";
                                temp_html += "Posted "+c.LastModified+" by "+unescape(c.FirstName)+"</span></div>";
                                text_div.innerHTML = temp_html;
                                // create the "holder" div
                                div = document.createElement("div");
                                div.setAttribute("class","item");
                                div.setAttribute("className","item");
                                div.setAttribute("onmouseover","hilite(this,'item_over')");
                                div.setAttribute("onmouseout","hilite(this,'item')");

                                // append the photo and the comment to our item div
                                div.appendChild(photo_div);
                                div.appendChild(clear_div);
                                div.appendChild(text_div);
                                div.appendChild(clear_div);
                                // append the item div to our comments div
                                comments_div.appendChild(div);
                            }
                        }
*/
loading(false);
                        },
            mimetype:   "text/html"
        });
    }
}


// update a photo status
function __update_photo_status()
{
    var args   = __update_photo_status.arguments;
    var status = args[0];
    var pid    = args[1];
    var uid    = args[2];
loading(true);

    // set the photo details
    dojo.byId("photo_id").value     = pid;
    dojo.byId("photo_status").value = status;
    dojo.byId("member_id").value    = uid;
    // generate an id and submit it
    var id = generate_id();
    __submit("update_photo_status.php",function(){ update_photo_status(id); },id);
}
function update_photo_status()
{
    var args = update_photo_status.arguments;
    var id   = args[0];
    var r    = _response[id];

    // sanity
    if( !r ) { loading(false); return; }

    // display the message
    display_message(r.error_message);
    __search_photos();
    __get_featured_photo();
}

// delete a photo
function __delete_photo()
{
    var args = __delete_photo.arguments;
    var pid  = args[0];

loading(false);
    dojo.byId("photo_id").value = pid;
    var id = generate_id();
    __submit("delete_photos.php",function(){ delete_photo(id); },id);
}
function delete_photo()
{
    var args = delete_photo.arguments;
    var id   = args[0];
    var r    = _response[id];

    // sanity
    if( !r ) { loading(false); return; }

    // check the errorcode and continue
    if( r.errorcode != 0 ) { display_message(r.error_message); }
    else { dojo.lfx.fadeHide("photo_"+r.pid, 1100).play(); }
}

// leave a comment!
function __leave_comment()
{
loading(true);
    var id = generate_id();
    __submit("leave_comment.php",function(){ leave_comment(id); },id);
}
function leave_comment()
{
    var args = leave_comment.arguments;
    var id   = args[0];
    var r    = _response[id];

    // sanity
    if( !r ) { loading(false); return; }

    // check the errorcode and continue
    if( r.errorcode != 0 ) { display_message(r.error_message); }
    else { __get_photo_details(); }
loading(false);
}


// gets the featured photo from the db
function __get_featured_photo()
{
    var args = __get_featured_photo.arguments;
    // generate an id and submit
    var id = generate_id();
    __submit("search_featured_photo.php",function(){ get_featured_photo(id); },id);
}
function get_featured_photo()
{
    var args = get_featured_photo.arguments;
    var id   = args[0];
    var r    = _response[id];
    var fdiv = dojo.byId("featured_photo_thumb");


    // sanity
    if( !r ) { loading(false); return; }

    // check the error code and continue
    if( r.errorcode != 0 ) { fdiv.innerHTML = r.error_message; }
    else
    {
        dojo.io.bind({
            url:        "html/photos_featured.html",
            load:       function( type, data )
                        {
                            // data is the html
                            data = data.replace(/{NAME}/g,unescape(r.data.FirstName));
                            data = data.replace(/{USERID}/g,r.data.ID);
                            data = data.replace(/{PHOTOID}/g,r.data.PhotoID);
                            data = data.replace(/{THUMBNAIL}/g,r.data.Thumbnail);
                            fdiv.innerHTML = data;
                        },
            mimetype:   "text/html"
        });
    }
}