function createProtector() {
	var protector = null;
	if (document.getElementById('protector')!=null) {
		protector = document.getElementById('protector');
		protector.parentNode.removeChild(protector);
	}
	if (document.getElementById('protector')==null) {
		protector = new Element('div', {'class':'protectorcss',
											'id':'protector'});
		document.body.appendChild(protector);
	}
}

function removeProtector() {
	var protector = null;
	if (document.getElementById('protector')!=null) {
		protector = document.getElementById('protector');
		protector.parentNode.removeChild(protector);
	}
}

function openDialog(destinURL, dwidth, dheight, dleft, dtop) {
	var dialogFrameObj = null;
	var Dleft = 180;
	var Dtop = 20+ parent.document.body.scrollTop;
	
	if (dleft!=null && typeof(dleft)=="number") {
		Dleft = dleft;
	}
	if (dtop!=null && typeof(dtop)=="number") {
		Dtop = dtop;
	}
	
	if (document.getElementById('popDialogDiv')!=null) {
		dialogFrameObj = document.getElementById('popDialogDiv');
		dialogFrameObj.parentNode.removeChild(dialogFrameObj);
	}
	/*
	if (Dleft+dwidth>990) {
		Dleft-=dwidth;
	}
	
	//var s = Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
	var s = Math.max(parent.document.body.clientHeight, parent.document.documentElement.clientHeight);
	if (Dtop+dheight-parent.document.body.scrollTop>s) {
		if (Dtop-dheight>0) {
			Dtop-=dheight;
		} else {
			Dtop-=dheight/2;
		}
	}
	*/
	if (document.getElementById('popDialogDiv')==null) {
		dialogFrameObj = new Element('div', {	'class':'dialog',
												'id':'popDialogDiv',
												'styles': {	'width':dwidth,
															'height':dheight,
															'left':Dleft,
															'top':Dtop,
															'position':'absolute',
															'z-index':90000,
															'overflow':'hidden'}});
		dialogIFrameObj = new Element('iframe', {	'class':'dialogIFrame',
													'id':'popDialogIFrame',
													'src':destinURL,
													'scrolling':'no',
													'frameborder':'0',
													'styles':{	'width':dwidth,
																'height':dheight,
																'overflow':'hidden',
																'border':'0px'}});
		dialogFrameObj.appendChild(dialogIFrameObj);
		document.body.appendChild(dialogFrameObj);
		
	} 
}

function closeDialog() {
	var dialogFrameObj = null;
	if (document.getElementById('popDialogDiv')!=null) {
		dialogFrameObj = document.getElementById('popDialogDiv');
		dialogFrameObj.parentNode.removeChild(dialogFrameObj);
	}
}

function resizeDialog(dWidth, dHeight) {
	var dialogFrameObj = null;
	if (document.getElementById('popDialogDiv')!=null) {
		dialogFrameObj = document.getElementById('popDialogDiv');
		if (document.getElementById('popDialogIFrame')!=null) {
		dialogiFrameObj = document.getElementById('popDialogIFrame');
			if (dWidth != null) {
				dialogFrameObj.setStyle('width',dWidth);
				dialogiFrameObj.setStyle('width',dWidth);
			}
			if (dHeight != null) {
				dialogFrameObj.setStyle('height',dHeight);
				dialogiFrameObj.setStyle('height',dHeight);
			}
		}
	}
}

function popDownloadCheckDialog(packid) {
	openDialog('/music/site/musicdownload/download/waitingDownloadDialog.jsp?packid='+packid,249,488,375,null);
}


function startCheckDownload(packid) {
	parent.startCheckDownload(packid);
}

function DLStatusItem(status, packid, download_url) {
	this.status = status;
	this.packid = packid;
	this.download_url = download_url;
}

function updateDownloadPanel(results, packid) {
	if (navigator.userAgent.match("MSIE")) {
		if (document.frames["popDialogIFrame"]!=null) {
			if (document.frames["popDialogIFrame"].waitingDialog != null && document.frames["popDialogIFrame"].waitingDialog == true) {
				document.frames["popDialogIFrame"].updateDownloadPanel(results, packid);
			}
		}
	} else {
		if (window.popDialogIFrame!=null) {
			if (window.popDialogIFrame.waitingDialog != null && window.popDialogIFrame.waitingDialog == true) {
				window.popDialogIFrame.updateDownloadPanel(results, packid);
			}
		}
	}
}

function refreshShoppingCart(){
	//ajaxcall updateCart
	var queryStr = 'http://moov.netvigator.com/music/site/musicdownload/shoppingcart/refreshCart_ajax.jsp';

	var jsonRequest = new Request.JSON({
		url:queryStr, 
		onComplete: function(resultObj){
			try {
				document.getElementById("cartItemNo").innerHTML = resultObj.count;
			} catch (e) {
				document.getElementById("cartItemNo").innerHTML = "Error";
			}
		}
	}).get();
}

