﻿function htmlEntitiesDecode(str){
	str = decodeURIComponent(str);
	str=str.replace(/&amp;/g,'&');						
	return str;	
}
function create_blog(){
	if(window.location.href.search('nghe-bai-hat') != -1){
		var blog_tpl = $('#divChiaSeBlogSong').html();
	}else if(window.location.href.search('nghe-album') != -1 || window.location.href.search('video-clip/xem-video') != -1 || 
			window.location.href.search('radio') != -1){
		var blog_tpl = $('#divChiaSeBlog').html();
	}
	return htmlEntitiesDecode(blog_tpl);
}

function create_forum(){
	if(window.location.href.search('nghe-bai-hat') != -1){
		var forum_tpl = $('#divChiaSeForumSong').html();
	}else if(window.location.href.search('nghe-album') != -1 || window.location.href.search('video-clip/xem-video') != -1 ||
			window.location.href.search('radio') != -1){
		var forum_tpl = $('#divChiaSeForum').html();
	}
	return htmlEntitiesDecode(forum_tpl);
}

$('#txtLinkPage').val(window.location.href);
$('#imgFaceBook').click(function(){
	hideEmbedSkinPanel();
	createEmbedCode('facebook');
})
$('#imgYahoo').click(function(){
	hideEmbedSkinPanel();
	createEmbedCode('yahoo');
})
$('#imgBlog').click(function(){
	hideEmbedSkinPanel();					 
	createEmbedCode('blog');
})
$('#imgForum').click(function(){
	hideEmbedSkinPanel();							  
	createEmbedCode('forum');
})
$('#imgEmail').click(function(){
	hideEmbedSkinPanel();							  
	SendMail();
})
$('#imgZingblog').click(function(){
	hideEmbedSkinPanel();							  
	createEmbedCode('zingblog');
})
$('#imgZingblogAlbum').click(function(){
	hideEmbedSkinPanel();							  
	createEmbedCode('zingblogalbum');
})
$('#imgZingblogCakhuc').click(function(){
	hideEmbedSkinPanel();							  
	createEmbedCode('zingblogcakhuc');
})
$('#imgZingblogVideo').click(function(){
	hideEmbedSkinPanel();							  
	createEmbedCode('zingblogvideo');
})
$('#imgZingblogVideoAlbum').click(function(){
	hideEmbedSkinPanel();							  
	createEmbedCode('zingblogvideoalbum');
})

