﻿/**
 * Media
 *
 * $Id: media.js 2331 2008-02-06 11:05:26Z alex $
 *
 * @copyright Copyright (c) 2007, Effective Solution Band Ltd, All rights reserved.
 * @url http://www.esb.com.ua
*/

var g_media_id;
var MEDIA_COUNT_PHOTO_ON_PAGE = 6;
var g_media_path = g_base_url + 'resource/media/';
var g_video_amateur_path = g_base_url + 'resource/video/amateur/';
var g_media_type = '';
var g_module = '';

/* Photo */

function MediaListClass(){
    this.module_name = '';
    this.media_type = 'photo';    
    this.items = new Object();
    this.page_index = new Object();
    this.items_index = new Array()    
    this.current_page = 1;
    this.last_page = 1;
    this.count_all = 0;
   
    this.addItems =  function(items){
        var media_id;
        for(var property in items){
            media_id = items[property].media_id;
            this.items[media_id] = items[property];
            this.items_index.push(media_id);
        }
    }
    
    this.getItem = function(id){
        return this.items[id];
    }
 
    this.getCurrentPage = function(){
        return this.current_page;
    }

    this.addItemsToIndex  = function(arr){
        var last = 0;
        for(var i=0; i< Math.ceil(arr.length/MEDIA_COUNT_PHOTO_ON_PAGE); i++ ){
            last = (i+1)*MEDIA_COUNT_PHOTO_ON_PAGE;
            if(last>arr.length){
                last = arr.length;
            }
            this.page_index[i+1] = new Array();
            for(var j=i*MEDIA_COUNT_PHOTO_ON_PAGE; j<last; j++){
                this.page_index[i+1].push(arr[j]);
            }
        }
    }
    
    
    this.setup = function(module_name, media_type, count_all, arr){
        this.module_name = module_name;
        this.media_type = media_type;
        this.count_all = count_all;
        this.last_page = Math.ceil(count_all/MEDIA_COUNT_PHOTO_ON_PAGE);
        this.addItemsToIndex(arr);
    }
    
    this.getModuleName = function(){
        return this.module_name;
    }
    
    this.getMediaType = function(){
        return this.media_type;
    }
}

var g_media_photo_list = new MediaListClass();
var imageArray = new Array();

