openTopSlide = '';

$(document).ready( function (){
    $('.extrasSliderClip a').click(showTopSlidePlayer);
    
    $('.close-player a').live('click', function(){
        extrasPlayerEffectClose(topSlidePlayerContainer, "top");
        $('#topSliderContainer .extrasSliderClip').removeClass('active');       
        return false;
    });
    
    function showTopSlidePlayer() {
        var div = $(this).parents('#topSliderContainer');
        var id = $(this).attr('item');
        var topSlidePlayer = $('<div id="topSlidePlayer"></div>');
        
        if(!(openTopSlide)) { //if clicked player isn't open
            topSlidePlayerContainer = $('<div id="topSlidePlayerContainer" class="slidePlayerContainer"></div>');
            $(div).after(topSlidePlayerContainer);
            topContentDiv = $('<div id="top-slide-clip-content" class="slide-clip-content"></div>');
            topSlidePlayerContainer.append(topSlidePlayer);
            topSlidePlayerContainer.prepend(topContentDiv);
            extrasPlayerEffectOpen(topSlidePlayerContainer);
            openTopSlide = id;  
            showExtrasPlayer(id, topSlidePlayer, topContentDiv, "top");
            $('#topSliderContainer .extrasSliderClip').removeClass('active');
            $(this).parents('.extrasSliderClip').addClass('active');
        }
        else if(openTopSlide == id){ //click same clip, close player
            extrasPlayerEffectClose(topSlidePlayerContainer, "top");
            $('#topSliderContainer .extrasSliderClip').removeClass('active');
        }
        else {
            openTopSlide = id;
            showExtrasPlayer(id, topSlidePlayer, topContentDiv, "top");
            $('#topSliderContainer .extrasSliderClip').removeClass('active');
            $(this).parents('.extrasSliderClip').addClass('active');
        }
        return false;
    }
    
    $('#submitDate').live('click',function(){
                // the minumum age you want to allow in 
                var min_age = 18;
    
                // change "age_form" to whatever your form has for a name="..." 
                
                var year = parseInt(document.forms["age_form"]["year"].value);
                var month = parseInt(document.forms["age_form"]["month"].value) - 1;
                var day = parseInt(document.forms["age_form"]["day"].value);
                
                
                //var $('form[name=age_form] select')
                var theirDate = new Date((year + min_age), month, day);
                var today = new Date;
    
                if ( (today.getTime() - theirDate.getTime()) < 0) {
                    //set temp session cookie
                    document.cookie = "epixAgeRating=0";
                    $('#rating-control').fadeOut(function(){$('#gatekeeper').show(); });     
                }
                else {                           
                    setCookie('epixAgeRating',1,365)
                    $('#rating-control').fadeOut(function(){createClipPlayer(clipData, divID); });
                }                
                return false;
    });

    function showExtrasPlayer(item, clipDiv, clipContentDiv, type) {
    
        if(type == "bottom"){
            var assetID = item.attr('id').replace('clip','');
        }
        else {
            var assetID = item;
        }

        divID = clipDiv[0].id;

        $.getJSON('/ajax/asset/',{'asset_id':assetID, 'type':2},function(data){
            clipData = data;
            if(data.success) {
                if (type != 'fader'){
                    if(data.movie)
                        $(clipContentDiv).html('<div id="close'+type+'" class="close-player"><a href="#"><img src="http://content.epixhd.com/styleassets/closebutton.png" /></a></div><h1>'+data.movie.title+':<br/>'+data.title+'</h1>');
                    else
                        $(clipContentDiv).html('<div id="close'+type+'" class="close-player"><a href="#"><img src="http://content.epixhd.com/styleassets/closebutton.png" /></a></div><h1>'+data.title+'</h1>');                    
                    if(data.description) $(clipContentDiv).append('<p>'+data.description+'</p>');
                    if(data.id == 2746) $(clipContentDiv).append('<p><a href="/fanboy-fest">Click here</a> to see all the action</p>');
                    if ( data.id == 3636 ) {
                        var generatedLink = $('<h2><a href="/8-nights-of-woody">GO TO THE MOVIE PAGE</a></h2>');
                        $(clipContentDiv).append(generatedLink);
                    }
                    //if(data.tags) $(clipContentDiv).append('<p>Tags: '+data.tags+'</p>');
                    
                    if((data.movie) && (data.movie.title != 'EPIX Hi-Defkus'))
                        $(clipContentDiv).append('<h2><a href="'+data.movie.url+'">Go to the movie page</a></h2>');
                    
                    if ((user_status.loggedin) && (data.movie)) {
                        if(data.movie.inwindow){
                            movieTagName = data.movie.url.split('.com/')[1];
                            $(clipContentDiv).append('<h2><a href="/submit_schedule/'+movieTagName+'">watch with friends</a></h2>');
                        }
                    } 
                }
                if(data.rating == "R"){ //show age gate
                    if((user_status.rating == "R") || (user_status.rating == "NC-17"))y=2; //set and can watch R
                    else if(user_status.rating) y=1; //set and cannot watch R
                    else y=0; //not set                              
                    var x = readCookie('epixAgeRating');
                    
                    if((x == 1) || (y == 2)) createClipPlayer(data, divID);
                    else{
                        $('#'+divID).empty().hide();
                        //$('#topSlidePlayerContainer').append('<div id="'+divID+'"></div>');
                        $('#mature-gate').appendTo($('#topSlidePlayerContainer')).show();
                        if ((x == 0) || (y == 1)) $('#gatekeeper').show(); //can't watch
                        else $('#rating-control').show(); //ask age
                    }
                }
                else {
                    $('#'+divID).show();
                    $('#mature-gate').hide();
                    createClipPlayer(data, divID);
                }
            }
        });
    }
        
    globalSEP = showExtrasPlayer;

    function createClipPlayer(data,divID){
        //alert(divID);
        // Pass the movie information
        if(data.movie) {
            var pid = data.movie.id;
            var pname = data.movie.title;
        }
        else {
            var pid = data.id;
            var pname = data.category;
        }
        
        var flash_data = {
            flashvars: {
                
                content_type:         'videoextra',
                content_subtype:         data.category,
                asset_id:        data.id,
                asset_name:    data.category,
                parent_title_asset_id:     pid,
                parent_title_asset_name:     pname,
                user_mso:       '0',
                user_coupon:       '0',
                anon_user_id:     '0',
                mode:         'solo',
                host:         false,
                jsAPI:        true,
                contentID:    'epix1',
                playlist:     data.smil,
                roomID:       '0',
                username:     '',
                userID:       '',
                token:        '',
                autoplay:      true
        
            },
            params: {
                id:            divID,
                width:         550,
                height:        375
            }
        };
        setupPlayer(flash_data);
          $('#slideshow-bg .loader .indicator').hide();
    }

    function autoScroll(div){
       $('html,body').animate({
          scrollTop: ($(div).offset().top-285)
       }, 500);
    }

    function extrasPlayerEffectOpen(div) {
      div.animate({height: '425px'}, 500, 'easeOutCubic', function() {
          autoScroll(div);
      });
    }
    
    function extrasPlayerEffectClose(div, type) {
        openTopSlide = "";  
        $('#mature-gate').appendTo($('#content')).hide();   
        div.animate({height: '0px'}, 500, 'jswing', function() {
         $(this).empty().remove();
        });
        return false;
    }
    
    $("#topSliderContainer .slider").easySlider({
        controlsBefore: '<div class="topSliderNav">',
        controlsAfter: '</div>',
        nextId: 's-nextBtn',
        prevId: 's-prevBtn',
        numeric: true,
        numericId: 'singlecontrols-t',
        continuous: false,
        bullets: false
    });
                           
    $("#top-movies .slider").easySlider({
        controlsBefore: '<div class="topSliderNav">',
        controlsAfter: '</div>',
        nextId: 's-nextBtn',
        prevId: 's-prevBtn',
        numeric: true,
        numericId: 'singlecontrols',
        continuous: false,
        bullets:false
    });
});
