﻿var InTesting = false;
var CanSwap = true;

var CanModifyPhotos = false;

var bigImageId;

function ConfigurePhotoLister_Admin() {

Debug(InTesting, "CanModifyPhotos " + CanModifyPhotos);
    if (!CanModifyPhotos)
        $get("ModifyPhotos").style.display = "none";
    else
        $get("ModifyPhotos").style.display = "block";    
    
    CanModifyPhotos = !CanModifyPhotos;
}

function loadPhotos() {  

   var propName = GetCurrentPropertyName();
   
   GetWebRequest('images/selectedpropertyphotos.ashx', 'SortList', "&propName=" + propName );
}

function GetCurrentPropertyName() {
    /// <summary></summary>
    /// <param name=""></param>
    /// <returns></returns>    
    
    return GetPathArray()[6]; // our first item in our array is empty because of the way split works with pathname
}


function GetPathArray() {
var currentPathName = window.location.pathname;    
    var pathObj = currentPathName.split("/");
   
    if ( InTesting) {
        for (var i = 0; i < pathObj.length; i++)
            Debug(InTesting, 'item: ' +  pathObj[i]);
    }
        
    if (pathObj == null) alert("Error, could not parse the current URL");
    if (pathObj.length < 1) alert("Error, could not parse the current URL");
    
    return pathObj;
}


function SetList() {
/// <summary>
/// This function creates our Sortable list based on the DOM object with ID = "SortList".
/// </summary>
/// <remarks>
/// </remarks
/// <returns>nothing</returns>

    Sortable.create("SortList",
         {tag:'div', overlap:'horizontal', containment:['SortList'], constraint:false, ghosting: false, dropOnEmpty: false})
   
   $get("EditPhotoList").style.display = "none";
   $get("SavePhotoList").style.display = "block";   
   $get("SortList").style.backgroundColor = "#FFFF00";
   
   CanSwap = false;
}

function SerializeSortList() {
/// <summary>
/// This function will call into our Sortable object and call its serialize function.  It will
/// then call into our KP.Web.Components.Service.PhotoService() to save our new list to the database
/// </summary>
/// <remarks>
/// This function will call Succeeded if the Webserivce succeeds.  If it fails, it will all FailedCallback function.
/// </remarks
/// <returns>nothing</returns>

    var SaveButton = $get("SavePhotoList");
    
    SaveButton.enabled = false;

    var sortListSerialized = Sortable.serialize("SortList", {});
    var photoService = new KP.Web.Components.Service.PhotoService();
    Debug(InTesting, 'SorList Serialized Request: ' + sortListSerialized);
    var request = new KP.Web.Components.Service.PhotoHelperRequestMessage();
    
    request.PropertyName = GetCurrentPropertyName();
    request.SortList = sortListSerialized;
    
    photoService.DeserializePhotoOrder(request, Succeeded, FailedCallback);
}

function Succeeded(result, userContext, methodName) { 
/// <summary>
/// This function will get called when our service successfully ran.  
/// </summary>
/// <param name="result">Of type PhotoHelperMessage</param>
/// <param name="userContext">the context of our current user</param>
/// <param name="methodName">name of calling method</param>
/// <returns>nothing</returns>

    Debug(InTesting, result.Description); // show message of success.
    Sortable.destroy( $("SortList") ); // clean up.
    $get("EditPhotoList").style.display = "block";
    $get("SavePhotoList").style.display = "none";
    $get("SortList").style.backgroundColor = "#FFFFFF";
    CanSwap = true;
    
     var SaveButton = $get("SavePhotoList");
    SaveButton.enabled = true;
}

function GetWebRequest(getPage, HTMLtarget, attributes) {
/// <summary>
/// This function will create a Sys.Net.WebRequest() object and do a HTTP GET
/// </summary>
/// <param name="getPage">the page that we are going to have our HTTP GET use</param>
/// <param name="HTMLtarget">The HTML DOM object that we are going to update with our results</param>
/// <returns>nothing</returns>

    Debug(InTesting, 'Calling GetWebRequest');    

    var random = Math.floor(Math.random()*50000);

    displayElement = $get(HTMLtarget);
    var wRequest = new Sys.Net.WebRequest();
    wRequest.set_url(getPage + "?" + random + attributes);
    wRequest.set_httpVerb("GET");    
    wRequest.set_userContext("user's context");
    wRequest.add_completed(OnWebRequestCompleted);
    wRequest.invoke();
}

function OnWebRequestCompleted(executor, eventArgs) {
    Debug(InTesting, 'OnWebRequestCompleted called');
    if (executor.get_responseAvailable()) {
           Debug(InTesting, 'displayElement.innerHTML = ' + displayElement.innerHTML);
           displayElement.innerHTML = "";              
           Debug(InTesting, 'displayElement.innerHTML after clean = ' + displayElement.innerHTML);
           var data = executor.get_responseData();
           Debug(InTesting, 'executor.get_responseData() = ' + data);
           displayElement.innerHTML = data;     
           Debug(InTesting, 'displayElement.innerHTML after new data = ' + displayElement.innerHTML);
    } else {
       if (executor.get_timedOut()) {
            Debug(InTesting, "Timed Out");
       } else {
            if (executor.get_aborted())
                Debug(InTesting, "Aborted");
       }
    }
}


function FailedCallback(error)
{
/// <summary>
/// This is the callback function invoked if the Web service failed.
/// </summary>
/// <param name="error">It accepts the error object as a parameter.</param>
/// <returns>nothing</returns>
    
    // Display the error.   
    alert("Service Error: " + error.get_message());
}


function SetDisplayImage(imgPath) {
/// <summary>
/// This function looks for the object by abosolute path and changes the src attribute.
/// </summary>
/// <param name="imgPath">The path to the image that is going to be swapped</param>
/// <returns>nothing</returns>

   // var bigImg = $get('ctl00_ctl00_BodyRegion_BigPictureRegion_ctl00_ctl00_imgPropImg');
   var bigImg = $get(bigImageId);
   
    if (bigImg == null)
        alert('bigImage could not be found');
    else if (CanSwap)
        bigImg.src = imgPath;
}


function RobTest() {
/// <summary>
/// This is a test function to use simply for the 
/// </summary>
/// <param name="imgPath">The path to the image that is going to be swapped</param>
/// <returns>nothing</returns>
    alert('Congratulations, the test worked!');
}


if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();