/*
 * JQuery Divider Plugin
 * 
 * Version 0.8
 * 
 * by Christian Bruun - 23. jan 2009
 * 
 * Like it/use it? Send me an e-mail: rockechris@rockechris.com
 * 
 * License: None. Use and abuse. Comes with no warranty, of course!
 * 
 * 
 * Usage:
 * $('div').divider({ options });
 * 
 * Options:
 * inSpeed/outSpeed:	animation speed
 * inEasing/outEasing:	animation easing
 * leftArrow/rightArrow:letters used to indicate 'clickability'
 * borderColor:			color of divider's borders and the arrow
 * bacgroundColor:		color of divider's background
 * direction:			direction to slide 'out', to hide the menu div
 * animateIntro:		turn startup animation on/off
 * load:				page to load into the hidable side of the divider
 * append:				append ajax-loaded page to content or not
 * fixed:				use fixed positioning instead of absolute
 * 
 */
(function($) {
    $.fn.divider = function(options) {
        var defaults = {
            inSpeed: 'fast',
            outSpeed: 'fast',
            inEasing: null,
            outEasing: null,
            leftArrow: '<',
            rightArrow: '>',
            borderColor: '#1C1C1C',
            backgroundColor: '#BBBBBB',
            direction: 'left',
			animateIntro: true,
            load: null,
            append: null,
			fixed: false
        }
        var opts = $.extend(defaults, options);

        /* cookie functions from http://www.quirksmode.org/js/cookies.html */
        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"; //if no cookie exist, divider stays in default position
        }

        return this.each(function() {
            $(this).append('<div class="divider">' + (opts.direction == "right" ? opts.rightArrow : opts.leftArrow) + '</div>');
            var theDivider = $(this).children('.divider').get(0);
            var theParent = $(this);
            var orgWidth = $(theParent).width();
			var positioning = opts.fixed == true ? 'fixed' : 'absolute';

            //empty the 'menu' before ajax load
            if (opts.load != false && !opts.append) {
                $(theParent).empty();
                $(theParent).append(theDivider);
            }

            //set up CSS and events
            if (opts.direction == 'right')
                $(theDivider).css({ left: '0px', right: 'auto' });
            else
                $(theDivider).css({ right: '0px', left: 'auto' });

            $(theParent).height($(document).height());

            $(theDivider).css({
                position: positioning, textAlign: 'center', color: opts.borderColor,
                width: '9px', height: $(document).height(), top: '0px', paddingTop: '2px', cursor: 'pointer',
                backgroundColor: opts.backgroundColor, borderLeftColor: opts.borderColor, borderRightColor: opts.borderColor,
                borderLeftWidth: '1px', borderRightWidth: '1px', borderLeftStyle: 'solid', borderRightStyle: 'solid'
            }).bind('click', function() {
                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);
                        $(theDivider).text(opts.rightArrow);
                    }
                    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);
                        $(theDivider).text(opts.leftArrow);
                    }
                    $(theParent).children().not($(theDivider)).hide(); //needed in IE :S
                    createCookie("divider", "inne", 730);
                }
                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);
                        $(theDivider).text(opts.leftArrow);
                    }
                    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);
                        $(theDivider).text(opts.rightArrow);
                    }
                    $(theParent).children().not($(theDivider)).show();
                    createCookie("divider", "ute", 730);
                }
            });

            //ajax load
            if (opts.load != null) {
                $(theDivider).before('<div id="divider-loaded-content"></div>');
                $('#divider-loaded-content').load(opts.load, function(responseText, textStatus, XMLHttpRequest) {
                    if (textStatus != 'success')
                        console.log(textStatus);
                }).width($(theParent).innerWidth() - $(theDivider).outerWidth()).css('z-index', $(theParent).css('z-index') + 8);
            }

            $(window).bind("resize", function() {
                $(theParent).next().css({ width: $(document).width() - orgWidth });
            });

            //startup animation
			if (opts.animateIntro) {
				if (readCookie("divider").indexOf('inne') >= 0) { 
					window.setTimeout(function(){ $(theDivider).click(); }, 1000);
				}
				else {
					window.setTimeout(function(){ $(theDivider).css({ 'backgroundColor': 'white' }); }, 200);
					window.setTimeout(function(){ $(theDivider).css({ 'backgroundColor': opts.backgroundColor }); }, 400);
					window.setTimeout(function(){ $(theDivider).css({ 'backgroundColor': 'white' }); }, 600);
					window.setTimeout(function(){ $(theDivider).css({ 'backgroundColor': opts.backgroundColor }); }, 800);
					window.setTimeout(function(){ $(theDivider).css({ 'backgroundColor': 'white' }); }, 900);
					window.setTimeout(function(){ $(theDivider).css({ 'backgroundColor': opts.backgroundColor }); }, 1000);
				}
			}		
			else {
                if (opts.direction == "left") {
                    //krymp <--
                    $(theParent).css({ width: $(theDivider).width() });
                    $(theParent).next().css({ left: $(theParent).position().left + $(theDivider).width() });
                    $(theDivider).text(opts.rightArrow);
                }
                else if (opts.direction == "right") {
                    //krymp -->
                    $(theParent).css({ width: $(theDivider).width() });
                    $(theParent).next().css({ width: $(theParent).parent().width() - $(theDivider).width() });
                    $(theDivider).text(opts.leftArrow);
                }
			}
		});
    }
})(jQuery);


