﻿
function gallery_navigationOnChange() {
    var optionvalue = $("#gallery_SelectNavigation option:selected").val();
    window.location = optionvalue;
}

function addToFavorites(photoID) {

    $("#gallery_AddToFavorites").html("please wait...");
    $("#gallery_AddToFavorites").attr("href", "javascript:;");
    $("#gallery_AddToFavorites").removeAttr("onclick");
    $("#gallery_AddToFavorites").unbind('click', addToFavorites);

    $.ajax({
        type: "POST",
        url: "/_resources/asmx/favorites.asmx/InsertFavorite",
        data: "{'PhotoID': '" + photoID.toString() + "' }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {

            var s = (data.d == "1");

            if (s) {
                $("#gallery_AddToFavorites").html("Remove from favorites");
                $("#gallery_AddToFavorites").attr("href", "javascript:deleteFavorite(" + photoID.toString() + ");");
    
               
            }
            else {

                alert("There was a problem and photo was not added to your favorites.");
            
            }

        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });

}

function deleteFavorite(photoID) {


    $("#gallery_AddToFavorites").html("please wait...");
    $("#gallery_AddToFavorites").attr("href", "javascript:;");

    $.ajax({
        type: "POST",
        url: "/_resources/asmx/favorites.asmx/DeleteFavorite",
        data: "{'PhotoID': '" + photoID.toString() + "' }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {

            var s = (data.d == "1");

            if (s) {
                $("#gallery_AddToFavorites").html("Add this to favorite");
                $("#gallery_AddToFavorites").attr("href", "javascript:addToFavorites(" + photoID.toString() + ");");
    
            }
            else {

                alert("There was a problem and photo was not removed from your favorites.");

            }

        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });

}



function likePhoto(anchor, photoID) {
    $.ajax({
        type: "POST",
        url: "/_resources/asmx/like.asmx/InsertLike",
        data: "{'PhotoID': '" + photoID.toString() + "' }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {

            var args = data.d.toString().split(',');

            var newhtml = "<div><img alt=\"\" src=\"http://files.ducks.org/photos.ducks.org/images/thumbsup.png\"/> You" + ((parseInt(args[0]) == 1) ? "" : " and " + args[0] + " other" + ((parseInt(args[0]) > 1) ? "s" : "")) + " like this</div>";
            $(".likeWrapper").html(newhtml);

            var pg_cookiename = "DUPhotoGallery_LikesPhotos";
            var pg_cookie = readCookie(pg_cookiename);
            if (pg_cookie != null) {
                eraseCookie(pg_cookiename);
                createCookie(pg_cookiename, pg_cookie.toString() + args[1] + ",");
            }
            else {
                createCookie(pg_cookiename, args[1] + ",");
            }






        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            //alert(errorThrown);
        }
    });

}

