/*
 * @classDescription MusicshakeActiveX
 */
var Musicshake = function() {
	this.initialize();
};

Musicshake.prototype = {
	
	iframeTagString : ['<iframe width="0" height="0" src="about:blank" id="StartIframe" style="display:none;">', '</iframe>'],
	options : { },

	/*
	 * @constructor
	 */
	initialize: function() {
		
		// removed
		//for (var i = 0; i < this.iframeTagString.length; i++) {
		//	document.write(this.iframeTagString[i]);
		//}
		
		var iframeEle = new Element("iframe", {'width':'0', 'height':'0', 'src':'about:blank', 'id':'StartIframe'});
		iframeEle.setStyle('display', 'none');
		$(document.body).appendChild(iframeEle);
		
		this.iframe = iframeEle;
		this.src = 'musicshake://' + location.host + '/XML/AppLaunch.php?PHPSESSID=' + Cookies.get('PHPSESSID');
		this.downloadurl = '/Guide/Intro/';
	},
	
	Launch: function(param) {
		
		if(param)
		{
			this.src = this.src + param;
		}
	
		if(IE6_)
		{
			this.iframe.src = this.src;
			
			location.href = this.downloadurl;
		}
		else if(IE7_)
		{
			var t = window.open(this.src, 'm', 'width=0,height=0,top=100000,left=1000000');
			t.close();
			location.href = this.downloadurl;
		}
		else 
		{
			this.iframe.src = this.src;
			location.href = this.downloadurl;
		}
	},
	
	Launch2: function(param) {
		if(param)
		{
			this.src = this.src + param;
		}
	
		if(IE6_)
		{
			this.iframe.src = this.src;
		}
		else if(IE7_)
		{
			var t = window.open(this.src, 'm', 'width=0,height=0,top=100000,left=1000000');
			t.close();
		}
		else 
		{
			this.iframe.src = this.src;
	
		}
	},

    // MSW
	editslot:function(slotnumber) {
		this.Launch('&E=' + slotnumber);
	},
	editfile:function(filename) {
		this.Launch('&L=http://dogeng.musicshake.com:8080/msw/' + filename);
	},
	editfile2:function(filename) {
		this.Launch2('&L=http://dogeng.musicshake.com:8080/msw/' + filename);
	},
	event:function(ev_code) {
		this.Launch(' /EV:' + ev_code);
	},
	
	/**
	 * Musicshake Application
	 */
	start:function() {
		this.Launch();
	},
	
	pattern:function(data) {
		this.Launch('&TRLOAD='+data);
	},
	
	editsong:function(song_num) {
		this.Launch('&L=http://'+location.hostname+'/APP/LoadSongMsw.php?SONG_NUM=' + song_num);
	},
	
	exportslot:function(slot_num) {
		this.Launch(' /EXPORTSLOT:EXPORT' + slot_num);
	},
	
	mp3downslot:function(slot_num) {
		this.Launch(' /EXPORTSLOT:BUY' + slot_num);
	}
};


var Shakelog = 
{
    UDIR : getComponentURL('Musicshake'),
    Open : function (UserNum)
    {
        window.open('/Shakelog/'+UserNum, 'Shakelog'+UserNum, "width=500");
    },
    
    addFriend : function (UserNum)
    {
        var Result = Ajax.exec(this.UDIR+'addFriend.php', { 'USER_NUM' : UserNum } );
        alert(Result.text);
        
    },
    
    removeFriend : function (UserNum)
    {
      if(confirm("Remove me from being a fan?"))
      {
        var Result = Ajax.exec(this.UDIR+'removeFriend.php', { 'USER_NUM' : UserNum } );
        alert(Result.text);
        document.location.href= '/MyMusicShake/FavShaker/';
        return false;
      }  
    },
    
    Scrap : function (CONTEST_NUM)
    {
       var Result = Ajax.exec(this.UDIR+'Scrap.php', { 'CONTEST_NUM' : CONTEST_NUM } );
       alert(Result.text);
        
    },
    
    showAllContest : function (UserNick)
    {
        document.location.href= '/Search/UserSongList/?USER_NICK='+UserNick;
    }    
    
}

