﻿//Define Kpw object only if it's not already defined
if ("undefined" == typeof Kpw) {Kpw = {};}
$(document).ready(function(){});

//Kpw Vendor Object
Kpw.ProfilePic = {
    jCropApi: null,
    init: function() {

        if ($("UploadifyUploader").length == 0) {
            $("#Uploadify").uploadify({
                "uploader": $("input.uploadifyPath").attr("value") + "uploadify.swf",
                "script": $("input.handlersPath").attr("value") + "AddImages.ashx",
                "fileDataName": "KpwVendorImage",
                "cancelImg": "../js/jquery/plugins/jquery.uploadify-v1.6.2.mit/cancel.png",
                "auto": true,
                "multi": false,
                "width": 80,
                "fileDesc": "*.gif;*.jpg;*.jpeg;*.png",
                "fileExt": "*.gif;*.jpg;*.jpeg;*.png",
                "buttonImg": $("input.uploadifyPath").attr("value") + "uploadifyButton.gif",
                "scriptData": { "vendor": $("input.vendorName").attr("value") },
                "onComplete": function(event, queueId, fileObj, response, data) {

                    //IMAGE SIZE
                    Kpw.ProfilePic.Photo.targetWidth = 235;
                    Kpw.ProfilePic.Photo.targetHeight = 280;

                    $('input.fileName').val(fileObj.name);

                    //$('div.dragWrapper img.thumbnail').attr('src', "");

                    if (Kpw.ProfilePic.jCropApi) {
                        Kpw.ProfilePic.jCropApi.destroy();
                    }

                    //need to preload the image otherwise if its not there, jcrop will fail.
                    var image = new Image();
                    $(image).load(function() {
                        $('div.dragWrapper img.thumbnail').attr('src', image.src);

                        if (jQuery.browser.safari) {
                            //safari needs a second...
                            setTimeout(Kpw.ProfilePic.InitJcrop, 300);
                            return;

                        } else {
                            Kpw.ProfilePic.InitJcrop();
                        }

                    });
                    image.src = $('input.uploadPath').val() + fileObj.name;
                    image.src = image.src;

                    return true;
                },
                "onError": function(event, queueId, fileObj, errorObj) {
                    alert(errorObj.info);
                }
            });
        }
    },
    InitJcrop: function() {
        Kpw.ProfilePic.jCropApi = $.Jcrop("img.thumbnail", {
            bgColor: "black",
            allowResize: true,
            aspectRatio: (Kpw.ProfilePic.Photo.targetWidth / Kpw.ProfilePic.Photo.targetHeight),
            minSize: [Kpw.ProfilePic.Photo.targetWidth, Kpw.ProfilePic.Photo.targetHeight],
            //trueSize: [500, 500],
            bgOpacity: .6,
            onChange: Kpw.ProfilePic.Photo.capture
        });
    },
    ConfirmSave: function() {
        if ($("input.cropData").val().length > 0) {
            return true;
        } else {
            alert("Please click on the picture and crop the desired image size before saving.  You can also click cancel to cancel the upload.");
            return false;
        }

    },
    Photo: {
        targetWidth: 0,
        targetHeight: 0,
        capture: function(c) {
            var img = $("img.thumbnail");
            var originalWidth = 0; //$("ul.original li input.width").val();
            var originalHeight = 0; //$("ul.original li input.height").val();

            var targetWidth = Kpw.ProfilePic.Photo.targetWidth; //parseInt($("ul.siteImages li.selected input.width").val(), 10);
            var targetHeight = Kpw.ProfilePic.Photo.targetHeight; //parseInt($("ul.siteImages li.selected input.height").val(), 10);
            var cropWidth = parseInt(c.w, 10);
            var cropHeight = parseInt(c.h, 10);

            var targetAspectRatio = targetWidth / targetHeight;
            var croppedlAspectRatio = cropWidth / cropHeight;

            if (croppedlAspectRatio != targetAspectRatio) {
                //add 1 to width
                cropWidth = cropWidth + 1;
                croppedlAspectRatio = cropWidth / cropHeight;
                if (croppedlAspectRatio != targetAspectRatio) {
                    //add 1 to height    
                    cropWidth = cropWidth - 1;
                    cropHeight = cropHeight + 1;
                    croppedlAspectRatio = cropWidth / cropHeight;
                    if (croppedlAspectRatio != targetAspectRatio) {
                        //add 1 to width and height
                        cropWidth = cropWidth + 1;
                        croppedlAspectRatio = cropWidth / cropHeight;
                        if (croppedlAspectRatio != targetAspectRatio) {
                            //can't fix - set back to original
                            cropWidth = cropWidth - 1;
                            cropHeight = cropHeight - 1;
                        }
                    }
                }
            }

            var image = {
                Original: {
                    width: originalWidth,
                    height: originalHeight,
                    title: img.attr("title"),
                    alt: img.attr("alt"),
                    src: img.attr("data-kpw-src")
                },
                Modified: {
                    x1: c.x,
                    y1: c.y,
                    x2: c.x2,
                    y2: c.y2,
                    cropWidth: cropWidth,
                    cropHeight: cropHeight
                },
                targetWidth: targetWidth,
                targetHeight: targetHeight
            }
            $("input.cropData").attr("value", JSON.stringify(image));
        }
    }
}
