﻿/************
Set global variables */
var portfolioTotal;
var portfolioArray = new Array();
var currSlide = 0;

$(document).ready(function() {
    /*************
    Set position on page load */
    if (window.location.hash == '') {
        window.location.hash = 'home';
    }


    /*************
    portfolio controls */
    portfolioTotal = $('#portfolioCont .portfolioItem').length;
    $('#portfolioCont > span').width(portfolioTotal * 696);
    var portfolioMargin = 0;
    for (i = 0; i < portfolioTotal; i++) {
        portfolioArray[i] = portfolioMargin;
        portfolioMargin -= 696;
        $('#portfolioCont .portfolioItem').eq(i).append('<p class="number">' + (i + 1) + '</p>');
    }

    /*************
    Begin page animations */
    //setInterval("flickerStar()", 2000);
    var carparkCars = new Array('1', '3', '5');
    for (i = 0; i < carparkCars.length; i++) { // random interval between cars floating
        setInterval('hoverCars(' + carparkCars[i] + ')', Math.floor(Math.random() * (2100 - 1300 + 1) + 1300));
    }
    for (i = 0; i < 12; i++) { // random interval between cars floating
        setInterval('flickerStar(' + i + ')', Math.floor(Math.random() * (1400 - 900 + 1) + 500));
    }
    hoverB(); setInterval('hoverB()', 1300);
});

function testFunc() {
    alert("hello world");
}

/*************
Page animations */
function flickerStar(star) {
    $('#moon img#star' + star).fadeTo(400, 0.3, function() { $('#moon img#star' + star).fadeTo(400, 1) });
}
function hoverCars(car) {
    $('#carparkContent img#carparkCar' + car).each(function() {
        $(this).animate({ top: (parseInt($(this).css('top'), 10) - 5) + 'px' }, 600, function() {
            $(this).animate({ top: (parseInt($(this).css('top'), 10) + 5) + 'px' }, 600);
        });
    });
}
function hoverB() {
    $('#globeCont #globeB').each(function() {
        $(this).animate({ bottom: (parseInt($(this).css('bottom'), 10) + 5) + 'px' }, 600, function() {
            $(this).animate({ bottom: (parseInt($(this).css('bottom'), 10) - 5) + 'px' }, 600);
        });
    });
}


/*************
portfolio controls */
function nextSlide() {
    currSlide++;
    if (currSlide >= portfolioTotal) {
        currSlide = 0;
    }
    $('#portfolioCont > span').animate({ marginLeft: portfolioArray[currSlide] }, 600);
}
function previousSlide() {
    currSlide--;
    if (currSlide < 0) {
        currSlide = portfolioTotal - 1;
    }
    $('#portfolioCont > span').animate({ marginLeft: portfolioArray[currSlide] }, 600);
}



/*************
page scroll */
function scrollPage(origin, destination) {
    var timer;
    switch (origin) {
        case "contact":
            switch (destination) {
                case "home":
                    timer = 3000;
                    break;
                case "portfolio":
                    timer = 5000;
            }
            break;
        case "home":
            switch (destination) {
                case "contact":
                    timer = 4000;
                    break;
                case "portfolio":
                    timer = 4000;
            }
            break;
        case "portfolio":
            switch (destination) {
                case "contact":
                    timer = 5000;
                    break;
                case "home":
                    timer = 3000;
            }
            break;
    }
    $('html,body').animate({
        scrollTop: $('#' + destination).offset().top
    }, timer, function() {
        window.location.hash = '#' + destination;
    });
}
