﻿jQuery.growl.settings.displayTimeout = 7200;
jQuery.growl.settings.dockCss.width = '225px';
jQuery.growl.settings.noticeTemplate = ''
	+ '<table width="225" border="0" cellpadding="0" cellspacing="0">'
	+ '	<tr>'
	+ '		<td style="background-image: url(' + __root + 'content/images/growl/dm_top.png); width: 225px; height: 49px; background-repeat: no-repeat; color: #fff;">'
	+ '			<img src="' + __root + 'content/images/growl/info.png" style="max-width: 25px; max-height: 25px; text-align: center; margin-left: 19px; margin-top: 19px;" />'
	+ '			<h1 style="font-size: 18px; margin: 0pt; margin-left: 5px; margin-bottom: 10px; display: inline;">%title%</h1>'
	+ '		</td>'
	+ '	</tr>'
	+ '	<tr>'
	+ '		<td style="background-image: url(' + __root + 'content/images/growl/dm_repeat.png); width: 225px; background-repeat: repeat-y; color: #ddd;">'
	+ '			<p style="margin: 20px;">%message%</p>'
	+ '		</td>'
	+ '	</tr>'
	+ '	<tr>'
	+ '	<td style="background-image: url(' + __root + 'content/images/growl/dm_bottom.png); background-repeat: no-repeat; width: 225px; height: 27px;" valign="top" align="right" >'
	+ '			<a style="margin-right: 25px; font-size: 10px; color: #fff; text-align: right;" href="" onclick="return false;" rel="close">Close</a>'
	+ '		</td>'
	+ '	</tr>'
'+ </table>';
jQuery.growl.settings.noticeCss = {
    position: 'relative'
};

function PopupForm(f_url, a_url) {
    this.formUrl = f_url;
    this.actionUrl = a_url;
    this.onLoad = null;
    this.onSuccess = function (resp) {
        if (resp == "ok") {
            this.onSuccessOk();
        } else {
            var jqh = jQuery('#__popupHolder');
            jqh.html(resp);
            jqh.jqm();
        }
        jQuery('#__popupHolder').html(resp);
    }
    this.onSuccessOk = function (resp) {};
}

PopupForm.prototype.initAjaxForm = function () {
    var th = this;
    jQuery('#__popup_form').ajaxForm({
        method: 'post',
        beforeSubmit: function () {
            jQuery('#__popupHolder').html("<div style='text-align:center;padding:20px;'><img src='" + __root + "Content/images/load_circle.gif' /></div>");
        },
        success: function (resp) {
            th.onSuccess(resp);
            th.initAjaxForm();
        }
    })
    if (th.onLoad)
        th.onLoad();
    var jqh = jQuery('#__popupHolder');
    var w = (utils.screenWidth() - jqh.width()) / 2 + utils.scrollOffsetX();
    jqh.css('margin-left', 0).css('left', w + 'px');
}

PopupForm.prototype.beforeHide = function () {
    jQuery('#__popupHolder')
        .css('top', (document.documentElement.scrollTop || document.body.scrollTop) + Math.round(17 * (document.documentElement.offsetHeight || document.body.clientHeight) / 100) + 'px')
        .css('width', '650px');
}

PopupForm.prototype.show = function () {
    this.hide();
    var jph = jQuery('#__popupHolder');
    var th = this;
    jph.jqm({
        ajax: th.formUrl,
        modal: true,
        onShow: function (hash) {
            hash.w.html("<div style='text-align:center;padding:20px;'><img src='" + __root + "Content/images/load_circle.gif' /></div>").show();
        },
        onLoad: function () {
            th.initAjaxForm();
        }

    });
    jph.jqmShow();
    jph.draggable({ handle: '.popup-title' });
    return false;
}

PopupForm.prototype.hide = function () {
    var jph = jQuery('#__popupHolder');
    if (jph.css('display') != 'none') {
        this.beforeHide();
        jph.jqmHide();
    }
}


//// login form
loginForm = new PopupForm(__root + 'Account/Load/Login', __root + 'Account/Run/Login');

