//// JavaScript Document

/* Xóa Video yêu thích */
$("a.nctVideoFavourite").click(function () {
	if(!checkLogin()){
		loadLogin('');
		return false;
	}else {	
		var id = $(this).attr("id");
		jConfirm('Bạn có chắc muốn xóa video này không?', 'Xác nhận xóa video', function(r) {
			if(r){			
				$.post("/includes/NewFace_NhacCuaToi_XoaVideo.php", { 	
					'id': id
				  },  						  
				  function(data){  
					//// set up thong bao + show			
					if(data == -1 ){ //// chua login												
						jAlert('warning', 'Bạn vui lòng Đăng Nhập trước khi xóa video.', 'Lưu ý');
					}else if(data == -2 ){ //// khong co thong tin
						jAlert('error', 'Bạn không có quyền truy cập vào chức năng này', 'Thông báo lỗi');
					}else if(data == 1 ){//// thanh cong											
						location.reload();
					}else if(data == 2 ){ //// xoa cua nguoi khac
						jAlert('error', 'Bạn không có quyền xóa video này.', 'Thông báo lỗi');
					}else{ //// khong gui duoc
						jAlert('error', 'Hệ thống đang bận hoặc đang quá tải, vui lòng gửi lại sau.', 'Thông báo lỗi');
					}									
				  }  
				);			
			}
		});
	}
	return false;
});

/* Xóa Album Video */
$("a.nctVideoAlbum").click(function () {
	if(!checkLogin()){
		loadLogin('');
		return false;
	}else {	
		var id = $(this).attr("id");
		jConfirm('Bạn có chắc muốn xóa album video này không?', 'Xác nhận xóa album video', function(r) {
			if(r){			
				$.post("/includes/NewFace_NhacCuaToi_XoaVideoAlbum.php", { 	
					'id': id
				  },  						  
				  function(data){  
					//// set up thong bao + show			
					if(data == -1 ){ //// chua login												
						jAlert('warning', 'Bạn vui lòng Đăng Nhập trước khi xóa album video.', 'Lưu ý');
					}else if(data == -2 ){ //// khong co thong tin
						jAlert('error', 'Bạn không có quyền truy cập vào chức năng này', 'Thông báo lỗi');
					}else if(data == 1 ){//// thanh cong											
						location.reload();
					}else if(data == 2 ){ //// xoa cua nguoi khac
						jAlert('error', 'Bạn không có quyền xóa album video này.', 'Thông báo lỗi');
					}else{ //// khong gui duoc
						jAlert('error', 'Hệ thống đang bận hoặc đang quá tải, vui lòng gửi lại sau.', 'Thông báo lỗi');
					}									
				  }  
				);			
			}
		});
	}
	return false;
});

//// open yeu thich
$("#addvideo_yeuthich").click(function () {
	if(!checkLogin()){
		loadLogin('yEUthIch=trUE');
		return false;
	}else {
		if($("#addvideo_yeuthich").attr("href") ==""){
			$.post("/includes/NewFace_XemVideo_YeuThich.php", { 	
			'id': $("#video_yeu_thich").val()
			  },  		  
			  function(data){  
				//// set up thong bao + show
				if(data == -1 ){ //// chua login								
					jAlert('error', 'Bạn vui lòng Đăng Nhập trước khi lưu video vào danh sách yêu thích.', 'Thông báo lỗi');
				}else if(data == 1 ){//// thanh cong
					jAlert('success', 'Lưu video vào danh sách yêu thích thành công.', 'Thành công');
				}else if(data == -2 ){//// da click yeu thich
					jAlert('warning', 'Bạn đã chọn video này là video yêu thích của mình.', 'Lưu ý');
					$('#addvideo_yeuthich').removeAttr('href');
				}else if(data == -3 ){//// da click yeu thich
					jAlert('error', 'Video này không tồn tại hoặc đã được xét trạng thái ẩn.', 'Thông báo lỗi');
				}else{ //// khong gui duoc
					jAlert('error', 'Hệ thống đang bận hoặc đang quá tải, vui lòng gửi lại sau.', 'Thông báo lỗi');
				}									
			  }  
			);
		}
		return false;
	}
});	

$("a[name=removeCookies]").click(function () {
	Os_Cookiess.$Set('_mBox','',0,'/',false);
	location.href = '/';
});	

