var TPBox = new Class({ initialize: function(){ this.currentData = -1; this.displayLimit = Number( 5 ); this.dataList = new Array(); this.dataTitleId = 'tp_data_title'; this.dataView = { 'msgNoData': 'tp_msg_nodata', 'msgLoading': 'tp_msg_loading', 'postList': 'tp_post_list' }; this.postImgHeight = 60; this.postImgLinkPrefix = 'tp_img_link_'; this.postImgPrefix = 'tp_post_img_'; this.postList = $$( '#tp_post_list li' ); this.postNameLinkPrefix = 'tp_post_link_'; this.postCatLinkPrefix = 'tp_cat_link_'; this.postCatNamePrefix = 'tp_cat_name_'; this.showCats = Number( 0 ); this.postExpandHeight = this.postImgHeight + 20; this.postExpandFx = new Fx.Elements( this.postList, { duration: 0x380, fps: 25, transition: Fx.Transitions.Expo.easeInOut, wait: false } ); } }); TPBox.implement({ expandPost: function( id ) { var o = {}; var top = -80; var len = ( this.getCurrentPostList().length > this.displayLimit ) ? this.displayLimit : this.getCurrentPostList().length; for ( var i = 0; i < len; i++ ) { if ( i != id ) { o[ i ] = { 'background-color': '#d7dbe6', 'color': '#778899', 'top': top }; } else { top += this.postExpandHeight; o[ i ] = { 'background-color': '#8c9bb8', 'color': '#000000', 'top': top }; } top -= this.postExpandHeight; } this.postExpandFx.start( o ); }, getCurrentData: function() { return this.dataList[ this.currentData ]; }, getCurrentPostList: function() { return this.dataList[ this.currentData ].postList; }, getData: function( value ) { var data = null; for ( var i = 0; i < this.dataList.length; i++ ) { if ( this.dataList[ i ].value == value ) { data = this.dataList[ i ]; break; } } return data; }, hideListItem: function( i ) { this.postList[ i ].style.display = 'none'; }, insertData: function( dataTitle, dataValue, postId ) { var data = new TPData( dataTitle, dataValue, postId ); this.dataList.push( data ); if ( this.currentData == -1 || this.dataList.length == 1 ) { this.refresh(); } if ( this.dataList.length == 2 ) { this.showNavButtons(); } data.fetchData( this ); }, integrate: function() { var self = this; $( 'tp_switch_previous' ).addEvent( 'click', function() { self.switchData( -1 ); self.refresh(); } ); $( 'tp_switch_next' ).addEvent( 'click', function() { self.switchData( 1 ); self.refresh(); } ); this.postList.each( function( li, i ) { var t; li.addEvent( 'mouseenter', function( event ) { var tId = new Event( event ).stop().target.id; var id = Number( tId.slice( tId.search( /\d$/ ) ) ); t = setTimeout( function() { self.expandPost( id ); }, 0x180 ); } ); li.addEvent( 'mouseleave', function( event ) { clearTimeout( t ); } ); } ); if ( !this.showCats ) { for ( var i = 0; i < this.displayLimit; i++ ) { $E( 'div', this.postList[ i ] ).remove(); } } }, refresh: function() { this.switchData( 0 ); }, refreshHeight: function() { var lastId = this.getCurrentPostList().length - 1; if ( lastId >= this.displayLimit ) { lastId = this.displayLimit - 1; } if ( lastId < 0 ) { return; } var height = this.postList[ 0 ].offsetHeight; for ( var i = 1; i <= lastId; i++ ) { height += this.postList[ i ].offsetHeight - this.postExpandHeight; } $( this.dataView[ 'postList' ] ).style.height = height + 'px'; }, setThumbSize: function( id, di, pi ) { var post = this.dataList[ di ].postList[ pi ]; if ( !post.img.complete ) { setTimeout( 'tpbox.setThumbSize( "' + id + '", ' + di + ', ' + pi + ' );', 200 ); return; } // height already defined $( id ).width = post.img.width * ( this.postImgHeight / post.img.height ); }, showListItem: function( i ) { this.postList[ i ].style.display = 'block'; }, showNavButtons: function( i ) { $( 'tp_switch_previous' ).style.display = 'block'; $( 'tp_switch_next' ).style.display = 'block'; }, switchData: function( orientation ) { // -1 = previous, 0 = current, 1 = next0 if ( orientation != 0 ) { if ( this.dataList.length <= 1 ) { return; } if ( orientation > 0 ) { if ( this.currentData == this.dataList.length - 1 ) { this.currentData = 0; } else { ++this.currentData; } } else { if ( this.currentData == 0 ) { this.currentData = this.dataList.length - 1; } else { --this.currentData; } } } else if ( this.currentData == -1 ) { this.currentData = 0; } this.switchTitle(); this.switchPostList(); this.switchView( this.getCurrentData().state ); this.expandPost( 0 ); this.refreshHeight(); }, switchPostList: function() { var data = this.getCurrentData(); var len = ( data.postList.length > this.displayLimit ) ? this.displayLimit : data.postList.length; var i; for ( i = 0; i < len; i++ ) { this.showListItem( i ); var postName = $( this.postNameLinkPrefix + i ); var postImgLink = $( this.postImgLinkPrefix + i ); var postImg = $( this.postImgPrefix + i ); var post = data.postList[ i ]; postName.innerHTML = post.name; postName.href = post.link; postName.title = post.name; postImgLink.href = post.link; postImgLink.title = post.name; postImg.alt = post.name; postImg.title = post.name; this.setThumbSize( postImg.id, this.currentData, i ); postImg.src = post.img.src; if ( !this.showCats || post.catList.length == 0 ) { continue; } var catHolder = $( this.postCatNamePrefix + i ); catHolder.innerHTML = ''; { var a = document.createElement( 'a' ); var text = document.createTextNode( post.catList[ 0 ].name ); a.id = this.postCatLinkPrefix + '0_' + i; a.href = post.catList[ 0 ].link; a.title = post.catList[ 0 ].name; a.appendChild( text ); catHolder.appendChild( a ); } for ( var j = 1; j < post.catList.length; j++ ) { var comma = document.createTextNode( ', ' ); var a = document.createElement( 'a' ); var text = document.createTextNode( post.catList[ j ].name ); a.id = this.postCatLinkPrefix + j + '_' + i; a.href = post.catList[ j ].link; a.title = post.catList[ j ].name; a.appendChild( text ); catHolder.appendChild( comma ); catHolder.appendChild( a ); } } while ( i < this.displayLimit ) { this.hideListItem( i ); ++i; } }, switchTitle: function() { $( this.dataTitleId ).innerHTML = this.dataList[ this.currentData ].title; { var prev; if ( ( this.currentData - 1 ) >= 0 ) { prev = this.currentData - 1; } else { prev = this.dataList.length - 1; } $( 'tp_switch_previous' ).title = this.dataList[ prev ].title; } { var next; if ( ( this.currentData + 1 ) < this.dataList.length ) { next = this.currentData + 1; } else { next = 0; } $( 'tp_switch_next' ).title = this.dataList[ next ].title; } }, switchView: function( dataView ) { if ( dataView == 'postList' ) { $( this.dataView[ 'postList' ] ).style.display = 'block'; $( this.dataView[ 'msgLoading' ] ).style.display = 'none'; $( this.dataView[ 'msgNoData' ] ).style.display = 'none'; } else if ( dataView == 'msgLoading' ) { $( this.dataView[ 'postList' ] ).style.display = 'none'; $( this.dataView[ 'msgLoading' ] ).style.display = 'block'; $( this.dataView[ 'msgNoData' ] ).style.display = 'none'; } else { // set to msgNoData anyway $( this.dataView[ 'postList' ] ).style.display = 'none'; $( this.dataView[ 'msgLoading' ] ).style.display = 'none'; $( this.dataView[ 'msgNoData' ] ).style.display = 'block'; } } }); var TPCategory = new Class({ initialize: function( name, link ){ this.name = name; this.link = link; } }); var TPData = new Class({ initialize: function( title, value, postId ){ this.title = title; this.value = value; this.postId = postId; this.postList = new Array(); this.state = 'msgLoading'; } }); TPData.implement({ insertPost: function( post ) { this.postList.push( post ); }, fetchData: function( tpbox ) { var self = this; var json = new XHR( { onRequest: function() { self.state = 'msgLoading'; if ( tpbox.getCurrentData().value == self.value ) { tpbox.refresh(); } }, onFailure: function() { self.state = 'msgNoData'; if ( tpbox.getCurrentData().value == self.value ) { tpbox.refresh(); } }, onSuccess: function( jsonObj ) { var ok = self.parse( jsonObj ); if ( ok || self.postList.length > 0 ) { self.state = 'postList'; } else { self.state = 'msgNoData'; } if ( tpbox.getCurrentData().value == self.value ) { tpbox.refresh(); } if ( self.state == 'postList' ) { tpbox.expandPost( 0 ); } } } ); json.send( 'http://www.podtersaude.com.br/wp/wp-content/plugins/TopPosts/TPDataOutput.php', 'tpDataType=' + Number( self.value ) + '&postId=' + Number( self.postId ) ); }, parse: function( jsonObj ) { var postList = Json.evaluate( jsonObj ).postList; for ( var i = 0; i < postList.length; i++ ) { var post = postList[i]; var postName = post.name; var postURL = post.url; var postImage = post.image; if ( !postName || !postURL || !postImage ) { continue; } var tppost = new TPPost( postName, postURL, postImage ); var catList = post.catList; for ( var j = 0; j < catList.length; j++ ) { var cat = catList[j]; var catName = cat.name; var catURL = cat.url; var tpcat = new TPCategory( catName, catURL ); tppost.addCategory( tpcat ); } this.insertPost( tppost ); } return true; } }); var TPPost = new Class({ initialize: function( name, link, img ){ this.name = name; this.link = link; this.catList = new Array(); this.imgLoaded = false; this.img = new Image(); this.img.src = img; } }); TPPost.implement({ addCategory: function( c ) { this.catList.push( c ); } }); var tpbox = null; window.addEvent( 'load', function() { if ( !$( "top_posts" ) ) { return; } tpbox = new TPBox(); tpbox.integrate(); } );