﻿// JSLint Config:
/*jslint browser: true */
/*global cc, jQuery, cc_resx_UPLOAD_AVATAR_URL, cc_resx_TR_Saving, cc_resx_TR_Save */

//  
//  Author: Kevin Burkitt
//  Date:   2010-04-11
//  
//  Profile Picture Crop Handler
//
cc.crop = function() {

    return {
        coords: {},
        saving: false,
        filename: '',
        redirect: '',
        imageWidth: 0,
        imageHeight: 0,
        initialCropSizeX: 210.0,
        initialCropSizeY: 210.0,
        onCompletePostBackUrl: '/myprofile/upload/profile-picture/',
        onCompletePostBackTarget: '#mp-info',
        avatarUrl: '',
        username: '',

        init: function(avatarUrl, redirectUrl, filename, username, onCompletePostBackUrl, onCompletePostBackTarget, filenameKey) {
            if (typeof redirectUrl !== 'undefined') {
                cc.crop.redirect = redirectUrl;
            }

            cc.crop.avatarUrl = avatarUrl;
            cc.crop.filename = filename;
            cc.crop.username = username;

            if (typeof onCompletePostBackUrl !== 'undefined' && onCompletePostBackUrl !== '') {
                cc.crop.onCompletePostBackUrl = onCompletePostBackUrl;
            }
            if (typeof onCompletePostBackTarget !== 'undefined' && onCompletePostBackTarget !== '') {
                cc.crop.onCompletePostBackTarget = onCompletePostBackTarget;
            }

            jQuery('#cropContainer').html('');

            jQuery("#preview").attr('src', avatarUrl);

            // Init Upload Object
            cc.upload.init({
                uploadUrl: cc_resx_UPLOAD_AVATAR_URL,
                fileListMax: 1,
                onCompleteRedirectUrl: location.href,
                username: username,
                filenameKey: filenameKey
            });

            // Show Upload Button:
            jQuery('#upload').show();
            

            jQuery(new Image()).load(function() {

                jQuery(this).appendTo('#mp-crop-container');

                var img = new Image();
                img.src = cc.crop.avatarUrl;

                cc.crop.imageWidth = img.width;
                cc.crop.imageHeight = img.height;

                jQuery('#cropbox').Jcrop({
                    onChange: cc.crop.showPreview,
                    onSelect: cc.crop.showPreview,
                    boxWidth: 200,
                    boxHeight: 200,
                    setSelect: [0, 0, 100, 100],
                    aspectRatio: 1,
                    onInit: function() { }
                });

            }).attr('id', 'cropbox').attr('src', avatarUrl);

            // Save Button
            jQuery('#mp-crop-save').click(function() {
                cc.crop.cropImage();
                return false;
            });


        },

        showPreview: function(c) {
            if (c.w !== 0 && c.h !== 0) {
                var rx = 99 / c.w;
                var ry = 99 / c.h;

                jQuery('#preview').css({
                    width: Math.round(rx * jQuery('#cropbox').width()) + 'px',
                    height: Math.round(ry * jQuery('#cropbox').height()) + 'px',
                    marginLeft: '-' + Math.round(rx * c.x) + 'px',
                    marginTop: '-' + Math.round(ry * c.y) + 'px'
                });

                cc.crop.coords = c;
            }
        },

        cropImage: function() {

            if (cc.crop.saving === true) {
                return false;
            }
            cc.crop.saving = true;

            jQuery('#mp-crop-btn').html(cc_resx_TR_Saving);

            var scaleX = (cc.crop.initialCropSizeX / cc.crop.imageWidth) * (200.0 / cc.crop.coords.w);
            var scaleY = (cc.crop.initialCropSizeY / cc.crop.imageHeight) * (200.0 / cc.crop.coords.h);

            //var x = parseInt(cc.crop.coords.x * (cc.crop.imageWidth / 200));
            //var y = parseInt(cc.crop.coords.y * (cc.crop.imageWidth / 200));
            // var scale = (200.0 / cc.crop.imageWidth);
            var x = parseInt((cc.crop.coords.x / 200.0) * (cc.crop.imageWidth * scaleX), 10);
            var y = parseInt((cc.crop.coords.y / 200.0) * (cc.crop.imageHeight * scaleY), 10);

            cc.ajax.getJSON(
                "/myprofile/info/profile-picture/crop/",
                {
                    'x': x,
                    'y': y,
                    'imgW': cc.crop.imageWidth,
                    'imgH': cc.crop.imageHeight,
                    'scale': scaleX,
                    'filename': cc.crop.filename,
                    'username': cc.crop.username
                },
                function(msg) {
                    if (cc.crop.redirect !== null && cc.crop.redirect !== '') {
                        location.href = cc.crop.redirect;
                    }
                    else {
                        if (typeof msg !== 'undefined' && typeof msg.success !== 'undefined' && msg.success === true) {
                            if (typeof msg.message !== 'undefined' && ( msg.message.test('.jpg$') || msg.message.test('.gif$') )) {
                                jQuery('#myprofile .mp-left .mp-owner img:first').attr('src', msg.message);
                                // cc.crop.filename
                                if (msg.message.length > msg.message.lastIndexOf('/') + 5) {
                                    cc.crop.filename = msg.message.substring(msg.message.lastIndexOf('/') + 1);
                                }

                                return;
                            }
                        }

                        // location.reload(true);
                    }
                },
                function() {
                    cc.crop.saving = false;
                    jQuery('#mp-crop-btn').html(cc_resx_TR_Save);
                }
            );

        }

    };



} ();

