﻿function getThumbnail(aurl, sz) {
    var ret = aurl + "&sz=" + sz + "&fitcrop=y";
    return ret;
}

function getCroppedThumbnail(aurl, sz) {
    var ret = aurl + "&sz=" + sz + "&fitcrop=y";
    return ret;
}


function getSmallThumbnail(aurl, sz) {
    var ret = aurl + "&sz=" + sz + "&crop=y";
    return ret;
}

function centerItem(item, w, h) {
    var sh = window.pageYOffset;
    if (!sh) sh = document.documentElement.scrollTop;

    var cendiv = window.innerHeight;
    if (!cendiv) cendiv = document.documentElement.clientHeight;

    var t = (sh - 70) + ((cendiv / 2) - (h / 2));
    if (t < (sh - 70)) t = (sh - 70);
    var l = Math.round((980 / 2) - (w / 2));

    item.style.top = t + "px";
    item.style.left = l + "px";
}

function centerOnScreen(item) {
    var sh = window.top.pageYOffset;
    if (!sh) sh = window.top.document.documentElement.scrollTop;
    alert("sh = " + sh);
    var cendiv = window.innerHeight;
    if (!cendiv) cendiv = document.documentElement.clientHeight;
    var cenwid = window.innerWidth;
    if (!cenwid) cenwid = document.documentElement.clientWidth;
    var w = item.offsetWidth;
    var h = item.offsetHeight;
    var t = sh;
    if (sh > 200) sh -= 200;
    var l = ((cenwid / 2) - (w / 2)) - 100;
    item.style.top = t + "px";
    item.style.left = l + "px";
}



var restores = new Array();

function swapImage(itemid, newsrc)
{
    var im = document.getElementById(itemid);
    if (im) {
        restores[itemid] = im.src;
        im.src = newsrc;
    }
}

function restoreImage(itemid) {
    var im = document.getElementById(itemid);
    if (im) {
        im.src = restores[itemid];
    }
}



function centerThisImage(im, parentId) {

    var el = document.getElementById(parentId);
    if (el == null) return;
    var elh = el.offsetHeight;
    var elw = el.offsetWidth;
    var x = (elw / 2) - (im.width / 2);
    var y = (elh / 2) - (im.height / 2);
    im.style.top = y + "px";
    im.style.left = x + "px";
    fadeUp(parentId);
}

function displayThisImage(im, parentId) {
    var el = document.getElementById(parentId);
    if (el == null) return;
    fadeUp(parentId);
}

function hiresLink(artistid, userid) {
    if (userid > 0) {
        window.location.href = "/PortfolioTrad.aspx?artist_id=" + artistid;
    }
    else {
        MasterShowSignIn();
    }
}

function quoteLink(artistid, imageid) {
    var href = "/Quote.aspx";
    if (artistid){
        href += "?artist_id=" + artistid;
    }
    if (imageid) {
        if (artistid) href += "&";
        else href += "?";
        href += "image_id=" + imageid;
    }
    window.location.href = href;
}

function getPos(obj) {
    var ret = new Object();
    ret.x = findPosX(obj);
    ret.y = findPosY(obj);
    return ret;
}

function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (obj.x) {
        curleft += obj.x;
    }
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else if (obj.y) {
        curtop += obj.y;
    }
    return curtop;
}

function is_child_of(parent, child) {
    if (child != null) {
        while (child.parentNode) {
            if ((child = child.parentNode) == parent) {
                return true;
            }
        }
    }
    return false;
}

var gArtistId = 0;
var gClipId = 0;

function showFavsPopup(item, artistName, artistId) {
    var pos = getPos(item);
    var nameHolder = document.getElementById("favadd1");
    if (nameHolder) {
        nameHolder.innerHTML = "Add " + artistName;
    }
    var favArtistName = document.getElementById("favArtistName");
    if (favArtistName) {
        favArtistName.innerHTML = artistName;
    }
    var popmenu = document.getElementById("favpopup");
    pos.y -= 60;
    popmenu.style.left = pos.x + "px";
    popmenu.style.top = pos.y + "px";
    popmenu.style.display = "block";
    gArtistId = artistId;
}

function reduceFav(element, event) {
    var mt = null;
    if (event.toElement) {
        mt = event.toElement;
    } else if (event.relatedTarget) {
        mt = event.relatedTarget;
    }
    if (!is_child_of(element, mt) && element != mt) {
        var obj = document.getElementById("favpopup");
        if (obj) {
            obj.style.display = "none";

        }
    }
}

function reduceAdd(element, event) {
    var mt = null;
    if (event.toElement) {
        mt = event.toElement;
    } else if (event.relatedTarget) {
        mt = event.relatedTarget;
    }
    if (!is_child_of(element, mt) && element != mt) {
        var obj = document.getElementById("addpopup");
        if (obj) {
            obj.style.display = "none";

        }
    }
}

function addFav() {
    if (gArtistId != 0) {
        MasterAddFavorite(gArtistId);
    }
}

function showAddPopup(item, clipId) {
    var pos = getPos(item);
    pos.y -= 65;
    pos.x -= 90;
    var popmenu = document.getElementById("addpopup");
    popmenu.style.left = pos.x + "px";
    popmenu.style.top = pos.y + "px";
    popmenu.style.display = "block";
    gClipId = clipId;
}

