/**
 * 	Easy Caruser 0.7 - jQuery plugin
 *	written by Vitaly Dryukov	
 *	http://sites.google.com/site/messirelv/easyCarusel
 *
 *	Copyright (c) 2009 Vitaly Dryukov ( http://sites.google.com/site/messirelv/easyCarusel )
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *   
 *  Requirements 
 *  jQuery Easing v1.1.1             - http://gsgd.co.uk/sandbox/jquery.easing.php
 *  jQuery JavaScript Library v1.3.2 -  http://jquery.com/
 *
 */
 
/*
 *	markup example for $("#outercont").easyCarusel();
 *	
 * 	<div id="outercont">
 *      [optional: insert control here]
 *      <div class="ec_cont">
 *      <div class="ec_outer">
 *
 *		    <div><img src="images/01.jpg" alt="" /></div>
 *		    <div><img src="images/02.jpg" alt="" /></div>
 *		    <div><img src="images/03.jpg" alt="" /></div>
 *		    <div><img src="images/04.jpg" alt="" /></div>
 *		    <div><img src="images/05.jpg" alt="" /></div>
 *		    <div><img src="images/06.jpg" alt="" /></div>
 *	    </div>
 *	    </div>
 *      [optional: insert control here]
 *	</div>
 *
 */

jQuery.fn.easyCarusel = function(options){

    var options = jQuery.extend({
        prevId:      'prev',
        nextId:      'next',
        duration: 	 600,
        orientation: 'vertical',
        easing:      'easeOutSine',
        height:      '450px',
        width:       '450px'
    }, options);

    return this.each(function() {

    var outer = $(this).find(" > .ec_cont > .ec_outer");
    
    function getDirection(orientation)
        {
    
            var directons;
            if (options.orientation == 'vertical')
            {
                directons = {dim:"height", offset: "marginTop"};
            }
            else
            {
                directons = {dim:"width", offset: "marginLeft"};
                var all_div_width = 0;
    
                $(outer).find(" > div").each(function(){ all_div_width += $(this).width(); });
                $(outer).css({"width": all_div_width});
            }
            return directons;
        }    
    $(this).css({"width":options.width, "height":options.height});

    $(this).find(" > .ec_cont").css({"overflow":"hidden", "width": "100%", "height": "100%"});


    var click_left = 0;
    var click_right = 0;

    $("#"+options.prevId).click(function(){
        click_left++;
        while (click_left > 0)
        {
            $(":animated").stop();
            directons = getDirection(options.orientation)
            var clone  = $(outer).find(" > div:last");
            var dim    = eval("clone."+directons.dim+"()");
            var offset = eval("(" + "{"+directons.offset+" : 0}" + ")");
            $(clone).prependTo($(outer));
            $(outer).css(directons.offset,  -1 * dim )
            .animate(offset, options.duration, options.easing, function(){});
            click_left--;
        }    

    });
    $("#"+options.nextId).click(function(){
        click_right++;
        while (click_right > 0)
        {
            $(":animated").stop();
            directons = getDirection(options.orientation)
            var clone  = $(outer).find(" > div:first");
            var dim    = eval("clone."+directons.dim+"()");
            var offset = eval("(" + "{"+directons.offset+": -1 * "+dim + "})");
            $(outer).animate(offset, options.duration, options.easing, function(){
                $(outer).css(directons.offset,  0 );
                $(clone).appendTo($(outer));
            });
            click_right--;
        }
    });
    });

    


};