function mediaShowFile(media_id){
    hideFlash();
    hideAllSelect();    
    g_media_type = 'photo';
    g_media_id = media_id;
    
    var img_desc = g_media_photo_list.items[media_id].desc;
    var img_width = parseInt(g_media_photo_list.items[media_id].width, 10);
    var img_height = parseInt(g_media_photo_list.items[media_id].height, 10);
    var img_path = g_media_path + g_media_photo_list.getMediaType() + '/' + media_id  + '.jpg';
    if(g_module == 'casting') var img_path = g_media_path + g_media_photo_list.getMediaType() + '/' + media_id  + '_1.jpg';

    
    var media_position = arrayPosition(g_media_photo_list.items_index, media_id);
    var media_item_number = (media_position + 1) + '/' + g_media_photo_list.count_all;
        
    var rewind_img = 'nav_button_revind.gif';
    var rewind_onclick = 'onclick="mediaShowPrevImage()" class="imgNavButton"';
    var forward_img = 'nav_button_forward.gif';
    var forward_onclick = 'onclick="mediaShowNextImage()" class="imgNavButton"';
    
    if(0 === media_position){
        rewind_img = 'nav_button_revind_dis.gif';
        rewind_onclick = 'class="imgNavButtonDisabled"';
    }
    if((g_media_photo_list.count_all -1) === media_position ){
        forward_img = 'nav_button_forward_dis.gif';
        forward_onclick = 'class="imgNavButtonDisabled"';
    }
    
    
    
    var oBody = document.body;
//    oBody.style.overflow = 'hidden';
    var oShadow = document.createElement('div');
    oShadow.id = 'modal_shadow';
    oShadow.style.height = window.getScrollHeight() + 'px';   
    oBody.appendChild(oShadow);

    var oModal = document.createElement('div');
    oModal.id = 'modal_window';
        
    out = '' +
        '<table border=0 cellpadding="0" cellspacing="0" style="position:relative;width:100%;height:100%">' +
        '<tr>' +
            '<td style="text-align: right; background: url(' +g_base_url+ 'img/photo_win_left.gif) no-repeat bottom right; height: 55px;"></td>' +
            '<td class="photo_header">' +
                '<table width="100%">' +
                '<tr>' +
                    '<td width="33%">' +
                    '<div id="mediaItemNumber" style="text-align:left;padding-bottom:8px;font-weight:bold;font-size:20px">' +media_item_number+ '</div>' +
                    '</td><td width="33%" style="text-align:center;">' +
                    '<img id="mediaPicturePrev" src="' +g_base_url+ 'img/' +rewind_img+ '" alt="" ' +rewind_onclick+ '/>&nbsp;&nbsp;' +
                    '&nbsp;&nbsp;<img id="mediaPictureNext" src="' +g_base_url+ 'img/' +forward_img+ '" alt="" ' +forward_onclick+ '/>&nbsp;&nbsp;' +
                    '</td>' +
                    '<td nowrap="nowrap" width="33%" style="text-align:right;"><img onclick="closeModalMessage()" class="imgNavButton" src="' +g_base_url+ 'img/nav_close.gif" alt="" /></td>' +
                '</tr>' +
                '</table>' +
            '</td>' +
            '<td style="text-align: left; background: url(' +g_base_url+ 'img/photo_win_right.gif) no-repeat bottom left; height: 55px;"></td>' +
        '</tr>' +
        '<tr>' +
            '<td style="background: url(' +g_base_url+ 'img/photo_frame_l_corner.gif) no-repeat top right; height: 9px; width: 15px;"></td>' +
            '<td style="background: url(' +g_base_url+ 'img/photo_frame_c_corner.gif) repeat-x top left; width: auto"></td>' +
            '<td style="background: url(' +g_base_url+ 'img/photo_frame_r_corner.gif) no-repeat top left; height: 9px; width: 15px;"></td>' +
        '</tr>' +
        '<tr>' +
            '<td style="background: url(' +g_base_url+ 'img/photo_frame_l_bg.jpg) repeat-y top right; width: 15px;"></td>' +
            '<td style="background-color: black; width: auto;height: 100%;" id="tdMediaPicture" align="center">' +
            	'<div id="divLoading"><img src="'+g_base_url+'img/load_img_'+g_language+'.gif" style="width:149px;height:20px"></div>' + 
//                '<img id="mediaPicture" src="' + img_path + '" style="width:'+img_width+'px;height:'+img_height+'px;" alt="" />' +
            '</td>' +
            '<td style="background: url(' +g_base_url+ 'img/photo_frame_r_bg.jpg) repeat-y top right; width: 15px;"></td>' +
        '</tr>' +
        '<tr>' +
            '<td style="background: url(/img/photo_frame_l_corner_d.gif) no-repeat top right; height: 10px; width: 15px;"></td>' +
            '<td style="background: url(/img/photo_frame_c_corner_d.gif) repeat-x top left; width: auto"></td>' +
            '<td style="background: url(/img/photo_frame_r_corner_d.gif) no-repeat top left; height: 10px; width: 15px;"></td>' +
        '</tr>' +
        '<tr>' +
            '<td style="text-align: right; background: url(' +g_base_url+ 'img/photo_frame_left_b.gif) no-repeat top right; height: 106px;"></td>' +
            '<td class="photo_title">' +
                '<table>' +
                '<tr>' +
                    '<td width="100%" id="mediaPictureDesc">' +img_desc+ '</td>' +
                    '<td nowrap="nowrap"></td>' +
                    '<td nowrap="nowrap">' +
                            
                    '</td>' +
                '</tr>' +
                '</table>' +
            '</td>' +
            '<td style="text-align: left; background: url(' +g_base_url+ 'img/photo_frame_right_b.gif) no-repeat top left; height: 106px;"></td>' +
        '</tr>' +
        '</table>' 
    ;
  
    oModal.innerHTML = out;    
    if(img_width <500){
        img_width = 500;
    };
    if(img_height<600){
        img_height = 600;
    }
    oModal.style.width = img_width + 30 + 'px';
    oModal.style.height = img_height + 'px';

    var modal_width = parseInt(oModal.style.width, 10);
    oModal.style.top = document.documentElement.scrollTop  + 25 + "px";
    
    var left = (document.documentElement.scrollWidth - modal_width)/2;
    if(left<0){
        left = 0;
    }
    oModal.style.left =  left + "px";

    oBody.insertBefore(oModal,oBody.firstChild);
    document.addEvent('keydown', modalEvent);
    
	showImage(img_path);
}

