﻿        function configChartItem(totalcountinfo, countbarinfo, countbarmultipler) {
            this.totalCountInfo = totalcountinfo;
            this.countBarInfo = countbarinfo;
            this.countBarMultipler = countbarmultipler;
        }
        
        function dataChartItem(rank, lastrank, productid, artistid, albumid, artistname, artistlist, productname, displayPrice, unitPrice, discountPrice, downloadBitRate, downloadFileType,
                             totalcount, barcount, barwidth, isexplicit, pcallow, downloadallow, producttype, itemtype) {
            this.rank = rank;
            this.lastRank =lastrank;
            this.productId = productid;
            this.albumId = albumid;
            this.artistId = artistid;
            this.artistName = artistname;
            this.artistList = artistlist;
            this.productName = productname;
            this.totalCount = totalcount;
            this.barCount = barcount;
            this.barWidth = barwidth;
            this.isExplicit = isexplicit;
            this.pcAllow = pcallow;
            this.downloadAllow = downloadallow;
            this.productType = producttype;
            this.itemType = itemtype;
            this.downloadBitRate = downloadBitRate;
            this.downloadFileType = downloadFileType;
            this.displayPrice = displayPrice;
            this.unitPrice = unitPrice;
            this.discountPrice = discountPrice;
        }
        
        /*
        var MSProduct = new Class({
            initialize: function(productid) {
                this.productid = productid;
                this.producttitle 
                this.artist
                this.artistid
                this.artistlist
                this.composer
                this.composerid
                this.composerlist
                this.author
                this.arranger
                this.arrangerid
                this.arrangerlist
                this.producer
                this.producerid
                this.producerlist
                this.lyrics
                this.lyricsid
                this.lyricist
                this.lyricistid
                this.lyricistlist
                this.productlength
                this.producttype
                this.albumname
                this.albumid
                this.lyricclob
                this.explicitlyrics
                this.albumtitle
                this.albumversiontitle
                this.cnote
                this.haslyric
            },
            setProductId: function(productid){
                this.productid = productid;
            }
        });
        */
        
        /* //ChartItemUI Config Function Example
        var dataObj = new dataChartItem(1, 5, 'VASB00039946', '00025767', 'VASB00003196A', '中島美嘉', new Array(new artistObj('00025767','中島美嘉'),new artistObj('00000080','鄧健泓')), 'Amazing Grace',1000,500,500, true, true, 'AUDIO', 'MsProduct');
        var max = 0;
        if (max < dataObj.barWidth) {
            max = dataObj.barWidth;
        }
        addChartItem("chartDiv",dataObj);
        function addChartItem(targetID, item) {
            var configObj = new configChartItem('累積:','本日:',540);
            var target = $(targetID);
            target.adopt(createChartItemMT(item, configObj, max));
        }*/
        
        function artistObj(artistid, artistname) {
            this.artistName = artistname;
            this.artistID = artistid;
        }
        
        function artistNameList(artistNameList,cssartistclassname ,cssdivclassname) {
            var artistspan = new Element('span', {'class':cssdivclassname});
            artistNameList.each(function(item,index){
                var artistName = new Element('a', { href:'/music/contributor/contributordetail.do?contributorid='+item.artistID+'&roleid=Artist&ptype=AUDIO',
                                                    'class':cssartistclassname,
                                                    'html':item.artistName,
                                                    'alt':item.artistName,
                                                    'title':item.artistName});
                artistspan.adopt(artistName);
                if(artistNameList.getLast()!=item){
                    var commaNode = new Element('span', {'html':', '});
                    artistspan.adopt(commaNode);
                }
            });
            return artistspan;
        }
        
        function createChartItemMT(dataObject, configObject, maxCount){
            var chartItem = new Element('div', {'class':'product'});

            var itemleft = new Element('div', {'class':'chart_left'});
            chartItem.adopt(itemleft);
            var itemright = new Element('div', {'class':'chart_right'});
            chartItem.adopt(itemright);
            
            var rankPos = new Element('div', {  'class':'item1',
                                                'html': dataObject.rank});
            itemleft.adopt(rankPos);
            
            var rank1 = dataObject.rank;
            var rank2 = dataObject.lastRank;
            var rankCompareStr = '';
            var rankCompareInt = '';
            if (rank2 == null) {
                rankCompareStr = 'new';
                rankCompareInt = '';
            } else if (rank1 > rank2) {
                rankCompareStr = 'down';
                rankCompareInt = ''+rank1-rank2;
            } else if (rank1 < rank2) {
                rankCompareStr = 'up';
                rankCompareInt = ''+rank2-rank1;
            } else {
                rankCompareStr = 'none';
                rankCompareInt = '';
            }
            
            var rankPos2 = new Element('div', { 'class':"item2"+rankCompareStr,
                                                'html':rankCompareInt});
            itemleft.adopt(rankPos2);
            var itemPhoto = new Element('div', {'class':'item3'});
            itemleft.adopt(itemPhoto);
            
            var photoAnchor = new Element('a', {'class':'cover', href:(dataObject.pcAllow)?('/music/product/productdetail.do?contributorid='+dataObject.artistId+'&roleid=Artist&productid='+dataObject.productId):("javascript:void(0)")});
            itemPhoto.adopt(photoAnchor);
                var photoAnchorImg = new Element('img', {'class':'coverImg', 'src':'http://moov.netvigator.com/mposter/album/s/'+dataObject.albumId+'_s.jpg', 'alt':''});
                photoAnchor.adopt(photoAnchorImg);
            
            var btnDiv = new Element('div', {'class':'item1'});
            itemright.adopt(btnDiv);
                if (dataObject.pcAllow || dataObject.downloadAllow) {
                    var btnSpan1 = new Element('span', {'class':'btnPrelisten'});
                    btnDiv.adopt(btnSpan1);
                
                    if (dataObject.productType != 'VIDEO') {
                    var btnImg1 = new Element('img', {  'class':'btnPrelistenImg', 
                                                        'src':'/music/images/buttons/chart_listen.gif', 
                                                        'alt':'免費Player試聽',
                                                        'title':'免費Player試聽',
                                                        events: {   click: function() {
                                                                        //alert('play: ['+dataObject.productId+']');
                                                                        if (dataObject.itemType == 'MsProduct') {
                                                                          previewall2(dataObject.productId);
                                                                        //} else if (itemType == 'MsAlbum') {
                                                                        //  previewall2(dataObject.productId);
                                                                        //} else if (itemType = playlist) {
                                                                        //  previewall2(dataObject.productId);
                                                                        } else {
                                                                          previewall2(dataObject.productId);
                                                                        }
                                                                    },
                                                                    mouseover: function() {
                                                                        this.src='/music/images/buttons/chart_listen_over.gif';
                                                                    },
                                                                    mouseout: function() {
                                                                        this.src='/music/images/buttons/chart_listen.gif';
                                                                    }
                                                        }});
                        btnSpan1.adopt(btnImg1);
                    }
                }
                if (dataObject.pcAllow) {
                    var btnSpan2 = new Element('span', {'class':'btnPlay'});
                    btnDiv.adopt(btnSpan2);
                    if (dataObject.productType != 'VIDEO') {
                        var btnImg2 = new Element('img', {  'class':'btnPlayImg', 
                                                            'src':'/music/images/buttons/chart_play.gif', 
                                                            'alt':'播放足本歌曲',
                                                            'title':'播放足本歌曲',
                                                            events: {   click: function() {
                                                                            //alert('play: ['+dataObject.productId+']');
                                                                            instantplay(dataObject.productId);
                                                                        },
                                                                        mouseover: function() {
                                                                            this.src='/music/images/buttons/chart_play_over.gif';
                                                                        },
                                                                        mouseout: function() {
                                                                            this.src='/music/images/buttons/chart_play.gif';
                                                                        }
                                                            }});
                        btnSpan2.adopt(btnImg2);
                    } else { //Video
                        var btnImg2 = new Element('img', {  'class':'btnPlayImg', 
                                                            'src':'/music/images/buttons/mv_play.gif', 
                                                            'alt':'播放足本影片',
                                                            'title':'播放足本影片',
                                                            events: {   click: function() {
                                                                            //alert('playvideo: ['+dataObject.productId+']');
                                                                            instantplay(dataObject.productId);
                                                                        },
                                                                        mouseover: function() {
                                                                            this.src='/music/images/buttons/mv_play_over.gif';
                                                                        },
                                                                        mouseout: function() {
                                                                            this.src='/music/images/buttons/mv_play.gif';
                                                                        }
                                                            }});
                        btnSpan2.adopt(btnImg2);
                    }
                }
                if (dataObject.pcAllow) {
                    var btnSpan3 = new Element('span', {'class':'btnAdd'});
                    btnDiv.adopt(btnSpan3);
                    if (dataObject.productType != 'VIDEO') {
                        var btnImg3 = new Element('img', {  'class':'btnAddImg', 
                                                            'src':'/music/images/buttons/chart_add.gif', 
                                                            'alt':'歌曲儲存至Playlist',
                                                            'title':'歌曲儲存至Playlist',
                                                            events: {   click: function() {
                                                                            //alert('play: ['+dataObject.productId+']');
                                                                            popPlaylist(dataObject.productId,dataObject.productName,dataObject.artistName,dataObject.productType,null);
                                                                        },
                                                                        mouseover: function() {
                                                                            this.src='/music/images/buttons/chart_add_over.gif';
                                                                        },
                                                                        mouseout: function() {
                                                                            this.src='/music/images/buttons/chart_add.gif';
                                                                        }
                                                            }});
                        btnSpan3.adopt(btnImg3);
                    } else { //Video
                        var btnImg3 = new Element('img', {  'class':'btnAddImg', 
                                                            'src':'/music/images/buttons/mv_add.gif', 
                                                            'alt':'影片儲存至Playlist',
                                                            'title':'影片儲存至Playlist',
                                                            events: {   click: function() {
                                                                            //alert('play: ['+dataObject.productId+']');
                                                                            popPlaylist(dataObject.productId,dataObject.productName,dataObject.artistName,dataObject.productType,null);
                                                                        },
                                                                        mouseover: function() {
                                                                            this.src='/music/images/buttons/mv_add_over.gif';
                                                                        },
                                                                        mouseout: function() {
                                                                            this.src='/music/images/buttons/mv_add.gif';
                                                                        }
                                                            }
                                                         });
                        btnSpan3.adopt(btnImg3);
                    }
                 }
                 
                 if (dataObject.downloadAllow) {
                 	var ptype = null;
                 	if (dataObject.itemType=='MsProduct') {
                 		ptype="product";
                 	} else if (dataObject.itemType=='MsAlbum'){
                 		ptype="album";
                 	}
                 
                    var btnSpan4 = new Element('span', {'class':'btnDownload'});
                    btnDiv.adopt(btnSpan4);
                    if (dataObject.productType != 'VIDEO') {
                    	var mp3desc = "";
                    	if (ptype=="album") {
                    		mp3desc = "購買MP3";
                    	} else {
                    		mp3desc = "購買或換領 MP3";
                    	}
                    	var btnImg4 = new Element('img', {  'class':'btnDownloadImg', 
                                                            'src':'/music/images/buttons/mp3.gif', 
                                                            'alt':mp3desc,
                                                            'title':mp3desc,
                                                            events: {   click: function(cevent) {
                                                                            //alert('download: ['+dataObject.productId+']');
                                                                            downloadItem(dataObject.productId, dataObject.productName, dataObject.artistName, ptype, dataObject.unitPrice, dataObject.discountPrice, dataObject.downloadBitRate, dataObject.downloadFileType, dataObject.displayPrice, dataObject.albumId, dataObject.pcAllow, dataObject.downloadAllow, dataObject.isExplicit, cevent);
                                                                        },
                                                                        mouseover: function() {
                                                                            this.src='/music/images/buttons/mp3_over.gif';
                                                                        },
                                                                        mouseout: function() {
                                                                            this.src='/music/images/buttons/mp3.gif';
                                                                        }
                                                            }});
                        btnSpan4.adopt(btnImg4);
                    }
                 }                  
            var itemInfoDiv = new Element('div', {'class':'item2'});
            itemright.adopt(itemInfoDiv);
                var artistNameSpan = new Element('span', {'class':''});
                itemInfoDiv.adopt(artistNameSpan);
                    artistNameSpan.adopt(artistNameList(dataObject.artistList,'singer','chartItemArtistDiv'));                                 
                var songNameSpan = new Element('span', {'class':''});
                itemInfoDiv.adopt(songNameSpan);
                    var songNameAnchor = new Element('a', { 'class':'song',
                                                            href:(dataObject.pcAllow)?('/music/product/productdetail.do?contributorid='+dataObject.artistId+'&roleid=Artist&productid='+dataObject.productId):("javascript:void(0)"),
                                                            'html':dataObject.productName,
                                                            'alt':dataObject.productName,
                                                            'title':dataObject.productName
                                                        });
                    songNameSpan.adopt(songNameAnchor);
                if (dataObject.isExplicit) {
                    var explicitImg = new Element('img', {'class':'explicitImg', 'src':'/music/images/revamp0808/common/explicit_button.gif', 'alt':'Explicit', 'title':'Explicit'});
                    itemInfoDiv.adopt(explicitImg);
                }
            var totalCountDiv = new Element('div', {'class':'item3'});
            itemright.adopt(totalCountDiv);
                var totalCountSpan = new Element('div', {  'class':'',
                                                            'html':configObject.totalCountInfo+' '+dataObject.totalCount});
                totalCountDiv.adopt(totalCountSpan);
            var countBarDiv = new Element('div', {'class':'countBarDiv'});
            itemright.adopt(countBarDiv);
                var barHeadSpan = new Element('div', {'class':'barHeadSpan'});
                countBarDiv.adopt(barHeadSpan);
                    var barHeadImg = new Element('img', {'class':'barHead', 'src':'/music/images/charts/spacer.gif', 'alt':''});
                    barHeadSpan.adopt(barHeadImg);
                var barInfoSpan = new Element('span', {'class':'barInfoSpan'});
                countBarDiv.adopt(barInfoSpan);
                    var barInfoImg = new Element('img', {'class':'barInfo', 'src':'/music/images/charts/spacer.gif', 'alt':''});
                    barInfoSpan.adopt(barInfoImg);
                    var barInfoTxt = new Element('div', {'class':'barInfoTxt','html':configObject.countBarInfo+' '+dataObject.barCount});
                    barInfoSpan.adopt(barInfoTxt);
                var barBodySpan = new Element('div', {'class':'barBodySpan'});
                countBarDiv.adopt(barBodySpan);
                var barwidth = dataObject.barWidth/maxCount*configObject.countBarMultipler;
                //alert("barwidth="+dataObject.barWidth+" maxCount="+maxCount+" countBarMultipler="+configObject.countBarMultipler+" Final="+barwidth);
                    var barBodyImg = new Element('img', {'class':'barBody', 'src':'/music/images/charts/spacer.gif', 'alt':'',
                                                        styles: {width:barwidth}
                                                        });
                    barBodySpan.adopt(barBodyImg);
                var barTailSpan = new Element('div', {'class':'barTailSpan'});
                countBarDiv.adopt(barTailSpan);
                    var barTailImg = new Element('img', {'class':'barTail', 'src':'/music/images/charts/spacer.gif', 'alt':''});
                    barTailSpan.adopt(barTailImg);
                
            var itemUnderlineDiv = new Element('div',{'class':'underlineDiv'});
            itemright.adopt(itemUnderlineDiv);
                var itemUnderline = new Element('img', {'class':'underline', 'src':'/music/images/charts/spacer.gif', 'alt':''});
                itemUnderlineDiv.adopt(itemUnderline);
            return chartItem;
        }