

(function () {

    var $ = jQuery,
        $j = jQuery,
		undefined;
	
	
	if(window.console == undefined){
		window.console = {
			log:function(){}
		};
	}
	
	if(window.assert == undefined){
		window.assert = function(condition, message){
			if(!condition){
				console.log(message);
				return false;
			}
			return true;
		}
	}
	

	window.popup = function (url, id, options) {
        var opts = $.extend({
            height: null,
            width: null,
            location: null,
            fullscreen: 0
        }, (typeof id === "object") ? id : options),
            win = window.open(url, id || window.location.host, (function () { var str = ""; for (var optName in opts) { str += optName + "=" + opts[optName] + ","; } return str; })());
        return win;
    };

    window.printHtml = function (htmlStr, opts) {
        var win = window.popup(null, "print", $.extend({ height: 500, width: 800 }, opts));
        win.document.writeln(htmlStr);
        win.print();
        return win;
    };
	
	


    /**
    *   @method jQuery.fn.buildFaqAnchors
    *   @description 
    *   @author Stephen Rushing, eSiteful
    */
    $.fn.buildFaqAnchors = function (ol) {
        if (!(ol instanceof $)) ol = $(ol);

        this.each(function (i, dl) {
            var $q = $(this).children("dt").clone(),
                $a = $(this).children("dd");

            $q.children("a").remove();
            var item = $("<li><a href='#" + $(this).attr("id") + "'>" + $q.html() + "</a></li>").appendTo(ol);

            item.find("a>a").remove();

        });

    };


    /**
    *   @method jQuery.fn.blinkCss
    *   @description 
    *   @author Stephen Rushing, eSiteful
    */
    $.fn.blinkCss = function (css, showTime, hideTime, count) {

        var B = this;
        showTime = showTime != null ? showTime : 500;
        hideTime = hideTime != null ? hideTime : showTime / 2;
        count = count != null ? count : 3;

        this.each(function (n, node) {
            var $node = $(node),
				orig = isClass ? $node.attr("class") : $node.attr("style");
            $node.data("blinkCssOrig", orig || "");
        });

        var isClass = typeof (css) === "string",
			counted = 0,
			showTimer = null,
			hideTimer = null;

        function show() {
            B.each(function (n, node) {
                if (isClass) {
                    $(node).addClass(css);
                } else {
                    $(node).css(css);
                }
                counted++;
            });
            hideTimer = setTimeout(hide, showTime);
        }

        function hide() {
            B.each(function (n, node) {
                var counted = 0,
					$node = isClass ? $(node).addClass(css) : $(node).css(css),
					orig = $node.data("blinkCssOrig") || "";
                $node.attr(isClass ? "class" : "style", orig);
            });
            if (counted < count) {
                setTimeout(show, hideTime);
            }
        }

        show();


    };


    /**
    *   @method jQuery.fn.getLabels
    *   @description 
    *   @author Stephen Rushing, eSiteful
    */
    $.fn.getLabels = function () {
        var labels = $([]),
			all = $("label");

        this.each(function (n, node) {
            var $node = $(node),
				lbl = all.filter("[for='" + node.id + "']");
            if (!lbl.length) lbl = $node.closest("label");
            if (lbl.length) labels = labels.add(lbl);
        });
        return labels;
    };


    /**
    *   @method imgToSprite
    *   @description converts <img/> tags to a specified element with the img src as the background. The original use case is for creating rounded corners on images with the border-radius property, which Firefox and IE+css3pie do not render correctly.
    *   @author Stephen Rushing, eSiteful
    */

    $.fn.imgToSprite = function (opts) {
        var I = this,
            defaults = {
                container: $("<div class='img-sprite'/>"),
                height: -1, //-1 dimension indicates that we should use the current dimension of the image
                width: -1,
                manipulator: "before"
            },
            o = $.extend({}, defaults, opts),
            images = this.filter("img");

        //search for child images if there are no images in this set
        if (!images.length) {
            images = this.find("img");
        }

        function replace(img) {
            var c = o.container.clone(true),
                $img = $(img),
                height = o.height !== -1 ? o.height : $img.height(),
                width = o.width !== -1 ? o.width : $img.width(),
                bgSize = ((height !== null && width !== null) ? height + "px " + width + "px" : null),
                bgUrl = "url('" + img.src + "')",
                cssAttrs = {
                    backgroundImage: bgUrl,
                    height: height,
                    width: width,
                    backgroundRepeat: "no-repeat",
                    backgroundSize: height + "px " + width + "px",
                    visibility: "",
                    display: ""
                };

            //We must remove any scaling from the image until browsers support background scaling better
            if (!$.browser.safari) {
                $img.removeAttr("height").removeAttr("width").css({ height: "auto", width: "auto" });
                cssAttrs.height = img.height;
                cssAttrs.width = img.width;
            }

            c.addClass($img.attr("class"));
            c.attr("style", $img.attr("style"));
            c.css(cssAttrs);

            $img[o.manipulator](c);
        }

        images.each(function (i, img) {
            var $img = $(img);
            if (!img.complete) {
                $img.load(function (evt) {
                    replace(img);
                });
            } else {
                setTimeout(function () { replace(img); }, 1); //IE needs the delay for some reason
            }
        });

        return this;
    };
	
	/* Converts a serialized string into a simple object */	
	$.unserialize = function(str){
		var chunks = str.split("&"),
			obj = {};
		for(var c=0; c<chunks.length; c++){
			var spl = chunks[c].split("=", 2);
			obj[spl[0]] = spl[1];
		}
		return obj;
	};
	
	/* Converts a standard link to a form POST submission */
	$.fn.anchorPost = function(postFn){

		if(postFn==null){
			postFn = function(){
				return $(this).attr("data-post");
			}
		}
		
		this.each(function(a, anchor){			
			
			var $a = $(anchor),
				form = $("<form id='entries_form' method='post' />").appendTo("body").hide(),
				formVars = postFn.call(anchor);
				
			form.attr("target", $a.attr("target"));
			form.attr("action", $a.attr("href"));
			
			if(!$.isPlainObject(formVars)){
				formVars = $.unserialize(formVars);	
			}
			
			for(var i in formVars){
				form.append("<input type='hidden' name='"+i+"' value='"+formVars[i]+"' />");
			}
			
			//Remove href and target from anchor so it doesn't do anything, which allows it to be used for something else, if the form is being opened in a diff window.
			$a.attr("href","javascript:;");
			$a.removeAttr("target");
		
			$a.click(function(){
				form.submit();
			});
		});
		
		return this;
	};
	
	


})();







