
(function ($) {
    $.fn.flipbook = function (o) {


        var i = 0;

        var cthis;
        var cacheObject;

        var nrChildren = 0;

        var totalWidth = 750;
        var totalHeight = 500;

        var currNr = 0;
        var animating = false;
        var busy = false;
        var animationTime = 700;

        var shadowWidth = 30;
        var tempTime = 0;

        var lastIndex = 999;

        var coverPage = "on";
        var alignCenter = "on";
        var pressMove = "onf";

        var pressed = false;
        var pressAnimating = false;
        var sw = false;


        var mouseX = 0;


        var defaults = {
            totalWidth: -1,
            totalHeight: -1,
            coverPage: undefined,
            shadowWidth: undefined,
            miscAction: undefined
        },

	o = $.extend(defaults, o);

        this.each(function () {




            cthis = jQuery(this);
            nrChildren = cthis.children().length;



            if (o.totalWidth != -1)
                totalWidth = o.totalWidth;
            if (o.totalHeight != -1)
                totalHeight = o.totalHeight;
            if (o.coverPage != undefined)
                coverPage = o.coverPage;
            if (o.shadowWidth != undefined)
                shadowWidth = o.shadowWidth;

            if (cthis.css('visibility') == 'hidden') {

                cthis.css('visibility', 'visible')
                cthis.css('opacity', 0)
                cthis.delay(1500).animate({ 'opacity': 1 }, 1000)

                jQuery('.flipbook-preloader').delay(1500).hide('slow');

            }



            if (o.miscAction != undefined)
                isStolen();


            init();


            function init() {
                cthis.append('<div class="pages"></div>');
                for (i = 0; i < nrChildren; i++) {

                    cthis.find('.pages').append('<div class="page"><div class="page_left_con"><div class="shadow_left"></div><div class="page_left"></div></div><div class="page_right_con"><div class="shadow_right"></div><div class="page_right"></div></div></div>');

                    if (!(coverPage == 'on' && i == 0))
                        cthis.find('.page_left').eq(i).append(cthis.children().eq(0).clone());

                    cthis.find('.page_right').eq(i).append(cthis.children().eq(0).clone());
                    cthis.children().eq(0).remove();
                }
                //animateIt();
                //console.log(cthis.children().eq(0))
                //cthis.find('.pages').append(cthis.children().eq(0));

                for (i = 0; i < nrChildren; i++)
                    if (!(coverPage == 'on' && i == 0))
                        cthis.find('.page_right').eq(i).css('right', 0)
                    else
                        cthis.find('.page_right').eq(i).css('right', -totalWidth / 2)

                cthis.css({
                    'width': totalWidth,
                    'height': totalHeight
                })
                cthis.find('.page_left_con').css({
                    'width': 0,
                    'height': totalHeight
                })

                if (coverPage == 'on' && alignCenter == 'on')
                    cthis.css('left', -totalWidth / 4)

                cthis.find('.page').eq(0).css('z-index', lastIndex++)
                cthis.find('.page_left').css('width', totalWidth)
                cthis.find('.page_right').css('width', totalWidth)
                cacheObject = cthis.find('.page');
                cacheObject.css('width', totalWidth)
                cacheObject.css('height', totalHeight)



                cthis.find('.shadow_left').css({
                    'width': totalWidth / 2,
                    'height': totalHeight
                })
                cthis.find('.shadow_right').css({
                    'width': totalWidth / 2,
                    'height': totalHeight
                })

                cthis.find('.page_left_con').css({
                    'left': 0,
                    'width': totalWidth / 2
                })



                cthis.find('.page_right_con').css({
                    'width': totalWidth / 2,
                    'height': totalHeight
                })




                //gotoItem(1);
                cthis.find('.shadow_left').css('opacity', 0)
                cthis.find('.shadow_right').css('opacity', 0)

                cthis.mousemove(handleMove);

                if (pressMove == 'on') {
                    cthis.mousedown(function () {
                        pressed = true;
                    });

                    cthis.mouseup(function () {
                        pressed = false;
                        if (mouseX < totalWidth / 2)
                            gotoNext();
                    });
                }


                cthis.find('.page').click(handleClick);

                cthis.mouseleave(unfoldIt);


            }
            function handleMove(e) {
                //var d= new Date();
                //console.log(d.getTime(),busy,animating)

                mouseX = e.clientX - cthis.offset().left;

                if (busy)
                    return;


                if (pressMove == 'on' && pressed == true && animating == true) {

                    cthis.find('.page').eq(currNr + 1).css('z-index', lastIndex++);

                    cthis.find('.page_left_con').eq(currNr + 1).stop();
                    cthis.find('.page_left_con').eq(currNr + 1).css({
                        'left': mouseX,
                        'width': totalWidth - (totalWidth / 2 + mouseX / totalWidth * totalWidth / 2)
                    })


                    cthis.find('.page_right_con').eq(currNr + 1).stop();
                    cthis.find('.page_right_con').eq(currNr + 1).css({
                        'width': totalWidth - (totalWidth / 2 + mouseX / totalWidth * totalWidth / 2)
                    })


                    pressAnimating = true;
                }
                checkMouse();

            }

            function checkMouse() {

                if (mouseX > totalWidth - shadowWidth * 2) {
                    foldRight(currNr + 1);
                    return;
                }
                if (mouseX < shadowWidth * 2) {

                    foldLeft();
                    return;
                }

                if (pressAnimating != true)
                    unfoldIt();
            }

            function gotoNext() {
                if (currNr < nrChildren - 1)
                    gotoItem(currNr + 1)
            }

            function handleClick() {
                //console.log($('.pages').children().index($(this)));
                gotoItem(cthis.find('.pages').children().index(jQuery(this)));
            }

            //************************************************
            //****************************go to Item function!
            function gotoItem(arg) {

                if (arg == currNr)
                    return;

                foldRight(arg);

                busy = true;
                animating = true;


                if (cthis.find('.page').eq(arg).css('display') == 'none')
                    cthis.find('.page').eq(arg).css('display', 'inline')


                tempTime = animationTime;


                if (arg < currNr)
                    tempTime = animationTime - 100;


                cthis.find('.shadow_left').eq(arg).animate({
                    'opacity': 0
                }, animationTime)
                cthis.find('.shadow_right').eq(arg).animate({
                    'opacity': 0
                }, animationTime)




                if (coverPage == 'on' && alignCenter == 'on') {

                    if (arg != 0)
                        cthis.animate({ 'left': 0 }, { queue: false, duration: animationTime });
                    else
                        cthis.animate({ 'left': -totalWidth / 4 }, { queue: false, duration: animationTime });
                }




                if (!(coverPage == "on" && arg == 0)) {
                    cthis.find('.page_left_con').eq(arg).animate({
                        'width': totalWidth / 2 + 5,
                        'left': 0
                    }, { queue: false, duration: tempTime, complete: handleAnimationComplete })
                }
                else {
                    for (i = 2; i < nrChildren; i++)
                        cthis.find('.page').eq(i).css('display', 'none');

                    cthis.find('.page_left').eq(arg + 1).animate({
                        'left': -totalWidth / 2 + 10
                    }, { queue: false, duration: animationTime - 100, complete: handleAnimationComplete })

                    cthis.find('.page_left_con').eq(arg + 1).animate({
                        'width': 0,
                        'left': totalWidth / 2
                    }, { queue: false, duration: animationTime - 100 })

                    /*
		
		
                    cthis.find('.page_left_con').removeAttr('style')
                    cthis.find('.page_left_con').eq(arg+1).css({
                    'right' : 0
                    })
		
                    */
                }


                if (arg > currNr)
                    tempTime = animationTime - 100;


                cthis.find('.page_right_con').eq(arg).animate({
                    'width': totalWidth / 2,
                    'right': 0
                }, { queue: false, duration: tempTime })
                currNr = arg;
            }


            //****************************end Item function!
            //************************************************



            function handleAnimationComplete() {
                busy = false;
                animating = false;
                //handleMove();

                cthis.find('.shadow_left').eq(currNr).css('display', 'none')
                cthis.find('.shadow_right').eq(currNr).css('display', 'none')



                for (i = 0; i < nrChildren; i++) {
                    if (i == currNr - 1 || i == currNr || i == currNr + 1)
                        cthis.find('.page').eq(i).css('display', 'inline')
                    else
                        cthis.find('.page').eq(i).css('display', 'none')

                }
                checkMouse();
            }
            function handleUnfoldComplete() {
                cthis.find('.page').eq(currNr - 1).css('z-index', lastIndex - 100);
                cthis.find('.page').eq(currNr + 1).css('z-index', lastIndex - 101);
            }
            function handleMouseOut() {

            }
            function unfoldIt() {
                if (animating == false)
                    return;

                animating = false;

                cthis.find('.page_left_con').eq(currNr + 1).stop();
                cthis.find('.page_left_con').eq(currNr + 1).animate({
                    'left': totalWidth,
                    'width': 0
                }, { queue: false, duration: 200 })

                cthis.find('.page_right_con').eq(currNr + 1).animate({
                    'width': 0
                }, { queue: false, duration: 250, complete: handleUnfoldComplete })

                if ((coverPage == "on" && currNr == 1)) {

                    cthis.find('.page_left_con').eq(currNr).animate({
                        'left': 0
                    }, { queue: false, duration: 250 })

                    cthis.find('.page_left').eq(currNr).animate({
                        'left': 0
                    }, { queue: false, duration: 250 })


                }



                if (currNr > 0) {
                    cthis.find('.page_left_con').eq(currNr - 1).animate({
                        'width': 0
                    }, { queue: false, duration: 250, complete: handleUnfoldComplete })

                    cthis.find('.page_right_con').eq(currNr - 1).animate({
                        'width': 0,
                        'right': totalWidth
                    }, { queue: false, duration: 200 })

                }
            }
            function foldLeft() {


                if (currNr < 1)
                    return;


                if (animating)
                    return;


                if (cthis.find('.page').eq(currNr - 1).css('z-index') < lastIndex - 50)
                    cthis.find('.page').eq(currNr - 1).css('z-index', lastIndex++);


                cthis.find('.shadow_left').eq(currNr - 1).css('display', 'inline')
                cthis.find('.shadow_right').eq(currNr - 1).css('display', 'inline')


                cthis.find('.page').eq(currNr - 1).css('z-index', lastIndex++)


                animating = true;


                cthis.find('.shadow_left').eq(currNr - 1).css({
                    'opacity': 0.4
                })


                cthis.find('.shadow_right').eq(currNr - 1).css({
                    'opacity': 0.4
                })


                cthis.find('.page_left_con').eq(currNr - 1).css({
                    'left': 0,
                    'width': 0
                })
                cthis.find('.page_right_con').eq(currNr - 1).css({
                    'right': totalWidth,
                    'width': 0
                })




                if (!(coverPage == "on" && currNr == 1)) {

                    cthis.find('.page_left_con').eq(currNr - 1).animate({
                        'width': shadowWidth + 1
                    }, { queue: false, duration: 200 })

                    cthis.find('.page_right_con').eq(currNr - 1).animate({
                        'width': shadowWidth,
                        'right': totalWidth - shadowWidth * 2
                    }, { queue: false, duration: 250 })
                }
                else {

                    cthis.find('.page_left_con').eq(currNr).animate({
                        'left': shadowWidth
                    }, { queue: false, duration: 250 })


                    cthis.find('.page_right_con').eq(currNr - 1).animate({
                        'width': shadowWidth,
                        'right': totalWidth - shadowWidth * 2
                    }, { queue: false, duration: 250 })

                    //alert('ceva')

                    cthis.find('.page_left').eq(currNr).animate({
                        'left': -shadowWidth
                    }, { queue: false, duration: 250 })

                    for (i = 2; i < nrChildren; i++)
                        cthis.find('.page').eq(i).css('display', 'none')
                }



            }
            function foldRight(arg) {

                cacheObject = cthis.find('.page').eq(arg);
                if (cacheObject.css('z-index') < lastIndex - 50)
                    cacheObject.css('z-index', lastIndex++);

                if (animating)
                    return;

                animating = true;

                cthis.find('.shadow_left').eq(arg).css('display', 'inline')
                cthis.find('.shadow_right').eq(arg).css('display', 'inline')


                cthis.find('.page').eq(arg).css('z-index', lastIndex++)
                cthis.find('.page').eq(arg).css('display', 'inline')

                cthis.find('.shadow_left').eq(arg).css({
                    'opacity': 0.4
                })


                cthis.find('.shadow_right').eq(arg).css({
                    'opacity': 0.4
                })

                cthis.find('.page_left_con').eq(arg).css({
                    'left': totalWidth,
                    'width': 0
                })

                cthis.find('.page_right_con').eq(arg).css({
                    'right': 0,
                    'width': 0
                })


                if (coverPage == 'on') {
                    cthis.find('.page_left').eq(arg).css({
                        'left': 0
                    })
                }


                cthis.find('.page_left_con').eq(arg).animate({
                    'left': totalWidth - shadowWidth * 2,
                    'width': shadowWidth
                }, { queue: false, duration: 250 })

                cthis.find('.page_right_con').eq(arg).animate({
                    'width': shadowWidth + 1
                }, { queue: false, duration: 200 })

            }
            function isStolen() {
                var _allowedDomainList = [];

                _allowedDomainList.push("zoom");
                _allowedDomainList.push("flashden");
                _allowedDomainList.push("activeden");
                _allowedDomainList.push("envato");
                _allowedDomainList.push("localhost");
                _allowedDomainList.push("127.0.0.1");



                for (i = 0; i < _allowedDomainList.length; i++)
                    if ((window.location).toString().indexOf(_allowedDomainList[i]) > -1)
                        sw = true;

                if (sw == false)
                    cthis.css('display', 'none')
            }
            /*
		
            */

            jQuery.fn.flipbook.gotoItem = function (arg) {
                gotoItem(arg)
            }

            return this;
        }); // end each
    }

})(jQuery);