$('#imgForum,#imgYahoo,#imgBlog,#imgFaceBook,#imgEmail,#imgZingblog,#imgZingblogAlbum,#imgZingblogCakhuc,#imgZingblogVideo,#imgZingblogVideoAlbum').css('cursor','pointer');
function createEmbedCode(type){
	var info = new Array();
	var label = '';
	var code  = '';
	var codeblog  = '';
	switch (type) {
		case 'facebook':
			label = 'Facebook';
			facebookcode = 'http://www.facebook.com/sharer.php?u='+encodeURIComponent(window.location.href);
			window.open(facebookcode,'_blank');
			break;
		case 'forum':
			label = 'Forum';
			forumcode = create_forum();
			break;
		case 'blog':
			label = 'Blog';
			codeblog = create_blog();
			break;
		case 'yahoo':
			label = 'Yahoo! 360';
			yahoocode = $("#yBlogDiv").html();
			break;
		case 'zingblog':
			if (!checkLogin()) {
				loadLogin();
			} else {
				tpl		= Base64.encode(encodeURIComponent(SkinEmbbed.createEmbbedCodeBlog()));
				label	= 'Zing Blog';
				zingblogcode	= 'http://me.zing.vn/apps/blog?params=' + getAccountName() + '/share/mp3?em=' + tpl;
				window.open(zingblogcode, '_blank');
			}
			break;
		case 'zingblogalbum':
			if (!checkLogin()) {
				loadLogin();
			} else {
				tpl = SkinEmbbed_lightbox.createEmbbedCodeBlog_lightbox('album');
				albumName = $('#nghealbum_name').val();
				var tpl = "{\"embed\": \""+Base64.encode(tpl)+"\", \"title\": \""+albumName+"\", \"type\": \"album\"}";
				tpl = Base64.encode(tpl);
				label	= 'Zing Blog';
				blogalbumcode	= 'http://me.zing.vn/apps/blog?params=' + getAccountName() + '/share/mp3?em=' + tpl;
				window.open(blogalbumcode, '_blank');
			}
			break;
		case 'zingblogcakhuc':
			if (!checkLogin()) {
				loadLogin();
			} else {
				tpl = SkinEmbbed_lightbox.createEmbbedCodeBlog_lightbox('cakhuc');
				artist = $('#hidArtist').val();
				songtitle = $('#txtSongTitle').val();
				author = $('#Composer').val();
				var tpl = "{\"embed\": \""+Base64.encode(tpl)+"\", \"artist\": \""+artist+"\", \"title\": \""+songtitle+"\", \"author\": \""+author+"\", \"type\": \"song\"}";
				tpl = Base64.encode(tpl);
				label	= 'Zing Blog';
				blogcakhuccode	= 'http://me.zing.vn/apps/blog?params=' + getAccountName() + '/share/mp3?em=' + tpl;
				window.open(blogcakhuccode, '_blank');
			}
			break;
		case 'zingblogvideo':
			if (!checkLogin()) {
				loadLogin();
			} else {
				tpl = SkinEmbbed_lightbox.createEmbbedCodeBlog_lightbox('clip');
				videotitle = $('#hidVideoName').val();
				artist = $('#Artist').val();
				var tpl = "{\"embed\": \""+Base64.encode(tpl)+"\", \"title\": \""+videotitle+"\", \"artist\": \""+artist+"\", \"type\": \"video\"}";
				tpl = Base64.encode(tpl);
				label	= 'Zing Blog';
				blogvideocode	= 'http://me.zing.vn/apps/blog?params=' + getAccountName() + '/share/mp3?em=' + tpl;
				window.open(blogvideocode, '_blank');
			}
			break;
		case 'zingblogvideoalbum':
			if (!checkLogin()) {
				loadLogin();
			} else {
				tpl = SkinEmbbed_lightbox.createEmbbedCodeBlog_lightbox('AlbumVideo');
				albumName = $('#VideoAlbumTitle').val();
				var tpl = "{\"embed\": \""+Base64.encode(tpl)+"\", \"title\": \""+albumName+"\", \"type\": \"video-album\"}";
				tpl = Base64.encode(tpl);
				label	= 'Zing Blog';
				blogvideoalbumcode	= 'http://me.zing.vn/apps/blog?params=' + getAccountName() + '/share/mp3?em=' + tpl;
				window.open(blogvideoalbumcode, '_blank');
			}
			break;
	}
	//$("#lblShareLocation").html(label);
	if (forumcode) {
		$('#txtEmbedCode').val(forumcode);
	}
	if(codeblog){
		$('#txtBlogCode').val(codeblog);
	}
}
createEmbedCode('forum');
createEmbedCode('blog');

function SendMail(){
	if(!checkLogin()){
		loadLogin();
		return false;
	}	
	var url = window.location.href;
	if(url.indexOf('nghe-bai-hat') != -1){
		var mod_send='nghe-bai-hat';
		var title_send= $('#txtSongTitle').val();
	}else
	if(url.indexOf('/mp3/radio') != -1){
		var mod_send='radio';
		var title_send= $('#hidTitle').val();
	}else
	if(url.indexOf('/mp3/video-clip/xem-video') != -1){
		var mod_send='xem-video';
		var title_send= $('#hidVideoName').val();
	}else
	if(url.indexOf('mp3/nghe-album') != -1){
		var mod_send='nghe-album';
		var title_send= $('#nghealbum_name').val();
	}
	tb_show('','/includes/NewFace_NgheAlbum_SendMail.php?mod='+mod_send+'&name='+encodeURIComponent(title_send)+'&TB_iframe=true&height=250&width=505&modal=false');	
	return false;
}


function hideEmbedSkinPanel(){
	var url = window.location.href;
	if(url.indexOf('nghe-bai-hat') != -1){
		$('#divSkinPanel').hide();
		$('#lblLinkSong').html('Link bài hát');
		$('#txtLinkPage').val(window.location.href);
	}
}


var Base64 = {

	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;

		input = Base64._utf8_encode(input);

		while (i < input.length) {

			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);

			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}

			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

		}

		return output;
	},

	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		while (i < input.length) {

			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}

		}

		output = Base64._utf8_decode(output);

		return output;

	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}
