﻿

function ChangeLanguage(val) {
    var s = window.location.href;
    s = s.replace("#", "");
    if (s.indexOf("?") >= 0) {
        if (s.indexOf("lang=") > 0) {
            s = s.replace("lang=de", "lang=" + val);
            s = s.replace("lang=en", "lang=" + val);
            window.location.href = s;
        }
        else {
            window.location.href = window.location.href + "&lang=" + val;
        }
    }
    else {
        window.location.href = window.location.href + "?lang=" + val;
    }
}


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 ParseJsonDate(dt) {
    dt = dt.replace(/\//g, '');
    return eval("new " + dt);
}

function SortImages(idlist, func) {
    GetJson("/Service.svc/SortImages", { ids: idlist }, function (data) {
        func(data.d);
    });
}

function AddKeywords(kids, iids, func) {
    GetJson("/Service.svc/AddKeywords", { keywordIds: kids, imageIds: iids }, function (data) {
        func(data.d);
    });
}

function RemoveKeywords(kids, iids, func) {
    GetJson("/Service.svc/RemoveKeywords", { keywordIds: kids, imageIds: iids }, function (data) {
        func(data.d);
    });
}

function AddGalleries(kids, iids, func) {
    GetJson("/Service.svc/AddGalleries", { galleryIds: kids, imageIds: iids }, function (data) {
        func(data.d);
    });
}

function GalleryLog(d, galid, imid) {
    GetJson("/IllusService.svc/GalleryLog", { desc: d, galleryId: galid, imageId: imid }, function (data) {
        alert("Logged " + d);
    });
}

function RemoveGalleries(kids, iids, func) {
    GetJson("/Service.svc/RemoveGalleries", { galleryIds: kids, imageIds: iids }, function (data) {
        func(data.d);
    });
}

function AddToLightbox(imid) {
    GetJson("/Service.svc/LightboxToggle", {imageid: imid}, function (data) {
        AddToLightboxCallback(data);
    });
}

function AddToLightboxFunction(imid, func) {
    GetJson("/Service.svc/LightboxToggle", { imageid: imid }, func);
}

//AdminImage GetAdminImage(int imageid)
function GetAdminImage(imid, func) {
    GetJson("/Service.svc/GetAdminImage", { imageid: imid }, func);
}

function GetJImage(imid, func) {
    GetJson("/Service.svc/GetJImage", { imageid: imid }, func);
}

function CheckLightbox() {
    GetJson("/Service.svc/GetLightbox", {}, function (data) {
        var images = data.d;
        if (images.length > 0) {
            $('#menu-lightbox').fadeIn('slow');
        }
        else {
            $('#menu-lightbox').fadeOut('slow');
        }
        PageLightboxCallback(images);
    });
}

function GetMailedLightbox(ref) {
    GetJson("/Service.svc/GetMailedLightbox", { id: ref }, function (data) {
        var images = data.d;
        PageLightboxCallback(images);
    });
}

//public bool ImageCheck(int imageId, bool value, string desc)

function ImageCheck(imageid, checked, descr, func) {

    GetJson("/Service.svc/ImageCheck", { imageId: imageid, value: checked, desc: descr }, function (data) {
        func(data.d);
    });
}

function GalleryToggle(imid, galid, func) {
    GetJson("/Service.svc/GalleryCheck", {galleryId: galid, imageId:imid}, function(data) {
        func(data.d);
    });
}

function DeleteImage(imid, func) {
    GetJson("/Service.svc/DeleteImage", { imageid: imid }, function () {
        func();
    });
}

function AddToLightboxCallback(data) {
    var images = data.d;
    
    if (images.length > 0) {
        $('#menu-lightbox').fadeIn('slow');
    }
    else {
        $('#menu-lightbox').fadeOut('slow');
    }
    if (PageLightboxCallback) {
        PageLightboxCallback(images);
    }
}

function LightboxRemoveAll() {
    GetJson("/Service.svc/LightboxRemoveAll", {}, function (data) {
        var images = data.d;
        if (images.length > 0) {
            $('#menu-lightbox').fadeIn('slow');
        }
        else {
            $('#menu-lightbox').fadeOut('slow');
        }
        PageLightboxCallback(images);
    });
}

function GoPortfolio(artid) {
    window.location.href = "/Portfolio.aspx?artist_id=" + artid;
}

function getUrlParameter(name) {
    return decodeURI(
        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, null])[1]
    );
}

function MessageBox(message) {
    $('#messagetext').html(message);
    $('#messagebox').slideDown('fast');
}

function ConfirmBox(message, func) {
    $('#confirmtext').html(message);
    $('#confirmbutton').click(function () {
        $('#confirmbox').slideUp();
        func.call();
    });
    $('#confirmbox').slideDown('fast');
}


