﻿//Photo
//SHARE, POST, WALLPAPER, SAVETODESKTOP, LINK, PRINT, COPY TO CLIPBOARD, BOOKMARK, CREATE

//Page
//SHARE, LINK, POST, BOOKMARK, PRINT

//Widget
//SHARE, POST, BOOKMARK, SAVETODESKTOP, LINK
var appendURL = "";
var boolFlashLoaded = false;
var pppFlash;
var imagePath;
var overrideLeft = 0;
var overrideTop = 0;

function onShareClick(MediaID, TypeID, BaseURL, Temp) {
    //alert("onShareClick");
    var url = siteRoot + "/PeelMedia/Share.aspx";
    appendURL = "";
    if (titleClientID != null && titleClientID.length > 0) {
        var title = document.getElementById(titleClientID);
        if (title != null && title.value.length > 0) {
            appendURL = appendURLParam() + "Title=" + title.value + "";
        }
    }
    if (Temp != null && Temp.length > 0) {
        appendURL = appendURLParam() + "MediaID=" + Temp + "";
    }
    if (TypeID != null && TypeID.length > 0) {
        appendURL = appendURLParam() + "TypeID=" + TypeID + "";
    }

    if (bookmarkURLClientID != null && bookmarkURLClientID.length > 0) {
        var bookmarkURL = document.getElementById(bookmarkURLClientID);
        if (bookmarkURL != null && bookmarkURL.value.length > 0) {
            appendURL = appendURLParam() + "LinkURL=" + bookmarkURL.value + "";
        }
    }
    if (TypeID == "1") {
        if (BaseURL.length > 0) {
            //alert(BaseURL);
            //appendURL = appendURLParam() + "LinkURL=" + BaseURL + "";
            appendURL = appendURLParam() + "ImageName=" + BaseURL + "";
        }
    }

    url = url + appendURL;
    //alert(appendURL);
    //alert(url);
    openPPPLightView(url, 285, 235, overrideTop, overrideLeft);
    closePPP();
}

function onPostClick(MediaID, TypeID, BaseURL, Temp) {
    //alert("onPostClick");
    var url = siteRoot + "/PeelMedia/Post.aspx";
    appendURL = "";
    //alert("postTitleClientID");    
    if (postTitleClientID != null && postTitleClientID.length > 0) {
        var title = document.getElementById(postTitleClientID);
        if (title != null && title.value.length > 0) {
            appendURL = appendURLParam() + "Title=" + title.value + "";
        }
    }
    //alert("postReferenceURIClientID"); 
    if (postReferenceURIClientID != null && postReferenceURIClientID.length > 0) {
        var referenceURI = document.getElementById(postReferenceURIClientID);
        if (referenceURI != null && referenceURI.value.length > 0) {
            appendURL = appendURLParam() + "ReferenceURI=" + referenceURI.value + "";
        }
    }
    //alert("postSynopsisClientID"); 
    if (postSynopsisClientID != null && postSynopsisClientID.length > 0) {
        var synopsis = document.getElementById(postSynopsisClientID);
        if (synopsis != null && synopsis.value.length > 0) {
            appendURL = appendURLParam() + "Synopsis=" + synopsis.value + "";
        }
    }
    //alert("postImagePathClientID"); 
    if (postImagePathClientID != null && postImagePathClientID.length > 0) {
        var imagePath = document.getElementById(postImagePathClientID);
        if (imagePath != null && imagePath.value.length > 0) {
            appendURL = appendURLParam() + "ImagePath=" + imagePath.value + "";
        }
    }
    //alert("url"); 
    url = url + appendURL;
    openPPPLightView(url, 280, 230, overrideTop, overrideLeft);
    closePPP();
}

function appendURLParam() {
    if (appendURL.length != 0)
        appendURL = appendURL + "&";
    else
        appendURL = appendURL + "?";
    //alert(appendURL);
    return appendURL;
}

function onWallpaperClick(MediaID, TypeID, BaseURL, Temp) {
    var browser = navigator.appName;
    if (browser == "Microsoft Internet Explorer") {
        var url = siteRoot + "/PeelMedia/Wallpaper.aspx?BaseURL=" + BaseURL;
        openPPPLightView(url, 575, 575, overrideTop, overrideLeft);
    }
    else {
        alert("Please save to desktop and then set as your background");
    }

    closePPP();
}

