﻿// The next line adds a reference to an intellisense file for JQuery
/// <reference path="jquery-1.3.2-vsdoc.js" />

// Globals
var slideLeft = false;

function SlideClick(id, url) {
    var seq = $('#slideSeq' + id).val();
    if (seq > 1) location = url + '?Image' + seq;
    else location = url;
}

function SlideNext(e, id) {
    try {
        SlideTray(e, id, false);
    }
    catch (ex) {
        HandleException(ex);
    }        
}

function SlidePrev(e, id) {
    try {
        SlideTray(e, id, true);
    }
    catch (ex) {
        HandleException(ex);
    }
}

var currentId = 0;

function SlideTray(e, id, prev) {
    slideLeft = !prev;
    if (id == null) return;
    currentId = id;

    var seq = jQuery("#slideSeq" + id).val();

    if (slideLeft) seq++;
    else seq--;

    if (seq < 1) seq = jQuery("#imageCount" + id).val();
    else if (seq > jQuery("#imageCount" + id).val()) seq = 1;

    jQuery("#slideSeq" + id).val(seq);

    // DEBUG
    var imgSrc = "";
    if (document.location.toString().search("localhost") >= 0) imgSrc = "http://localhost:4885/Listings/NextImage/" + id + "?size=225&ratio=1&seq=" + seq;
    else imgSrc = "http://www.eatoutadmin.com/Listings/NextImage/" + id + "?size=225&ratio=1&seq=" + seq;
    
    // load tray and wait for image to load
    jQuery("#wait" + currentId).show();

    if (slideLeft) {
        jQuery("#slideR" + id).html("<img class='slide' src='" + imgSrc + "' alt='' title='' />");
        jQuery("#slideR"  + id + " > img")[0].onload = SlideTray2;
    }
    else {
        jQuery("#slideL" + id).html("<img class='slide' src='" + imgSrc + "' alt='' title='' />");
        jQuery("#slideL"  + id + " > img")[0].onload = SlideTray2;
    }    
}

function SlideTray2() {
    try {
        if (slideLeft) jQuery("#slideTray" + currentId).animate({ left: "-450px" }, 500, "swing", SlideTray3);
        else jQuery("#slideTray" + currentId).animate({ left: "0px" }, 500, "swing", SlideTray3);
    }
    catch (ex) {
        HandleException(ex);
    }
}

function SlideTray3() {
    // copy the slide to the center slide and reset
    try {
        var img = jQuery("#slideL" + currentId + " > img")[0];
        if (img != null) img.onload = null;
        img = jQuery("#slideR" + currentId + " > img")[0];
        if (img != null) img.onload = null;

        if (slideLeft) jQuery("#slide" + currentId).html(jQuery("#slideR" + currentId).html());
        else jQuery("#slide" + currentId).html(jQuery("#slideL" + currentId).html());
        jQuery("#slideTray" + currentId)[0].style.left = "-225px";
    
        jQuery("#wait" + currentId).hide();
    }
    catch (ex) {
        HandleException(ex);
    }
}

function HandleException(ex) {
}

// Event handlers
jQuery.ready = body_onready;
function body_onready() {
    StarListings();
}

