/*
 * Aliona Kazakova - Site JavaScript
 */
 
var kaz = kaz || {};

// List your pictures here in order
kaz.PICTURES = [
    {
        image_file:     "deliverance.jpg",
        title:          "Deliverance",
        description:    "Mixed media on wood, 20'' x 30'' ",
        date:           "2010"
    },
{
        image_file:     "snow.jpg",
        title:          "Untitled",
        description:    "Mixed media on wood, 36'' x 48'' ",
        date:           "2009"
    },
{
        image_file:     "good_intentions.jpg",
        title:          "Good Intentions",
        description:    "Mixed media on wood, 16'' x 20'' ",
        date:           "2009"
    },
   {
        image_file:     "core.jpg",
        title:          "Core",
        description:    "Mixed media on wood, 16'' x 20'' ",
        date:           "2009"
    }, 
  {
        image_file:     "underneath.jpg",
        title:          "Underneath",
        description:    "Mixed media on wood, 16'' x 20'' ",
        date:           "2009"
    }, 
{
        image_file:     "shine.jpg",
        title:          "Untitled",
        description:    "Mixed media on wood, 16'' x 20'' ",
        date:           "2009"
    },
 {
        image_file:     "kaz_ptg_secret.jpg",
        title:          "Guardians",
        description:    "Mixed media on wood, 24'' x 30'' ",
        date:           "2009"
    },
    {
        image_file:     "kaz_ptg_7.jpg",
        title:          "7",
        description:    "Mixed media on wood, 24'' x 30'' ",
        date:           "2009"
    },
    {
        image_file:     "kaz_ptg_untitled1.jpg",
        title:          "Untitled",
        description:    "Mixed media on wood, 24'' x 24'' ",
        date:           "2009"
    },
    {
        image_file:     "kaz_ptg_untitled2.jpg",
        title:          "Untitled",
        description:    "Mixed media on wood, 24'' x 30'' ",
        date:           "2009"
    },
    {
        image_file:     "kaz_ptg_ferita.jpg",
        title:          "Ferita",
        description:    "Mixed media on wood, 36'' x 8'' ",
        date:           "2009"
    },
    {
        image_file:     "cave.jpg",
        title:          "Cave",
        description:    "Mixed media on wood, 36'' x 8'' ",
        date:           "2009"
    }
];

// Constants
kaz.HEADER_LINK_COLOR =         '#5180bc';
kaz.HEADER_LINK_HOVER_COLOR =   '#000';
kaz.PICTURE_OFFSET =            750;

// Variables - Don't modify!
kaz.cur_page =                  0;
kaz.num_pages =                 0;
kaz.downloading_image_idx =     0;

// ..........................................................................

/** 
 * Performs the downloading of all images in sequential order.  This is to
 * help assist the downloading of the large images.
 */
kaz.downloadAllImages = function () {
    if (kaz.downloading_image_idx < kaz.PICTURES.length) {
        // OK, download the image!
        kaz.imagePreloader({
            url: 'gallery/' + kaz.PICTURES[kaz.downloading_image_idx].image_file,
            callback: function (didSucceed, url) {
                // Install the image!
                kaz.pictures_map[kaz.downloading_image_idx].removeClass('kaz-downloading-wait');
                kaz.pictures_map[kaz.downloading_image_idx][0].innerHTML = '<img src="gallery/' + kaz.PICTURES[kaz.downloading_image_idx].image_file + '" />';

                // Go to next image.
                kaz.downloading_image_idx++;
                
                kaz.downloadAllImages();
            }
        });
    }
}

// ..........................................................................

/**
 * Preloads an image and when it's ready ping a callback if necessary.
 *
 * @param params - (Obj. lit.) Supported options:
 *
 *     url: (String) Image source
 *     callback: (Function) (optional) Callback to call when this image
 *         has finished downloading.  The callback should look like:
 *
 *         callback(didSucceed, url) { ... }
 *
 *         didSucceed will be true if successful, false if there was an
 *         error like an invalid image or if the user aborted the image
 *         download.
 */
