﻿//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 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;
        //alert(x);
        if (x > 0) {
            var browser = navigator.appName;
            var isIE6 = navigator.userAgent.toLowerCase().indexOf('msie 6') != -1;
            if (browser == "Microsoft Internet Explorer" && isIE6)
                obj.style.left = x-300 + "px";
            else   
                obj.style.left = x + "px";
            
            obj.style.top = y + "px";
        }

        imagePath = baseURL;
        if (obj.style.visibility != 'visible') {
            obj.style.visibility = 'visible';
        }        
        if (obj.style.visibility == 'visible') {
            //alert("here2");
            //CallPeelClosed("false", "");
            pppIsReady(imgID, typeID, baseURL, temp);
        }
    }
    else {
        //alert("here");
        window.setTimeout("setVisible('" + imgID + "','" + typeID + "','" + baseURL + "'," + temp + "," + x + "," + y + ")", 2000);
    }
    
}

function setSingleImage() {
}

function closePPP() {
    var obj = document.getElementById('layer1');
    obj.style.left = "-3000px";
    obj.style.top = "0px";
    //obj.style.visibility = "hidden";
}

//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";
    }
}   