var webPath = getWebPath();
//Dialog initialize
$(function(){
	$("#alert").dialog({
		bgiframe: true,
		modal: true,
		autoOpen: false,
		closeOnEscape: false,
		width: 300,
		height: 170,
		title: alertMsg,
		close: function() {
			$("#alert").unbind("dialogclose");
		},
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		}
	});	
	
	$("#confirm").dialog({
		bgiframe: true,
		resizable: false,
		modal: true,
		autoOpen: false,
		closeOnEscape: false, 
		width: 300,
		height: 170,
		title: confirmMsg,
		close: function() {
			$("#confirm").unbind("dialogclose");
			$("#confirm").dialog('option', 'buttons', {
					'Ok': function(){
							$(this).dialog('close');
					},
					'Cancel': function(){
							$(this).dialog('close');
					}
			}); 
		},
		buttons: {
			'Ok': function() {
				$(this).dialog('close');
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		}
	});
	
	$("#dialog").dialog({
	  bgiframe: true,
	  modal: true,
	  autoOpen: false,
	  resizable: false,
	  draggable: true,
	  closeOnEscape: false,
	  close: function(ev, ui) {
	  			if(window.navigator.userAgent.indexOf("MSIE") != -1 && $("#MediaPlayer").length > 0) 
	  				MediaPlayer.stop();
	  }
	});
	
	$("#dialog_on_dialog").dialog({
	  bgiframe: true,
	  modal: true,
	  autoOpen: false,
	  resizable: false,
	  draggable: true,
	  closeOnEscape: false,
  	  close: function(ev, ui) {
	  }
	});
	
	$("#dialog").siblings(".ui-dialog-titlebar").remove();
	$("#dialog").css("padding", "0px");
	$("#dialog").parent(".ui-dialog").css("padding", "0px");
	
	$("#dialog_on_dialog").siblings(".ui-dialog-titlebar").remove();
	$("#dialog_on_dialog").css("padding", "0px");
	$("#dialog_on_dialog").parent(".ui-dialog").css("padding", "0px");
});

function jAlertWithTitle(title, message, callback)
{	
	$(function(){
		if(callback != null)
			$("#alert").bind('dialogclose', callback);
		
		$("#alert").dialog("open")
		.data("title.dialog", title)
		.find(".message")
		.html(message)
		.css("font-size", "12px");			
	});	
}

function jAlertWithTitleAndWidth(title, message, width, callback)
{	
	$(function(){
		if(callback != null)
			$("#alert").bind('dialogclose', callback);
		
		$("#alert").dialog("open")
		.data("title.dialog", title)
		.data("width.dialog", width)
		.find(".message")
		.html(message)
		.css("font-size", "12px");		
	});	
}

function jAlert(title, message, callback)
{	
	$(function(){
		if(callback != null)
			$("#alert").bind('dialogclose', callback);
		
		$("#alert").dialog("open")
		//.data("title.dialog", title)
		.find(".message")
		.html(message)
		.css("font-size", "12px");		
	});	
}

function jConfirm(title, message, okCallback, cancelCallback)
{
	$(function(){			
		if(okCallback != null && cancelCallback != null)
		{
			$("#confirm").dialog('option', 'buttons', {'Ok': okCallback, 'Cancel': cancelCallback});
			$("#confirm").bind('dialogclose', cancelCallback); 
		}
		else if(okCallback != null)
		{
			$("#confirm").dialog('option', 'buttons', {'Ok': okCallback, 'Cancel': function(){$(this).dialog('close');}});
		}
		else if(cancelCallback != null)
		{
			$("#confirm").dialog('option', 'buttons', {'Ok': function(){$(this).dialog('close');}, 'Cancel': cancelCallback});
			$("#confirm").bind('dialogclose', cancelCallback);
		}
			
		$("#confirm").dialog("open")
			//.data("title.dialog", title)
			.find(".message")
			.html(message)
			.css("font-size", "12px");			
	});
}

function openDialog(domId, width, height, url)
{
	$.get(url, function(data){
		rtMsg = jQuery.trim(data); 
  		if(rtMsg == 'not_login')
  		{
			$("#" + domId).text('')
				.dialog("option", "width", 390)
				.dialog("option", "height", 500)
				.dialog("option", "position", "center")
				.dialog("open")
				.load(webPath + "/popup/pop_play_guide.jsp");
		}
		else if(rtMsg == 'nocontent')
		{
			jAlert('alert', noExistContentsMsg);
		}
		else
		{
			$("#" + domId).text('')
				.dialog("option", "width", width)
				.dialog("option", "height", height)
				.dialog("option", "position", "center")
				.dialog('open')
				.html(data);	
		}
	});
}

function openMemberDialog(domId, width, height, url, errMsg)
{
	if(memberCosType == 'NOT_MEMBER')
	{
		jAlert("",errMsg);
		return false;
	}
	else
	{
		openDialog(domId, width, height, url);
	}
}

function closeDialog(domId)
{
	$(function(){
		$("#" + domId).dialog('close');
	});
}

