(function($) {
    $.fn.divider = function(options) {
        var defaults = {
            inSpeed: 'fast',
            outSpeed: 'fast',
            inEasing: null,
            outEasing: null,
            leftArrow: '<',
            rightArrow: '>',
            borderColor: '#CC0000',
            backgroundColor: '#BBBBBB',
            direction: 'left',
			animateIntro: false,
            load: null,
            append: null,
			fixed: false
        }
        var opts = $.extend(defaults, options);

        function createCookie(name, value, days) {
            if (days) {
                var date = new Date();
                date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                var expires = "; expires=" + date.toGMTString();
            }
            else var expires = "";
            document.cookie = name + "=" + value + expires + "; path=/";
        }
        function readCookie(name) {
            var nameEQ = name + "=";
            var ca = document.cookie.split(';');
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) == ' ') c = c.substring(1, c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
            }
            return "ute";
        }

        return this.each(function() {
			$(this).append('<div class="divider"><img src="../img/logo_canvas.png" onclick=closeContent();clearMenu(); /></div>');
            var theDivider = $(this).children('.divider').get(0);
            var theParent = $(this);
            var orgWidth = $(theParent).width();
			var positioning = opts.fixed == true ? 'fixed' : 'absolute';

            if (opts.direction == 'right')
                $(theDivider).css({ left: '0px', right: 'auto' });
            else
                $(theDivider).css({ right: '0px', left: 'auto' });

			$(theParent).height('64px');

            $(theDivider).css({
                position: 'absolute', textAlign: 'center', 
                width: '90px', height: '64px', top: '0px', paddingTop: '0px', paddingLeft: '5px', cursor: 'pointer'
            }).bind('click', function() {
				//FECHA A BARRA
                if ($(theDivider).width() < $(theParent).width()) {
                    if (opts.direction == "left") {
                        //krymp <--
                        $(theParent).animate({ width: $(theDivider).width() }, opts.inSpeed, opts.inEasing);
                        $(theParent).next().animate({ left: $(theParent).position().left + $(theDivider).width() }, opts.inSpeed, opts.inEasing);
                    }
                    else if (opts.direction == "right") {
                        //krymp -->
                        $(theParent).animate({ width: $(theDivider).width() }, opts.inSpeed, opts.inEasing);
                        $(theParent).next().animate({ width: $(theParent).parent().width() - $(theDivider).width() }, opts.inSpeed, opts.inEasing);
                    }
                   $(theParent).children().not($(theDivider)).hide();
                    createCookie("divider", "inne", 730);
                }
				//abre a barra
                else {
                    if (opts.direction == "left") {
                        //utvid -->
                        $(theParent).animate({ width: orgWidth }, opts.inSpeed, opts.outEasing);
                        $(theParent).next().animate({ left: $(theParent).position().left + orgWidth }, opts.outSpeed, opts.outEasing);
                    }
                    if (opts.direction == "right") {
                        //utvid <--
                        $(theParent).animate({ width: orgWidth }, opts.inSpeed, opts.outEasing);
                        $(theParent).next().animate({ width: $(document).width() - orgWidth - $(theDivider).width() }, opts.outSpeed, opts.outEasing);
                    }
                    $(theParent).children().not($(theDivider)).show();
                    createCookie("divider", "ute", 730);
                }
            });
			//ESCONDE NO INÍCIO
            $(theParent).children().not($(theDivider)).hide();

            $(window).bind("resize", function() {
                $(theParent).next().css({ width: $(document).width() - orgWidth });
            });

			if (opts.animateIntro) {
				if (readCookie("divider").indexOf('inne') >= 0) { 
					window.setTimeout(function(){ $(theDivider).click(); }, 1000);
					
				}
			}		
			else {
                if (opts.direction == "left") {
                    //krymp <--
                    $(theParent).css({ width: $(theDivider).width() });
                    $(theParent).next().css({ left: $(theParent).position().left + $(theDivider).width() });
                }
                else if (opts.direction == "right") {
                    //krymp -->
                    $(theParent).css({ width: $(theDivider).width() });
                    $(theParent).next().css({ width: $(theParent).parent().width() - $(theDivider).width() });
                }
			}
		});
    }
})(jQuery);