/**
 * Отображение фото, если вставлять код IMG непосредственно при создании
 * окна, то в ИЕ он не загрузиться
 */
function showImage(src, is_slide_mode){
//  console.log('showImage: src=' + src + ' , imageArray.length=' + imageArray.length);
	var last_index, img_index;
	is_slide_mode = (undefined === is_slide_mode) ? false : is_slide_mode;
	$('divLoading').style.display = 'block';
	if($('mediaPicture')){
	  $('mediaPicture').style.display = 'none';
	}
  if(false === (img_index = getImageIndex(src)))
  {
    last_index = imageArray.length;
    imageArray[last_index] = {src: src, imgObject: new Image()};
    imageArray[last_index].imgObject.id = 'mediaPicture';
    imageArray[last_index].imgObject.style.display = 'block';
    imageArray[last_index].imgObject.onload = function(){
        $('divLoading').style.display = 'none';
        removeMySelf($('mediaPicture'));
        $('tdMediaPicture').appendChild(imageArray[last_index].imgObject);
        imageArray[last_index].imgObject.onload = new Function();
    }
    imageArray[last_index].imgObject.src = src + '?' + new Date();
  }
  else
  {
    $('divLoading').style.display = 'none';
    removeMySelf($('mediaPicture'));
    imageArray[img_index].imgObject.style.display = 'block';
    $('tdMediaPicture').appendChild(imageArray[img_index].imgObject);
  }
}

function getImageIndex(src){
	for(var i=0; i<imageArray.length; i++)
	{
		if(imageArray[i].src == src)
			return i;
	}
	return false;
}



function hideAllSelect(){
    var oSel = document.getElementsByTagName("select");
    for(var i=0;i<oSel.length;i++){
        oSel[i].style.visibility = 'hidden';
    }
}

function showAllSelect(){
    var oSel = document.getElementsByTagName("select");
    for(var i=0;i<oSel.length;i++){
        oSel[i].style.visibility = 'visible';
    }
}

function closeModalMessage(){
/*    alert('closeModalMessage');*/
    document.removeEvent('keydown', modalEvent);
    
    if($('divFlvPlayer')){
        $('divFlvPlayer').parentNode.removeChild($('divFlvPlayer'));
    }
    
    var oDiv = document.getElementById('modal_shadow');
    if(oDiv){
        oDiv.parentNode.removeChild(oDiv);
    }

    var oDiv = document.getElementById('modal_window');
    if(oDiv){
        oDiv.parentNode.removeChild(oDiv);
    }
    showFlash();
    showAllSelect();
}

function modalEvent(event){
    var event = new Event(event);
    // Close
    if(event.code == Event.keys.esc){
        document.removeEvent('keydown', modalEvent);
        closeModalMessage();
    }

    if('photo' == g_media_type){
        // Ctrl + right
        if(event.code == Event.keys.right && true === event.control ){
            mediaShowNextImage();
        }
        // Ctrl + left
        if(event.code == Event.keys.left && true === event.control ){
            mediaShowPrevImage();
        }
    }
}


function mediaPhotoShowNextPage(){
    if(g_media_photo_list.current_page === g_media_photo_list.last_page){
        return true;
    }
    var oImg = $('mediaPrevButton');
    if( /nav_button_revind_dis.gif/.test(oImg.src) ){
        oImg.src = g_base_url + 'img/nav_button_revind.gif';
    }
    g_media_photo_list.current_page = g_media_photo_list.current_page + 1;
    if(g_media_photo_list.current_page === g_media_photo_list.last_page){
        $('mediaNextButton').src =  $('mediaNextButton').src.replace(/nav_button_forward.gif/, 'nav_button_forward_dis.gif');
    }
    mediaBuildLiObject();
//    console.log('mediaPhotoShowNextPage: g_media_photo_list.current_page=' + g_media_photo_list.current_page);
}

