﻿
//--------------Slide show --------

var img1 = new Image();
img1.src = "/images/slideshowpause.gif";
var slbg;
//var sideds = new Spry.Data.XMLDataSet("/ServiceFarm/PhotoView.asmx/photoStream2", "/PhotoStream/Paging/Photo", { useCache: false, method: "POST", postData: "pID=" + document.getElementById('firstimg').value + "&psize=100", headers: { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"} });
var sideds = new Spry.Data.XMLDataSet();
sideds.addObserver(sidedscallback);

function initslideds(alID) {
    if (alID == 0) {
        sideds.setURL("/ServiceFarm/PhotoView.asmx/photoStream2", { method: "POST", postData: "pID=" + document.getElementById('firstimg').value + "&psize=100", headers: { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"} })
    }
    else {
        sideds.setURL("/ServiceFarm/PhotoView.asmx/photoStream_album", { method: "POST", postData: "albumID=" + alID + "&psize=500", headers: { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"} })
    }
    sideds.setXPath("/PhotoStream/Paging/Photo");
    sideds.loadData();
}



function sidedscallback(notificationType, notifier, data) {
    if (notificationType == "onPostLoad") {
        // alert(sideds.getRowCount());
        document.getElementById('sld_loading').style.display = "none";
        slide_toggle();
    }
}

function slide_toggle() { 

    if (!onslideshow) {
        if (sideds.getDataWasLoaded()) {
            
            slideshow();
            window.scroll(0, 0);
            Spry.$$("html").setStyle("overflow:hidden;");
            onslideshow = true;
           // initslide();
            document.getElementById('slide_pn').style.display = "block";
            document.getElementById('imgloader').style.display = "inline";
            start_play();

        }
      }
    else {
        onslideshow = false;
        Spry.$$("html").setStyle("overflow:auto;");
        document.getElementById('slide_pn').style.display = "none";
        start_play();

    }

}  


function slide(alID) {
   
            document.getElementById('sld_loading').style.display = "block";
            initslideds(alID);
 
}
function initslide() {

    document.getElementById('desc_bound').style.top = innerSize('h') - 100 + "px";
    if (!onslideshow) {
        document.getElementById('slide_pn').style.display = 'none';
        document.getElementById('photo').style.display = "none";

    }
    else {

        sideds.setCurrentRow(0);
        var row = sideds.getRowByRowNumber(0);

        setimg(processlink(row.URL), row.NAME);

    }
}
function setimg(imgurl, desc) {

    if ((sideds.getCurrentRowNumber()) == 0)
        document.getElementById('prev_btn').style.display = 'none';
    else
        document.getElementById('prev_btn').style.display = 'block';


    if ((sideds.getCurrentRowNumber() + 1) == sideds.getRowCount()) document.getElementById('next_btn').style.display = 'none'; else document.getElementById('next_btn').style.display = 'block';

    document.getElementById('imgpreload').src = imgurl;
    document.getElementById('lb_desc').innerHTML = desc;


}
function next_img() {

    KillSlideShowTimer();
    rebuiltlayout();
    var current = sideds.getCurrentRowNumber() + 1;
    if ((current + 1) <= sideds.getRowCount()) {

        document.getElementById('imgloader').style.display = "inline";
        var row = sideds.getRowByRowNumber(current);

        sideds.setCurrentRow(current);
        setimg(processlink(row.URL), row.NAME);
    }


}

function prev_img() {

    KillSlideShowTimer();
    var current = sideds.getCurrentRowNumber() - 1;
    if (current >= 0) {
        document.getElementById('imgloader').style.display = "inline";
        var row = sideds.getRowByRowNumber(current);

        sideds.setCurrentRow(current);
        setimg(processlink(row.URL), row.NAME);
    }


}


function processlink(url) {
    //  url=url.replace('&amp;','&');
    url = url.replace(/\&amp;/g, '&');
    var maxsize = url.substr(url.length - 1, 1);
    var actions = url.substr(url.indexOf('actions='), 9);
    //alert(maxsize);
    switch (parseInt(maxsize) * 1) {
        case 1:
            return url.replace(actions, 'actions=o')
            break;
        case 2:
            return url.replace(actions, 'actions=o')
            break;
        case 3:

            return url.replace(actions, 'actions=o')
            break;
        case 4:
            return url.replace(actions, 'actions=o')
            break;
        case 5:

            return url.replace(actions, 'actions=b')

            break;
        default:
            return url;
    }
}
var onslideshow = false;
function slideshow() {
    //slbg = new Spry.Effect.Fade('slide_pn',{from:"0", to:"500", duration:1000,toggle:true,finish:initslide,setup:function(){if (slbg.direction == Spry.forwards) {document.getElementById('slide_pn').style.display='block';document.getElementById('imgloader').style.display="inline";}}});
    SetMouseShowTimer();
    //  sideds.loadData();
    $(document).mousemove(SetMouseShowTimer);
    document.getElementById('controlpn').style.top = innerSize('h') - 60 + "px";
}


function imgloaded() {
    var imgpre = document.getElementById('imgpreload');
    var img = new Image();
    img.src = imgpre.src;
    var height = img.height;
    var width = img.width;
    document.getElementById('imgloader').style.display = "none";
    if (parseInt(height) > parseInt(innerSize('h'))) {
        var newheight = (innerSize('h') - 20);
        var newwidth = Math.round((newheight / height) * width);

        SetMainImage(imgpre.src, newwidth, newheight);
    }
    else SetMainImage(imgpre.src, width, height); //photo.height= height;

}
function hide_desc() {
    var linkdesc = document.getElementById('desc_control');
    var desc = document.getElementById('lb_desc');
    if (desc.style.display == 'none') {
        desc.style.display = 'inline';
        linkdesc.innerHTML = 'Ẩn lời tựa';
    }
    else {
        desc.style.display = 'none';
        linkdesc.innerHTML = 'Hiện lời tựa';
    }
}

function start_play() {
    var btn = document.getElementById('pause_btn');
    if (btn.src.indexOf('slideshowpause') != -1) {
        btn.src = btn.src.replace('slideshowpause', 'slideshowplay');
        StopSlideShow();
    }
    else {
        btn.src = btn.src.replace('slideshowplay', 'slideshowpause');
        StartSlideShow(false);
    }



}

function rebuiltlayout() {
    var imgpre = document.getElementById('imgpreload');
    var img = new Image();
    img.src = imgpre.src;
    var height = img.height;
    var width = img.width;
    var photo = document.getElementById('photo');
    if (height > innerSize('h')) {
        photo.height = innerSize('h') - 20;
        var newheight = (innerSize('h') - 20);
        var newwidth = Math.round((newheight / height) * width);
        document.getElementById('img_bg').style.width = newwidth + 'px';
        document.getElementById('img_bg').style.height = newheight + 'px';

    }
    else photo.height = height;
    document.getElementById('controlpn').style.top = innerSize('h') - 60 + "px";
    document.getElementById('desc_bound').style.top = innerSize('h') - 100 + "px";
}
function alertmsg(e) {

    objEvent = e || window.event;

    // Get the keycode
    var keycode = objEvent.keyCode;
    var escapeKey = objEvent.DOM_VK_ESCAPE /* moz */ || 27;

    // Get key
    //	var key = String.fromCharCode(keycode).toLowerCase();

    //	alert(keycode);

    if (keycode == 39 && onslideshow) next_img();
    if (keycode == 37 && onslideshow) prev_img();
    if (keycode == 72 && onslideshow) hide_desc();
    //if (keycode == 83) slide();
    if (keycode == 27 && onslideshow) slide_toggle();
}

function inc_time() {
    gSlideShowInterval = gSlideShowInterval + 1000;
    document.getElementById('lb_min').innerHTML = gSlideShowInterval / 1000;

}

function des_time() {
    if (gSlideShowInterval > 1000) {
        gSlideShowInterval = gSlideShowInterval - 1000;
        document.getElementById('lb_min').innerHTML = gSlideShowInterval / 1000;
    }

}

//gallery
// JavaScript Document
var gBehaviorsArray = [];
function SetMainImage(imgPath, width, height) {
    var img = document.getElementById("photo");
    if (!img)
        return;

    gBehaviorsArray["mainImage"] = new Spry.Effect.Opacity(img, Spry.Effect.getOpacity(img), 0, { duration: 400,
        finish: function() {
            gBehaviorsArray["mainImage"] = new Spry.Effect.Size(img.parentNode, Spry.Effect.getDimensions(img.parentNode), { width: width, height: height, units: "px" }, { duration: 400,
                finish: function() {
                    // Use an image loader to make sure we only fade in the new image after
                    // it is completely loaded.
                    gImageLoader = new Image();
                    gImageLoader.onload = function() {
                        img.height = height;
                        //	img.width = width;
                        img.src = gImageLoader.src;
                        gImageLoader = null;
                        gBehaviorsArray["mainImage"] = new Spry.Effect.Opacity(img, 0, 1, { duration: 400,
                            finish: function() {
                                gBehaviorsArray["mainImage"] = null;

                                // Our new image is fully visible now. Remove any opacity related
                                // style properties on the img to workaround the IE bug that creates
                                // white dots/holes in the images. Removing the properties forces
                                // IE to re-render the image correctly.

                                img.style.opacity = "";
                                img.style.filter = "";
                                if (gSlideShowOn) SetSlideShowTimer();



                            } 
                        });
                        gBehaviorsArray["mainImage"].start();
                    };
                    gImageLoader.src = imgPath;
                }
            });
            gBehaviorsArray["mainImage"].start();
        }
    });
    gBehaviorsArray["mainImage"].start();
}

// - -----------
var gSlideShowInterval;
if (gSlideShowInterval == undefined)
    gSlideShowInterval = 3000; // msecs between images.

var gAutoStartSlideShow;
if (gAutoStartSlideShow == undefined)
    gAutoStartSlideShow = true;

var gBehaviorsArray = [];
var gSlideShowOn = false;
var gSlideShowTimer = null;
var gImageLoader = null;


function SetSlideShowTimer() {
    KillSlideShowTimer();
    gSlideShowTimer = setTimeout(function() { gSlideShowTimer = null; next_img(); }, gSlideShowInterval);
}

function KillSlideShowTimer() {
    if (gSlideShowTimer)
        clearTimeout(gSlideShowTimer);
    gSlideShowTimer = null;
}

// Start the slide show that runs forwards through all
// the rows in dsPhotos.

function StartSlideShow(skipTimer) {
    gSlideShowOn = true;
    if (!skipTimer)
        SetSlideShowTimer();
    var playLabel = document.getElementById("playLabel");
    if (playLabel)
        playLabel.firstChild.data = "Pause";
}

// Kill any slide show that is currently running.

function StopSlideShow() {
    gSlideShowOn = false;
    KillSlideShowTimer();
    var playLabel = document.getElementById("playLabel");
    if (playLabel)
        playLabel.firstChild.data = "Play";
}
// mouse move
var MouseTimer = null;
function SetMouseShowTimer() {
    document.getElementById('controlpn').style.display = "block";
    KillMouseTimer();
    MouseTimer = setTimeout(function() { MouseTimer = null; document.getElementById('controlpn').style.display = "none" }, 3000);
}

function KillMouseTimer() {
    if (MouseTimer)
        clearTimeout(MouseTimer);
    MouseTimer = null;
}


$(window).resize(rebuiltlayout);


$(document).keydown(alertmsg);

