
$(function() {

	var str = "";
	var name = $("#name2"),
		password = $("#password2"),
		allFields = $([]).add(password),
		tips = $("#validateTips");

	function updateTips(t) {
		tips.text(t).effect("highlight",{},1500);
	}
	
	function checkValue(o, callback) {

		$.getJSON("supe.php5", { cmd : "info" }, make_info);

		function make_info(info) {
			str = info["itemPassword"];
			if ( o.val()!==str ) {
				o.addClass('ui-state-error');
				updateTips("Enter the super secret password.");
				return false;
			} else {
				callback();
			}
		}
	}
	
	function download() {
		allFields.removeClass('ui-state-error');
		var bValid = checkValue(password, function() {
			window.location.href=myVal;
			$("#dialog2").dialog('close');		
		});
	}	


	$("#dialog2").dialog({
		bgiframe: true,
		autoOpen: false,
		width:	740,
		modal: true,
		close: function() {
			allFields.val('').removeClass('ui-state-error');
		}
	});

	$('.download-dropbox').click(function(event) {
		myVal=$(this).attr("href");
		var htmlStr = $(this).html();
		var playList = $(this).attr("name");
		$('#dialog2').dialog('open');
		$('#dialog2 .left-side .art-info p').html(htmlStr);
		$('#dialog2 .right-side').html(playList);
		event.preventDefault();
	});
	
	$("#dialog2").keypress(function(e) {
		var charCode;

		if(e && e.which){
			charCode = e.which;
		}else if(window.event){
			e = window.event;
			charCode = e.keyCode;
		}
		
		if(charCode == 13 || charCode == 3) {
			e.preventDefault();
			download();
		}	
	});
	
	$('#dwnClose2').click(function(event) {
		$('#dialog2').dialog('close');
	});

	$("#dwnClose2").hover(
		function () {
			$(this).addClass("ui-state-hover");
		},
		function () {
			$(this).removeClass("ui-state-hover");
		}
	);	
	
	$('#dwnDownload').click(function(event) {
		download();
	});
	
	$("#dwnDownload").hover(
		function () {
			$(this).addClass("ui-state-hover");
		},
		function () {
			$(this).removeClass("ui-state-hover");
		}
	);		
});