Site = {};

function hide(el) {
//    alert('hide ' + el.id);
    $(el)._hidden = true;
    $(el).stop(true, true);
//    $(el).hide('fade', {}, 500);
//    $(el).stop();
    $(el).animate({opacity: 0.3}, 1500).css('z-index', 0);
}

function show(el, zIndex) {
    zIndex = typeof zIndex == 'undefined' ? zIndex : 1000;
//    alert('show ' + el.id);
    if (false == $(el)._hidden) {

    } else {
        $(el)._hidden = false;
        $(el).fadeIn(500).animate({opacity: 1}, 1500, null, function() {
            elementAnimation(null, $(el));
        }).css('z-index', zIndex);
//        $(el).show('fade', {}, 500);
    }
//    $(el).dequeue();
//    $(el).fadeIn(500);
}

function getAnimationDetails(step, projectNo)
{
    var details = {};

    if (projectNo == 2) {
        var fullRounds = Math.floor(step / 5);
        step -= fullRounds * 5;
//        alert('Project ' + projectNo + ': ' + step);
        switch (step) {
            case 0:
                details = {
                    duration : 9250
                }; //sleep
                break;

            case 1:
                details = {
                    animate : {
                        zIndex: '1500',
                        left: '+=300'
                    },
                    duration : 7750
                };
                break;

            case 2:
                details = {
                    duration : 3000
                }; //sleep
                break;

            case 3:
                details = {
                    animate : {
                        left: '-=300',
                        top: '50'
                    },
                    duration : 10000
                };
                break;

            case 4:
                details = {
                    duration : 5000
                }; //sleep
                break;
        }
    } else if (projectNo == 3) {
        var fullRounds = Math.floor(step / 6);
        step -= fullRounds * 6;
//        alert('Project ' + projectNo + ': ' + step);
        switch (step) {
            case 0:
                details = {
                    animate : {
                        top: '50'
                    },
                    duration : 7750
                };
                break;

            case 1:
                details = {
                    duration : 1500
                }; //sleep
                break;

            case 2:
                details = {
                    animate : {
                        left: '350',
                        top: '50'
                    },
                    duration : 7750
                };
                break;

            case 3:
                details = {
                    duration : 10000
                }; //sleep
                break;

            case 4:
                details = {
                    animate : {
                        left: '700',
                        top: '150'
                    },
                    duration : 10000
                };
                break;

            case 5:
                details = {
                    duration : 12000
                }; //sleep
                break;

        }
/*animate({
//        left: '700',
        top: '50'
    }, 7750).delay(1500).animate({
        left: '350',
        top: '50',
        zIndex: '1500'
    }, 7750).delay(10000).animate({
        left: '700',
        top: '150'
    }, 10000).delay(12000).animate({
//        left: '+=9000'
    }, 13000);*/
    }

    return details;
}

$(document).ready(function() {
//    $('#move-header').hide();
//    $('#move-subheader').css('visibility', 'hidden');
//    $('#move-paragraph').hide();

//    $('#move-project3').hide();
//    $('#move-project2').hide();
//    $('#move-project1').hide();

    projectsAnimation();
    loadProject();
    showProject();
    preloadImages();

    $('.tTip').betterTooltip({speed: 150, delay: 300});

    $(".containerPlus").buildContainers({
        containment:"document",
        elementsPath:"/public/elements/",
        onClose:function(o){},
        effectDuration:200
      });

//    $('#move-subheader').slideDown();
//    $('#move-paragraph').slideDown();
});

function elementAnimation(projectNo, el)
{
//    alert(projectNo);
    el = typeof el == 'undefined' ? $('#move-project' + projectNo) : el
    if ((typeof el._hidden == 'undefined' || el._hidden == false)) {
        if ((typeof el.queue('fx') == 'undefined' || el.queue('fx').length == 0)) {
            el._step = typeof el._step == 'undefined' ? 0 : el._step;
            var animationDetails = getAnimationDetails(el._step, projectNo);
            if (animationDetails.animate) {
                el.animate(animationDetails.animate, animationDetails.duration);
            } else if (animationDetails.duration) {
                el.delay(animationDetails.duration);
            }
            el._step++;
//            alert(el._step + ' - ' + animationDetails.duration);
            if (animationDetails.duration) {
                setTimeout(function() {
//                    alert(projectNo + ' - ' + (animationDetails.duration + 100));
                    elementAnimation(projectNo, el);
                }, animationDetails.duration + 100);
            }
        }
    }
}

function project1move() {
    var el = $('#move-project1');
    if ((typeof el._hidden == 'undefined' || el._hidden == false) && el.queue('fx').length == 0) {
        el._step = typeof el._step == 'undefined' ? 0 : el._step;
        var animationDetails = getAnimationDetails(el._step, 1);
        if (animationDetails.animate) {
            el.animate(animationDetails.animate, animationDetails.duration);
        } else if (animationDetails.duration) {
            el.delay(animationDetails.duration);
        }

        setTimeout(arguments.callee, 100);
    }

//    var effs = ['bounce', 'pulsate', 'shake'];
//    var effectNo = Math.floor(Math.random() * 3);
//    var duration = 0;
//    if (typeof effs[effectNo] == 'string') {
//        duration = Math.random() * 2000;
//        $('#move-project1').effect(effs[effectNo], getAnimationOptions(effs[effectNo]), duration);
//    }
//    setTimeout(arguments.callee, duration + 10000);
//    return;
    el = $('#move-project1').delay(2250).animate({
//        zIndex: '1000'
//        left: '400',
//        top: '50'
    }, 750);
}