function lbContains(lb, id) {

    for (var i in lb) {
        if (lb[i].Id == id) return true;
    }
    return false;
}

/*
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" id="movie_name" align="middle">
    <param name="movie" value="movie_name.swf"/>
    <!--[if !IE]>-->
    <object type="application/x-shockwave-flash" data="movie_name.swf" width="550" height="400">
        <param name="movie" value="movie_name.swf"/>
    <!--<![endif]-->
        <a href="http://www.adobe.com/go/getflash">
            <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player"/>
        </a>
    <!--[if !IE]>-->
    </object>
    <!--<![endif]-->
</object>
*/

function getFlvEmbedCode(moviename, width, height) {
    var str = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="' + width + '" height="' + height + '" id="movie_name" wmode="opaque" align="middle">' +
        '<param name="movie" value="/flash/flvplayer.swf?swf=/media/' + moviename + '"/>' +
        '<!--[if !IE]>-->' +
        '<object type="application/x-shockwave-flash" data="/flash/flvplayer.swf?swf=/media/' + moviename + '" width="' + width + '" height="' + height + '" wmode="opaque">' +
        '<param name="movie" value="/flash/flvplayer.swf?swf=/media/' + moviename + '"/>' +
        '<!--<![endif]-->' +
        '<a href="http://www.adobe.com/go/getflash">' +
        '<img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player"/>' +
        '</a>' +
        '<!--[if !IE]>-->' +
        '</object>' +
        '<!--[if !IE]>-->' +
        '</object>';
    return str;

}

function getMovieEmbedCode(location, ext) {
    var buf = "";
    if (ext == ".swf") {
        buf = "<OBJECT classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' wmode='opaque' align='middle'";
        buf += " codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0' WIDTH='732' HEIGHT='500' id=OBJECT1>";
        buf += "<PARAM NAME=movie VALUE='/media/" + location + "'> <PARAM NAME=quality VALUE=high> <PARAM NAME=bgcolor VALUE=#FFFFFF> ";
        buf += "	<EMBED src='/media/" + location + "' wmode='opaque' quality='high' bgcolor='#FFFFFF'  WIDTH='732' HEIGHT='500' TYPE='application/x-shockwave-flash' PLUGINSPAGE='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash'></EMBED>";
        buf += "</OBJECT>";
    }
    else if ((ext == ".mov") || (ext == ".mp4")) {
        buf = "<OBJECT CLASSID='clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B' WIDTH='732' HEIGHT='500' CODEBASE='http://www.apple.com/qtactivex/qtplugin.cab' >";
        buf += "	<PARAM NAME='SRC' VALUE='/media/" + location + "'>";
        buf += " <PARAM NAME='AUTOPLAY' VALUE='TRUE'>";
        buf += "	<PARAM NAME='CONTROLLER' VALUE='true'>";
        buf += "	<PARAM NAME='target' VALUE='myself'>";
        buf += "	<PARAM NAME='LOOP' VALUE='false'>";
        buf += "	<EMBED WIDTH='732' SRC='/media/" + location + "' ";
        buf += "	 HEIGHT='500' PLUGINSPAGE='http://www.apple.com/quicktime/download/index.html' ";
        buf += "	 AUTOPLAY='TRUE' LOOP='false' ";
        buf += "	 CONTROLLER='true' CACHE='true' >";
        buf += "</OBJECT>";
    }
    else if (ext == ".dcr") {
        buf = "<object classid='clsid:166B1BCA-3F9C-11CF-8075-444553540000' " +
				"codebase='http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=8,0,0,0' " +
				"ID=test width='732' height='500'> " +
				"<param name=src value='/media/" + location + "'> " +
				"<param name=swRemote value=\"swSaveEnabled='true' swVolume='true' swRestart='true' swPausePlay='true' swFastForward='true' swContextMenu='true' \"> " +
				"<param name=swStretchStyle value=none> " +
				"<PARAM NAME=bgColor VALUE=#FFFFFF> " +
				"<embed src='/media/" + location + "' bgColor=#FFFFFF  width='732' height='500' swRemote=\"swSaveEnabled='true' swVolume='true' swRestart='true' swPausePlay='true' swFastForward='true' swContextMenu='true' \" swStretchStyle=none " +
				"type='application/x-director' pluginspage='http://www.macromedia.com/shockwave/download/'></embed> " +
				"</object>";
    }
    
    return buf;
}

function getType(im) {
    var fname = im.FullSrc.toLowerCase();
    var wh = fname.indexOf(".");
    return fname.substring(wh);
}
