var video_container;
var run_cycle = true;

$(document).ready(function () {
    bind_top_menu();
    input_focus_replace();
    footer_plus_bind();
    init_homepage();

    $("#partners, #opdrachtgevers").cycle({
        random: 1
    });

    $("a[rel=external]").each(function () {
        $(this).attr("target","_blank");
    });

    $(".phone_input").keyup(function () {
        var value = $(this).val();
        $(this).val(value.replace(/[^\+,\d]/g, ''));
    });

    setInterval("next_slide()", 5000);
});

function next_slide() {
    if (run_cycle == true) {
        $(".controls a.active").each(function () {
           if ($(this).next().length > 0) {
               $(this).next().click();
           } else {
               $(this).parent().find("a:first").click();
           }
        });
    }
}

function init_homepage() {
    $(".slides .image:first-child").each(function () {
        var copy = $(this).clone();

        $(copy).appendTo($(this).parent());
    });
    $(".controls a").click(function () {
        var last = false;
        $("a.active", $(this).parent()).each(function () {
            var prevs = $(this).siblings();
            if ($(this).text() > prevs.length) {
                last = true;
            }
        });

        $("a", $(this).parent()).removeClass("active");
        $(this).addClass("active");

        if (last == true) {
            var sibs = $(this).siblings();
            var itemnr = sibs.length + 1;

            $(".slides", $(this).parent().parent().parent()).stop().animate({
                marginLeft: ((itemnr * 970) * -1) + "px"
            }, 800, function () {
                $(this).css("marginLeft", "0px");
            });
        } else {
            var prev = $(this).prevAll();
            var itemnr = prev.length;

            $(".slides", $(this).parent().parent().parent()).stop().animate({
                marginLeft: ((itemnr * 970) * -1) + "px"
            }, 800);
        }
    });
    $(".slider").hover(function () {
        run_cycle = false;
    }, function () {
        run_cycle = true;
    });
    $(".featured .thumb").click(function () {
        $(".featured .thumb").removeClass("active");
        $(this).addClass("active");

        var prev = $(this).prevAll();
        var itemnr = prev.length;


        $(".slide_container").stop().animate({
            marginTop: ((itemnr * 487) * -1) + "px"
        }, 500);
    });

    video_container = document.getElementById("player1");
    $(".play").hover(function () {
        if (video_container.paused == true) {
            $(this).stop().animate({
                opacity: 0.8
            });
        }
    }, function () {
        if (video_container.paused == true) {
            $(this).stop().animate({
                opacity: 1
            });
        }
    }).click(function () {
        if (video_container.paused == true) {
            video_container.play();
            $(this).stop().animate({
                opacity: 0
            }, 500, "linear", function () {
                $(this).css("display", "none");
            });
        }
    });
    $(video_container).bind("ended", function () {
        video_container.pause();
        $(".play").css({
            display: "block",
            opacity: 1
        });
    });
}

function footer_plus_bind() {
    $(".footer_top .plus").click(function () {
        $(".child", $(this).parent()).toggle();
    });
}

function input_focus_replace() {
	$(".call_me").submit(function () {
        var validate = true;

        $("input[type=text]", this).each(function () {
            if ($(this).attr("data-initval") == $(this).val()) {
                validate = false;
                $(this).css({
                    color: "red"
                }).focus(function () {
                    $(this).css({
                        color: "#817979"
                    });
                });
            }
        });

        return validate;
    });

    $(".focustext input[type=text]").each(function () {
        $(this).attr("data-initval", $(this).val());

        $(this).focus(function () {
            if ($(this).val() == $(this).attr("data-initval")) {
                $(this).val("");
            }
        }).blur(function () {
            if ($(this).val() == "") {
                $(this).val($(this).attr("data-initval"));
            }
        });
    });
}

function bind_top_menu() {
    $(".parent").hover(function () {
        if ($("ul", this).length > 0) {
            $(this).addClass("opened");

            $(".child", this).css({
                "display": "block"
            });

            var min_width = $(this).outerWidth();
            var new_width = 0;
            $("ul", this).each(function () {
                new_width += $(this).outerWidth();
            });

            if (min_width > new_width) {
                new_width = min_width;
            }

            $(".child", this).css({
                "width": (new_width + 1) + "px"
            });
        }
    }, function () {
        $(this).removeClass("opened");
        $(".child", this).css("display", "none");
    });
}

/********************/
/* Twitter functies */
/********************/

function goTwitter() {
    $.getJSON("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=triAV&count=4&callback=?", function(data) {
        $("#tweets-container").html("");
        for (var i = 0; i < 4; i++) {
            var obj = data[i];

            var text = hyperlinks(obj.text);
            var text = twitter_users(text);

            var tweet = $("<article></article>").addClass("tweet").html("<p>" + text + "</p>").appendTo("#tweets-container");
            if (i == 3) {
                $(tweet).addClass("last");
            }
        }
    });
}

function hyperlinks(text) {
    text = text.replace(/\s(http:\/\/[a-z][a-zA-Z0-9\/\*\-\?\.\&\%\$]*)/ig," <a href=\"$1\" target=\"_blank\" class=\"twitter-link\">$1</a>");
    text = text.replace(/\s([a-zA-Z]+:\/\/[a-z][a-z0-9\_\.\-]*[a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%]*)([\s|\.|\,])/ig," <a href=\"$1\" target=\"_blank\" class=\"twitter-link\">$1</a>$2");
    // match www.something.domain/path/file.extension?some=variable&another=asf%
    text = text.replace(/\s(www\.[a-z][a-z0-9\_\.\-]*[a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%]*)([\s|\.|\,])/ig," <a href=\"http://$1\" target=\"_blank\" class=\"twitter-link\">$1</a>$2");

    return text;
}

function twitter_users(text) {
    text = text.replace(/([\.|\,|\:|\¡|\¿|\>|\{|\(]?)@{1}(\w*)([\.|\,|\:|\!|\?|\>|\}|\)]?)\s/ig, "$1<a href=\"http://twitter.com/$2\" target=\"_blank\" class=\"twitter-user\">@$2</a>$3 ");
    return text;
}
