
var oWaitingScreen = {
    'sWaitingIconSrc':    '/fileadmin/templates/css/images/ajax_loader.gif'

    , 'show':    function(sDomId, sWaitingDom) {
        var iWidth = $(sDomId).width();
        var iHeight = $(sDomId).height();
        var sWaitingIcon = '<img id="' + sWaitingDom + '" src="'+oWaitingScreen.sWaitingIconSrc+'" alt="" />';
        $(sDomId).append(sWaitingIcon);
        $(sDomId+' #'+sWaitingDom).css({
            position: 'absolute'
            ,top: (iHeight - 32) / 2 + 'px'
            ,left: ((iWidth - 32) / 2) + 'px'
        });
    }

    , 'hide':    function(sDomId, sWaitingDom) {
        $(sDomId+' #'+sWaitingDom).remove();
    }
}

var oImageUpload = {

    // image
    'sImage':		null


	// language
	, 'sLanguage':	null


	// readonly
	, 'bReadonly':	true


	// type
	, 'sType':		'community_user'
	, 'oType':		null
	, 'oTypes':		{
		'community_user':	{
			'sMainDom':			'#user_image'
			,'sCurrentImage':	'#current-image'
			,'sForm':			'#image-upload-form'
			,'sRemoveBtn':		'#btn-remove'
			,'sRemoveBtnConf':	'#btn-remove-confirm'
			,'sGetImageAjax':	'?eID=json&action=totoyaRequest&sClass=cUserImageUpload&sFunction=ajaxGetUserImage'
			,'sUploadAjax':		'?eID=json&action=totoyaRequest&sClass=cUserImageUpload&sFunction=ajaxUploadUserImage'
			,'sRemoveAjax':		'?eID=json&action=totoyaRequest&sClass=cUserImageUpload&sFunction=ajaxRemoveUserImage'
			,'sWaitingDom':		'user_waiting_image'
			,'sDefaultImage':	'' //'/fileadmin/templates/media/defaul_user_small.jpg'
			,'iButtonWidth':	145
			,'bHmmm':			false
			,'sJsName':			'oImageUpload'
		}
		,'community_activity':	{
			'sMainDom':			'#activity_image'
			,'sCurrentImage':	'#com_detail_my_activity_info_img_border'
			,'sForm':			'#image-upload-form'
			,'sRemoveBtn':		'#btn-remove'
			,'sRemoveBtnConf':	'#btn-remove-confirm'
			,'sGetImageAjax':	'?eID=json&action=totoyaRequest&sClass=cActivityImageUpload&sFunction=ajaxGetActivityImage'
			,'sUploadAjax':		'?eID=json&action=totoyaRequest&sClass=cActivityImageUpload&sFunction=ajaxUploadActivityImage'
			,'sRemoveAjax':		'?eID=json&action=totoyaRequest&sClass=cActivityImageUpload&sFunction=ajaxRemoveActivityImage'
			,'sWaitingDom':		'activity_waiting_image'
			,'sDefaultImage':	'' //'/fileadmin/templates/media/default_activity.jpg'
			,'iButtonWidth':	139
			,'bHmmm':			true
			,'sJsName':			'oImageUpload'
		}
		,'chequebook_logo':{
			'sMainDom':			'.cbw-logo'
			,'sCurrentImage':	'.cbw-logo_wrapper'
			,'sForm':			'#image-upload-form'
			,'sRemoveBtn':		'#btn-remove'
			,'sRemoveBtnConf':	'#btn-remove-confirm'
			,'sGetImageAjax':	'?eID=json&action=totoyaRequest&sClass=cChequebookImageUpload&sFunction=ajaxGetImage'
			,'sUploadAjax':		'?eID=json&action=totoyaRequest&sClass=cChequebookImageUpload&sFunction=ajaxUploadImage'
			,'sRemoveAjax':		'?eID=json&action=totoyaRequest&sClass=cChequebookImageUpload&sFunction=ajaxRemoveImage'
			,'sWaitingDom':		'chequebook_waiting_image'
			,'sDefaultImage':	'' //'/fileadmin/templates/media/default_activity.jpg'
			,'iButtonWidth':	139
			,'bHmmm':			true
			,'sJsName':			'oSiteControl.oLogoUpload'
		}
		,'chequebook_bg':{
			'sMainDom':			'.cbw-bg'
			,'sCurrentImage':	'.cbw-bg_wrapper'
			,'sForm':			'#image-upload-form-bg'
			,'sRemoveBtn':		'#btn-remove'
			,'sRemoveBtnConf':	'#btn-remove-confirm'
			,'sGetImageAjax':	'?eID=json&action=totoyaRequest&sClass=cChequebookImageUpload&sFunction=ajaxGetImage'
			,'sUploadAjax':		'?eID=json&action=totoyaRequest&sClass=cChequebookImageUpload&sFunction=ajaxUploadImage'
			,'sRemoveAjax':		'?eID=json&action=totoyaRequest&sClass=cChequebookImageUpload&sFunction=ajaxRemoveImage'
			,'sWaitingDom':		'chequebook_waiting_image'
			,'sDefaultImage':	'' //'/fileadmin/templates/media/default_activity.jpg'
			,'iButtonWidth':	139
			,'bHmmm':			true
			,'sJsName':			'oSiteControl.oBackgroundUpload'
		}
	}


	// element id
	, 'iElementId':	null


    // inits image upload
    ,'init': function(sLanguage, sType, iElementId, bReadonly, sColumn) {
		var oSelf = this;

        // set language
		oSelf.sLanguage = sLanguage;

        // set readonly
		oSelf.bReadonly = bReadonly;

		// set type
		switch (sType) {
			case 'community_user':
			case 'community_activity':
			case 'chequebook_logo':
			case 'chequebook_bg':
				oSelf.sType = sType;
				break;
		}
		oSelf.oType = oSelf.oTypes[oSelf.sType];
		
		// check if column is set
		oSelf.oType.sColumn = (typeof sColumn != 'string' ? 'IMAGE' : sColumn);

		// set element id
		oSelf.iElementId = iElementId;

		// get image id
		var oData = {
			sColumn:	oSelf.oType.sColumn
		};
		if (oSelf.oType.bHmmm) {
			oData.iId = oSelf.iElementId;
			oData.sType = oSelf.sType;
		}
		$.get(
			oNavControl.sBaseUrl + oSelf.oType.sGetImageAjax
			, oData
			, function(o) {
				oSelf.sImage = o.mData;

				// start render
				oSelf.render();
			}
			, 'json'
		);
    }


    // renders image upload
    ,'render': function() {
		var oSelf = this;
		
        // set image
        oSelf.showImage(oSelf);

		// check readonly
		if (oSelf.bReadonly) {
			return;
		}

        // hide waiting screen
        oWaitingScreen.hide(oSelf.oType.sMainDom, oSelf.oType.sWaitingDom);
		$(oSelf.oType.sForm).show();
		$(oSelf.oType.sCurrentImage).show();

        // set image upload iframe
        var sCss = '<style type="text/css">'
            +'* {\n'
            +'    margin:                0;\n'
            +'    padding:                0;\n'
            +'}\n'
            +'.image_upload_button_container {\n'
            +'    line-height:         30px;\n'
            +'    height:		       30px;\n'
            +'    overflow:            hidden;\n'
            +'    position:            absolute;\n'
            +'    width:               ' + oSelf.oType.iButtonWidth + 'px;\n'
            +'    bottom:              0px;\n'
            +'    cursor:              pointer;\n'
            +'    background-color:    #0969b5;\n'
			+'	  color: 			   #ffffff;\n'
            +'    border:              medium none;\n'
			+'    font-family:         Arial,Helvetica,sans-serief;\n'
			+'    font-weight:		   bold;\n'
			+'    text-align:		   center;\n'
            +'}\n'
            +'.image_upload_button_inner_container {\n'
            +'    height:              30px;\n'
            +'    margin-top:         -30px;\n'
            +'    margin-left:         -370px;\n'
            +'    overflow:            hidden;\n'
            +'    position:            relative;\n'
            +'    text-align:          right;\n'
            +'    width:               515px;\n'
            +'    cursor:              pointer;\n'
            +'}\n'
            +'.image_upload_button_button {\n'
            +'    opacity:             0;\n'
            +'}\n'
            +'</style>';
        $(oSelf.oType.sForm).contents().find('head').html(sCss);
        var sBodyHtml = '<form id="upload" method="post" enctype="multipart/form-data" action="' + oNavControl.sBaseUrl + oSelf.oType.sUploadAjax + '">'
            +'<input type="hidden" name="sLanguage" value="' + oSelf.sLanguage + '" />'
            +(
				oSelf.oType.bHmmm ?
				'<input type="hidden" name="iId" value="' + oSelf.iElementId + '" />'
				+ '<input type="hidden" name="sType" value="' + oSelf.sType + '" />' :
				''
			)
			+'<input type="hidden" name="sColumn" value="' + oSelf.oType.sColumn + '" />'
			+'<input type="hidden" name="sJsName" value="' + oSelf.oType.sJsName + '" />'
            +'<div class="image_upload_button_container">Bild hochladen'
            +'<div class="image_upload_button_inner_container">'
            +'<input type="file" class="image_upload_button_button" name="uploads" id="image_upload_input" maxlength="104857600" />'
            +'</div>'
            +'</div>'
            +'</form>';
        $(oSelf.oType.sForm).contents().find('body').css('border', 'none');
        $(oSelf.oType.sForm).contents().find('body').html(sBodyHtml);
        $(oSelf.oType.sForm).contents().find('.image_upload_button_button').css('opacity', '0');

        // add change-handler
        $(oSelf.oType.sForm).contents().find('.image_upload_button_button').unbind().change(function() {
            oWaitingScreen.show(oSelf.oType.sMainDom, oSelf.oType.sWaitingDom);
			$(oSelf.oType.sCurrentImage).hide();
			$(oSelf.oType.sForm).contents().find('.image_upload_button_container').hide();
			$(oSelf.oType.sRemoveBtn).hide();
            window.setTimeout(
                function() {
                    $(oSelf.oType.sForm).contents().find('#upload').submit();
                }
                ,200
            );
        });

        // set "remove image"-button handler
        $(oSelf.oType.sRemoveBtn).unbind().click(oSelf.removeImage);
    }


    // sets image
    ,'showImage': function() {
		var oSelf = this;
		
		// if no image is left, show default
		if (oSelf.sImage == null) {
			$(oSelf.oType.sCurrentImage+' img').attr('src', oSelf.oType.sDefaultImage);
			$(oSelf.oType.sRemoveBtn).hide();
			return;
		}

        // show image
		var sImagefile = oSelf.sImage.toString();
        $(oSelf.oType.sCurrentImage+' > img').attr('src', sImagefile);
        window.setTimeout(function() {
                if ($(oSelf.oType.sCurrentImage+' > img').height() != 60) {
                    $(oSelf.oType.sCurrentImage+' > img').attr('src', sImagefile);
                }
            }
            , 500
        );
        $(oSelf.oType.sRemoveBtn).show();
    }


	// removes image
    ,'removeImage': function() {
		var oSelf = this;
		
		// check readonly
		if (oSelf.bReadonly) {
			return;
		}

		var sDeleteConfirm = $(oSelf.oType.sRemoveBtnConf).text();
		if (!confirm(sDeleteConfirm)) {
			return;
		}

		var oData = {};
		if (oSelf.oType.bHmmm) {
			oData.iId = oSelf.iElementId;
			oData.sType = oSelf.sType;
		}
        $.post(
            oNavControl.sBaseUrl + oSelf.oType.sRemoveAjax
            , oData
            , function(o) {
				if (o.mData == null) {
					return;
				}

				// set default image
                oSelf.sImage = o.mData;

                // render again
                oSelf.render();
            }
            ,'json'
        );
    }

}

