var intervalID = -1;
var currentHash;
var picCount = 0;
var isDragged;

$(function(){
/*
    currentHash = window.location.hash;
    intervalID = setInterval('checkHash()', 250);
    if(window.location.hash != '')
    {
        currentHash = '';
        if(window.location.hash.substr(0, 2) == '#s') {
            goto(getNr(window.location.hash), true)
        }
        // checkHash();
    }
*/

    // De ogen:
    $(window).mousemove(function(e){
        var pos1 = $("div.slide2 div.inner").position();
        var pos2 = $("#meesterwerk").position();
        // console.log(e.pageX - pos1.left, e.pageY - pos2.top);
        // 295 / 322
        // 860 = 242 = 120
        // 456
        // 15 - 528
        var oog1X = 124 + ((e.pageX - pos1.left) / 6);
        var oog1Y = 296 + ((e.pageY - pos2.top) / 15);
        var oog2X = 337 + ((e.pageX - pos1.left) / 8);
        var oog2Y = 309 + ((e.pageY - pos2.top) / 15);

        if(oog1X < 124) { oog1X = 124; }
        if(oog1X > 242) { oog1X = 242; }
        if(oog1Y < 296) { oog1Y = 296; }
        if(oog1Y > 338) { oog1Y = 338; }
        if(oog2X < 337) { oog2X = 337; }
        if(oog2X > 456) { oog2X = 456; }
        if(oog1Y < 309) { oog1Y = 309; }
        if(oog1Y > 351) { oog1Y = 351; }


        $("img.oog1").css({left: oog1X, top: oog1Y});
        $("img.oog2").css({left: oog2X, top: oog2Y});
    });

    // Portfolio:
    $("div.item").each(function(){
        $(this).addClass("kader" + Math.floor(Math.random() * 6));
        $("a", this).click(function(){
            window.open($(this).attr("href"));
            return false;
        });
        $(this).hover(function(){
            $("div.info", this).show();
        }, function(){
            $("div.info", this).hide();
        });
    });

    // Interessant:
    $("#twitter a, #flickr a").click(function(){
        window.open($(this).attr("href"));
        return false;
    });

    // Verberg tweets adhv de beschikbare ruimte:
    $(window).resize(function(){
        var h = $("#twitter").height();
        // benodigde ruimte = h - 100:
        $("li.tweet").hide();
        $("li.tweet").each(function(){
            if($("#twitter ul").height() < h - 100)
            {
                $(this).show();
            }
        });
        // Scroll naar de juiste spot:
        if(window.location.hash.substr(0, 2) == '#s') {
            goto(getNr(window.location.hash), true)
        }
    }).resize();



    $("#flickr img").each(function(){
        if($(this).height() == 180) {
            $(this).css({marginTop: 30});
        }
        $(this).parent().parent().hide();
        var rot = Math.floor(-10 + (Math.random() * 21));
        $(this).css("-webkit-transform", "rotate(" + rot + "deg)");
        $(this).css("-moz-transform", "rotate(" + rot + "deg)");
        $(this).css("-o-transform", "rotate(" + rot + "deg)");
        $(this).css("transform", "rotate(" + rot + "deg)");
    });
    picCount = 0;
    updatePic();

    $("form").validate();

    // Vind ik leuk:
    $("div.slide5 a.image").each(function(){
/*
        var yPos = ($(window).height() - 300) * Math.random();
        if(yPos > 300) {
            var xPos = 660 * Math.random();
        } else {
            var xPos = 400 + (260 * Math.random());
        }
        $(this).css({top: yPos, left: xPos});
*/
        $(this).click(function(){
            return false;
        });
        $(this).draggable({
            start: function(){
                isDragged = true;
            }
        }).mousedown(function(){
            isDragged = false;
        }).mouseup(function(){
            if(!isDragged){
                window.open($(this).attr("href"));
            }
        });
    });
});

function goto(slideNr, fast)
{
    // var w = $(window).width();
    // window.scrollX = w * (slideNr - 1);

    if(fast == undefined || fast == false)
    {
        $(window).scrollTo($('div.slide' + slideNr), 1000);
        window.location.hash = 's' + slideNr;
        currentHash = window.location.hash;
    } else {
        $(window).scrollTo($('div.slide' + slideNr), 0);
    }

    // return false;
}

function checkHash()
{

    if(currentHash != window.location.hash)
    {
        var oldHash = currentHash;
        currentHash = window.location.hash;
        if(currentHash == '')
        {
/*
            if(oldHash.substr(0, 2) == '#s') {
                goto(getNr(oldHash), true)
            }
*/
            goto(1);
        } else {
/*
            if(oldHash.substr(0, 2) == '#s') {
                goto(getNr(oldHash), true)
            }
*/
            if(currentHash.substr(0, 2) == '#s') {
                goto(getNr(currentHash), false);
            }
        }
    }
    
}

function getNr(hash)
{
    return parseInt(hash.replace('#s', ''));
}

function updatePic()
{
    $("#flickr div.picture").hide();
    var nr = Math.floor(Math.random() * $("#flickr div.picture").length);
    $("#flickr div.picture:eq(" + nr + ")").show();
    picCount++;
    if(picCount < 10) {
        setTimeout('updatePic()', 50);
    } else {
        picCount = 0;
        setTimeout('updatePic()', 3000);
    }
}
