/**
 * Exclusive E-System
 * EES v4.0.0
 * Ñêðèïòû ðàçäåëà ãëàâíîé ñòðàíèöû
 *
 * @author		Eugeny P. Kuznetsov
 * @package		Exclusive E-System FMS
 */

// ÃËÎÁÀËÜÍÛÅ ÏÅÐÅÌÅÍÍÛÅ

var act_video_cat = 'new',			// ACTIVE VIDEO CAT
    act_video_ch = null,			// ACTIVE VIDEO CHANNEL
    host = '',						// ROOT PAGE HTTP ADDRESS
    vp_ani_t = 300,					// VIDEO PREV BLOCK ANIMATION TIME
    vs_ani_delay = 150,				// VIDEO STRIP DELAY BETWEEN ANIMATIONS
    vs_ani_t = 300,					// VIDEO STRIP ANIMATION TIME
    vs_data = [],					// VIDEO STRIP DATA
    vs_rew_next = false,			// VIDEO STRIP ALLOW REWIND TO NEXT SERIES
    vs_rew_prev = false;			// VIDEO STRIP ALLOW REWIND TO PREV SERIES
	
// ÂÑÏÎÌÎÃÀÒÅËÜÍÛÅ ÃËÎÁÀËÜÍÛÅ ÏÅÐÅÌÅÍÍÛÅ

var an_list_a = null;				// Ñïèñîê îáúåêòîâ ññûëîê ALLNEWS íà ÷¸ðíîì ôîíå

/**
 * GET CAT OR CHANNEL NAME FROM HREF OF CHILDREN ELEMENT A
 */
function getNameFromHref(elem)
{
    var href = elem.firstChild.getAttribute('href');
    var pos = href.lastIndexOf('/');
    if (pos != -1) {
        // Symbol / is found, so, this is CAT
        return href.substr(pos + 1);
    } else {
        // Not found, therefore, this is CHANNELS
        return 'channels';
    }
}

/**
 * REWIND VIDEO STRIP
 */





// ------------------------------------------------
// ON READY
// ------------------------------------------------