var Pop = 
{
    open: function (URL, Param)
    {
        switch (Param)
        {
            case "checkF" :
            break;
            
            default : 
            alert(Param);
            break;
        }
        
    },
    
    close: function ()
    {
      _Window.closePopUp();
    }
}



var Album =
{
    removeAlbum : function (AlbumNum)
    {
        if(confirm("The posted text and comments will also be deleted.\n Are you sure you want to delete this playlist?"))
      {
        
          var Result = Ajax.exec(Shakelog.UDIR+'album_exec.php', { "MODE" : "REMOVE" , 'ALBUM_NUM' : AlbumNum } );
          
          if(Result.text == "OK")
          {
                
                alert('Playlist deleted.');
                document.location.href = "/MyMusicShake/MyAlbum/PR_Album/";     
          }
          else 
          {
         
            alert(Result.text);
    
          }
      } 
               
    },

	removescrapAlbum : function (AlbumNum)
    	{
		if(confirm("Remove this playlist from your favorites?"))
     		{
      			    var Result = Ajax.exec(Shakelog.UDIR+'album_exec.php', { "MODE" : "DELETESCRAP" , 'ALBUM_NUM' : AlbumNum } );
          
			    if(Result.text == "OK")
			    {

				    alert('Playlist deleted.');

				    document.location.href = "/MyMusicShake/MyAlbum/Scrap_Album/";     

			    }
			    else 
			    {

				    alert(Result.text);

			    }
		}
      	},
    
    recommend : function (AlbumNum)
    {
        var Result = Ajax.exec(Shakelog.UDIR+'album_exec.php', { "MODE" : "RECOMMEND" , 'ALBUM_NUM' : AlbumNum } );
       
        if(isNaN(Result.text)) alert(Result.text);
        else 
        {
            alert('Thank you for rating.');        
           $('recommendCount').innerHTML = Result.text;
        }
        
    },

    
    Scrap : function ( AlbumNum)
    {
      
      if(confirm("Add this playlist to your favorites?"))
      {
      
          var Result = Ajax.exec(Shakelog.UDIR+'album_exec.php', { "MODE" : "SCRAP" , 'ALBUM_NUM' : AlbumNum } );
          
          if(Result.text == "OK")
          {
                
                alert('The playlist has been added to your favorites.');
                document.location.href = "/MyMusicShake/MyAlbum/Scrap_Album/";     
              
          }
          else 
          {
            alert(Result.text);
          }
          
      }
        
    }

}