function viewReview(reviewType, reviewId, fileId)
{
	if(reviewType == 'music')
		requestUrl =  webPath + '/common/music_review_info.jsp?musicReviewId=' + reviewId + '&fileId=' + fileId + '&reviewType=music';
	else if(reviewType == 'album')
		requestUrl =  webPath + '/common/music_review_info.jsp?musicReviewId=' + reviewId + '&fileId=' + fileId + '&reviewType=album';
	else if(reviewType == 'sharing_album')
		requestUrl =  webPath + '/common/playlist_review_info.jsp?playListReviewId=' + reviewId + '&playListId=' + fileId + '&reviewType=sharing_album';	
	else if(reviewType == 'album_showcase')
		requestUrl =  webPath + '/common/music_review_info.jsp?musicReviewId=' + reviewId + '&fileId=' + fileId + '&reviewType=album_showcase';
	else if(reviewType == 'mypage_sharing_album')
		requestUrl =  webPath + '/common/playlist_review_info.jsp?playListReviewId=' + reviewId + '&playListId=' + fileId + '&reviewType=mypage_sharing_album';	
		
	$.get(requestUrl, function(data){
  		if(jQuery.trim(data) == 'not_login')
  			jAlert('review info', loginNeedMsg);
		else
			$(".review_area").html(data);	
	});
}

function modifyReview(reviewType, reviewId, fileId)
{
	if(reviewType == 'music')
		requestUrl =  webPath + '/common/music_review_modify.jsp?musicReviewId=' + reviewId + '&fileId=' + fileId + '&reviewType=music';
	else if(reviewType == 'album')
		requestUrl =  webPath + '/common/music_review_modify.jsp?musicReviewId=' + reviewId + '&fileId=' + fileId + '&reviewType=album';
	else if(reviewType == 'sharing_album')
		requestUrl =  webPath + '/common/playlist_review_modify.jsp?playListReviewId=' + reviewId + '&playListId=' + fileId + '&reviewType=sharing_album';	
	else if(reviewType == 'album_showcase')
		requestUrl =  webPath + '/common/music_review_modify.jsp?musicReviewId=' + reviewId + '&fileId=' + fileId + '&reviewType=album_showcase';
	else if(reviewType == 'mypage_sharing_album')
		requestUrl =  webPath + '/common/playlist_review_modify.jsp?playListReviewId=' + reviewId + '&playListId=' + fileId + '&reviewType=mypage_sharing_album';	
		
	$.get(requestUrl, function(data){
  		if(jQuery.trim(data) == 'not_login')
  			jAlert('review info', loginNeedMsg);
		else
			$(".review_area").html(data);	
	});
}

function goReviewForm(reviewType, fileId)
{
	if(reviewType == 'music')
		requestUrl =  webPath + '/common/music_review_write.jsp?fileId=' + fileId + '&reviewType=music';
	else if(reviewType == 'album')
		requestUrl =  webPath + '/common/music_review_write.jsp?fileId=' + fileId + '&reviewType=album';
	else if(reviewType == 'sharing_album')
		requestUrl =  webPath + '/common/playlist_review_write.jsp?playListId=' + fileId + '&reviewType=sharing_album';	
	else if(reviewType == 'album_showcase')
		requestUrl =  webPath + '/common/music_review_write.jsp?fileId=' + fileId + '&reviewType=album_showcase';
	else
		requestUrl =  webPath + '/common/playlist_review_write.jsp?playListId=' + fileId + '&reviewType=mypage_sharing_album';	
		
	$.get(requestUrl, function(data){
  		if(jQuery.trim(data) == 'not_login')
  			jAlert('write review', loginNeedMsg);
		else
			$(".review_area").html(data);	
	});
}

function voteAlbum(playListId)
{
	$.get(webPath + '/action/vote_album_action.jsp?playListId=' + playListId, function(data){
  			jAlert('vote album', data, function(){
  				location.reload();
  			});	
	});
}

function getWebPath(){
  var path = './';
  var e = document.createElement('span');
  e.innerHTML = '<a href="' + path + '" />';
  url = e.firstChild.href;
  var p = url.split('/');
  if(p[3] == '')
  	return '';
  else
  	return '/'+p[3];
}

function changeLocale(locale)
{
    SetCookie("locale", locale, null, "/");
    window.location.reload();
}

function getCookieVal (offset) {
   var endstr = document.cookie.indexOf (";", offset);
   if (endstr == -1) endstr = document.cookie.length;
   return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
   var arg = name + "=";
   var alen = arg.length;
   var clen = document.cookie.length;
   var i = 0;
   while (i < clen) { //while open
      var j = i + alen;
      if (document.cookie.substring(i, j) == arg)
         return getCookieVal (j);
      i = document.cookie.indexOf(" ", i) + 1;
      if (i == 0) break;
   } //while close
   return null;
}

function SetCookie(name, value) {
   var argv = SetCookie.arguments;
   var argc = SetCookie.arguments.length;
   var expires = (2 < argc) ? argv[2] : null;
   var path = (3 < argc) ? argv[3] : null;
   var domain = (4 < argc) ? argv[4] : null;
   var secure = (5 < argc) ? argv[5] : false;
   document.cookie = name + "=" + escape (value) +
      ((expires == null) ? "" :
         ("; expires=" + expires.toGMTString())) +
      ((path == null) ? "" : ("; path=" + path)) +
      ((domain == null) ? "" : ("; domain=" + domain)) +
      ((secure == true) ? "; secure" : "");
}

function toggleMenu(menu) {
	$(menu).parent().find("img").eq(0).toggle();
	$(menu).parent().find("img").eq(1).toggle();
	$(menu).parent().find("ul").toggle("fast");
}