$( document ).ready( function() {
	
    // -----------
    // ØÀÏÊÀ ÑÀÉÒÀ
    // -----------

    $('#h-videocats li')
    // HANDLE MOUSEOVER AGAINST VIDEO CHANNELS
    .mouseover(function() {
        // Event is fired on video channels
        if (this.firstChild.getAttribute('href').indexOf('#') != -1) {
            // Show channel's list
            $( '#h-videochs').removeClass('vh');
            if (act_video_cat == 'channels') {
                $('#h-currch').addClass('dn');
            }
        }	
    })	
    // HANDLE MOUSEOUT AGAINST VIDEO CHANNELS
    .mouseout(function() {
        // Event is fired on video channels
        if(this.firstChild.getAttribute("href").indexOf('#') != -1) {			
            // Hide channel's list
            $('#h-videochs').addClass('vh');
            // Show current channel info
            if (act_video_cat == 'channels') {
                $('#h-currch').removeClass('dn');
            }
        }	
    })
    // HANDLE CLICK AGAINST VIDEO CATS & VIDEO CHANNELS	
    .click(function() {				
        if (this.firstChild.getAttribute('href').indexOf('#') != -1) {
            return;
        }
        // Event is fired on video cats
        else if ( this.parentNode.getAttribute('id') == 'h-videocats')
        {
            // Already active
            if ($(this).hasClass('active')) {
                return;
            }
            // Not active
            else {
                // Send XHR request and rebuild video strip on success
                sendXHR( '/video-' + getNameFromHref(this) + '/1', null, get_vstrip_ready, get_vstrip_error, this )
				video_cat_act = 'video-' + getNameFromHref(this);
				




                // Set this cat active
                $(this).siblings().removeClass('active');
                $(this).addClass('active');
                // Set global var of active cat
                act_video_cat = getNameFromHref(this);
                // Clear global var of active channel
                act_video_ch = null;
                // Hide current channel info
                $('#h-currch').addClass('dn')				
            }
        }
        // Event is fired on video channels
        else {
            // Already active
            if ($(this).hasClass('active')
                && (act_video_cat == 'channels')
            ) {
                return;
            }
            // Not active
            else {
                // Send XHR request and rebuild video strip on success
                sendXHR( '/video-channel/' + getNameFromHref(this) + '/1', null, get_vstrip_ready, get_vstrip_error, this )
				video_cat_act = 'video-channel';
				video_channel_act  = getNameFromHref(this)

                // Set this cat active
                $(this.parentNode.parentNode).siblings().removeClass('active');
                $(this.parentNode.parentNode).addClass('active');					
                // Set this channel active
                $(this).siblings().removeClass('active');
                $(this).addClass('active');
                // Set global var of active cat
                act_video_cat = 'channels';
                // Set global var of active channel
                act_video_ch = getNameFromHref(this);
                // Change content of current channel info
                $('#h-currch').text( $(this.firstChild).text() );	
                // Mouseout against channels' list
                $(this.parentNode.parentNode).mouseout()
            }			
        }
    })
    // PREPARE LINKS TO XHR REQUESTS
    .children('a').click(function(e) {
        e.preventDefault();
    });
	
    // -------------------
    // VIDEO PROFILE BLOCK
    // -------------------

    // HANDLE CLICK AGAINST CLOSE-PROFILE-BUTTON
    $('#mp-videoprofile-x').click(function() {
        // Clear swf-player container entity

        // Clear comments

        // Clear video's description

        // Reset votes

        // Close videoprofile block
        $('#mp-videoprofile').addClass('vh9999');
        // Get back top of video prev block
        $('#mp-videoprev').removeClass('videoprev-top');
        // Get back top of video strip block
        $('#mp-videostrip').removeClass('videostrip-top');
    });
	
    // -----------------
    // VIDEO STRIP BLOCK
    // -----------------


    $('#mp-videostrip > a')
    // HANDLE MOUSEOVER AGAINST VIDEO STRIP ITEM
    .mouseover( function() {
        // Event is fired on video item
        if ($(this).hasClass('item') ) {
            // Obtain index
            var index = $(this.parentNode).children('.item').index(this);
            // Already active
            if ($('#mp-videoprev-icon').attr('src') == vs_data[index][3]) {
                return;
            }
            // Update video prev data
            $('#mp-videoprev-icon').attr('src', vs_data[index][3]);
            $('#mp-videoprev-name').html(vs_data[index][1]);
            $('#mp-videoprev-brief').html(vs_data[index][2]);
            $('#mp-videoprev-date').text(vs_data[index][5]);
            $('#mp-videoprev-comments').text(vs_data[index][4]);
            $('#mp-videoprev-votes').text(vs_data[index][7]);
            $('#mp-videoprev-duration').text(vs_data[index][6]);
			
			videos_info_i = index;
			
            // Move video prev block
            var total_moveable_lenght = $('div.mp-videoprev-wr').width() - $('#mp-videoprev').width();
            $('#mp-videoprev').stop().animate(
                {left:Math.ceil(total_moveable_lenght * index / 6) + 'px'},
                {duration: vp_ani_t, easing: 'easeInOutQuad'}
            );
        }	
    } )	
    // PREPARE LINKS TO XHR REQUESTS
    .click( function( e ) {
        e.preventDefault();
        // Rewind strip to next series
        if (this.getAttribute('id') == 'mp-videostrip-next') {
            // Send XHR request and rebuild video strip on success





        }
        // Rewind strip to prev series
        else if (this.getAttribute( 'id') == 'mp-videostrip-prev') {
            // Send XHR request and rebuild video strip on success





        }
        // Select current video item
        else {
            // Send XHR request and open video profile on success





            // Show video profile block
            $('#mp-videoprofile').removeClass('vh9999');
            // Adjust top of video prev block
            $('#mp-videoprev').addClass('videoprev-top');
            // Adjust top of video strip block
            $('#mp-videostrip').addClass('videostrip-top');
        }
    } );

    // ------------
    // ALLNEWS ÁËÎÊ
    // ------------

    // Ïîäãîòàâëèâàåì mouseOver è mouseOut äëÿ ññûëîê íà ÷¸ðíîì ôîíå
    an_list_a = $('#mp-allnews-list a:not(".img")');
    an_list_a.mouseover(function() {
        // Current link is already mouseovered
        if ($(this).hasClass('active')) {

        }
        else {
            // Show img for current link
            if ($(this).next().length) {
                $(this).next().animate({opacity: 1, height: 75 + 'px'}, {duration: 333});
            }
            else {
                $(this).parent().parent().next().animate({opacity: 1, height: 75 + 'px'}, {duration: 333});
            }
            // Hide img for previousely active link & remove active class
            if (an_list_a.filter('.active').next().length) {
                an_list_a.filter('.active').removeClass('active').next().animate({opacity: 0, height: 0}, {duration: 222});
            }
            else {
                an_list_a.filter('.active').removeClass('active').parent().parent().next().animate({opacity: 0, height: 0}, {duration: 222});
            }
            // Add active class to current link
            $(this).addClass('active');			
        }
    }).eq(0).mouseover();
	
} );