var Share = 
{
	getShareLayerUrl : function()
	{
		return '/Listen/Song/SongContent/ShareLayer.php';
	},
	
	closeAll : function()
	{
		$$('.listen_mb_wrap').each(function(el) {
			el.style.display = 'none';	
		}, this);
	},
	
	
	openShareEmbed : function(ShareType, ShareNum)
	{
		Share.ShareType = ShareType;
		Share.ShareNum = ShareNum;
		
		
		var Param = {  'MODE' : 'Call'  };
		
		Param["Type"] =  "Page";
		Param["File"] =  this.getShareLayerUrl();
		Param["SHARE_TYPE"] = ShareType;
		Param.onComplete = function ()
		{
			this.viewEmbed();
			Share.setValue();
			Clipboard.init();
		}.bind(this);

        AutoRun.setDocument($('ShareContent'), Param, '/Listen/Song/SongContent/ajax.share.php');
	},
	
	
	openShareEmail : function(ShareType, ShareNum)
	{
		Share.ShareType = ShareType;
		Share.ShareNum = ShareNum;
		
		Share.Windows = _Window.showPopUp();
		Share.Windows.style.width = '484px';
		
		var Param = {  'MODE' : 'Call'  };
		
		Param["Type"] =  "Page";
		Param.onComplete = function ()
		{
			Share.setValue();
			Share.viewEmail();
		}.bind(Share.Windows);
        	  
        AutoRun.setDocument(Share.Windows, Param, '/Listen/Song/SongContent/ShareLayer.php');
	},
	
	
	submitShare : function()
	{
		var F =  document.ShareForm;
		
		
		if (F.TO.value.trim() == '')
		{
			alert('Please enter the email address.');
			F.TO.focus();
			return false;
		}
		
		var From = F.TO.value.split('\,');
		
		for(var i = 0, len = From.length ; i < len ; i++)
		{
			if(!isEmail(From[i]))
			{
				alert('The "Send To" address is not valid.');
				return false;
			}
		}
		
		if (!isEmail(F.FROM.value))
		{
			alert('Your email address is not valid.');
		}
		else if (F.CONTENT.value.trim() == '')
		{
			alert('Please enter a message for your email.');
		}
		else
		{
			var Result = Ajax.exec(getComponentURL('FormMail') + 'Component.php', {
				'MAIL': 'SHAREMAIL',
				'SHARE_TYPE': Share.ShareType,
				'SHARE_NUM': Share.ShareNum,
				'NAME' : F.NAME.value,
				'TO': F.TO.value,
				'FROM': F.FROM.value,
				'CONTENT': F.CONTENT.value
			});

			F.reset();
			if(Result.text == 'OK')
			{
				alert('The email has been sent.');
				
			}
			else
			{
				alert(Result.text);
			}
			Share.setValue();
		}
	},
	
	viewLink : function ()
	{
		$('share_embed').style.display = "none";
		$('share_link').style.display = "block";
		$('share_email').style.display = "none";
	},
	
	viewEmail : function ()
	{
		$('share_embed').style.display = "none";
		$('share_link').style.display = "none";
		$('share_email').style.display = "block";
	
	},
	
	viewEmbed : function ()
	{
		$('share_embed').style.display = "block";
		$('share_link').style.display = "none";
		$('share_email').style.display = "none";
	},
	
	setValue : function()
	{
		var F =  document.ShareForm;
		var embedurl = 'http://'+document.location.hostname+'/musicshakePlayer.swf?lang=eng&DIR='+this.ShareType+'/'+this.ShareNum+'&NUM=1&auto='+(F.AUTO.checked?1:0);
		
		
		$('share_url_link').value = document.location.href;
		$('embedtag').value = getFlashCode(embedurl, 'flashPlayer', 375, 150);
	},
	setContestEmbed : function()
	{
		var ShareNum = ParentNum; 
		var embedurl = 'http://'+document.location.hostname+'/musicshakePlayer.swf?lang=eng&DIR=CONTEST/'+ShareNum+'&NUM=1&auto=1';
		var embedcode = getFlashCode(embedurl, 'flashPlayer', 375, 150);
		document.GFORM.GT.value = embedcode;
	},
	setAlbumEmbed : function()
	{
		var ShareNum = ParentNum; 
		var embedurl = 'http://'+document.location.hostname+'/musicshakePlayer.swf?lang=eng&DIR=ALBUM/'+ShareNum+'&NUM=1&auto=1';
		var embedcode = getFlashCode(embedurl, 'flashPlayer', 375, 150);
		document.GFORM.GT.value = embedcode;
	},
	setdebugembed : function()
	{
		var ShareNum = ParentNum; 
		var embedurl = 'http://'+document.location.hostname+'/musicshakePlayer.swf?lang=eng&DIR=CONTEST/'+ShareNum+'&NUM=1&auto=1';
		var embedcode = getFlashCode(embedurl, 'flashPlayer', 375, 150);
		document.GFORM.GT.value = embedcode;
		alert(embedcode);
	},
	copyString : function(Str)
	{
		
	}
}