function onSaveClick(MediaID, TypeID, BaseURL, Temp) {
    //alert("MediaID: " + MediaID)
    //alert("TypeID: " + TypeID);
    //alert("BaseURL: " + BaseURL);
    var url = siteRoot + "/PeelMedia/SaveToDesktop.aspx";
    if (TypeID == "1" || TypeID == "2") { //Image or page
        //alert("onSaveClick");
        if (BaseURL != "") {
            appendURL = "";
            url = url + appendURLParam() + "BaseURL=" + BaseURL;
            //alert("url=" + url);
            openPPPLightView(url, 280, 235, overrideTop, overrideLeft);
        }
    }
    //alert("onSaveClick");
    closePPP();
}

function onLinkClick(MediaID, TypeID, BaseURL, Temp) {
    //alert("MediaID: " + MediaID)
    //alert("TypeID: " + TypeID);
    //alert("BaseURL: " + BaseURL);
    var url = siteRoot + "/PeelMedia/ImageSizeSelector.aspx";
    if (TypeID == "1" || TypeID == "2") { //Image
        if (BaseURL != "") {
            url = url + "?BaseURL=" + BaseURL;
            //alert("url: " + url);
            if (postReferenceURIClientID != null && postReferenceURIClientID.length > 0) {
                //alert("inside");
                var postReferenceURI = document.getElementById(postReferenceURIClientID);
                if (postReferenceURI != null && postReferenceURI.value.length > 0) {
                    url = url + "&PageURL=" + postReferenceURI.value
                }
            }
            if (trailerURLClientID != null && trailerURLClientID.length > 0) {
                //alert("inside");
                var trailerURL = document.getElementById(trailerURLClientID);
                if (trailerURL != null && trailerURL.value.length > 0) {
                    url = url + "&TrailerURL=" + trailerURL.value
                }

                var title = document.getElementById(postTitleClientID);
                if (title != null && title.value.length > 0) {
                    url = url + "&Title=" + title.value;
                }

                var synopsis = document.getElementById(postSynopsisClientID);
                if (synopsis != null && synopsis.value.length > 0) {
                    url = url + "&Description=" + synopsis.value;
                }
            }

            openPPPLightView(url, 285, 235, overrideTop, overrideLeft);
        }
    }
    //alert("onLinkClick");
    closePPP();
}

function onPrintClick(MediaID, TypeID, BaseURL, Temp) {
    //alert("onPrintClick");

    //    if (TypeID == "1") {
    //        if (BaseURL != "") {
    //            var url = siteRoot + "PeelMedia/Print.aspx";
    //            url = url + "?BaseURL=" + BaseURL;
    //            openPPPLightView(url, 575, 575);
    //        }        
    //    }
    //    else {
    window.print();
    //    }
    closePPP();
}

function onCopyClick(MediaID, TypeID, BaseURL, Temp) {
    var div = document.getElementById(MediaID);
    div.contentEditable = 'true';
    var controlRange;
    if (document.body.createControlRange) {
        controlRange = document.body.createControlRange();
        controlRange.addElement(div);
        controlRange.execCommand('Copy');
    }
    div.contentEditable = 'false';
    closePPP();
}

function onBookmarkClick(MediaID, TypeID, BaseURL, Temp) {
    var title = "";
    var url = "";
    if (bookmarkTitleClientID != null && bookmarkTitleClientID.length > 0) {
        var bookmarkTitle = document.getElementById(bookmarkTitleClientID);
        if (bookmarkTitle != null && bookmarkTitle.value.length > 0) {
            title = bookmarkTitle.value
        }
    }
    if (bookmarkURLClientID != null && bookmarkURLClientID.length > 0) {
        var bookmarkURL = document.getElementById(bookmarkURLClientID);
        if (bookmarkURL != null && bookmarkURL.value.length > 0) {
            url = bookmarkURL.value
        }
    }
    if (window.sidebar) { // Mozilla Firefox Bookmark
        window.sidebar.addPanel(title, url, "");
        //alert("Bookmark Added");
    }
    else if (window.external) {
        // IE Favorite
        window.external.AddFavorite(url, title);
        //alert("Bookmark Added");
    }
    else if (window.opera && window.print) {
        // Opera Hotlist
        //return true;
        alert("Your browser doesn't support this functionality");
    }
    closePPP();
}