function mediaPhotoShowPrevPage(){
    if(g_media_photo_list.current_page === 1){
        return true;
    }
    var oImg = $('mediaNextButton');
    if( /nav_button_forward_dis.gif/.test(oImg.src) ){
        oImg.src = g_base_url + 'img/nav_button_forward.gif';
    }
    g_media_photo_list.current_page = g_media_photo_list.current_page - 1;
    if(1 === g_media_photo_list.current_page){
        $('mediaPrevButton').src =  $('mediaPrevButton').src.replace(/nav_button_revind.gif/, 'nav_button_revind_dis.gif');
    } 
    mediaBuildLiObject();
}


/**
 * g_media_photo_preview[page] = {src:[], oImg:[]}
 */
var g_media_photo_preview = new Array();


function mediaBuildLiObject(page){
    var page = g_media_photo_list.current_page;
    var items = new Object();
    var id;
    var is_upload_photo = false;

    if('undefined' === typeof(g_media_photo_preview[page])){
        is_upload_photo = true;
        g_media_photo_preview[page] = {src:[], oImg:[]};
    }
    
    for(var i=0; i< g_media_photo_list.page_index[page].length; i++){
        id = g_media_photo_list.page_index[page][i];
        items['0' + id] = g_media_photo_list.getItem(id); // for Opera
        if(true === is_upload_photo){
            g_media_photo_preview[page].src.push( g_media_path + g_media_photo_list.getMediaType() + '/' + id  + '_sm.jpg' );
        }
    }
    mediaBuildLiHtml(items);
    
    if(true === is_upload_photo){
        // loading image
        for(var i=0; i<g_media_photo_preview[page].src.length; i++){
            g_media_photo_preview[page].oImg[i] = new Image(135, 120);
            g_media_photo_preview[page].oImg[i].onload = new Function('showPhotoPreviewImage(this,'+i+','+id+')');
            g_media_photo_preview[page].oImg[i].src = g_media_photo_preview[page].src[i];
        }
    } else{
        // show image
        for(var i=0; i<g_media_photo_preview[page].src.length; i++){
            showPhotoPreviewImage(g_media_photo_preview[page].oImg[i],i,id);
        }
    }
}

function showPhotoPreviewImage(oImg, image_number, media_id){
    $('photoPreview'+image_number).style.backgroundImage = 'url('+oImg.src+')';
    $('photoPreview'+image_number).style.backgroundPosition = 'top left';
    $('photoPreview'+image_number).style.backgroundRepeat = 'no-repeat';
}

function mediaBuildLiHtml(items){
    var out = '', count_items=0;
    for(var media_id in items){
      media_id = parseInt(media_id, 10);
        out += '<li>';
            out += '<a href="javascript:void(0)" onclick="mediaShowFile(' + media_id + ')"><img src="'+ g_base_url + 'img/photo_frame.gif" id="photoPreview'+count_items+'" alt="" ';
                out += ' style="width:135px;height:120px;';
//                out += 'background: url(' + g_media_path + g_media_photo_list.getMediaType() + '/' + media_id  + '_sm.jpg) ';
//            out += ' no-repeat top left;';
            out += '" /></a> '; // space need
        out += '</li>\n';
        count_items++;
    }
    
    for(var i=count_items; i<MEDIA_COUNT_PHOTO_ON_PAGE; i++){
        out += '<li><img src="' + g_base_url + 'img/photo_empty.gif" alt="" /> </li>\n';// space need
    }

    $('mediaListItems').innerHTML = out;
}

function mediaShowNextImage(){
    var media_position = arrayPosition(g_media_photo_list.items_index, g_media_id);
    var next_media_position = media_position + 1;
      
//   console.log('mediaShowNextImage=' + g_media_id + ', media_position=' + media_position + ', last_load_index=' + (g_media_photo_list.items_index.length - 1) );
   
    if( next_media_position < g_media_photo_list.items_index.length  ){
        var page = Math.ceil((next_media_position+1)/MEDIA_COUNT_PHOTO_ON_PAGE);
        if(page > g_media_photo_list.current_page){
            mediaPhotoShowNextPage();
        }
        mediaShowImage(next_media_position);
    }
}


