$(document).ready(function(){//3e6c86
    var isLogged = $.aqCookie.get(['username']);
    if(isLogged['username']){
        $('#loginForm').html("<div id='message'></div>");
        $('#message').html("Logged in as: "+isLogged['username']+" | <a id='logout' href='rpc/logout.rpc.php'>Logout</a>")
        .hide()
        .fadeIn(3000, function() {
            })
    }else{
    }
    var baseTimeZone = +-7;
    var ratesLow = 6;
    var ratesHigh = 22;
    var currentTimeZone = +-7;
    var math = 0;

    $(".mmCharDel").livequery(function(e){
        $(this).click(function(ev){
			ev.preventDefault();
			var answer = confirm ("Are you sure you wish to delete this player?")
			if (answer){
				var dataString = 'action=' + 'delete'
				+ '&id=' + $(this).attr('numfield');
				$.ajax({
					type: "POST",
					url: "rpc/my_mesa.rpc.php",
					data: dataString,
					success: function(data) {
						$(this).parent().parent().remove();
					}
				});
			}else{
				return 0;
			}
		});
    });
    $(".mmClear").livequery(function(e){
        $(this).click(function(ev){
			ev.preventDefault();
			var $ref = $(this);
			var answer = confirm ("This will delete all of the items in this bank.  This is permanent.  Do you still want to clear this bank?")
			if (answer){
				var dataString = 'action=' + 'clear'
				+ '&bank=' + $(this).attr('numfield');
				$.ajax({
					type: "POST",
					url: "rpc/my_mesa.rpc.php",
					data: dataString,
					success: function(data) {
						if(data=="logged"){
							$ref.parents('tr:first').children(':eq(2)').html("Error");
						}else if(data=="idDupe"){
							$ref.parents('tr:first').children(':eq(2)').html("Error");
						}else{
							$ref.parents('tr:first').children(':eq(2)').html("0");
						}
					}
				});
			}else{
				return 0;
			}
		});
    });
    $("#mmAccountAccept").livequery(function(e){
        $(this).click(function(ev){
	        var email     = $('#email').val();
	        var password  = $('#password').val();
	        var password2 = $('#password2').val();
	        var phone     = $('#phone').val();
	        var address   = $('#address').val();
				var dataString = {
					'action'   :'updateAccount',
					'email'    :email,
					'password' :password,
					'password2':password2,
					'phone'    :phone,
					'address'  :address
				}
				$.ajax({
					type: "POST",
					url: "rpc/my_mesa.rpc.php",
					data: dataString,
					success: function(data) {
						if(data==1){
							$("#accountNotification").html("Account Information Saved.");
						}else if(data=="e"){
							$("#accountNotification").html("You require a valid email address.");
						}else if(data=="p"){
							$("#accountNotification").html("Your passwords did not match.");
						}else{
							$("#accountNotification").html("Oops, there was an error.");
						}
					}
				});
		});	    
	});
    $(".mmActivate").livequery(function(e){
        $(this).click(function(ev){
			ev.preventDefault();
			var $ref = $(this);
			var answer = confirm ("Before clicking OK, make sure you are not in your storage in-game or you might lose items. Are you sure you want to activate this Storage Bank?")
			if (answer){
				var dataString = 'action=' + 'activate'
				+ '&bank=' + $(this).attr('numfield');
				$.ajax({
					type: "POST",
					url: "rpc/my_mesa.rpc.php",
					data: dataString,
					success: function(data) {
						if(data=="logged"){
							$(".mmStoreActive").html("");
							$ref.parents('tr:first').children(':eq(1)').html("Error");
						}else if(data=="idDupe"){
							$(".mmStoreActive").html("");
							$ref.parents('tr:first').children(':eq(1)').html("Error");
						}else{
							$(".mmStoreActive").html("");
							$ref.parents('tr:first').children(':eq(1)').html("Active");
						}
					}
				});
			}else{
				return 0;
			}
		});
    });

    $(".mmt").livequery(function(e){
        $(this).click(function(ev){
			ev.preventDefault();
			$(this).next(".mmc").toggle();
		});
    });
    $(".mmm").livequery(function(e){
        $(this).click(function(ev){
			ev.preventDefault();
			var $this = $(this).parent().parent().parent().parent();
			$this.nextAll(".mmTable").toggle();
				if($this.nextAll(".mmTable").is(':visible')){
					$(this).html("(-)");
				}else{
					$(this).html("(+)");
				}
		});
    });

    $("#accordion").livequery(function(e){
        $("#accordion").accordion({
            header: "h3"
        });
    });
    $("#registerShow").livequery(function(e){
        $('#registerShow').click(function(){
            $('#registerDialog').dialog("open");
        });
    });
    $("#registerDialog").livequery(function(e){
        $("#registerDialog").dialog();
        $("#registerDialog").dialog("close");
    });
    $("#resizable").livequery(function(e){
        $(".resizable").resizable({
            maxHeight: 350,
            maxWidth: 910,
            minHeight: 100,
            minWidth: 300
        });
    });
    $('#hourSlider').livequery(function(e){
        $(this).slider({
            min: 0,
            max: 23,
            step: 1,
            slide: function(event, ui) {
                $("#currentHour").val(ui.value+':00');
                if(currentTimeZone > +-6){
                    if (ui.value >= ratesHigh && ui.value < ratesLow){
                        $("#currentXPRates").val('100x');
                    }
                    if (ui.value < ratesHigh || ui.value >= ratesLow){
                        $("#currentXPRates").val('70x');
                    }
                }else{
                    if (ui.value < ratesLow || ui.value >= ratesHigh){
                        $("#currentXPRates").val('100x');
                    }
                    if (ui.value >= ratesLow && ui.value < ratesHigh){
                        $("#currentXPRates").val('70x');
                    }
                }
            }
        });
    });
    $('#timeZoneSlider').livequery(function(e){
        $(this).slider({
            value: -7,
            min: -12,
            max: 12,
            step: 1,
            slide: function(event, ui) {
                $("#currentTimeZone").val(ui.value+':00');
                currentTimeZone = ui.value;
                ratesLow = 6;
                ratesHigh = 22;
                if(ui.value > baseTimeZone){
                    math = ui.value - baseTimeZone;
                    ratesLow = ratesLow + math;
                    if(ratesLow > 24){
                        ratesLow = ratesLow - 24;
                    }
                    if(ratesLow==24){
                        ratesLow=0;
                    }
                    ratesHigh = ratesHigh + math;
                    if(ratesHigh > 24){
                        ratesHigh = ratesHigh - 24;
                    }
                    if(ratesHigh==24){
                        ratesHigh=0;
                    }
                }
                if(ui.value < baseTimeZone){
                    math = ui.value - baseTimeZone;
                    ratesLow = ratesLow + math;
                    if(ratesLow > 24){
                        ratesLow = ratesLow - 24;
                    }
                    if(ratesLow==24){
                        ratesLow=0;
                    }
                    ratesHigh = ratesHigh + math;
                    if(ratesHigh > 24){
                        ratesHigh = ratesHigh - 24;
                    }
                    if(ratesHigh==24){
                        ratesHigh=0;
                    }
                }
                if(ui.value == baseTimeZone){
                    ratesLow = 6;
                    ratesHigh = 22;
                }
            }
        });
    });
    $('.break').livequery(function(e){
        Nifty("h4.break","normal transparent");
    });
    $('.dropdown').hover(function() {
        $('ul', this).css('display', 'block');
    },
    function() {
        $('ul', this).css('display', 'none');
    }
    );

    $('.hideLink').livequery('click',function(e){
        e.preventDefault();
        var loc = $(this).attr('href');
        $('#hiddenContainer').load(loc, {
            blind: 0
        }, function(){
            $('#contentContainer').hide();
            $('#hiddenContainer').show();
        });
    });
    $('.homeLink').livequery('click',function(e){
        e.preventDefault();
        $('#hiddenContainer').hide();
        $('#contentContainer').show();
    });
    $('#loginHiddenCol').livequery('click',function(){
        $('#loginHidden').toggle();
    });
    $('#ajaxRegister').click(function(e){
        e.preventDefault();
        $('#regMsg').fadeTo(0, 0.3);
        var username = $('#loginR').val();
        var password = $('#passwordR').val();
        var password2 = $('#password2R').val();
        var email = $('#emailR').val();
        var email2 = $('#email2R').val();
        var sex = $('#sexR').val();

        if(username == "" || password == "" || password2 == "" || email == "" || email2 == "" || sex == ""){
            $('#regMsg').html("One or more fields are empty.").fadeTo(3000, 1.0).fadeTo(3000, 0.3);
            return false;
        }
        if(password != password2){
            $('#regMsg').html("Your passwords do not match.").fadeTo(3000, 1.0).fadeTo(3000, 0.3);
            return false;
        }
        if(email != email2){
            $('#regMsg').html("Your emails do not match.").fadeTo(3000, 1.0).fadeTo(3000, 0.3);
            return false;
        }
        if(username.length < 4){
            $('#regMsg').html("Your username is too short.").fadeTo(3000, 1.0).fadeTo(3000, 0.3);
            return false;
        }
        if(password.length < 4){
            $('#regMsg').html("Your password is too short.").fadeTo(3000, 1.0).fadeTo(3000, 0.3);
            return false;
        }

        var dataString = 'passThrough='+ 'register'
        + '&username=' + username
        + '&password=' + password
        + '&email=' + email
        + '&sex=' + sex;
        //alert (dataString);return false;
        $.ajax({
            type: "POST",
            url: "rpc/register.rpc.php",
            data: dataString,
            success: function(data) {
                if(data=="0"){
                    $('#regMsg').html("Registration failed, try again later.")
                    .fadeTo(3000, 1.0)
                    .fadeTo(3000, 0.3)
                }
                if(data=="1"){
                    $('#regMsg').html("Registered Successfully")
                    .fadeTo(3000, 1.0)
                    .fadeTo(3000, 0.3)
                }
                if(data=="2"){
                    $('#regMsg').html("Username is taken, please try again.")
                    .fadeTo(3000, 1.0)
                    .fadeTo(3000, 0.3)
                }
            }
        });
        return false;
    });
    $('#ajaxUpdate').click(function(){
        var chPassword1 = $('#chPassword1').val();
        var chPassword2 = $('#chPassword2').val();
        var chEmail = $('#chEmail').val();

        var dataString = 'passThrough='+ 'update'
        + '&chEmail=' + chEmail
        + '&chPassword1=' + chPassword1
        + '&chPassword2=' + chPassword2;
        //alert (dataString);return false;

        $.ajax({
            type: "POST",
            url: "rpc/update.rpc.php",
            data: dataString,
            success: function(data) {
                if(data=="0"){
                    $('#loginHidden').append("<div id='messageHidden'></div>");
                    $('#messageHidden').html("Update Failed")
                    .hide()
                    .fadeIn(3000, function() {
                        })
                    .fadeOut("slow");
                }
                if(data=="1"){
                    $('#loginHidden').append("<div id='messageHidden'></div>");
                    $('#messageHidden').html("Updated Successfully")
                    .hide()
                    .fadeIn(3000, function() {
                        })
                    .fadeOut("slow");
                }
            }
        });
        return false;
    });
    $(function() {
        $('.pretty').each( function() {
            $(this).tooltip({
                track: true,
                delay: 0,
                showURL: false,
                showBody: " - ",
                extraClass: "pretty",
                fixPNG: true,
                left: -120
            })
        });
    });
});