function onFacebookClick(MediaID, TypeID, BaseURL, Temp) {

    var queryString = "";
    var title = "";
    var referenceURI = "";
    var objTitle = document.getElementById(postTitleClientID);
    if (objTitle != null && objTitle.value.length > 0) {
        title = objTitle.value;
    }
    var objReferenceURI = document.getElementById(postReferenceURIClientID);
    if (objReferenceURI != null && objReferenceURI.value.length > 0) {
        referenceURI = objReferenceURI.value;
    }
    var queryString = "u=" + referenceURI + "&t=" + title;

    window.open("http://www.facebook.com/sharer.php?" + queryString);
}

function onMyspaceClick(MediaID, TypeID, BaseURL, Temp) {
    //var queryString = "&t=" + hdnTitle.Value + "&c=" + hdnContent.Value + "&u=" + hdnURL.Value + "&r=" + hdnReturnURL.Value + "&l=" + hdnLocation.Value;
    var queryString = "";
    var title = "";
    var synopsis = "";
    var imageURL = "";
    var referenceURI = "";
    var content = "";
    var objTitle = document.getElementById(postTitleClientID);
    if (objTitle != null && objTitle.value.length > 0) {
        title = objTitle.value;
    }

    var objSynopsis = document.getElementById(postSynopsisClientID);
    if (objSynopsis != null && objSynopsis.value.length > 0) {
        synopsis = objSynopsis.value;
    }

    var objImgageURL = document.getElementById(postImagePathClientID);
    if (objImgageURL != null && objImgageURL.value.length > 0) {
        imageURL = objImgageURL.value;
    }

    var objReferenceURI = document.getElementById(postReferenceURIClientID);
    if (objReferenceURI != null && objReferenceURI.value.length > 0) {
        referenceURI = objReferenceURI.value;
    }

    content = utf8_decode(unescape(synopsis));

    if (imageURL != "") {
        var img = "<img src=&quot;" + imageURL + "&quot; border='0' />";
        content = img + "<br /><br />" + content;
    }

    if (content != "") {
        if (content.length > 60000)
            content = content.substring(0, 59999);
        var s = "'";
        var t = '"';
        content = content.replace(new RegExp(s, 'g'), "&rsquo;");
        //alert(content);
        content = content.replace(new RegExp(t, 'g'), '\"');
        content = escape(content);
        //alert(content);
    }

    queryString = "&t=" + escape(title) + "&c=" + content + "&u=" + referenceURI + "&r=&l=1"

    window.open("http://www.myspace.com/index.cfm?fuseaction=postto" + queryString);
}

// private method for UTF-8 decoding
function utf8_decode(utftext) {
    var string = "";
    var i = 0;
    var c = c1 = c2 = 0;

    while (i < utftext.length) {

        c = utftext.charCodeAt(i);

        if (c < 128) {
            string += String.fromCharCode(c);
            i++;
        }
        else if ((c > 191) && (c < 224)) {
            c2 = utftext.charCodeAt(i + 1);
            string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
            i += 2;
        }
        else {
            c2 = utftext.charCodeAt(i + 1);
            c3 = utftext.charCodeAt(i + 2);
            string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }

    }

    return string;
}

function onTwitterClick(MediaID, TypeID, BaseURL, Temp) {
    var queryString = "";
    var title = "";
    var referenceURI = "";
    var objTitle = document.getElementById(postTitleClientID);
    if (objTitle != null && objTitle.value.length > 0) {
        title = objTitle.value;
    }
    var objReferenceURI = document.getElementById(postReferenceURIClientID);
    if (objReferenceURI != null && objReferenceURI.value.length > 0) {
        referenceURI = objReferenceURI.value;
    }
    var queryString = "On fathomevents.com: " + title + " " + referenceURI;
    window.open("http://twitter.com/home?status=" + queryString);
}

