var player;
function playerReady(obj) {
    player = document.getElementById(obj['id']);
    player.addModelListener("state","videoStateTracker");
}

function videoStateTracker(obj) {
    var position = Math.round(player.getConfig().position);
};

$(document).ready(function() {
    $('#webinars-list').isotope({
        //sortBy : 'random',
        itemSelector : '.video',
        masonry: {
            columnWidth : 160
        },
        getSortData: {
            name : function ( $elem ) {
                var result = $elem.find('h2 a').text();
                return result;
            },
            popularity: function ( $elem ) {
                var result = $elem.find('.views').text();
                result = result.replace(',', '');
                result = parseInt(result);
                return result;
            },
            date: function ( $elem ) {
                var date_string = $elem.find('.date').text();
                var date_array = date_string.split('-');
                var date_obj = new Date(date_array[2], date_array[1], date_array[0]);
                result = date_obj.getTime();
                return result;
            }
            
        },
        animationOptions: {
            duration: 1000,
            easing: 'linear',
            queue: false
       }
        
    });    

    $('#video-cat a').click(function(event){
        event.preventDefault();
        var filter_name = $(this).attr('rel');
		if(filter_name == 'show-all') {
			$('#webinars-list').isotope({filter: '*'});	
		} else {
			$('#webinars-list').isotope({filter: '.' + filter_name});
		}
		
        
    });

    $('#video-sort a').click(function(event){
        event.preventDefault();
        var sortName = $(this).attr('rel');
        if(sortName == 'name') {
            $('#webinars-list').isotope({ sortBy : sortName, sortAscending : true });    
        } else {
            $('#webinars-list').isotope({ sortBy : sortName, sortAscending : false });    
        }
    });
    

    webinar_video_handler.setup();
    
});


function renderVideo(video_id, video_width, video_height, mp4_file, webm_file, ogv_file, jpg_file) {
	var ua = navigator.userAgent.toLowerCase();
	
    if(ua.indexOf('android') != -1 || ua.indexOf('ipad') != -1 || ua.indexOf('iphone') != -1 || ua.indexOf('ipod') != -1) {
        
		function html_5_video_string() {
			var video_string = '';
			video_string += '<video id="' + video_id + '-object" width="' + video_width + '" height="' + video_height + '" controls="controls" preload="none">';
			video_string += '<source type=\'video/webm; codecs="vp8, vorbis"\' src="' + webm_file + '" />';
			video_string += '<source type=\'video/ogg; codecs="theora, vorbis"\' src="' + ogv_file + '" />';
			video_string += '<source type="video/mp4" src="' + mp4_file + '" />';
			video_string += '</video>';
			return video_string;
		}
		
		$('#'+video_id).html(html_5_video_string());

	} else {

        var flashvars = { file:mp4_file, image:jpg_file, autostart:'false' };
        var params = { allowfullscreen:'true', allowscriptaccess:'always', wmode:'transparent' };
        var attributes = { id: video_id + '-object', name: video_id + '-object'};
        swfobject.embedSWF('/flash/jwplayer/player-licensed.swf', video_id, video_width, video_height, '9.0.115', 'false', flashvars, params, attributes);
	}
	
}




var webinar_video_handler = {
    setup: function() {
        
        $('#toggle-video-size a').click(function(event){
            
            event.preventDefault();
            var parent_p = $(event.currentTarget).parents('p');
            
            $('#webinars-detail-container').addClass('animating');
            
            if($(parent_p).hasClass('expanded')) {
                $(event.currentTarget).text('expand');
                $(parent_p).removeClass('expanded');
                webinar_video_handler.collapseVideo();
            } else {
                $(event.currentTarget).text('shrink');
                $(parent_p).addClass('expanded');
                webinar_video_handler.enlargeVideo();
            }
            
        });
        
        if($('#webinars-detail-container').hasClass('enlarged')) {
            $('#toggle-video-size').addClass('expanded');
            $('#toggle-video-size a').html('shrink');
            webinar_video_handler.addVideoBackground();
        }
    },
    
    enlargeVideo: function() {
        
        webinar_video_handler.addVideoBackground();
        
        var large_bkg = $('#video-enlarged-bkg');

        $('#banners-container').animate({
            queue: false,
            marginTop: 515
          }, 500, function() {
            // Animation complete.
        });

        $('#related-webinars').animate({
            queue: false,
            marginTop: 516
          }, 500, function() {
            // Animation complete.
        });

        $('#webinar-details').animate({
            queue: false,
            marginTop: 483
          }, 500, function() {
            // Animation complete.
        });
        
        $(large_bkg).css({'opacity':'0'});
        $(large_bkg).animate({
            queue: false,
            opacity: 1
          }, 500, function() {
            $('#webinar-video').css({'width': 854});
            $('#webinar-video').css({'height': 483});
            $('#webinar-video').css({'marginLeft': 61});
            $('#webinars-detail-container').removeClass('animating');
            // Animation complete.
        });           
    },
    
    collapseVideo: function() {
        
        var large_bkg = $('#video-enlarged-bkg');

        $(large_bkg).animate({
            queue: false,
            opacity: 0
          }, 500, function() {
            // Animation complete.
            $(large_bkg).remove();
        });            

        $('#related-webinars').animate({
            queue: false,
            marginTop: -55
          }, 500, function() {
            // Animation complete.
        });

        $('#banners-container').animate({
            queue: false,
            marginTop: -56
          }, 500, function() {
            // Animation complete.
        });
        
        /*
        $('#webinar-video').animate({
            queue: false,
            width: 610,
            height: 345,
            marginLeft: 0
          }, 0, function() {//500
            // Animation complete.
            $('#webinars-detail-container').removeClass('animating');
        });
        */
        
        $('#webinar-video').css({'width': 610});
        $('#webinar-video').css({'height': 345});
        $('#webinar-video').css({'marginLeft': 0});
        
        $('#webinar-details').animate({
            queue: false,
            marginTop: 345
          }, 500, function() {
            // Animation complete.
            $('#webinars-detail-container').removeClass('animating');
        });                    
        
    },
    
    addVideoBackground: function() {
        
        var large_bkg = $('#video-enlarged-bkg');
        if(large_bkg.length == 0) {
            $('body').prepend('<div id="video-enlarged-bkg"></div>');
            var large_bkg = $('#video-enlarged-bkg');
        }
        var video_offset = $('#single-video-container').offset();
        $(large_bkg).css({'top': Math.round(video_offset.top)});
        
    }
    
}