function removelink(packid) {
	document.getElementById('chkItem'+packid).parentNode.removeChild(document.getElementById('chkItem'+packid));
	parent.removelink(packid);
	setTimeout('closeDialog()',3000);
}


function btnDataItem(productid, albumid, artistname, productname, displayPrice, unitPrice, discountPrice, downloadBitRate, downloadFileType,
                             pcallow, downloadallow, producttype, itemtype, isexplicit, playallid, ids) {
			this.productId = productid;
            this.albumId = albumid;
            this.artistName = artistname;
            this.productName = productname;
            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;
            this.isExplicit = isexplicit;
            this.playallid = playallid;
            this.ids = ids;
}

function createBtnDiv(dataObject) {
	var btnDiv = new Element('div', {'class':'btnDivCss'});
	if (dataObject.pcAllow || dataObject.downloadAllow) {
		var btnSpan1 = new Element('span', {'class':'btnPrelisten',
											'styles':{'margin-left':'3px'}});
		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試聽',
											'styles':{'cursor':'hand'},
											events: {   click: function() {
												if (dataObject.itemType == 'product') {
													previewall2(dataObject.productId);
												} else if (dataObject.itemType == 'album' || dataObject.itemType == 'editoralplaylist') {
													previewall2(dataObject.ids);
												} 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',
											'styles':{'margin-left':'3px'}});
		btnDiv.adopt(btnSpan2);
		if (dataObject.productType != 'VIDEO') {
			var btnImg2 = new Element('img', {  'class':'btnPlayImg', 
				'src':'/music/images/buttons/chart_play.gif', 
				'alt':'播放足本歌曲',
				'title':'播放足本歌曲',
				'styles':{'cursor':'hand'},
				events: {   click: function() {
								if (dataObject.itemType=='product') {
									instantplay(dataObject.productId);
								} else {
									if (dataObject.playallid!=null) {
										playall2(dataObject.playallid,dataObject.ids);
									} else if (dataObject.itemType == 'editoralplaylist') {
										playall2('playlist_'+dataObject.productId, dataObject.ids);
									} else {
										playall2('album_'+dataObject.albumId, dataObject.ids);
									}
								}
							},
                            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() {
                            	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',
											'styles':{'margin-left':'3px'}});
            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',
					'styles':{'cursor':'hand'},
					events: {   click: function() {
					
									if (dataObject.itemType=='product') {
										popPlaylist(dataObject.productId,dataObject.productName,dataObject.artistName,dataObject.productType,null);
	                         		} else {
										if (dataObject.playallid!=null) {
											saveAllPopPlaylist(dataObject.productName,dataObject.ids,dataObject.playallid);
										} else if (dataObject.itemType=='editoralplaylist') {
											saveAllPopPlaylist(dataObject.productName, dataObject.ids,'editoralplaylist_'+dataObject.productId);
										} else {
											saveAllPopPlaylist(dataObject.productName, dataObject.ids,'album_'+dataObject.albumId);
										}
									}
	                            	//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() {
	                                                	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=='product') {
				ptype="product";
			} else if (dataObject.itemType=='album'){
				ptype="album";
			} else if (dataObject.itemType=='editoralplaylist'){
				ptype="editoralplaylist";
			}
			
            var btnSpan4 = new Element('span', {'class':'btnDownload',
											'styles':{'margin-left':'3px'}});
				btnDiv.adopt(btnSpan4);
			if (dataObject.productType != 'VIDEO') {
				var mp3desc = "";
				if (dataObject.itemType=='product') {
					mp3desc = "購買或換領 MP3";
				} else {
					mp3desc = "購買MP3";
				}
            	var btnImg4 = new Element('img', {  'class':'btnDownloadImg', 
                                                    'src':'/music/images/buttons/mp3.gif', 
                                                   	'alt':mp3desc,
                                                   	'title':mp3desc,
                                                   	'styles':{'cursor':'hand'},
                                                   	events: {   click: function(cevent) {
                                                   					var playallid = null;
                                                   					if (dataObject.playallid) {
                                                   						if (dataObject.playallid != null && dataObject.playallid!="") {
                                                   							playallid = dataObject.playallid;
                                                   						}
                                                   					}
                                                   					if (dataObject.itemType=='album' || dataObject.itemType=='product') {
                                                              			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, playallid);
                                                                	} else if (dataObject.itemType=='editoralplaylist') {
                                                                		mp3DlAll(dataObject.itemType,dataObject.productId,dataObject.itemType,dataObject.productName,null,cevent, playallid);
                                                                	}
                                                                },
                                                                mouseover: function() {
                                                                	this.src='/music/images/buttons/mp3_over.gif';
                                                                },
                                                                mouseout: function() {
                                                                	this.src='/music/images/buttons/mp3.gif';
                                                                }
                                                            }});
                btnSpan4.adopt(btnImg4);
            }
      	}
      	
      	return btnDiv;
}