function project2move() {
//    var effs = ['bounce', 'pulsate', 'shake'];
//    var effectNo = Math.floor(Math.random() * 3);
//    var duration = 0;
//    if (typeof effs[effectNo] == 'string') {
//        duration = Math.random() * 2000;
//        $('#move-project2').effect(effs[effectNo], getAnimationOptions(effs[effectNo]), duration);
//    }
//    setTimeout(arguments.callee, duration + 10000);
//    return;
    el = $('#move-project2').delay(9250).animate({
        zIndex: '1500',
        left: '+=300'
//        top: '50'
    }, 7750).delay(3000).animate({
        left: '-=300',
        top: '50'
    }, 10000).delay(5000).animate({
//        top: '-=9000'
    }, 13000);
}

function project3move() {
//    var effs = ['bounce', 'pulsate', 'shake'];
//    var effectNo = Math.floor(Math.random() * 3);
//    var duration = 0;
//    if (typeof effs[effectNo] == 'string') {
//        duration = Math.random() * 2000;
//        $('#move-project3').effect(effs[effectNo], getAnimationOptions(effs[effectNo]), duration);
//    }
//    setTimeout(arguments.callee, duration + 10000);
//    return;
//    alert('123');
    el = $('#move-project3').animate({
//        left: '700',
        top: '50'
    }, 7750).delay(1500).animate({
        left: '350',
        top: '50',
        zIndex: '1500'
    }, 7750).delay(10000).animate({
        left: '700',
        top: '150'
    }, 10000).delay(12000).animate({
//        left: '+=9000'
    }, 13000);
}

function projectsAnimation() {
    return;
    setInterval(function() {
        var el = $('#move-project3');
        if (el.queue('fx').length == 0 && true != el._hidden) {
            var dur1 = 2500 * Math.random() + 600;
            var dur2 = 2500 * Math.random() + 600;
            var dur3 = 2500 * Math.random() + 600;
            var op1 = 0.8 * Math.random() + 0.2;
            var op2 = 0.8 * Math.random() + 0.2;
//            var l = Math.random() * 300 - 150;
//            l = l >= 0 ? '+=' + l : '-=' + (-l);
//            var t = Math.random() * 300 - 150;
//            t = t >= 0 ? '+=' + t : '-=' + (-t);
            el.fadeTo(dur1, op1).delay(dur2).fadeTo(dur3, op2);
//            el.animate({
//                left: l,
//                top: t
//            }, dur1 + dur2 + dur3);
        }
    }, 50);

    setInterval(function() {
        var el = $('#move-project2');
        if (el.queue('fx').length == 0 && true != el._hidden) {
            var dur1 = 2500 * Math.random() + 600;
            var dur2 = 2500 * Math.random() + 600;
            var dur3 = 2500 * Math.random() + 600;
            var op1 = 0.8 * Math.random() + 0.2;
            var op2 = 0.8 * Math.random() + 0.2;
//            var l = Math.random() * 300 - 150;
//            l = l >= 0 ? '+=' + l : '-=' + (-l);
//            var t = Math.random() * 300 - 150;
//            t = t >= 0 ? '+=' + t : '-=' + (-t);
            el.fadeTo(dur1, op1).delay(dur2).fadeTo(dur3, op2);
//            el.animate({
//                left: l,
//                top: t
//            }, dur1 + dur2 + dur3);
        }
    }, 50);

    setInterval(function() {
        var el = $('#move-project1');
        if (el.queue('fx').length == 0 && true != el._hidden) {
            var dur1 = 2500 * Math.random() + 600;
            var dur2 = 2500 * Math.random() + 600;
            var dur3 = 2500 * Math.random() + 600;
            var op1 = 0.8 * Math.random() + 0.2;
            var op2 = 0.8 * Math.random() + 0.2;
//            var l = Math.random() * 300 - 150;
//            l = l >= 0 ? '+=' + l : '-=' + (-l);
//            var t = Math.random() * 300 - 150;
//            t = t >= 0 ? '+=' + t : '-=' + (-t);
            el.fadeTo(dur1, op1).delay(dur2).fadeTo(dur3, op2);
//            el.animate({
//                left: l,
//                top: t
//            }, dur1 + dur2 + dur3);
        }
    }, 50);
}

