﻿var serverURL = "http://www.ogreisland.com/";
var search_timeout = null;
var menu_timeout = null;


function openwin(url, win, attribs) { window.open(url, win, attribs); }
//onload = function() { document.body.style.background = 'url(/images/backtest4.jpg)' }


$(document).ready(function() {

    // custom extensions
    jQuery.fn.fadeToggle = function(speed, easing, callback) {
        return this.animate({ opacity: 'toggle' }, speed, easing, callback);
    };
    $.jCache.maxSize = 30;


    $(".menulist li").hover(
        function() {
            $(this).addClass("selected");
            $(this).find("ul").css("display", "block");
            $(".menulist li:not(.selected)").find("ul").css("display", "none");
            if (menu_timeout != null) {
                clearTimeout(menu_timeout);
            }
        },
        function() {
            clearTimeout(menu_timeout);
            menu_timeout = setTimeout(function() { $(".menulist li ul").css("display", "none") }, 500);
            $(this).removeClass("selected");
        });

    $(".menulist li").click(function(e) {
        document.location = $(this).attr("page");
        //$.jGrowl($(this).attr("page"));
        return false;
    });

    $(".charlistchar").click(function(e) {
        webcore("clickButton", JSON.stringify({ button: "charlink", data: $(this).attr("charid") }), "/ajax/clickButton/" + $(this).attr("id") + "/" + $(this).attr("data"));
    });

    $(".btnplay").click(function(e) {
        e.preventDefault();
        // need to leave window open here, otherwise popup blockers try to block
        var gamewin = window.open('/game2/client.aspx', 'webclient', 'width=1000,height=680,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,maximize=1');
        gamewin.focus();
        webcore("clickButton", JSON.stringify({ button: "play", data: "" }));
    });

    $(".btnbankreload").click(function(e) {
        window.location.reload();
    });






    $("#mask").click(function(e) {
        e.preventDefault();
        $(this).fadeOut('fast');
        $("#loginpanel").fadeOut('fast');
        $('#maskcontent').hide();
    });

    $('input').blur(function() {
        $('input').removeClass("focus");
    })
        .focus(function() {
            $(this).addClass("focus")
        });

    $(".pollanswer").live("click", function() {
        $("#maskcontent").hide();
        webcore("submitPollAnswer", JSON.stringify({ pollid: $(this).attr("pollid"), answer: $("#pollanswer").val(), notes: $("#pollnotes").val() }), "/ajax/clickButton/submitPoll");
    });


    $(".autobutton").live("click", function(e) {
        e.preventDefault();
        if ($('#A').attr('myattr')) {
            webcore("clickButton", JSON.stringify({ button: $(this).attr("id"), data: $(this).attr("data") }), "/ajax/clickButton/" + $(this).attr("id") + "/" + $(this).attr("data"));
        } else {
            webcore("clickButton", JSON.stringify({ button: $(this).attr("id"), data: "" }), "/ajax/clickButton/" + $(this).attr("id"));
        }
    });

    $("#btnlogout").click(function(e) {
        document.location = '/home.aspx?logout=true';
    });

    // forums
    $(".forum_header A, .FL, .pagebar A, .TL").live("click", function(e) {
        //e.preventDefault();
        //webcore("clickForum", JSON.stringify({ forumid: $(this).attr("forumid"), threadid: $(this).attr("threadid"), page: $(this).attr("page") }), "/ajax/clickForum");
        window.location = "/publicsquare.aspx?f=" + $(this).attr("forumid") + "&t=" + $(this).attr("threadid") + "&p=" + $(this).attr("page");
    });
    $(".viewnew").live("click", function(e) {
        //webcore("clickForum", JSON.stringify({ forumid: "0", threadid: "0", page: "0" }), "/ajax/clickForum");
        window.location = "/publicsquare.aspx?f=0&t=0&p=0";
    });
    $(".newthread").live("click", function(e) {
        webcore("clickShowAddThread", JSON.stringify({ forumid: $(this).attr("forumid") }), "/ajax/clickForum");
    });
    $(".newpost").live("click", function(e) {
        webcore("clickShowAddPost", JSON.stringify({ forumid: $(this).attr("forumid"), threadid: $(this).attr("threadid") }), "/ajax/clickForum");
    });

    $("#btnSendThread").click(function() {
        webcore("clickAddThread", JSON.stringify({ forumid: $(this).attr("ForumId"), threadtitle: $("#threadtitle").val(), posttext: $("#posttext").val() }));
        $("#forumpost").hide();
        $("#sending").show();
    });

    $("#btnSendPost").click(function() {
        webcore("clickAddPost", JSON.stringify({ forumid: $(this).attr("ForumId"), threadid: $(this).attr("ThreadId"), posttext: $("#posttext").val() }));
        $("#forumpost").hide();
        $("#sending").show();
    });

    $(".postreply").click(function() {
        $("#posttext").focus();
    });

    $(".quotereply").click(function() {
        var quote = "\n\n[quote]" + $("#" + $(this).attr("postid") + " > .P_post").html().replace(/<br>/g, "\n") + "[/quote]\n\n";
        $("#posttext").val(quote);
        $("#posttext").focus();
        $("#posttext").setSelectionRange(quote.length, quote.length);
    });

    $("#btnSearch").click(function() {
        window.location = "/publicsquare.aspx?q=" + $("#q").val();
    });

    $("#message").click(function() {
        window.location = "/viewmessages.aspx";
    });

    $(".P_user, .WO_name, .alertname, .charlist_name").live("click", function() {
        window.location = "/players/" + $(this).text();
    });

    // tabs
    $(".tabs A:not(.on)").live("click", function(e) {
        e.preventDefault();
        $(".tabs li.on").removeClass("on");
        $(this).parent().addClass("on");
        webcore("clickButton", JSON.stringify({ button: $(this).parent().parent().attr("id"), data: $(this).text() }), "/ajax/clickTab/" + $(this).parent().parent().attr("id") + "/" + $(this).text());
    });



});                                               // document ready 


