﻿var slides = 5;
var current_pic = 1;
var first_slide = 1;
var galleryimages = new Array();
galleryimages[0] = new galleryimage('', '', '', '', '', 'Submit Photo', '');
var next_test = 1;
var testimonials = new Array();
testimonials[0] = new testimonial('', '');
var submit_url = '';  //set this value on the page


//call this function to initialize the gallery
function load_gallery() {
    new Request.HTML({
        url: '/gallery/_thumbnails',
        method: 'post',
        update: 'gallerysliderow',
        onComplete: function() {
            for (a = 1; a <= slides; a++) {
                $('galleryslideimage' + a.toString()).set('src', '/Content/images/gallery/frame_big_off.gif');
            }
            load_gallery_complete();
        }
    }).send();
}
//load a set of thumbnails and the first image in the set
function load_gallery_complete() {
    //show current set of thumbnails
    show_thumbnails();
    //show first pic from current thumbnail set
    open_image(1);

    if (user_logged_in) {
        $('btnSubmitPhoto').set('href', 'javascript:submit_photo();');
    }
}

function show_login_modal(rntURL, redirURL) {
    if (!user_logged_in) {
        $('modal_loginform').set("action", "javascript: modal_process_login('" + rntURL + "', '" + redirURL + "');");
        TB_show('', '#TB_inline?&width=344&height=294&inlineId=modal_login&modal=true', '::::');
    } else {
        document.location = redirURL;
    }
}

//display current set of thumbnails
function show_thumbnails() {
    for (s = 0; s < slides; s++) {
        if (galleryimages[first_slide + s] == null) {
            galleryimages[first_slide + s] = new galleryimage('', '', '', '', '', 'Submit Photo', '');
        }
        $('galleryslide' + (s + 1).toString()).setStyle('background-image', 'url(' + galleryimages[first_slide + s].thumburl + ')');
        $('gallerylegend' + (s + 1).toString()).set('html', galleryimages[first_slide + s].title);
    }
    $('galleryarrowleft').setStyle('opacity', (first_slide == 1) ? 0 : 1);
    $('galleryarrowright').setStyle('opacity', (galleryimages[first_slide + slides] == null) ? 0 : 1);
}
// display main image and highlight thumbnail
function show_gallery_image() {
    var i = first_slide + current_pic - 1;
    $('gallerypic').setStyle('opacity', 0);
    $('gallerypic').setStyle('background-image', 'url(' + galleryimages[i].imgurl + ')');
    $('gallerypic').set('tween', {
        duration: 500,
        transition: Fx.Transitions.Cubic.easeIn
    });
    $('gallerypic').tween('opacity', 1);
    for (s = 1; s <= slides; s++) {
        $('galleryslideimage' + s.toString()).set('src', '/Content/images/gallery/frame_big_off.gif');
    }
    if (current_pic != 0) {
        $('galleryslideimage' + current_pic.toString()).set('src', '/Content/images/gallery/frame_big_on.gif');
    }
}
// display text associated with current main image
function show_gallery_text() {
    var i = first_slide + current_pic - 1;
    $('galleryauthor').set('html', galleryimages[i].author);
    $('gallerylocation').set('html', galleryimages[i].location);
    $('gallerytitle').set('html', galleryimages[i].title);
    $('gallerytestimonial').set('html', galleryimages[i].text);
}
// display next testimonial
function show_testimonial(n) {
    next_test += n;
    if (next_test == 0) { next_test = testimonials.length - 1; }
    if (next_test == testimonials.length) { next_test = 1; }
    $('galleryquotetext').set('html', '<div style="overflow:auto;width:225px;height:74px;"><span class="gallerybigquote">' + testimonials[next_test].highlight + '</span><br /><span class="gallerysmallquote">' + testimonials[next_test].author + '</span><br/><span class="gallerysmalltestimonial">' + testimonials[next_test].text + "</span></div>");
}
// called from click on right arrow (advance slides)
function gallery_arrow_right() {
    first_slide += slides;
    load_gallery_complete();
}
// called from click on left arrow (back slides)
function gallery_arrow_left() {
    first_slide -= slides;
    load_gallery_complete();
}
// called from click on thumbnail
function open_image(o) {
    current_pic = o;
    if (galleryimages[o].title == "Submit Photo") { submit_photo(); }
    show_gallery_image();
    show_gallery_text();
    show_testimonial(1);
}
// image object
function galleryimage(id, thumburl, imgurl, author, location, title, text) {
    this.id = id;
    this.thumburl = thumburl;
    this.imgurl = imgurl;
    this.author = author;
    this.location = location;
    this.title = title;
    this.text = text;
}
// testimonial object
function testimonial(author, highlight, text) {
    this.author = author;
    this.highlight = highlight;
    this.text = text;
}
// submit photo
function submit_photo() {
    location.href = submit_url;
}