/* FAVORITE { */
function addToFavourite(favEl, videoId)
{
    favEl.old = favEl.onclick;
    favEl.onclick = '';
    var params = new Array(favEl, videoId);
    sendHTTPRequest('ajax/playlists_add_video/?&video_id=' + videoId + '&playlist_type_id=2', setFavouriteStatus, params);
}

function removeFromFavourite(favEl, videoId)
{
    favEl.old = favEl.onclick;
    favEl.onclick = '';
    var params = new Array(favEl, videoId);
    sendHTTPRequest('ajax/playlists_remove_video/?&video_id=' + videoId + '&playlist_type_id=2', unsetFavouriteStatus, params);
}

function unsetFavouriteStatus(xml_http, params)
{
    favEl = params[0];
    videoid = params[1]; 
    
    showAlert(i18n['m0524'] ? i18n['m0524'] : 'This video has been removed from your favorites.');
    favEl.className = 'favorite';
    if(is_ie)
    {   
        favEl.setAttribute("onclick", new Function ("addToFavourite(this," + videoid + ");"));    
    }
    else
    {
        favEl.setAttribute('onclick', 'addToFavourite(this,' + videoid +');');
    } 
    favEl.firstChild.nodeValue = i18n['m0116'] ? i18n['m0116'] : 'Add to favorites';    
}

function setFavouriteStatus(xml_http, params)
{
    favEl = params[0];
    videoid = params[1]; 
    
    showAlert(i18n['m0525'] ? i18n['m0525'] : 'This video has been added to your favorites.');
    favEl.className = 'remfavorite';
    favEl.firstChild.nodeValue = i18n['m0117'] ? i18n['m0117'] : 'Remove from favorites';
    
    if(is_ie)
    {   
        favEl.setAttribute("onclick", new Function ("removeFromFavourite(this," + videoid + ");"));    
    }
    else
    {
        favEl.setAttribute('onclick', 'removeFromFavourite(this,' + videoid +');');
    }     
}
/* } FAVORITE */

/* MORE VIDEO { */
function sendVideoItemList(listStr, pageNo, perPage, sortId, videoId)
{
    listEl = document.getElementById(listStr);
    if (listEl.pageNo && listEl.pageNo != 0 && pageNo == 0)
    {
        if (listEl.perPage && listEl.perPage == perPage) 
        {
            pageNo = listEl.pageNo;
            sortId = listEl.sortId;
        }
    }
    listEl.pageNo = pageNo;
    listEl.perPage = perPage;
    listEl.sortId = sortId;
    
	fixItem(listEl, 'hidden');

	els = listEl.childNodes;
	existEl = null;
    for (i = 0; i < els.length; i++)
    {
        if (els[i].id == pageNo + '_' + perPage + '_' + sortId)
        {
            els[i].className = '';
            existEl = els[i];
        }
        else
        {
            els[i].className = 'hidden';
        }
    }
    if (!existEl)
    {
        divEl = listEl.appendChild(document.createElement('div'));
        divEl.setAttribute('id', pageNo + '_' + perPage + '_' + sortId);
        loadingMessage(divEl);
        
        var params = new Array(divEl, 1);
        sendHTTPRequest('ajax/xml_videolist/?&video_id=' + videoId + '&sort=' + sortId + '&list_id=' + listStr + '&pn=' + pageNo + '&pp=' + perPage, setItemList, params);
    }
}
/* } MORE VIDEO */

/* EMBED PLAYER CUSTOMIZATION { */
function setEmbedPlayerSize(size, wide)
{
    var width_el = document.getElementById('embeded_width');
    var height_el = document.getElementById('embeded_height');
    var aspect_ratio_el = document.getElementById('embeded_lock_aspect_ratio');
    var custom_size_el = document.getElementById('change_size_custom');
    
    switch (size)
    {
        case 1: //  big
            width_el.value = 480;
            height_el.value = wide ? 270 : 360;
            break;
        case 2: //  small
            width_el.value = 285;
            height_el.value = wide ? 160 : 240;
        case 3: //  custom
            break;
    }
    
    applyEmbedPlayerChanges();
}

function setEmbedPlayerColor(color_1, color_2)
{
    document.getElementById('embeded_color').value = new Array(color_1, color_2);
    applyEmbedPlayerChanges();
}

function keepEmbededAspectRatio(id, wide)
{
    var va = wide ? 16 : 4;
    var ha = wide ? 9 : 3;
    
    if (document.getElementById('embeded_lock_aspect_ratio').checked)
    {
        var width_el = document.getElementById('embeded_width');
        var height_el = document.getElementById('embeded_height');
        
        if (id == 'embeded_width')
        {
            height_el.value = Math.round(ha*width_el.value/va);
        }
        else
        {
            width_el.value = Math.round(va*height_el.value/ha);
        }
        //el_2.value = (int)el_1.value*4/3;
        
        var custom_size_el = document.getElementById('change_size_custom');
        
        custom_size_el.checked = true;  //  for FF && IE
        custom_size_el.setAttribute('checked', 'checked');  //  for
    }
    
    applyEmbedPlayerChanges();
}

function applyEmbedPlayerChanges()
{
    var container_el = document.getElementById('embeded_container');
    var source_el = document.getElementById('embeded_source');
    var width = document.getElementById('embeded_width').value;
    var height = document.getElementById('embeded_height').value;
    var video_id = document.getElementById('embeded_video_id').value;
    var lang_id = document.getElementById('embeded_lang_id').value;
    var sort_id = document.getElementById('embeded_sort_id').value;
    var colors = document.getElementById('embeded_color').value;
    if (colors == null)
    {
        colors = new Array('0xa2a2a2', '0x3e3e3e');
    }
    var source = '<object width="' + width + '" height="' + height + '">' +
        '<param name="allowfullscreen" value="true" />' +
        '<param name="allowscriptaccess" value="always"/>' +
        '<param name="movie" value="http://openfilm.com/flvplayer5.swf?link=http://openfilm.com/' +
        'player/video/%3Fvideo%5Fid%3D' + video_id + '%26lang%3D' + lang_id + 
        '%26sort%5Fid%3D' + sort_id + '&color1=' + colors[0] +'&color2=' + colors[1] + 
        '&embed=true&showsettings=false&autostart=false&reload_on_video_click=true&fullscreen=true"/>'+
        '<embed src="http://openfilm.com/flvplayer5.swf?link=http://openfilm.com/' +
        'player/video/%3Fvideo%5Fid%3D' + video_id + '%26lang%3D' + lang_id + 
        '%26sort%5Fid%3D' + sort_id + '&color1=' + colors[0] + '&color2=' + colors[1] + 
        '&embed=true&showsettings=false&autostart=false&reload_on_video_click=true&fullscreen=true" allowscriptaccess="always" ' +
        'type="application/x-shockwave-flash" allowfullscreen="true" width="' + width + '" height="' + height + '"></embed></object>';
    container_el.innerHTML = source;
    source_el.value = source;
}
/* } EMBED PLAYER CUSTOMIZATION */

function shareStats(resource_name, link)
{
    sendHTTPRequest('ajax/sharing_stats/?resource_name=' + resource_name, shareStatsForceLink, link);
}

function shareStatsForceLink(xml_http, link)
{
    window.location = link;
}