setTimeout(function() {						
	var strVideoID = Os_Cookiess.$Get('strVideoID');		
	if (!strVideoID) {
		strVideoID = $('#hidVideoID').val();
	} else {
		strVideoID += ',' + $('#hidVideoID').val();   
	}
    $.post(ajaxListen, { id: $('#hidVideoID').val(), type: "video",name:$('#hidVideoName').val(),genre:$('#hidVideoClipName').val() },
           function(data){
               //$('#totalListen').html(data);
           });
	Os_Cookiess.$Set('strVideoID', strVideoID, 24, '/', false);
}, 30000); //120000
/*
if(document.all){// ie
	var scollHeight = '275px';
	if (location.pathname.search("tao-album-nhanh") != -1) {
		scollHeight = '400px';
	}
	if($("div.scrollbar").length > 0){		
		$("div.scrollbar").css("height", scollHeight);		
	}	
	if($("div.listleft").length > 0){
		$("div.listleft").css("height", '232px');
	}	
}
*/
////
if($("#sortable").length > 0){	
	var tanTotalSong = 0;	
	var helperStatus = '';
	
	$(function(){
		$('#tan_Comm').keyup(function(){
			limitChars('tan_Comm', 600, 'charlimitinfo');
		})
	});
	$(function(){
		$('#tan_title').keyup(function(){
			if($("#tan_title").val().trim().length > 2){
				$("#tan_titleerr").html('');
			}
		})
	});	
	$(".block").draggable({
		//connectToSortable: '#sortable',
		helper:'clone',
		revert:'invalid',				
		scroll: false,
		appendTo: 'body',		
		containment: '#main',					
		drag:  function(ev, ui) {
			$(this).addClass("ui-draggable-disabled");									
		},
		stop:   function(ev, ui) {			
			$(this).removeClass("ui-draggable-disabled");
		}
	});
	
	$(function() {
		$("#sortable").sortable({
			revert: true
		});
		$("#sortable").disableSelection();
	});
	
	$(".drop").droppable({		
		accept: ".block",
		activeClass: 'ui-state-active',
		hoverClass: 'ui-state-hover',	
		drop: function(ev, ui) {
			
			if($("#tan_firstInfo").length > 0){
				$("#tan_firstInfo").remove();				
			}	
			
			var dropElemId = ui.draggable.attr("id");
			var dropElem = ui.draggable.html();
			var removeID = $("#xoa"+dropElemId).attr("id");
			var outPutHTML = '';			
			outPutHTML = '<li id="'+dropElemId+'" class="ui-state-default"><div class="ui-state-conleft">'+dropElem+'</div><div class="ui-state-conright"><a href="#tan_list" class="remove"><img src="'+GLOBAL_IMAGE_PATH+'skins/gentle/images/icon-xoa1.png" width="21" height="18" border="0"/></a></div><br class="clr"/></li>';
			$(this).append(outPutHTML);			
			$('a.remove').click(function(){				
				$(this).parents("li").remove();				
				tanTotalSong--;
				$("#tan_totalSongs").html(tanTotalSong);			
				$("#"+dropElemId).removeClass("ui-draggable-selected");
				if(!tanTotalSong){
					$("#sortable").html('<li id="tan_firstInfo" style="font-size:16px; padding:30px; color:#0066FF;">Kéo thả video từ box bên trái để tạo album cho riêng bạn</li>');
				}
			});						
			
			
			if(!isNaN(dropElemId)){
				tanTotalSong++;
				$("#tan_totalSongs").html(tanTotalSong.toString());
			}			
			
			$(ui.draggable).addClass("ui-draggable-selected");
			
			if(document.all){// ie	
				if($("li.ui-state-default").length > 0){	
					$("li.ui-state-default").css("height", '1%');
				}
			}
		}
	});	
	
	$("#tanClear").click(function () {
		$("#sortable").html('<li id="tan_firstInfo" style="font-size:16px; padding:30px; color:#0066FF;">Kéo thả video từ box bên trái để tạo album cho riêng bạn</li>');
		$("#tan_totalSongs").html('0');
		tanTotalSong = 0;
		document.recForm.reset();
		
		$("div.ui-draggable-selected").removeClass("ui-draggable-selected");
	});	
	
	$("#tanSubmit").click(function () {
		var result = $('#sortable').sortable('toArray');
		var songArr= '';			
		for(i = 0; i < result.length; i++){
			if(!isNaN(result[i]) && songArr.search(result[i]) == -1){
				songArr += result[i]+",";
			}
		}		
		if($("#tan_title").val().trim().length < 2){			
			$("#tan_titleerr").html("Nhập tên từ 2 - 100 kí tự.");
			$("#tan_titleerr").focus();
			return false;
		}else if(!songArr){
			$("#tan_firstInfo").css('color', '#FF0000');
			return false;
		}else{				
			$("#tanSubmit").attr("disabled", true);
			$("#nfQTAN_ThongBao").html('Đang thực hiện lưu video album...');
			var albumname = ucwords($('#tan_title').val());
			$('#tan_title').val(albumname);
			$.post("/includes/NewFace_TaoVideoAlbumNhanh.php", {
				'name': encodeURIComponent($('#tan_title').val()),
				'comm': encodeURIComponent($('#tan_Comm').val()),
				'songids': songArr
			  },  
			  function(data){
				//// set up thong bao + show
				if(data == -1 ){ //// chua login
					$("#nfQTAN_ThongBao").html('Bạn vui lòng Đăng Nhập trước khi lưu video album');
				}else if(data = 1 ){//// thanh cong
					$("#nfQTAN_ThongBao").html('Lưu video album thành công.');
					$("#tan_albumid").val(data);
					$("#tan_albumtit").val($("#tan_title").val());
					$("#tan_title").val('');
					$("#tan_Comm").val('');
					$("#sortable").html('<li id="tan_firstInfo" style="font-size:16px; padding:30px; color:#0066FF;">Kéo thả video từ box bên trái để tạo album cho riêng bạn</li>');					
					$("#tan_totalSongs").html('0');
					$("#tanNghe").attr("disabled", false);
					$("#tanSubmit").attr("disabled", false);
					$(".block").removeClass("ui-draggable-disabled");
					tanTotalSong = 0;
					setTimeout(function() {$("#nfQTAN_ThongBao").html('');}, 5000);
				}
			  }
			);
		}
	});
		
	$("#tanNghe").click(function () {
		location.href = '/mp3/nghe-album/'+$("#tan_albumtit").val().stripViet('-')+'.'+$("#tan_albumid").val()+'.html';		
	});	
	
	$("#tan_chonlist").change(function () {
		$("#tan_SongListByType").html('<div style="vertical-align:middle; padding-left:180px"><img src="'+GLOBAL_IMAGE_PATH+'skins/gentle/images/loadingAnimation.gif"></div>');
		$.post("/includes/NewFace_TaoVideoAlbumListSong.php", { 				
			'idlist': $('#tan_chonlist').val()			
		  },  
		  function(data){  		  	
			//// set up thong bao + show
			if(data){
				$("#tan_SongListByType").html(data);
				$(".block").draggable({
					helper:'clone',
					revert:'invalid',				
					scroll: false,
					appendTo: 'body',
					containment: '#main',		
					drag:  function(ev, ui) {
						$(this).addClass("ui-draggable-disabled");									
					},
					stop:   function(ev, ui) {			
						$(this).removeClass("ui-draggable-disabled");
					}
				});
			}			
		  }  
		);	
	});		
}

