﻿/**
*
*  AJAX IFRAME METHOD (AIM)
*  http://www.webtoolkit.info/
*
**/

//if ($('AvatarImage') != null || $('ProfileCoverImage') != null)
//{
    AIM = {

	    frame : function(c) {

		    var n = 'f' + Math.floor(Math.random() * 99999);
		    var d = document.createElement('DIV');
		    d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></iframe>';
		    document.body.appendChild(d);

		    var i = document.getElementById(n);
		    if (c && typeof(c.onComplete) == 'function') {
			    i.onComplete = c.onComplete;
		    }

		    return n;
	    },

	    form : function(f, name) {
		    f.setAttribute('target', name);
	    },

	    submit : function(f, c) {
		    var obj = $(f);
		    var input_file = obj.getElement('input[type=file]');
		    if (input_file != null)
		    {
		        if (input_file.value == "")
		        {
		            //alert ('Выберите изображение');
                    var ErrorText = $('ErrorText');
                    if (ErrorText != null)
                        ErrorText.innerHTML = "Выберите изображение!";
                    ShowModalBlock('ErrorWindow');
		            return false;
		        }
                var parts = input_file.value.split(".");
                var Error = false;
                if (parts.length > 0)
                {
                    var file_extension = parts[parts.length - 1].toLowerCase();
                    if (file_extension != "jpg" && file_extension != "jpeg" && file_extension != "png" && file_extension != "gif")
                    {
                        Error = true;
                    }
                }
                else
                {
                    Error = true;
                }
                if (Error == true)
                {
                    //alert ('Недопустимый тип файла!')
                    var ErrorText = $('ErrorText');
                    if (ErrorText != null)
                        ErrorText.innerHTML = "Недопустимый тип файла!";
                    ShowModalBlock('ErrorWindow');
                    return false;
                }
		            
		    }
		    AIM.form(obj, AIM.frame(c));
		    if (c && typeof(c.onStart) == 'function') {
			    return c.onStart();
		    } else {
			    return true;
		    }
	    },

	    loaded : function(id) {
		    var i = document.getElementById(id);
		    if (i.contentDocument) {
			    var d = i.contentDocument;
		    } else if (i.contentWindow) {
			    var d = i.contentWindow.document;
		    } else {
			    var d = window.frames[id].document;
		    }
		    if (d.location.href == "about:blank") {
			    return;
		    }

		    if (typeof(i.onComplete) == 'function') {
			    i.onComplete(d.body.innerHTML);
		    }
	    }

    }

    function startCallback() {
	    // make something useful before submit (onStart)
	    return true;
    }

    function completeCallbackAvatar(response) {
	    // make something useful after (onComplete)
	    var img = document.getElementById('AvatarImage');
	    img.src = 'BinaryImage.axd?ProfileAvatar=' + Math.floor(Math.random() * 99999);
	    var img_small = document.getElementById('AvatarImageSmall');
	    img_small.src = 'BinaryImage.axd?ProfileAvatarSmall=' + Math.floor(Math.random() * 99999);
    }

    function completeCallbackPortfolioCover(response) {
	    // make something useful after (onComplete)
	    var img = document.getElementById('PortfolioCoverImage');
	    var ProjectId = document.getElementById('ProjectId').value;
	    img.src = 'BinaryImage.axd?PortfolioCover=' + Math.floor(Math.random() * 99999) + '&ProjectId=' + ProjectId;
    }

    function completeCallbackCommunityBanner(response) {
	    // make something useful after (onComplete)
	    var div = document.getElementById('CommunityBannerImage');
	    var CommunityId = document.getElementById('CommunityId').value;
	    //div.style.background = "none";
	    div.style.backgroundColor = "#8e8e8e";
	    div.style.backgroundImage = "url('BinaryImage.axd?CommunityCreateBanner=" + Math.floor(Math.random() * 99999) + "&CommunityId=" + CommunityId + "')";
	    div.style.backgroundRepeat = "no-repeat";
	    div.style.backgroundPosition = "50% 0";
	    //div.style.background = '#8e8e8e url(BinaryImage.axd?CommunityCreateBanner=' + Math.floor(Math.random() * 99999) + '&CommunityId=' + CommunityId + ') no-repeat 50% 0;';
	    var div = document.getElementById('CommunityBannerImage');
    }
//}