// Beginning working out DOM
$(document)
    .ready(function() {
        // Boostrap FX
        var SE = new ISEFX();

        // $("#fade")[0].style.height = $(window).height() + 0 + "px";
        // Datepicker
        if (typeof $.datepicker != "undefined")
            $("#user_dob").datepicker({ firstDay: 1, showAnim: 'drop' });

        // Global validation settings
        $.validator.addMethod(
        // slavin: that's my first regular expression. i'm very proud of it. ;-)
            "edate",
            function(value, element) {
                if (value.match(/^([0-9]){1,2}(\/){1}([0-9]){1,2}(\/){1}([0-9]){4}$/)) {
                    var date = value.split('/');
                    if (date[0] > 31 || date[0] < 1) return false;
                    if (date[1] > 12 || date[1] < 1) return false;
                    if (date[2] > 2009 || date[2] < 1900) return false;
                    return true;
                }
                else {
                    return false;
                }
            },
            "dd/mm/yyyy"
        );
        $.validator.addMethod(
        "integer",
        function(value, element) {
            if (value.match(/^\d+$/)) {
                return true;
            }
            else {
                return false;
            }
        },
        "dd/mm/yyyy"
        );

        $(".today", "form").click(function() {
            var today = new Date();
            var dd = today.getDate();
            var mm = today.getMonth() + 1; //January is 0!
            var yyyy = today.getFullYear();
            if (dd < 10) { dd = '0' + dd }
            if (mm < 10) { mm = '0' + mm }
            $(".date", "form").val(dd + '/' + mm + '/' + yyyy);
        });
        // Clean up text elements
        $("textarea").not("#Description").not(".nasty").one("focus", function() { this.value = ""; });
        $("input[type='text']").not("#no_clear").not(".no_clear").not(".nasty").one("focus", function() { this.value = ""; });

        // Accordion control
        $("ul#list_220_collapsable li h3 a").click(function() {
            var anchor = $(this).parent().parent();
            var context = anchor.children("ul");
            SE.MakeUpYourMindAnchor(anchor);
            SE.MakeUpYourMindContext(context);
            return false;
        }).parent().parent().addClass("closed");

        // Accordion control (Stats on Settings)
        $("ul#list_300_collapsable li h3 a").click(function() {
            var anchor = $(this).parent().parent();
            var context = anchor.children("ul");
            SE.MakeUpYourMindAnchor(anchor);
            SE.MakeUpYourMindContext(context);
            return false;
        }).parent().parent().addClass("closed");

        // Header Accordion (Stats information)
        $("a#button_context_info").click(function() {
            var anchor = $(this);
            var context = $("div#context_info");
            SE.MakeUpYourMindAnchor(anchor);
            SE.MakeUpYourMindContext(context);
            return false;
        });

        // Gallery accordion
        $(".title").not(".buddy").click(function() {
            var anchor = $(this);
            var context = anchor.next();
            SE.MakeUpYourMindAnchor(anchor);
            SE.MakeUpYourMindContext(context);
            return false;
        })

        // Shouts
        $("div#shout_new textarea").keyup(function(event) {
            var counter = 140;
            $("#shout_count").text(counter - this.value.length);
        });
        $("textarea#caption").not(".s350").keyup(function(event) {
            var counter = 140;
            $("#shout_count").text(counter - this.value.length);
        });
        $("textarea#Description").not(".s350").keyup(function(event) {
            var counter = 140;
            $("#shout_count").text(counter - this.value.length);
        });

        $("textarea.s350").keyup(function(event) {
            var counter = 350;
            $("#shout_count").text(counter - this.value.length);
        });

        // Invitation feedback
        $("form#invitationForm").submit(function() {
            var x = window.confirm("Are you sure?");
            if (!x) return false;
            var SE = new ISEFX();
            SE.AsianAlert("Message has been sent.");
        });

        // Groups. New Forum button
        $("a#group_new_forum").click(function() {
            return false;
        });

        // Close pops
        $(".closePopup").live("click", function() {
            $(".poppa").remove();
            $("body").css({ "overflow": "auto" });
            return false;
        });

        // Zebra
        $(".messages li:odd").addClass("alternate");
        $(".zebra tr:odd").addClass("alternate");

    })
    .ajaxSend(function() {
        var SE = new ISEFX();
        SE.AsianAlertStart("working out..");
    })
    .ajaxStop(function() {
        var SE = new ISEFX();
        SE.AsianAlertStop();
    });