function mediaShowPrevImage(){
    var media_position = arrayPosition(g_media_photo_list.items_index, g_media_id);
    var prev_media_position = media_position - 1;
    
//    console.log('mediaShowPrevImage=' + g_media_id + ', media_position=' + media_position);

    if(prev_media_position >= 0){
        var page = Math.ceil((prev_media_position+1)/MEDIA_COUNT_PHOTO_ON_PAGE);
        if(page < g_media_photo_list.current_page){
            mediaPhotoShowPrevPage();
        }
        mediaShowImage(prev_media_position);
    }
}

function mediaShowImage(media_position){
//    console.log('mediaShowImage: media_position = ' + media_position);
    media_id = g_media_photo_list.items_index[media_position];
    g_media_id = media_id;
    media_position = arrayPosition(g_media_photo_list.items_index, media_id);
    
    $('mediaItemNumber').innerHTML = (media_position + 1) + '/' + g_media_photo_list.count_all;

    var img_desc = g_media_photo_list.items[media_id].desc;
    var img_width = parseInt(g_media_photo_list.items[media_id].width, 10);
    var img_height = parseInt(g_media_photo_list.items[media_id].height, 10);
    var img_path = g_media_path + g_media_photo_list.getMediaType() + '/' + media_id  + '.jpg';
    if(g_module == 'casting') var img_path = g_media_path + g_media_photo_list.getMediaType() + '/' + media_id  + '_1.jpg';
    
    var document_scroll_width = document.documentElement.scrollWidth;

//    console.log('mediaShowImage: media_id=' + media_id + ', media_position='+media_position + ' , img_path=' + img_path);
    
    $('mediaPictureDesc').innerHTML = img_desc;
//    $('mediaPicture').style.width = img_width + 'px';
//    $('mediaPicture').style.height = img_height + 'px';
//    $('mediaPicture').src = img_path;
	  showImage(img_path, true);
    
    var oImgPrev = $('mediaPicturePrev');
    if(0 === media_position){
        oImgPrev.onclick = '';
        oImgPrev.className = 'imgNavButtonDisabled';
        oImgPrev.src = g_base_url + 'img/nav_button_revind_dis.gif';
    } else {
        oImgPrev.onclick = new Function('mediaShowPrevImage()');
        if( /nav_button_revind_dis.gif/.test(oImgPrev.src) ){
            oImgPrev.className = 'imgNavButton';
            oImgPrev.src =  oImgPrev.src.replace(/nav_button_revind_dis.gif/, 'nav_button_revind.gif');
        }
    }

    var oImgNext = $('mediaPictureNext');
    if( (g_media_photo_list.count_all -1) === media_position){
        oImgNext.onclick = '';
        oImgNext.className = 'imgNavButtonDisabled';
        oImgNext.src = g_base_url + 'img/nav_button_forward_dis.gif';
    } else {
        oImgNext.onclick = new Function('mediaShowNextImage()');
        if( /nav_button_forward_dis.gif/.test(oImgNext.src) ){
            oImgNext.className = 'imgNavButton';
            oImgNext.src =  oImgNext.src.replace(/nav_button_forward_dis.gif/, 'nav_button_forward.gif');
        }

    }
    
    var oModal = $('modal_window');
    var modal_width = img_width;
    var modal_height = img_height;
    if(modal_width <500){
        modal_width = 500;
    };
    if(modal_height <600){
        modal_height = 600;
    };
    oModal.style.width = modal_width + 30 + 'px';
    oModal.style.height = modal_height + 'px';
    
    var modal_width = parseInt(oModal.style.width, 10);
    
    // Изображение меньше размеров экрана
    var left = 0;
    if(oModal.offsetWidth < document_scroll_width){
        var left = (document.documentElement.scrollWidth - modal_width)/2;
    }    
    oModal.style.left =  left + "px";    
}

function arrayPosition(arr, id){
    var pos = -1;
    for(var i = 0; i < arr.length; i++){
        if(arr[i] == id){
            pos = i;
        }
    }
    return pos;
}

/* Video */
// var g_media_video = new Array(); in media_list.php

