var uploader_params;
var uploader_event;

$(document).ready(function(){
    
    $.datepicker.setDefaults( $.datepicker.regional[ "ru" ] );
    
    $(".dateToAge").each(function() {
        $(this).text( calcAge( $(this).text() ) );
    });
});

function calcAge ( birthday ) {
    var tmp = birthday.split('.', 3);
    var birth_day = tmp[0];
    var birth_month = tmp[1];
    var birth_year = tmp[2];
    
    var now = new Date;
    var now_day = now.getDate();
    var now_month = now.getMonth() + 1;
    var now_year = now.getFullYear();
    
    var age = now_year - birth_year;
    months_diff = now_month - birth_month;
    days_diff = now_day - birth_day;

    if ( isNaN(age) ) return 0;

    if ( months_diff > 0 ) {
        return age;
    } else if ( months_diff == 0 ) {
        if ( days_diff >= 0 ) return age;
        return --age;
    } else {
        return --age;
    }
}
function insertImageUploader( target, upload_url, width, height ) {
    var _callback = function(e) {
        if ( e.success ) {
            $('#AvatarUploadBtn').show();
        } else {
            $('#'+target).html(
                '<a href="http://www.adobe.com/go/getflashplayer">'+
                    '<img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" />'+
                '</a>'
            );
        }
    }
    var img_width = width || 100;
    var img_height = height || 100;
    var flashvars = { "upload_url": ""+upload_url+"&photo_max_x="+img_width+"&photo_max_y="+img_height };
    
    var params = {
        "quality":           "high",
        "scale":             "noscale",
        "allowScriptAccess": "sameDomain",
        "allowFullScreen":   "false",
        "wmode":             "transparent"
    };
    
    swfobject.embedSWF("/flash/avatar_uploader.swf", target, "166", "36", "9", "/flash/expressInstall.swf", flashvars, params, null, _callback)
//"pictureUploadBtn"+Math.round(Math.random()*1000)
}
function insertPlayer( place, type, rep_id, video_id ) {
    if (!place || !type || !rep_id) return false;
    
    var _callback = function(e) {
        if ( !e.success ) {
            $('#'+place).html(
                '<a href="http://www.adobe.com/go/getflashplayer">'+
                    '<img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" />'+
                '</a>'
            );
        }
    }
    
    var url = '';
    switch (type) {
        case 'photo':
            url = '/p/'+rep_id;
            break;
        case 'video':
            url = '/v/'+rep_id+'/'+video_id;
            break;
        default:
            return false;
    }
    //var flashvars = { 'xml': url };
    var params = {
        "quality":           "high",
        "scale":             "noscale",
        "allowScriptAccess": "always",
        "allowFullScreen":   "true",
        "wmode":             "transparent"
    };
    swfobject.embedSWF(url, place, "480", "406", "10", "/flash/expressInstall.swf", null, params, { 'id': "flashObject"+type+Math.round(Math.random()*1000) }, _callback)
}
function insertUploader( target ) {
    var _callback = function(e) {
        if ( e.success ) {
            $('#UploadFilesBtn').show();
        } else {
            $('#'+target).html(
                '<a href="http://www.adobe.com/go/getflashplayer">'+
                    '<img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" />'+
                '</a>'
            );
        }
    }
    var flashvars = { 'uid': '1' };
    var params = {
        "quality":           "high",
        "scale":             "noscale",
        "allowScriptAccess": "sameDomain",
        "allowFullScreen":   "false",
        "wmode":             "transparent"
    };
    
    swfobject.embedSWF("/flash/file_uploader.swf", target, "200", "30", "10", "/flash/expressInstall.swf", flashvars, params, null, _callback)
//"UploadBtn"+Math.round(Math.random()*1000)
}
function checkNewMessages() {
    $.post(
        URLS.check_messages,
        function (data) {
            if ( data.count > 0 ) {
                $('.InboxMessages').addClass('IsUnread');
                $('.NewMessages').text(' ('+data+')');
            }
            else {
                $('.InboxMessages').removeClass('IsUnread');
                $('.NewMessages').text('');
            }
        }
    );
    setTimeout( checkNewMessages, 5*60*1000 );
}