function switchMainPhoto(li) {


    var anchor = $($('a', li))[0];
    var args = anchor.id.split("_");
    var photoID = args[0];
    var filename = args[1];
    var likes = parseInt(args[2]);




    var gallery_MainPhotoLink = $("#gallery_MainPhotoLink");
    var gallery_MainPhoto = $(".gallery_MainImage");
    var gallery_MainPhotoMoreInfoLink = $("#gallery_MainPhotoMoreInfoLink");

    var loc = window.location.href;

    var querystring = "";
    if (loc.indexOf("?") > 0) {
        querystring = loc.substring(loc.lastIndexOf("?"), loc.length);


        var re = new RegExp("&poe=(.+)&|&poe=(.+)$|\\?poe=(.+)&|\\?poe=(.+)$", "g");
        querystring = querystring.replace(re, '');

        
    }
    var link = loc.substring(0, loc.lastIndexOf("/"));
    gallery_MainPhotoLink.attr("href", link + "/details/" + photoID + "/" + querystring);
    gallery_MainPhotoMoreInfoLink.attr("href", link + "/details/" + photoID + "/" + querystring);
    gallery_MainPhoto.attr("src", "/gallery/photos/large/" + filename + "/photo.html");
    

    $(".thumb").removeClass("selected");
    $(li).addClass('selected');
    if ($(".likeWrapper").length > 0) {


        var pg_cookiename = "DUPhotoGallery_LikesPhotos";
        var pg_cookie = readCookie(pg_cookiename);
        if (pg_cookie) {
            var currentlikes = pg_cookie.split(',');
            var likeButton = "<a href=\"javascript:likePhoto(this, '" + photoID + "');\"><div><img alt=\"\" src=\"http://files.ducks.org/photos.ducks.org/images/thumbsup.png\"/> like</div></a>";
            var alreadyLikes = "<div><img alt=\"\" src=\"http://files.ducks.org/photos.ducks.org/images/thumbsup.png\"/> You" + ((likes > 1) ? " and "+likes + " others" : "") + " like this</div>";
            var newhtml = likeButton;
            for (var i = 0; i < currentlikes.length; i++) {
                if (currentlikes[i].toString() == photoID.toString()) {
                    newhtml = alreadyLikes;
                }
            }

            $(".likeWrapper").html(newhtml);
        }
    }
    if ($(".gallery_Navigation").length > 0) {

        $("#gallery_AddToFavorites").html("Add this to favorites");

        $("#gallery_AddToFavorites").attr("href", "javascript:addToFavorites(" + photoID + ");");
        $("#gallery_Wallpaper").attr("href", "/wallpaper/" + photoID + "/");

    }

    if ($(".admin_Form").length > 0) {
        FillForm(photoID);
    }


    /*if ($("#previewUpdateWrapper").length > 0) {
        $("#previewUpdateWrapper").show();
        //cropbox - done above
        
        //preview
        $("#preview").attr("src", "/gallery/photos/large/" + filename + "/photo.html");
        $(".jcrop-holder img").attr("src", "/gallery/photos/large/" + filename + "/photo.html");
        
        //btnUpdateThumbnail

        var btn = document.getElementById("btnUpdateThumbnail");
        btn.onclick = function() { UpdateThumbnailCoords(filename, photoID, true); };
       

        var getcurrentsize = jQuery('#cropbox')[0];
        imgWidth = getcurrentsize.width;
        imgHeight = getcurrentsize.height;
    }*/
    
}





function home_SwitchMainPhoto(thumb, photoID) {

    var home_MainPhotoLink = $("#home_MainPhotoLink");
    var home_MainPhoto = $("#home_MainPhoto");


    home_MainPhotoLink.attr("href", "/pictureoftheday/" + photoID + "/");
    home_MainPhoto.attr("src", "/home/pictureoftheday/" + photoID + "/photo.html");

    $(".MostRecentPics ul li").removeClass("selected");
    thumb.parentNode.className += ' selected';

    //$(".SubmittedBy").hide();







    $.ajax({
        type: "POST",
        url: "/_resources/asmx/photo.asmx/GetPOTDPhotoDetails",
        data: "{'sDateIncrement': '" + photoID.toString() + "' }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {
            if (data.d != "0") {
                var args = data.d.split("|"); ;
                if (args.length > 0) {

                    var categoryID = args[0];
                    var credit = args[1];
                    var tags = args[2];
                    var description = args[3];
                    var filename = args[4];
                    var memberName = args[5];
                    var memberWebID = args[6];
                    var datecreated = args[7];

                    var htm = "Submitted on " + datecreated + " <br/> by <a href=\"/member/" + memberWebID + "/index.html\">" + memberName + "</a>";
                    $(".SubmittedBy").html(htm);
                }
            }

        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });


    try {
        urchinTracker('/like/' + photoID.toString() + '/');
    }
    catch (err) { }

}





function wallpaper_GetCustomWallpaper() {

    var filename, credit, width, height;
    filename = $("#wallpaper_hiddenCustomFileName").val();
    credit = $("#wallpaper_hiddenCustomPhotoCredit").val();
    width = $("#wallpaper_inputCustomWidth").val();
    height = $("#wallpaper_inputCustomHeight").val();
    if (isInteger(width) && isInteger(height)) {

        var link = "/wallpaper/" + filename + "/" + width.toString() + "/" + height.toString() + "/" + credit + "/photo.html";
        window.open(link, 'Wallpaper Window', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes');
    }
    else
        alert("Only whole numbers may be entered into custom resolution fields.");
}



   function isInteger (s)
   {
      var i;

      if (isEmpty(s))
      if (isInteger.arguments.length == 1) return 0;
      else return (isInteger.arguments[1] == true);

      for (i = 0; i < s.length; i++)
      {
         var c = s.charAt(i);

         if (!isDigit(c)) return false;
      }

      return true;
   }

   function isEmpty(s)
   {
      return ((s == null) || (s.length == 0))
   }

   function isDigit (c)
   {
      return ((c >= "0") && (c <= "9"))
   }



function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}