function mediaVideoShowPreview(media_id){
    var oImg = $('mediaVideoPreview');
    var suffix = '_sm';
    if(g_module == 'casting') suffix = '';
    var re = new RegExp('/video/' + media_id + suffix + '.jpg');

    if(false === re.test(oImg.style.background)){
        if(g_module == 'casting') $('mediaVideoPreview').src = g_media_path + 'video/' + media_id + suffix + '.jpg';
        else $('mediaVideoPreview').style.background = 'url(' + g_media_path + 'video/' + media_id + suffix + '.jpg' + ') no-repeat left top';
    }
    
    for(var i=0; i<g_media_video.length; i++){
        if(g_media_video[i] != media_id){
            $('mediaVideoBt'+g_media_video[i]).className = '';
        }
    }
    $('mediaVideoBt'+media_id).className = 'active';
}


function mediaShowVideoPlayer(media_id, desc, is_show_video_amateur){
    hideFlash();
    hideAllSelect();
    g_media_type = 'video';
           
    if(undefined === desc){
        var img_desc = $('mediaVideoBt'+media_id).getAttribute('_desc');
    } else {
        var img_desc = desc;
    }
    
    var img_path = '';
    var img_width = 300;
    var img_height = 300;
    var media_item_number = 1;
    
    var oBody = document.body;
    var oShadow = document.createElement('div');
    oShadow.id = 'modal_shadow';
    oShadow.style.height = window.getScrollHeight() + 'px';
    oBody.appendChild(oShadow);

    var oModal = document.createElement('div');
    oModal.id = 'modal_window';

    out = '' +
        '<table border=0 cellpadding="0" cellspacing="0" style="position:relative;width:100%">' +
        '<tr>' +
            '<td style="text-align: right; background: url(' +g_base_url+ 'img/photo_win_left.gif) no-repeat bottom right; height: 55px;"></td>' +
            '<td class="photo_header">' +
                '<table>' +
                '<tr>' +
//                    '<td width="100%">Видео</td>' +
                    '<td width="100%">&nbsp;</td>' +
                    '<td nowrap="nowrap"><img onclick="closeModalMessage()" class="imgNavButton" src="' +g_base_url+ 'img/nav_close.gif" alt="" /></td>' +
                '</tr>' +
                '</table>' +
            '</td>' +
            '<td style="text-align: left; background: url(' +g_base_url+ 'img/photo_win_right.gif) no-repeat bottom left; height: 55px;"></td>' +
        '</tr>' +
        '<tr>' +
            '<td style="background: url(' +g_base_url+ 'img/photo_frame_l_corner.gif) no-repeat top right; height: 9px; width: 15px;"></td>' +
            '<td style="background: url(' +g_base_url+ 'img/photo_frame_c_corner.gif) repeat-x top left; width: auto"></td>' +
            '<td style="background: url(' +g_base_url+ 'img/photo_frame_r_corner.gif) no-repeat top left; height: 9px; width: 15px;"></td>' +
        '</tr>' +
        '<tr>' +
            '<td style="background: url(' +g_base_url+ 'img/photo_frame_l_bg.jpg) repeat-y top right; width: 15px;"></td>' +
            '<td style="background-color: black; width: auto;height: 100%">' +
                '<div id="divFlvPlayer"><p style="background: #ffffff">' +
                localization.web_main_non_flash +
                '<br/><br/><a href="http://www.macromedia.com/go/getflashplayer"><img src="http://www.macromedia.com/images/shared/download_buttons/get_flash_player.gif" alt="Get macromedia Flash Player" style="border: none;" /></a></p></div>' + 
            '</td>' +
            '<td style="background: url(' +g_base_url+ 'img/photo_frame_r_bg.jpg) repeat-y top right; width: 15px;"></td>' +
        '</tr>' +
        '<tr>' +
            '<td style="background: url(/img/photo_frame_l_corner_d.gif) no-repeat top right; height: 10px; width: 15px;"></td>' +
            '<td style="background: url(/img/photo_frame_c_corner_d.gif) repeat-x top left; width: auto"></td>' +
            '<td style="background: url(/img/photo_frame_r_corner_d.gif) no-repeat top left; height: 10px; width: 15px;"></td>' +
        '</tr>' +
        '<tr>' +
            '<td style="text-align: right; background: url(' +g_base_url+ 'img/photo_frame_left_b.gif) no-repeat top right; height: 106px;"></td>' +
            '<td class="photo_title">' +
                '<table>' +
                '<tr>' +
                    '<td width="100%" id="mediaPictureDesc">' +img_desc+ '</td>' +
                '</tr>' +
                '</table>' +
            '</td>' +
            '<td style="text-align: left; background: url(' +g_base_url+ 'img/photo_frame_right_b.gif) no-repeat top left; height: 106px;"></td>' +
        '</tr>' +
        '</table>'
    ;

    oModal.innerHTML = out;
    if(img_width <500){
        img_width = 500;
    };
    if(img_height<600){
        img_height = 600;
    }
    oModal.style.width = img_width + 30 + 'px';
    oModal.style.height = img_height + 'px';

    var modal_width = parseInt(oModal.style.width, 10);
    oModal.style.top = document.documentElement.scrollTop  + 25 + "px";

    var left = (document.documentElement.scrollWidth - modal_width)/2;
    if(left<0){
        left = 0;
    }
    oModal.style.left =  left + "px";

    oBody.insertBefore(oModal,oBody.firstChild);
    document.addEvent('keydown', modalEvent);

    var FO = { movie: g_base_url + "img/flvplayer.swf", width:"420", height:"320", majorversion:"7", build:"0", id:"flvPlayer" };
    if(undefined === is_show_video_amateur){
        FO.flashvars = 'file='+ g_media_path + 'video/' + media_id +'.flv&showdigits=true&autostart=true';
    } else {
        FO.flashvars = 'file='+ g_video_amateur_path + media_id +'.flv&showdigits=true&autostart=true';
    }
    var obj = UFO.create(FO, "divFlvPlayer");
}