$.fn.betterTooltip = function(options){

    /* Setup the options for the tooltip that can be
       accessed from outside the plugin              */
    var defaults = {
        speed: 200,
        delay: 300
    };

    var options = $.extend(defaults, options);

    /* Create a function that builds the tooltip
       markup. Then, prepend the tooltip to the body */
    getTip = function() {
        var tTip =
            "<div class='tip'>" +
                "<div class='tipMid'>"  +
                "</div>" +
                "<div class='tipBtm'></div>" +
            "</div>";
        return tTip;
    }
    $("body").prepend(getTip());

    /* Give each item with the class associated with
       the plugin the ability to call the tooltip    */
    $(this).each(function(){

        var $this = $(this);
        var tip = $('.tip');
        var tipInner = $('.tip .tipMid');

        var tTitle = '<img src="' + this.title + '" alt="" />';
        this.title = "";

        var offset = $(this).offset();
        var tLeft = offset.left;
        var tTop = offset.top;
        var tWidth = $this.width();
        var tHeight = $this.height();

        /* Mouse over and out functions*/
        $this.hover(
            function() {
                tipInner.html(tTitle);
                setTip(tTop, tLeft);
                setTimer();
            },
            function() {
                stopTimer();
                tip.hide();
            }
        );

        /* Delay the fade-in animation of the tooltip */
        setTimer = function() {
            $this.showTipTimer = setInterval("showTip()", defaults.delay);
        }

        stopTimer = function() {
            clearInterval($this.showTipTimer);
        }

        /* Position the tooltip relative to the class
           associated with the tooltip                */
        setTip = function(top, left){
            var topOffset = tip.height();
            var xTip = (left-30)+"px";
            var yTip = (top-topOffset-60)+"px";
            tip.css({'top' : yTip, 'left' : xTip});
        }

        /* This function stops the timer and creates the
           fade-in animation                          */
        showTip = function(){
            stopTimer();
            tip.animate({"top": "+=20px", "opacity": "toggle"}, defaults.speed);
        }
    });
};


var projectIndex = 0;
function loadProject() {
    if (typeof projects != 'undefined') {
        var projectsCount = projects.length;
        if (projectIndex >= projectsCount) {
            projectIndex = 0;
        } else if (projectIndex < 0) {
            projectIndex = projectsCount - 1;
        }

        if (typeof projects[projectIndex] != 'undefined') {
            $('#move-header').text(projects[projectIndex].metaTitle);
            $('#move-subheader').text(projects[projectIndex].title);
            $('#move-paragraph').html(projects[projectIndex].description);

            $('#move-project1').attr('src', '/public/images/projects/' + projects[projectIndex].image1);
            $('#move-project2').attr('src', '/public/images/projects/' + projects[projectIndex].image2);
            $('#move-project3').attr('src', '/public/images/projects/' + projects[projectIndex].image3);
        }
    }
}

var projectChange = 0;
function nextProject() {
    var projectChangeNumber = ++projectChange;
    projectIndex++;
    if (projectChangeNumber == projectChange) {
        hideProject(1000);
        setTimeout(function() {
            if (projectChangeNumber == projectChange) {
                loadProject();
                if (projectChangeNumber == projectChange) {
                    showProject();
                }
            }
        }, 1000);
    }
}

function lastProject() {
    var projectChangeNumber = ++projectChange;
    projectIndex--;
    if (projectChangeNumber == projectChange) {
        hideProject(1000);
        setTimeout(function() {
            if (projectChangeNumber == projectChange) {
                loadProject();
                if (projectChangeNumber == projectChange) {
                    showProject();
                }
            }
        }, 1000);
    }
}

function hideProject(duration) {
    $('#move-project1').stop(true, true).fadeOut(duration);
    $('#move-project2').stop(true, true).fadeOut(duration);
    $('#move-project3').stop(true, true).fadeOut(duration);

    $('#move-header').stop(true).animate({
        left: '-1000'
    }, duration);

    $('#move-subheader').stop(true).animate({
        top: '-1000'
    }, duration);

    $('#move-paragraph').stop(true).animate({
        left: '-1000'
    }, duration);
}

function showProject() {
    $('#move-header').css('position' , 'relative');
    $('#move-header').css('left' , '-1000px');
    $('#move-subheader').css('position' , 'relative');
    $('#move-subheader').css('top' , '-1000px');
    $('#move-paragraph').css('position' , 'relative');
    $('#move-paragraph').css('left' , '-1000px');

    $('#move-project1').hide().fadeIn(1000);
    $('#move-project2').hide().fadeIn(1000);
    $('#move-project3').hide().fadeIn(1000);

    $('#move-header').stop(true).animate({
        left: '0'
    }, 1000, null, function() {
        $('#move-subheader').stop(true).animate({
            top: '100'
        }, 1000).animate({
            top: '-100'
        }, 200).animate({
            top: '0'
        }, 150, null, function() {
            $('#move-paragraph').stop(true).animate({
                left: '0'
            }, 1000);
        });
    });

}

function preloadImages() {
    if (typeof projects != 'undefined') {
        var img;
        for (var i in projects) {
            if (typeof projects[i] != 'function') {
                for (var k = 1; k <= 3; k++) {
                    img = document.createElement('img');
                    img.src = '/public/images/projects/' + projects[i]['image' + k];
                    img.style.display = 'none';
                    document.body.appendChild(img);
                }
            }
        }
    }
}