function setFirstTab() {
    $(".tabs li.on").removeClass("on");
    $(".tabs > li:first").addClass("on");
}

function getTipInfo(tiptype, tipkey) {
    //$.jCache.hasItem(
}

function makehover() {
    $('.charlink').simpletip({ 
        position:  ["0","-150"],
        content: 'loading..',
        onBeforeShow: function(){
            //alert(this.getParent().attr("charid"));
            var url = '/playertip.aspx?cid=' + this.getParent().attr("charid");
            if($.jCache.hasItem(url))
                this.update("*" + $.jCache.getItem(url));
            else 
                this.load(url); 
        }
        ,
        onContentLoad: function() {
            var url = '/playertip.aspx?cid=' + this.getParent().attr("charid");
            $.jCache.setItem(url,this.getTooltip().html());
        }
    });
    $(".button").each(function() {
        $(this).find("span:last").css("width",$(this).css("width")).css("height",$(this).css("height"));
    });
    $(".button").hover(function () {
        $(this).find("span:last").stop().animate({
            opacity: .8
        }, 'slow');
    },function () {
        $(this).find("span:last").stop().animate({
            opacity: 0
        }, 'slow');
    });
}


  function webcore(cmd, json, tracking) {
    $("#logo").find("span:last").stop().animate({
        opacity: .8
    }, 'slow');
    $.ajax({
        type: "POST",
        url: "/services/webcore.asmx/" + cmd + "?t=" + Math.floor(Math.random() * 1000001),
        data: json,
        async: true,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        error: function(e) {
            //$("#content").html("Error: " + e.responseText);
            $.jGrowl("ERROR: " + e.responseText);
            //alert("Error: " +   json.toString());
            //$("#logo").find("span:last").stop().animate({
            //    opacity: .0
            //}, 'fast');
        },
        success: function(msg) {
            $.taconite(msg.d);
            //$("#logo").find("span:last").stop().animate({
            //    opacity: .0
            //}, 'fast');
            //if (cmd != null && tracking != null && tracking != "") {
            //    pageTracker._trackPageview("/");
            //}
        }
    });
  }