/* Video amateur vote*/
var g_rate_active = new Array();
var g_rate_inactive = new Array();
for(var i=0; i<5; i++){
    g_rate_active[i] = new Image();
    g_rate_active[i].src = g_base_url + 'img/digit_' + (i+1) + '_a.gif';

    g_rate_inactive[i] = new Image();
    g_rate_inactive[i].src = g_base_url + 'img/digit_' + (i+1) + '.gif';
}
var g_list = new Array();

function voteOnMouse(id, is_active, rate_num){
	var div = document.getElementById('rate'+id);
    var imglist = div.getElementsByTagName('img');

    if(is_active){
        g_list = new Array();
        for(var i=0; i<rate_num; i++){
            g_list.push(i);
            imglist[i].src = g_rate_active[i].src;
        }
    } else{
        for(var i=0; i<g_list.length; i++){
            imglist[i].src = g_rate_inactive[i].src;
        }
    }
}

function voteVideo(id, rate_num){
    var aj = new Ajax(g_ajax_url + '?mod=video&act=set_vote&id='+id+'&rate='+rate_num , {method:'get', onComplete:voteVideo_ok}).request();
}

function voteVideo_ok(text){
    var obj = Json.evaluate(text);
    $('voteRating'+obj.video_id).innerHTML = obj.rating;
    $('voteCount'+obj.video_id).innerHTML = obj.vote_count;
    $('rate'+obj.video_id).innerHTML = '<span class="video_atention">Вы уже голосовали!</span>';
}

// ---------------------------------------------------

function showFlash(){
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i < flashObjects.length; i++) {
		flashObjects[i].style.visibility = "visible";
	}

	var flashEmbeds = document.getElementsByTagName("embed");
	for (i = 0; i < flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "visible";
	}
}


function hideFlash(){
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i < flashObjects.length; i++) {
		flashObjects[i].style.visibility = "hidden";
	}

	var flashEmbeds = document.getElementsByTagName("embed");
	for (i = 0; i < flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "hidden";
	}

}

function toLog(value){
  if($('debugLog'))
  {
    var oDiv = $('debugLog');
    oDiv.innerHTML += value + '<br/>';
  }
  else
  {
    var oDiv = document.createElement('div');
    oDiv.id = 'debugLog';
    oDiv.style.textAlign = 'left';
    oDiv.innerHTML = 'Logging...' + '<br/>';
    document.body.appendChild(oDiv);
    toLog(value);
  }
}

function removeMySelf(id){
    if($(id)){
        $(id).parentNode.removeChild($(id));
        return true;
    }
    return false;
}