function onCreateClick(MediaID, TypeID, BaseURL, Temp) {
    //alert("onCreateClick");
    //closePPP();
}

function onCloseClick(MediaID, TypeID, BaseURL, Temp) {
    //CallPeelClosed("true", "");
    closePPP();
}

function ReceiveServerData(rValue) {
    //Receive server's response of a string rValue, which is prepared in the server's function
}

function setVisible(imgID, typeID, baseURL, temp, x, y) {
    obj = document.getElementById('layer1');
    //alert(obj);
    if (obj != null) {
        var browser = navigator.appName;
        if (browser == "Microsoft Internet Explorer") {
            obj.style.display = "block";
            obj.style.left = x + "px";
            obj.style.top = y + "px";
            pppIsReady(imgID, typeID, baseURL, temp);

        }
        else {

            obj.style.display = "block";
            if (obj.style.display != "block") {
                obj.style.display = "block";
            }
            if (obj.style.display == "block") {
                pppIsReady(imgID, typeID, baseURL, temp);
            }

        }
        imagePath = baseURL;
     
    }
    else {
        //alert("here");
        window.setTimeout("setVisible('" + imgID + "','" + typeID + "','" + baseURL + "'," + temp + "," + x + "," + y + ")", 2000);
    }
    //alert(obj.style.display");
}

function setSingleImage() {
}

function closePPP() {
    var obj = document.getElementById('layer1');
    //    obj.style.left = "-3000px";
    //    obj.style.top = "0px";
    //obj.style.visibility = "hidden";

    var browser = navigator.appName;
    if (browser == "Microsoft Internet Explorer") {

        obj.style.left = "-3000px";
        obj.style.top = "0px";
    }
    else {
        obj.style.display = "none";
    }
}

//Calls from Flash
function pppIsReady(imgID, typeID, baseURL, temp) {
    pppFlash = document.getElementById("pppFlashID");
    try {
        //alert("test");
        //alert("imgID: " + imgID)
        //alert("typeID: " + typeID);
        //alert("baseURL: " + baseURL);
        //alert("pppFlash: " + pppFlash);        
        pppFlash.startAnimation(imgID, typeID, baseURL, temp);
        //alert("test2");          
    }
    catch (err) { window.setTimeout("pppIsReady('" + imgID + "','" + typeID + "','" + baseURL + "','" + temp + "')", 500); }  //alert("Error:" + err); pppIsReady(imgID, baseURL); }
    //}
    //
    boolFlashLoaded = true;
}

function setPPPDims(ImageID, PPPID) {
    //obj = document.getElementById(ImageID);
    //objImagePPP = document.getElementById(PPPID);
    //if (obj.offsetWidth > 0 && obj.offsetWidth < 110) {
    //     objImagePPP.style.right = "20px";
    //}
}
function openPPPLightView(url, width, height, top, left) {
    var objLightView = document.getElementById('divLightView');
    objLightView.style.visibility = "visible";
    if (url.toString().length > 0) {
        var objiframePPP = document.getElementById('iframePPP');
        objiframePPP.src = url;
        if (width > 0) {
            objiframePPP.width = width;
            objLightView.style.width = (width + 25) + "px";
        }
        else {
            objiframePPP.width = "575";
        }

        if (height > 0) {
            objiframePPP.height = height;
            //objLightView.style.height = height + 25;
        }
        else {
            objiframePPP.height = "575";
        }

        if (top > 0) {
            objLightView.style.top = top + "px";
        }
        if (left > 0) {
            objLightView.style.left = left + "px";
        }
    }
}

function closePPPLightView() {
    var objiframePPP = document.getElementById('iframePPP');
    if (objiframePPP != null) {
        objiframePPP.width = 0;
        objiframePPP.height = 0;
    }
}

function setSocialNetwork() {
    var objLightView = document.getElementById('divLightView');
    objLightView.style.width = "650px";
    objLightView.style.height = "600px";

    var objiframePPP = document.getElementById('iframePPP');
    objiframePPP.width = "625px";
    objiframePPP.height = "575px";

    if (objLightView.style.left != "0px") {
        objLightView.style.left = "330px";
    }
}   