﻿// JScript File
//<!--
//
function picSwap(strPicPath) {
    document.images['mainPic'].src = strPicPath;
    document.getElementById("photoCaption").innerHTML = '';
    gallery.reset_index();
}

function launchIC(memberID, destinationMemberID) {
    window.open("videoChat/ic.aspx?destinationMemberID=" + destinationMemberID, "ICWindow_" + memberID + "_" + destinationMemberID, "width=360,height=420,toolbar=0,directories=0,menubar=0,status=0,location=0,scrollbars=0,resizable=0");
}

function picSwap2(strPicPath, caption) {
    document.images['mainPic'].src = strPicPath;
    document.getElementById("photoCaption").innerHTML = caption;
    gallery.reset_index();
}





function image_gallery() {
    this.base_url = "http://www.ratemybody.com/";
    this.images = [];
    this.user = '';
    this.current = 0;
    var me = this; // using me instead of this inside methods to solve problem with event handlers

    this.attach_to_elements = function(image, caption, previous, next) {
        me.image_element = document.getElementById(image);
        me.caption_element = document.getElementById(caption);
        me.previous_element = document.getElementById(previous);
        me.previous_element.onclick = me.previous;
        me.next_element = document.getElementById(next);
        me.next_element.onclick = me.next;
        me.reset_index();
    }

    this.image_url = function(image_data) {
        switch (image_data.id) {
            case "main":
                return me.base_url + "mainPics/" + me.user.substring(0, 1).toLowerCase() + "/" + me.user.substring(0, 2).toLowerCase() + "/" + me.user + ".jpg";
            case "pending":
                return me.base_url + "images/picPending.gif";
            case "login":
                return me.base_url + "images/logintoview.gif";
            default:
                return me.base_url + "extraPics/" + me.user.substring(0, 1).toLowerCase() + "/" + me.user.substring(0, 2).toLowerCase() + "/" + me.user + "_" + image_data.id + ".jpg";
        }
    }

    this.switch_image = function(new_index) {
        if (new_index < 0) {
            new_index = 0;
        } else if (new_index >= me.images.length) {
            new_index = me.images.length - 1;
        }
        me.image_element.src = me.image_url(me.images[new_index]);
        var caption = me.images[new_index].caption;
        if (typeof (caption) != "string") {
            caption = "";
        }
        me.image_element.title = caption;
        me.caption_element.innerHTML = caption;
        me.current = parseInt(new_index);
        me.set_arrows();
    }

    this.set_arrows = function() {
        if (me.images[me.current - 1]) {
            me.previous_element.style.display = "";
        } else {
            me.previous_element.style.display = "none";
        }
        if (me.images[me.current + 1]) {
            me.next_element.style.display = "";
        } else {
            me.next_element.style.display = "none";
        }
    }

    this.previous = function() {
        me.switch_image(me.current - 1);
    }

    this.next = function() {
        me.switch_image(me.current + 1);
    }

    this.reset_index = function() {
        new_index = 0;
        pattern = /_(\d+)\.jpg/;
        result = pattern.exec(me.image_element.src);
        if (result) {
            id = result[1];
            for (i in me.images) {
                if (me.images[i].id == id) {
                    new_index = i;
                    break;
                }
            }
        }
        me.switch_image(new_index);
    }
}

//-->