loginForm.onLoad = function () {
    jQuery('#Email').focus();
    //FB.init(_fb_apikey, __root + "fb_point/xd_receiver.htm");
    InitFacebook();
}

loginForm.onSuccess = function (resp) {
    if (resp == "ok") {
        var s = window.location.pathname.toLowerCase().indexOf('account/signup');
        if (s == -1)
            window.location.reload();
        else
            window.location = __root;
    }
    else if (resp == "first")
        window.location = __root + 'Profile/FirstLogin';
    else {
        var jqh = jQuery('#__popupHolder');
        jqh.html(resp);
        jqh.jqm();
        //FB.init(_fb_apikey, __root + "fb_point/xd_receiver.htm");
        InitFacebook();
    }
}

//// login as admin form
loginAsAdminForm = new PopupForm(__root + 'Account/Load/LoginAsAdmin', __root + 'Account/Run/LoginAsAdmin');

loginAsAdminForm.onLoad = function () {
    jQuery('#Password').focus();
}

loginAsAdminForm.onSuccessOk = function (resp) {
        window.location = __root + 'Admin';
}

//// forgot password form
forgotPasswordForm = new PopupForm(__root + 'Account/Load/ForgotPassword', __root + 'Account/Run/ForgotPassword');

forgotPasswordForm.onLoad = function () {
    jQuery('#Email').focus();
}
forgotPasswordForm.onSuccessOk = function (resp) {
    window.location = __root + 'Account/PasswordRestoreComplete';
}


//// send message form
msgForm = new PopupForm(null, __root + 'Messages/Run/Send');

msgForm.onLoad = function () {
    var jsbj = jQuery('#Subject');
    var s = jsbj.val();
    if (s && s.length > 0)
        jQuery('#Text').focus();
    else
        jsbj.focus();
}

msgForm.onSuccess = function (resp) {
    if (resp.substring(0, 2) == "ok") {
        this.hide();
        jQuery.growl('Successful', 'Your message has been sent to ' + resp.substring(2));
    }
    else {
        var jqh = jQuery('#__popupHolder');
        jqh.html(resp);
        this.onLoad();
        jqh.jqm();
    }
}

msgForm.send = function (rcvrId, threadID) {
    this.formUrl = __root + 'Messages/Run/SendNew?RecepientID=' + rcvrId;
    if (threadID)
        this.formUrl += '&ThreadID=' + threadID;
    this.show();
}

//// login as admin form
function CropperForm() {}

CropperForm.prototype = new PopupForm();

CropperForm.prototype.crop = function (phid, targetType, targetID) {
    this.photoID = phid;
    this.TargetType = targetType ? targetType : 0;
    this.TargetID = targetID ? targetID : 0;
    this.avtCoords = null;
    this.formUrl = __root + 'Photos/Run/CropDialog?PhotoID=' + phid;
    this.jqh = jQuery('#__popupHolder');
    this.jqh.css('top', '10px');
    this.show();
}

CropperForm.prototype.onLoad = function () {
    var th = this;
    var jcb = th.jqh.find('#cropbox');
    jcb.Jcrop({
        aspectRatio: this.TargetType == 1 ? 1 : 113 / 172,
        allowResize: true,
        minSize: [50, 50],
        onChange: th.saveCoords,
        onSelect: th.saveCoords
    });
    var w = jcb.width() + 371;
    th.jqh.css('width', w + 'px');
}

CropperForm.prototype.saveCoords = function (c) {
    cropForm.avtCoords = c;
}

CropperForm.prototype.save = function () {
    if (!this.avtCoords) {
        alert('Please select an area of the picture to use it as a primary photo');
        return
    };
    var th = this;
    var prm = this.avtCoords;
    prm.PhotoID = this.photoID;
    prm.TargetType = this.TargetType;
    prm.TargetID = this.TargetID;
    jQuery.get(__root + 'Photos/Run/UpdateAvatar', prm, function (resp) {
        jQuery('.user-avatar').attr('src', resp.replace('~/', __root));
        th.hide();
    });
}

cropForm = new CropperForm();