var xmlurl = "";
/*var SkinEmbbed = {
	songIDEnCode: $('#songIDEnCode').val(),
	totalIcon: 11,
	firstID:1,
	totalIconDisplay:7,
	currentID:1,
	width:413,	
	height:360,
	autoPlay:'false',
	encodeVideo:$('#EncodeVideo').val(),
	txtForumID:'txtLinkPage',
	txtBlogID: 'txtEmbedCode',	
	createIcon: function(id,display){
		var template = '<a href="#" onclick="SkinEmbbed.changeSkin('+id+');return false;" id="lnkSkin'+id+'" style="display:'+display+'"><img height="30" width="37" border="0" alt=" " src="'+imageServer+'skins/gentle/images/iconskin'+id+'.gif"/></a>';
		return template;
	},
	createEmbbedCodeBlog: function(){
		var tpl = '<object width='+this.width+' height='+this.height+'><param name=quality value=high /><param name=wmode value=transparent /><embed wmode=transparent id=vidPlayer height='+this.height+' width='+this.width+' allowfullscreen=true quality=high name=vidPlayer src='+imageServer+'skins/gentle/flash/channelzPlayer.swf?xmlURL=http://'+window.location.hostname+'/play/?pid='+this.encodeVideo+'||6&songID=0&autoplay=true&wmode=transparent&1 type=application/x-shockwave-flash></embed><br /></object><br />';
	
		return tpl;
	},
	createEmbbedCodeForum: function(){	
		var tpl = '[FLASH]'+imageServer+'skins/default/flash/player/mp3Player_skin'+this.currentID+'.swf?xmlurl=http://'  + window.location.hostname  + '/blog/?'+this.xmlUrl+'[/FLASH]';
		return tpl;
	},
	createListIcon: function(){
		var html = '';
		var display = '';
		for(i=1;i<=this.totalIcon;i++){
			if((i>=this.firstID) && (i<=this.firstID+this.totalIconDisplay-1)){
				display = '';
			}else{
				display = 'none';				
			}
			html=html+this.createIcon(i,display);			
		}
		return html;
	},
	changeSkin: function(id){
		var src = imageServer+'skins/appme/images/SkinPlayer/'+id+'.jpg';
		$('#imgSkinPlayer').attr('src',src);
		this.currentID = id;
		$('#divSkinPanel').show('fast');
	},
    setEmbededCode: function(){
		var forumCode = this.createEmbbedCodeForum();
		var blogCode = this.createEmbbedCodeBlog();		
		$('#lblShareLocation').html('Blog');
		$('#lblLinkSong').html('Forum');		
		$('#'+this.txtForumID).val(forumCode);
		$('#'+this.txtBlogID).val(blogCode);		
	},	
	setDemoSkin: function(){
		this.width = 260;
		this.height = 51;	
		$('#divPanelLeft').html(this.createEmbbedCodeBlog());
	},	
	changeAutoPlay: function(){
		var chkAutoPlay = $('#chkAutoPlay').attr('checked');
		if(chkAutoPlay){
			this.autoPlay = 'true';
			this.xmlUrl = $('#xmlUrlAuto').val();
		}else{
			this.autoPlay = 'false';
			this.xmlUrl = $('#xmlUrlNoAuto').val();			
		}
		this.setEmbededCode();
	},
	changeBackGround: function(){
		var chkBackGround = $('#chkBackGround').attr('checked');		
		if(chkBackGround){
			$('#chkAutoPlay').attr('checked','checked');
			this.changeAutoPlay();
			$('#chkAutoPlay').attr('disabled','disabled');
			this.width = 1;
			this.height = 1;			
		}else{
			$('#chkAutoPlay').attr('disabled','');
			this.width = 360;
			this.height = 61;						
		}
		this.setEmbededCode();		
	},	
	show: function(id){
		$('#lnkSkin'+id).show();
	},
	
	hide: function(id){
		$('#lnkSkin'+id).hide();
	},	

	moveLeft: function(){
		var firstID_new = this.firstID-1;
		if(firstID_new>=1){
			this.hide(firstID_new+this.totalIconDisplay);						
			this.show(firstID_new);
			this.firstID = firstID_new;
		}
		return false;
	},
	
	moveRight: function(){
		var lastID_new = this.firstID+this.totalIconDisplay;
		if(lastID_new<=this.totalIcon){
			this.hide(this.firstID);									
			this.show(lastID_new);			
			this.firstID +=1;
		}
		return false;
	}
}

$(".btn_share").click(function (){
	var id = $(this).attr("id");
	if(!isNaN(id)){
		var albumLink  = $("#VideoLink").val();
		var albumTitle = $("#hidVideoName").val();
		var alPic;
		
		if($("#Style").val() == "album"){
			// share album
			isAlbum = 1;
		}else{
			// Share video clip	
			isAlbum = 0;
		}
		alPic = $("#PictureVideo").val();
		var msg = '<div class="iAlbum">'+
					'<div class="avatarmp3">'+
						'<div class="mask_avatarmp3"></div>'+
						'<div class="img_avatarmp3"><img src="'+alPic+'" alt="Friend avatar" width="50" height="50" /></div>'+
					'</div>'+
					'<div class="txtAlbum">'+
						'<h1><a href="http://mp3.zing.vn" target="_blank">Zing Mp3 - Đỉnh Cao Âm Nhạc</a></h1>'+
						'<p>Chia sẻ <a href="'+albumLink+'" target="_blank">'+albumTitle+'</a> với bạn bè ZingMe</p>'+
					'</div>'+
					'<br class="clr" />'+
				'</div>'+
				'<p class="rwText">Viết lời nhắn<span class="key">(Giới hạn<strong id="countmakeFriend"> 300</strong> ký tự)</span></p>'+
				'<p class="rwText"><textarea class="txtShare" name="content" id="content" onkeyup="limitChars(\'content\', 300, \'countmakeFriend\');"/></p>';
				
		Boxy.confirm(msg, function(){
			if(isAlbum == 0) {
//				var encodeVideo = document.getElementById("EncodeVideo").value;
				alPic = SkinEmbbed.createEmbbedCodeBlog();
			}
			url ="/includes/App_MyVideo_Share.php";	
			$.ajax({
				type : "POST",
				url : url,
				data: ({content: $("#content").val(),albumPic : alPic, albumLink : $("#VideoLink").val(), albumTitle : $("#hidVideoName").val(), Album: isAlbum}),	
				dataType: "html",
				success : function(msg){	
					Boxy.alert(msg,function(){},{"title":"Chia sẻ âm nhạc", "cancelTitle":"Đóng"});
				}	
			});},{"cancelTitle":"Đóng", "okTitle":"Chia sẻ", "title":"Chia sẻ âm nhạc", "checkBeforeHide":false});					
	}
	return false;
});*/