kaz.imagePreloader = function (params) {
    var i = new Image();
    
    if (params.callback) {
        (function (im, cb, u) {
            im.onload   = function () {cb(true, u);};
            im.onerror  = function () {cb(false, u);};
            im.onabort  = function () {cb(false, u);};
        })(i, params.callback, params.url);
    }
    
    i.src = params.url;
};

// ..........................................................................

/**
 * Main init.
 */
kaz.init = function () {
    // Initialize the slideshow if it exists.
    kaz.slideshowInit();
    
    // Hook up info email "link"
    $('.kaz-contact-info-email').click(function () {
        window.location.href = 'ma' + 'ilt' + 'o:inf'+'o@'+'alio'+
            'nakazako'+'va.c'+'om';
    });
};

// ..........................................................................

/**
 * Slideshow init.
 */
kaz.slideshowInit = function () {
    // See if we need to init this at all?
    kaz.slideshow_div = $('.kaz-slideshow');
    if (kaz.slideshow_div.length != 1) {
        return;
    }
    
    // Make some jQuery maps to controls and stuff.
    kaz.slideshow_description = $('.kaz-description', kaz.slideshow_div);
    kaz.scroll_left_button = $('.kaz-arrow-left', kaz.slideshow_div);
    kaz.scroll_right_button = $('.kaz-arrow-right', kaz.slideshow_div);
    kaz.pictures_div = $('.kaz-pictures', kaz.slideshow_div);
    
    // Hide the pictures div for now
    kaz.pictures_div.hide();
    
    // Populate the pictures frame
    kaz.num_pages = kaz.PICTURES.length;
    kaz.pictures_map = [];
    for (var k = 0; k < kaz.num_pages; k++) {
        var kpage = kaz.PICTURES[k];
        
        // Add the frame and sourcefor the picture.
        var pic_left = kaz.PICTURE_OFFSET * k;
        var html = '<div data-pic-num="' + k + '" class="kaz-picture kaz-downloading-wait" style="left: ' + pic_left + 'px;">' +
            '{ Downloading image ' + (k + 1) + ' }' +
            '</div>';
            
        kaz.pictures_map[k] = $(html);
        
        kaz.pictures_div.append(kaz.pictures_map[k]);
    }
    
    // Show the page
    kaz.slideshowPage(kaz.cur_page);
    
    // Hook up left/right
    kaz.scroll_left_button.click(function () {
        kaz.slideshowSeek(-1);
    });
    kaz.scroll_right_button.click(function () {
        kaz.slideshowSeek(1);
    });
    
    // Fade in the pictures div
    window.setTimeout(function () {
        kaz.pictures_div.fadeIn(700);
    }, 500);
};

// ..........................................................................

/**
 * Slideshow seek and titles set.
 *
 * @param page_num - (int) Page number (0 .. kaz.PICTURES.length - 1)
 */
kaz.slideshowPage = function (page_num) {
    var kpage = kaz.PICTURES[page_num];
    
    // First change the titles
    kaz.slideshow_description.html(kpage.title + '<br />' + kpage.description + '<br />' + kpage.date);
    
    // Animate the slidey
    kaz.pictures_div.animate({left: 0 - (kaz.PICTURE_OFFSET * page_num)}, 800);
    
    // Enable/disable scrollers?
    if (page_num < 1) {
        // Disable left.
        kaz.scroll_left_button.fadeOut(300);
    } else {
        kaz.scroll_left_button.fadeIn(500);
    }

    if (page_num >= (kaz.num_pages - 1)) {
        // Disable left.
        kaz.scroll_right_button.fadeOut(300);
    } else {
        kaz.scroll_right_button.fadeIn(500);
    }
};

// ..........................................................................

/**
 * Shift the current page number by page_incr.
 *
 * @param page_incr - (int) Page increment.
 */
kaz.slideshowSeek = function (page_incr) {
    kaz.cur_page += page_incr;
    if (kaz.cur_page < 0) {
        kaz.cur_page = 0;
    }
    if (kaz.cur_page >= kaz.num_pages) {
        kaz.cur_page = kaz.num_pages - 1;
    }
    
    kaz.slideshowPage(kaz.cur_page);
};

// ..........................................................................

$(document).ready(function () {
    kaz.init();
    kaz.downloadAllImages();
});