var Contest =
{
    
	removeSong : function(SONG_NUM)
	{
		if(confirm("The posted text and comments will also be deleted.\n Are you sure you want to delete this song?"))
		{
			var Result = Ajax.exec(Shakelog.UDIR+'contest_exec.php', { "MODE" : "REMOVESONG" , 'SONG_NUM' : SONG_NUM} );
          
			if(Result.text == "OK")
			{
				alert('Song deleted.');
				document.location.href = "/MyMusicShake/MySong/SaveSong/";     
			}
			else 
			{
				alert(Result.text);
			}
		}
	},
	
	removeContest : function (ConNum)
	{
    
      if(confirm("The posted text and comments will also be deleted.\n Are you sure you want to delete this song?"))
      {
        
          var Result = Ajax.exec(Shakelog.UDIR+'contest_exec.php', { "MODE" : "REMOVE" , 'CONTEST_NUM' : ConNum } );
          
          if(Result.text == "OK")
          {
                
                alert('Song deleted.');
                document.location.href = "/MyMusicShake/MySong/PR_Song/";     
              
          }
          else 
          {
         
            alert(Result.text);
    
          }
      } 
    
   },
   
   recommend : function (ConNum)
   {
       
       var Result = Ajax.exec(Shakelog.UDIR+'contest_exec.php', { "MODE" : "RECOMMEND" , 'CONTEST_NUM' : ConNum } );
       
        if(isNaN(Result.text)) alert(Result.text);
        else 
        {
            alert('Thank you for rating.');        
           $('recommendCount').innerHTML = Result.text;
        }
   },
   
   Scrap : function (ConNum)
   {
      
       if(confirm("Add this song to your favorites?"))
      {
          var Result = Ajax.exec(Shakelog.UDIR+'contest_exec.php', { "MODE" : "SCRAP" , 'CONTEST_NUM' : ConNum } );         
          if(Result.text == "OK")
          {
                alert('The song has been added to your favorites.');
                document.location.href = "/MyMusicShake/MySong/Scrap_Song/";     
          }
          else 
          {
            	alert(Result.text);
          }
      }
   }
}

function DownLoad(SONG_NUM, USER_NUM) {
	
	var songNumStr = '';
	
	if (typeof SONG_NUM == 'string' || typeof SONG_NUM == 'number') {
		
		songNumStr = SONG_NUM;
		
	} else if (typeof SONG_NUM == 'object') {
		
		var checkboxLength = SONG_NUM.length;
		
		if (!checkboxLength && SONG_NUM.checked == true) {
			
			songNumStr = (songNumStr == '' ? '' : ',') + SONG_NUM.value;
			
		} else if (checkboxLength) {
			
			for(i=0; i<checkboxLength; i++) {
				if (SONG_NUM[i].checked) {
					songNumStr += (songNumStr == '' ? '' : ',') + SONG_NUM[i].value;
				}
			}	
		}
	}
	
	if (songNumStr && USER_NUM) {
		location.href = 'http://mp3.musicshake.com/procfile/download.php?SONG_NUM='+songNumStr+'&USER_NUM='+USER_NUM;
	} else {
		alert('Please select an item you want to download.');
	}
} 


var FindPW = 
{
	submitShare : function()
	{
		var F =  document.ShareForm;
		
		
		if (F.TO.value.trim() == '')
		{
			alert('Please enter the email address.');
			F.TO.focus();
			return false;
		}
		
		var From = F.TO.value.split('\,');
		
		for(var i = 0, len = From.length ; i < len ; i++)
		{
			if(!isEmail(From[i]))
			{
				alert('The "Send To" address is not valid.');
				return false;
			}
		}
		
		if (!isEmail(F.FROM.value))
		{
			alert('Your email address is not valid.');
		}
		else if (F.CONTENT.value.trim() == '')
		{
			alert('Please enter a message for your email.');
		}
		else
		{
			var Result = Ajax.exec(getComponentURL('FormMail') + 'Component.php', {
				'MAIL': 'SHAREMAIL',
				'SHARE_TYPE': Share.ShareType,
				'SHARE_NUM': Share.ShareNum,
				'TO': F.TO.value,
				'FROM': F.FROM.value,
				'CONTENT': F.CONTENT.value
			});
			
			F.reset();
			if(Result.text == 'OK')
			{
				alert('The email has been sent.');
				_Window.closePopUp();
			}
			else
			{
				alert(Result.text);
			}
		}
	},
	viewEmail : function ()
	{
		$('EmailBox').style.display = "block";
		$('EmbedBox').style.display = "none";
	},
	
	viewEmbed : function ()
	{
		$('EmailBox').style.display = "none";
		$('EmbedBox').style.display = "block";
	},
	
	setValue : function()
	{
		var F =  document.ShareForm;
		var embedurl = 'http://'+document.location.hostname+'/musicshakePlayer.swf?DIR='+this.ShareType+'/'+this.ShareNum+'&NUM=1&lang=kor&auto='+(F.AUTO.checked?1:0);
		$('share_url_link').value = document.location.href;
		$('embedtag').value = getFlashCode(embedurl, 'flashPlayer', 300, 400);
	},
	copyString : function(Str)
	{
		
	}
}


