﻿$(document).ready(function() {
    $('input[type="text"]').focus(function() { $(this).addClass("focus"); });
    $('input[type="text"]').blur(function() { $(this).removeClass("focus"); });
    $('input[type="password"]').focus(function() { $(this).addClass("focus"); });
    $('input[type="password"]').blur(function() { $(this).removeClass("focus"); });
    $('textarea').focus(function() { $(this).addClass("focus"); });
    $('textarea').blur(function() { $(this).removeClass("focus"); });
    //    $('a').click(function() {
    //        var html = $(this).attr("href");
    //        alert(html);
    //    });

    $('viewer').height($(document).height() - $('viewer').position.top);

    //hide stuff
    setTimeout(function() { $('.information').hide(1000); }, 5000);
    $(".denyreason").hide();

    //end hide stuff
});

var pollItemCount = 2;
function addPollItem() {
    if (pollItemCount < 10) {
        pollItemCount += 1;
        $('#polls').append("<li><input type='text' id='pollitem" + pollItemCount + "' /></li>");
    }
}

function removePollItem() {
    if (pollItemCount > 2) {
        $('#pollitem' + pollItemCount).parent().remove();
        pollItemCount -= 1;
    }

    if (pollItemCount <= 2) {

    }
}

$.fn.insertAtCaret = function(myValue) {
    return this.each(function() {
        //IE support
        if (document.selection) {
            this.focus();
            sel = document.selection.createRange();
            sel.text = myValue;
            this.focus();
        }
        //MOZILLA/NETSCAPE support
        else if (this.selectionStart || this.selectionStart == '0') {
            var startPos = this.selectionStart;
            var endPos = this.selectionEnd;
            var scrollTop = this.scrollTop;
            this.value = this.value.substring(0, startPos) + myValue + this.value.substring(endPos, this.value.length);
            this.focus();
            this.selectionStart = startPos + myValue.length;
            this.selectionEnd = startPos + myValue.length;
            this.scrollTop = scrollTop;
        } else {
            this.value += myValue;
            this.focus();
        }
    });
};

function flagMessage(messageid, clubid) {
    $.ajax({
        type: "GET",
        url: "/ajax?action=flagmessage&messageid=" + messageid + "&clubid=" + clubid,
        success: function(msg) {
            if (msg == "invalid") {
                alert('There was an error in flagging this message. Please try again later.');
            } else {
                alert('This message has been successfully flagged.');
            }
        }
    });
}

jQuery.jqURL = {

    url: // returns a string
	function(args) {
	    args =
			jQuery.extend({
			    win: window
			},
			args);
	    return args.win.location.href;
	},

    loc:
	function(urlstr, args) {
	    args =
			jQuery.extend({
			    win: window,
			    w: 500,
			    h: 500,
			    wintype: '_top'
			},
			args);

	    if (!args.t) {
	        args.t = screen.height / 2 - args.h / 2;
	    }
	    if (!args.l) {
	        args.l = screen.width / 2 - args.w / 2;
	    }
	    if (args['wintype'] == '_top') {
	        args.win.location.href = urlstr;
	    }
	    else {
	        open(
			urlstr,
			args['wintype'],
			'width=' + args.w + ',height=' + args.h + ',top=' + args.t + ',left=' + args.l + ',scrollbars,resizable'
			);

	    }
	    return;
	},

    qs:
	function(args) {
	    args = jQuery.extend({
	        ret: 'string',
	        win: window
	    },
		args);

	    if (args['ret'] == 'string') {
	        return jQuery.jqURL.url({ win: args.win }).split('?')[1];
	    }

	    else if (args['ret'] == 'object') {

	        var qsobj = {};
	        var thisqs = jQuery.jqURL.url({ win: args.win }).split('?')[1];

	        if (thisqs) {
	            var pairs = thisqs.split('&');
	            for (i = 0; i < pairs.length; i++) {
	                var pair = pairs[i].split('=');
	                qsobj[pair[0]] = pair[1];
	            }
	        }
	        return qsobj;
	    }
	},

    strip:
	function(args) {
	    args = jQuery.extend({
	        keys: '',
	        win: window
	    },
			args);

	    if (jQuery.jqURL.url().indexOf('?') == -1) { // no query string found
	        return jQuery.jqURL.url({ win: args.win });
	    }
	    // if no keys passed in, just return url with no querystring
	    else if (!args.keys) {
	        return jQuery.jqURL.url({ win: args.win }).split('?')[0];
	    }
	    else { //return stripped url

	        var qsobj = jQuery.jqURL.qs({ ret: 'object', win: args.win });  // object with key/value pairs		
	        var counter = 0;
	        var url = jQuery.jqURL.url({ win: args.win }).split('?')[0] + '?';
	        var amp = '';

	        for (var key in qsobj) {
	            if (args.keys.indexOf(key) == -1) {
	                // pass test, add this key/value to string
	                amp = (counter) ? '&' : '';
	                url = url + amp + key + '=' + qsobj[key];
	                counter++;
	            }
	        }
	        return url;
	    }
	},

    get:
	function(key, args) {
	    args = jQuery.extend({
	        win: window
	    }, args);

	    qsobj = jQuery.jqURL.qs({ ret: 'object', win: args.win });
	    return qsobj[key];
	},

    set:
	function(hash, args) {
	    args = jQuery.extend({
	        win: window
	    }, args);

	    // get current querystring
	    var qsobj = jQuery.jqURL.qs({ ret: 'object', win: args.win });

	    // add/set values from hash
	    for (var i in hash) {
	        qsobj[i] = hash[i];
	    }

	    var qstring = '';
	    var counter = 0;
	    var amp = '';

	    // turn qsobj into string
	    for (var k in qsobj) {
	        amp = (counter) ? '&' : '';
	        qstring = qstring + amp + k + '=' + qsobj[k];
	        counter++;
	    }
	    return jQuery.jqURL.strip({ win: args.win }) + '?' + qstring;
	}

};

$(document).ready(function() {
    $("fieldset legend").click(function() {
        $(this).parent().children().filter("ul, fieldset").toggle("fast");
    });
    $("fieldset").children().filter("p,img,table,ul,span,li,input,label,ol, fieldset").hide("fast");
});

function ShowWikiHelp() {
    var w = window.open("/wikicode.html", "wikicode", "location=1,status=1,width=600,height=400,scrollbars=yes");
    return !(w != null && w != undefined);
}

function validateReason(obj) {
    var reason = obj.parent().children("[name=reason]");
    if (reason.val().length > 0) {
        return true;
    } else {
        //show error
        //alert(reason);
        //$(reason).css({'background-color' : '#ffcece', 'border' : 'solid 2px #ff7575'});
        $(reason).addClass("errorText");
        obj.parent().children("span:last").show();
        $(reason).focus(function() {
            obj.parent().children("span:last").hide();
            $(reason).removeClass("errorText");
            $(this).addClass("focus");
        });
        return false;
    }
}