// UIX SportEgg
function ISEFX()
{
	var object = new Object();
	object = {
	    Log: function(message) {
	        if (typeof jQuery == "undefined") {
	            alert(message + " (WTF jQuery?)");
	        }
	        else {
	            if (jQuery.browser.mozilla) {
	                if (typeof console != "undefined")
	                    console.log(message);
	            }
	            else alert(message);
	        }
	    },
	    MakeUpYourMind: function(anchor, context) {
	        this.MakeUpYourMindAnchor(anchor);
	        this.MakeUpYourMindContext(context);
	    },

	    MakeUpYourMindContext: function(object) {
	        if (object.hasClass("hidden"))
	        { object.removeClass("hidden"); }
	        else { object.addClass("hidden"); }
	    },
	    MakeUpYourMindAnchor: function(object) {
	        if (object.hasClass("closed"))
	        { object.removeClass("closed"); }
	        else { object.addClass("closed"); }
	    },

	    /*
	    Lightbox POP UPS
	    */
	    PopItUp: function() {


	    },

	    /* Asian (Yellow) POP UP */
	    AsianAlert: function(message) {
	        $('<div class="feedback_bar">' + message + '</div>').insertAfter("body").fadeIn(1400).fadeOut(800).remove();
	    },

	    /* Asian Asyncronous(Yellow) POP UP */
	    AsianAlertStart: function(message) {
	        if ($(".feedback_bar").length > 0) { $(".feedback_bar").remove(); }
	        else { $('<div class="feedback_bar">' + message + '</div>').insertAfter("body").fadeIn(1400); }
	    },

	    AsianAlertStop: function(message) {
	        $(".feedback_bar").fadeOut(800).remove();
	    },
	    /*	Dependancy Injection 
	    Expects: {name_of_the_js_file}, by default it searches this file 
	    at the root folder, however you can specify the folder in the second parameter.
	    Ex 1: Import("jquery-1.3.2.min.js")
	    Ex 2: Import("jquery-1.3.2.min.js","Scripts")
	    */
	    Import: function(what, where) {
	        var scriptTag = document.getElementById("ScriptTag");
	        var headTag = document.getElementsByTagName("head").item(0);
	        if (scriptTag) {
	            headTag.removeChild(scriptTag);
	        }
	        scriptTag = document.createElement("script");
	        scriptTag.src = what;
	        if (where)
	            scriptTag.src = where + "/" + what;
	        scriptTag.type = "text/javascript";
	        scriptTag.id = what;
	        headTag.appendChild(scriptTag);
	    },

	    /*
	    Dependancy Injection
	    Injects the script inside of the code.
	    Pretty much like Using statement in C#.
	    Ex 1: Inject("mysuperlogger.js")
	    Callback: mysuperlogger
	    */
	    Inject: function(what, where) {
	        if (typeof jQuery == "undefined") {
	            this.Log("WTF jQuery?");
	        }
	        else {
	            var scriptToInject = what;
	            if (!where) this.Log("Do something with the callback");
	            if (typeof where == "function") {
	                jQuery.getScript(scriptToInject, where);
	            }
	            else {
	                scriptToInject = where + "/" + what;
	                var callback = what.split('.')[0];
	                jQuery.getScript(scriptToInject, eval(callback));
	            }
	        }

	    }
	};
	return object;
}

function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}