var User = 
{
    Body : document.getElementsByTagName('body')[0],
    myNick : '',
    Bar : null,
    
    setNickBar : function ()
    {
        User.groupColor = new Array();
        User.groupColor[3] = "#7a1d85"; // ????? 1?
        User.groupColor[4] = "#199cdf"; // ???
         
        addEvent(User.Body, "click", function ()
        {
            if(User.Bar) 
            { 
                 User.Bar.removeNode(true);
                 User.Bar = false;
            }
        }
       );
       
         var UserNick = $$('.NickName');
         UserNick.each(User.showBar, this);
    },
    
	addFriend : function (UserNum)
	{	
		var Result = Ajax.exec(getComponentURL('Musicshake') + "addFriend.php", { 'USER_NUM' : UserNum } );
		alert(Result.text);
    },
    
	showBar : function (Obj)
	{
		addEvent(Obj, 'click', function (e) { cancel(e); } );

         var Pnode = Obj.parentNode;
         var PSize = UI.getObjSize(Pnode);
         
         var Size = UI.getObjSize(Obj);
         
         var Nick = Obj.innerHTML.trim();
         
         if(PSize.w < Size.w)
         {
               Obj.style.position = 'absolute';
               Obj.style.width = PSize.w +  'px';
               Obj.style.overflowX = 'hidden';      
               
              
               
         }
        
        
        
        
        
        var Param = DOM.getAttributes(Obj);
	    var USER_NUM = Param.user_num;
		var USER_GROUP = Param.user_group;
			
		
		if(User.groupColor[USER_GROUP])
		{
		    Obj.style.color = User.groupColor[USER_GROUP];
		    
		}	
	

		Obj.onclick = function ()
		{

			if(User.Bar) User.Bar.removeNode(true);
			

			var Div = document.createElement('div');
			User.Body.appendChild(Div);

			User.Bar = Div;
			var Pos = UI.getOffsetPos(this);

			Div.style.position   = 'absolute';
			Div.style.width      = '105px';
			Div.style.border     = 'solid 1px #999';
			Div.style.height     = '60px';
			Div.style.textAlign  = 'left';
			Div.style.zIndex     = '2';
			
			Div.style.backgroundColor     = '#FFFEE6';
			Div.style.left       = Pos.x + 'px';
			Div.style.top        = Pos.y + 15 + 'px';
			Div.style.fontSize = "11px";
			
			
			var Div1  = document.createElement('div');
			Div1.innerHTML = "Posted songs &rsaquo;";
			Div1.style.fontSize = "11px";
			Div1.style.padding = "3px 3px 3px 10px";
			Div1.style.cursor = "pointer";
			
			if (USER_GROUP == 6)
				Div1.onclick = function () { document.parent.location.href = '/Search/UserSongList/?USER_NUM='+USER_NUM };			
			else
				Div1.onclick = function () { document.location.href = '/Search/UserSongList/?USER_NUM='+USER_NUM };
				
			Div.appendChild(Div1);
			
			
			if(USER_GROUP == 6)
			{
			    Div.style.height     = '20px';
			    return false;
			}
			
			
			var Div2  = document.createElement('div');
		    Div2.innerHTML = "Profile page &rsaquo;";
			Div2.style.fontSize = "11px";
			Div2.style.cursor = "pointer";
			Div2.style.borderTop = "dotted 1px #999";
			Div2.style.padding = "3px 3px 3px 10px";
			Div2.onclick = function () 
			{
			    document.location.href = '/People/profile/?USER_NUM='+USER_NUM;
			    return;
			}
			
			Div.appendChild(Div2);
			
			if(User.myNick && Nick !== User.myNick.trim())
			{
				Div2.style.borderBottom = "dotted 1px #999";
				var Div3  = document.createElement('div');
				Div3.style.cursor = "pointer";
				Div3.style.padding = "3px 3px 3px 10px";
				Div3.innerHTML = "Become a fan &rsaquo;";
				Div3.onclick = function()
				{
					User.addFriend(USER_NUM);
				}
				Div.appendChild(Div3);
			}
			else
			{
				Div.style.height     = '40px';
			}
			
			return false;
			
		}.bind(Obj);
   }       
}

//var msActiveX = new Musicshake();
addEvent(window, 'load', User.setNickBar);