function addscrap() {
    if (gClipId != 0) {
        MasterAddLightbox(gClipId);
    }
}

function hideEditor(itemName) {
    var editorDiv = document.getElementById(itemName);
    if (editorDiv) {
        editorDiv.style.width = "16px";
        editorDiv.style.height = "16px";
        editorDiv.style.border = "none";
        editorDiv.style.backgroundColor = "transparent";
    }
    return false;
}

function showEditor(itemName) {
    var editorDiv = document.getElementById(itemName);
    var sz = "auto";
    if (editorDiv.className.indexOf("small") > 0) sz = "100px";
    if (editorDiv) {
        editorDiv.style.width = "auto";
        editorDiv.style.height = sz;
        editorDiv.style.border = "1px solid #666666";
        editorDiv.style.backgroundColor = "rgba(255,255,255,0.9)";
    }
    return false;
}

function GetJson(url, parms, func) {
    parms = JSON.stringify(parms);
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: url,
        data: parms,
        dataType: "json",
        processData: false,
        success: func
    });
}

function GalleryToggle(imid, galid, func) {
    
    GetJson("/IllusService.svc/GalleryCheck", { galleryId: galid, imageId: imid }, function (data) {
        func(data.d);
    });
}

//GalleryLog(string desc, int galleryId, int imageId)
// this is for logging admin activity
function GalleryLog(d, galid, imid) {
    GetJson("/IllusService.svc/GalleryLog", { desc: d, galleryId: galid, imageId: imid }, function (data) {
        //alert("Logged " + d);
    });
}

// public void LogGalleryClick(int image_id, int gallery_id)
function LogGalleryClick(imid, galid) {
    GetJson("/IllusService.svc/LogGalleryClick", { image_id: imid, gallery_id: galid });
}

//public GalleryLogImage[] GetGalleryStats(int galleryId, int daysback)
function GetGalleryStats(galid, days, func) {
    GetJson("/IllusService.svc/GetGalleryStats", { galleryId: galid, daysback: days }, func);
}

//public GalleryLogImage[] GetGalleryStatsDates(int galleryId, string startDate, string endDate)
function GetGalleryStatsDates(galid, start, end, func) {
    GetJson("/IllusService.svc/GetGalleryStatsDates", { galleryId: galid, startDate: start, endDate: end }, func);
}

//public GalleryLogImage[] GetArtistGalleryStatsDates(int artistId, int galleryId, string startDate, string endDate)
function GetArtistGalleryStatsDates(artid, galid, start, end, func) {
    GetJson("/IllusService.svc/GetArtistGalleryStatsDates", { artistId: artid, galleryId: galid, startDate: start, endDate: end }, func);
}

//public GalleryLogImage[] GetPortfolioStats(int artistId, int daysback)
function GetPortfolioStats(artid, days, func) {
    GetJson("/IllusService.svc/GetPortfolioStats", { artistId: artid, daysback: days }, func);
}

//public GalleryLogImage[] GetPortfolioStatsDates(int artistId, string startDate, string endDate)
function GetPortfolioStatsDates(artid, start, end, func) {
    GetJson("/IllusService.svc/GetPortfolioStatsDates", { artistId: artid, startDate:start, endDate: end }, func);
}

//public void LogAllClick(int imageid, int artistid)
function LogAllClick(imid, artid) {
    GetJson("/IllusService.svc/LogAllClick", { imageid: imid, artistid: artid });
}

//public GalleryLogImage[] GetAllStats(int artistId, int daysback)
function GetAllStats(days, func) {
    GetJson("/IllusService.svc/GetAllStats", { daysback: days }, func);
}

//public GalleryLogImage[] GetAllStatsDates(string startDate, string endDate)
function GetAllStatsDates(start, end, func) {
    GetJson("/IllusService.svc/GetAllStatsDates", { startDate: start, endDate: end }, func);
}

//public JKeyword[] GetKeywordMatches(string startsWith)
function GetKeywordMatches(word, func) {
    GetJson("/IllusService.svc/GetKeywordMatches", { startsWith: word }, func);
}

//public JImageResult NewKeywordSearch(string keywordIds, int pagesize)
function NewKeywordSearch(ids, psize, func) {
    GetJson("/IllusService.svc/NewKeywordSearch", { keywordIds: ids, pagesize: psize }, func);
}

//public JImage[] SearchPage(string keywordIds, int page, int pagesize)
function SearchPage(ids, p, psize, func) {
    GetJson("/IllusService.svc/SearchPage", { keywordIds: ids, page: p, pagesize: psize }, func);
}

//public string UpdateBlog(int howmany)
function UpdateBlog(numItems, func) {
    GetJson("/IllusService.svc/UpdateBlog", { howmany: numItems }, func);
}

//public void FlushArtistList()
function FlushArtistList(func) {
    GetJson("/IllusService.svc/FlushArtistList", {}, func);
}

//public bool RemoveFromGallery(int galleryId, int imageId)
function RemoveFromGallery(galid, imid, func) {
    GetJson("/IllusService.svc/RemoveFromGallery", { galleryId: galid, imageId: imid }, func);
}   
