$(document).ready(function() {
	
/*****************************************************************
*	Corner
******************************************************************/

	$(".navigation, .main, .footer").corner("5px");	
	$(".panel-select-upload").corner("top 5px");
	$(".panel-upload").corner("bottom 5px");
	$(".panel-user").corner("5px");
	
/*****************************************************************
*	Local
******************************************************************/	
	$("#local-flag").mouseover(function() {
		$(".local").show();
	});
	
	$(".local").hover(
		function () {			
		}, 
		function () {
			$(this).hide();
		}
	);

/*****************************************************************
*	Login and Reg
******************************************************************/
	var defultVal = {
		'email[0]':     'e-mail',
        'email[1]':     'e-mail',
        'email[2]':     'e-mail',
        'user[login]':  'e-mail',
        'user[pass]':   'pass',
        'reg[login]':   'e-mail',
        'reg[pass]':    general.reg.pass        
    };
    
    
    $(".text").focus(function() {
        var val = $(this).val();
        var key = $(this).attr("name");
        
        if (defultVal[key] == val) {
            $(this).val("");
        }
    });
    
    $(".text").blur(function() {
        var val = $(this).val();
        var key = $(this).attr("name");
        
        if (val == "") {
            $(this).val(defultVal[key]);
        }
    });

/*****************************************************************
*	User mail
******************************************************************/	
	$("#btnSubmit").click(function() {
        var error = false;
        var countEmple = 3;
        
        $("#mail input:text").each(function () {            
            var mail = $(this).val();
            
            if (mail == 'e-mail') {
                mail = "";
                $(this).val('');
            }
            
            if (mail != "") {
                if (! /([a-z0-9_\.\-]{1,20})@([a-z0-9\.\-]{1,20})\.([a-z]{2,4})/.test(mail)) {
                    $(this).css('backgroundColor', '#f88888');
                    error = true;
                } else {
                    $(this).css('backgroundColor', '#53DF73');
                }
            } else {
                --countEmple;
                $(this).css('backgroundColor', '#fff');
            }
        });

        if (error) {
            $("#msg-mail").text(general.email.valide);
        } 
        
        if (countEmple == 0) {
            $("#msg-mail").text(general.email.emply);
            error = true;
        }
        
        if (! error) {      
            $("#msg-mail").text(general.email.send);
            $.ajax({
                type: "POST",
                url: "/ajax/mail/linkfile",
                data: $(this).parent().serialize(),
                success: function(msg){
                    $("#msg-mail").text(general.email.ok);
                    
                    $("#mail input:text").each(function () {            
                        $(this).css('backgroundColor', '#fff');
                        $(this).val();
                    });
                }
            });
        }

    });
	
/***************************************************
*           ftp
***************************************************/
    $("#create-new-ftp").click(function() {
        $.ajax({
            type: "GET",
            url: "/ajax/ftpuser/create",
            dataType: "json",
            success: function(msg){
                if (msg['is_success']) {
                    redrawFtpUsersArea(msg['respont']);
                } else {
                    alert(msg['error']);
                }
            }
        });
    });
    
    function redrawFtpUsersArea(respont) {
        $('#ftp-usrs-area').empty();
        $.each(respont, function() {
            var li = "<tr id='ftp"+this.num+"' name='"+this.login+"'><td>"+this.login+" / "+this.pass+"</td><td><img class='ftp-del' src='/img/icon/cancel_small.png'></td></tr>";
            $('#ftp-usrs-area').append(li);
        });
        $('.ftp-del').bind('click', function(){
            deleteFtpUsr($(this).parent().parent().attr('id'), $(this).parent().parent().attr('name'));
        });
    }
    
    $('.ftp-del').click(function() {
        deleteFtpUsr($(this).parent().parent().attr('id'), $(this).parent().parent().attr('name'));
    });
    
    function deleteFtpUsr(fuid, funame) {
        if (!confirm(general.ftp.deleteAccount + funame + '?')) {return;}       
        $.ajax({
            type: "GET",
            url: "/ajax/ftpuser/delete/"+fuid,
            dataType: "json"
        });
        $("#"+fuid).remove();
    }
    
    $(".ftp-move").click(function() {
        $(this).text(general.ftp.moveBegin);
        
        $.ajax({
            type: "GET",
            url: "/ajax/ftpuser/moveFiles/"+ $(this).attr('id'),
            dataType: "json",
            success: function(res) {
                $('#'+res.id).text(general.ftp.moveEnd);
                
                if (res.is_success) {
                    alert(general.ftp.moveSuccessfully + res.data);
                } else {
                    alert("Error: "+res.error);
                }
            }
        });

    });
    
/***************************************************
*           Rule box
***************************************************/
    $("#panel-upload-rule").toggle(
        function () {                       
            $(this).prev().attr('src', '/img/main/upload-minus.gif');
            $("#panel-upload-rule-text").show();
        },
        function () {
            $(this).prev().attr('src', '/img/main/upload-plus.gif');
            $("#panel-upload-rule-text").hide();
        }
    );

/***************************************************
*           Premium account
***************************************************/
    $("#memory-link").click(function() {
        if ($("#upload-url").val() == "") {
            alert(general.premium.emply);
        } else {
            $(".authorization").load(
                "/user/urlfiles/memoryForm",
                {'url' : $("#upload-url").val()},
                function(){
                    $(this).show();
                    $('.memory-link').hide();
                    $(".ui-dialog").css({
                        'height': 85 + $('.ui-dialog').height()  + "px",
                        "overflow": "hidden"
                    });
                    $("#upload-add-url").css({
                        'height': 85 + $('#upload-add-url').height()  + "px",
                        "overflow": "hidden"
                    });
                }
            );
        }
    });
    
/***************************************************
*           Short url
***************************************************/
    $(".shorturl-box").click(function() {
        $(".shorturl-title").hide();
        $(".shorturl-link").show()
        $(".shorturl-link input").focus().select();
    });	

});

/***************************************************
*           radiobuttons links
***************************************************/    
function go(loc) {
	window.location.href = loc;
}

/***************************************************
*           Tooltip
***************************************************/
function overTooltip(obj) {
	$("#tooltip").text( $(obj).attr('alt') ).show();
}

function outTooltip() {
	$("#tooltip").hide();
}

function moveTooltip(e) {
	$("#tooltip").css({
		'left': e.clientX + 10 + 'px',
		'top':  e.clientY + 7 + document.documentElement.scrollTop + 'px'
	});
}

/***************************************************
*           flashVersion
***************************************************/
// JavaScript helper required to detect Flash Player PlugIn version information
function flashVersion() {
	var d, n = navigator, m, f = 'Shockwave Flash';
	if((m = n.mimeTypes) && (m = m["application/x-shockwave-flash"]) && m.enabledPlugin && (n = n.plugins) && n[f]) {d = n[f].description}
	else if (window.ActiveXObject) { try { d = (new ActiveXObject((f+'.'+f).replace(/ /g,''))).GetVariable('$version');} catch (e) {}} 
	return d ? d.replace(/\D+/,'') : 0;
}