﻿var OverlayName;
var SceneCount;

$(function() {

    OverlayName = $(".GroupRoomScene img").attr("alt");
    setOverlay();

    SceneCount = $(".scenes li").size();

    // handle switch of room scene with click handler on room scene link
    $("ul.scenes .scene a").click(function(e) {
        e.preventDefault();

        OverlayName = $(this).attr("title");

        // update the sharing information
        var newPath = $(this).attr("href");
        ShareURL = window.location.protocol + "//" + window.location.host + newPath;
        ShareTitle = document.title;

        // get room scene image and fade out main scene
        var img = $(this).html();
        $(".GroupRoomScene").fadeOut(500, function() {
            // replace the classname on the image for imagegen
            img = img.replace("class=thumbSceneL", "class=mainGroupL").replace("class=thumbSceneP", "class=mainGroupP");
            // reinsert the updated image and fadein
            $(".GroupRoomScene").html(img);
            $(".GroupRoomScene").fadeIn(500);
            setOverlay();
        });

        // update hires download link
        var imgName = img.split("/services/ImageGen.ashx?image=")[1].split("&")[0];
        imgName = "/images/hires/" + imgName.replace("jpg","tif");
        $(".securedContent a").attr("title", OverlayName).attr("href",imgName);
    });

    // add click handler for navigation inside overlay
    $(".OverlayNav ul.nav li a").click(function(e) {
        e.preventDefault();
        var iChild = $(this).html();
        var imgSrc = $(".scenes li").eq(iChild - 1).children(".scene").children("a").children("img").attr("src");
        imgSrc = imgSrc.replace("class=thumbSceneL", "class=mainGroupL").replace("class=thumbSceneP", "class=mainGroupP");
        $("#overlay img").attr("src", imgSrc);
        $("#overlay .details .titlenav .title").html(OverlayName);
        getSceneDescriptionByImgSrc(imgSrc);
    });

    // initially hide overlay markup
    $("#overlay").hide();
    // apply apple effect to overlay
    $(".GroupRoomScene img[rel]").overlay({ effect: 'apple' });

    $(".prod img[rel]").overlay({ effect: 'apple' });

});

function setOverlay() {
    var imgSrc = $(".GroupRoomScene img").attr("src");
    $("#overlay img").attr("src", imgSrc);
    $("#overlay .details .titlenav .title").html(OverlayName);
    $(".GroupRoomScene").find("img").attr("rel", "#overlay");
    $(".GroupRoomScene img[rel]").overlay({ effect: 'apple' });
    //$("#overlay .details .description").html('scene description');
    getSceneDescriptionByImgSrc(imgSrc);
}

var addthis_config = {
    ui_cobrand: "Southern Motion",
    ui_header_color: "#fff",
    ui_header_background: "#C21500"

    // ui_click: true /* normally would disable mouseover behavior */
}

// ajax call to get roomscene record object
function getSceneDescriptionByImgSrc(imgsrc) {
    $.ajax({
        type: "POST",
        url: "/services/Scenes.asmx/getSceneDescriptionByImgSrc",
        data: "{ImageSrc: '" + encodeURI(imgsrc) + "' }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result) {
            RoomSceneDescriptionFetched(result);
        },
        error: AjaxFailed
    });
};

function RoomSceneDescriptionFetched(result) {
    var oRoomSceneDescription = result.d;
    $("#overlay .details .description").html(oRoomSceneDescription.Description);

}

// alert user if ajax request failed
function AjaxFailed(result) {
    alert(result.status + ' ' + result.statusText);
}  

