/*
    Twisted JS script
    Author: Twisted
*/

var slideWidth = 0;
var headerStick = false;
var beating = false;
var all_projects_height = 0;
var menu_opened = false;

var items_load_height = 0;
var loading_items = false;
var current_page = 1;
var categories = "all";
var master_frame = "";

var home_screen_index = 100;

$(document).ready(function () {
    //Footer nieuwsbrief
    $("#nieuwsbrief-email").focus(function() {
       if ($(this).val() == "Jouw e-mail...") {
           $(this).val("");
       }
    });
    $("#nieuwsbrief-email").blur(function() {
       if ($(this).val() == "") {
           $(this).val("Jouw e-mail...");
       }
    });

    bind_footer_anims();
    bind_links();
});

function bind_links() {
    //animate anchors
    $("a[href^='#']").anchorAnimate();

    //external links
    $('a[rel="external"]').attr('target','_blank');
}

function bind_footer_anims() {
    if (mobileDevice()) {
        $(".part-footer .back, .part-footer ul").css({
                bottom: "5px"
        });
    } else {
        //footer nav anim
        $(".part").hover(function () {
            $(".part-footer .back, .part-footer ul", this).stop().animate({
                bottom: "5px"
            }, 200);
        }, function () {
            $(".part-footer .back, .part-footer ul", this).stop().animate({
                bottom: "-25px"
            }, 400);
        });
    }
}

function sandbox_init() {
    Shadowbox.init({
        skipSetup: true,
        overlayOpacity: 0.75,
        handleOversize: "resize",
        onOpen: function() {
                if((navigator.userAgent.match(/iPhone/i))||(navigator.userAgent.match(/iPad/i))) {
                        $("#sb-container").css("top", $(window).scrollTop());
                        $(window).bind('scroll', function() {
                                $("#sb-container").css("top", $(window).scrollTop());
                        });
                }
        },
        onClose: function() {
                if((navigator.userAgent.match(/iPhone/i))||(navigator.userAgent.match(/iPad/i))) {
                        $(window).unbind('scroll');
                }
        } 
    });
}

/**********************/
/* Homepage functions */
/**********************/

function open_reel(vimeo_id) {
    Shadowbox.open({
        content:    '<iframe src="http://player.vimeo.com/video/' + vimeo_id + '?byline=0&amp;portrait=0&amp;color=ffffff&amp;autoplay=1" width="970" height="546" frameborder="0" webkitAllowFullScreen allowFullScreen></iframe>',
        player:     "html",
        width:      970,
        height:     546
    });
}

function open_officemovie(vimeo_id) {
    Shadowbox.open({
        content:    '<iframe src="http://player.vimeo.com/video/' + vimeo_id + '?byline=0&amp;portrait=0&amp;color=ffffff&amp;autoplay=1" width="970" height="303" frameborder="0" webkitAllowFullScreen allowFullScreen></iframe>',
        player:     "html",
        width:      970,
        height:     303
    });
}


var reel_index = 20;
function home() {
    $(".logos li:not(.active) img").css("opacity", 0.5);
    $(".logos li:not(.active) img").hover(function () {
        $(this).stop().animate({
            "opacity": 1
        }, 100);
    }, function () {
        $(this).stop().animate({
            "opacity": 0.5
        }, 100);
    });
    $(".logos li a").click(function (e) {
        e.preventDefault();

        $(".logos li a").unbind("click");

        var obj = $(this);

        var offset = $(e.currentTarget).offset();
        var offset_dash = $(".active .dash").offset();

        $(".active .dash").stop().animate({
            marginLeft: (offset.left - offset_dash.left) + "px",
            width: $(e.currentTarget).innerWidth() + "px"
        }, 200, "linear", function () {

        });
        var iframe_overlay = $("<iframe></iframe>").attr({
            id: "overlay_frame",
            src: $(obj).attr("href"),
            scrolling: "no",
            frameborder: "0"
        }).css({
            opacity: 0,
            height: $("#container").innerHeight()
        }).appendTo($("body")).load(function () {
            home_screen_index = (home_screen_index - 1);
            var contents = $(this).contents();
            var new_container = $(contents).find("#container").clone();
            $(new_container).css({
                zIndex: home_screen_index
            }).appendTo($(this).parent());

            $(this).animate({
                opacity: 1
            }, 800, "linear", function () {
                $("body #container:eq(0), #overlay_frame").remove();
                home();
            });
        });
    });

    $(".reel .left").hover(function () {
        if ($.browser.webkit) { reel_index++; }
        $("#reel_left").css({
            zIndex: reel_index
        }).stop().animate({
            width: "424px"
        }, 300).find("img").stop().animate({
            width: "478px"
        }, 500);
        $("#reel_right").stop().animate({
            width: "0px"
        }, 200).find("img").stop().animate({
            width: "0px"
        }, 200);
    }, function () {
        $("#reel_left").stop().animate({
            width: "243px"
        }, 500).find("img").stop().animate({
            width: "243px"
        }, 300);
        $("#reel_right").stop().animate({
            width: "223px"
        }, 500).find("img").stop().animate({
            width: "223px"
        }, 500);
    });

    $(".reel .right").hover(function () {
        if ($.browser.webkit) { reel_index++; }
        $("#reel_right").css({
            zIndex: reel_index
        }).stop().animate({
            width: "424px"
        }, 300).find("img").stop().animate({
            width: "478px"
        }, 500);
        $("#reel_left").stop().animate({
            width: "0px"
        }, 200).find("img").stop().animate({
            width: "0px"
        }, 200);
    }, function () {
        $("#reel_right").stop().animate({
            width: "223px"
        }, 500).find("img").stop().animate({
            width: "223px"
        }, 300);
        $("#reel_left").stop().animate({
            width: "243px"
        }, 500).find("img").stop().animate({
            width: "243px"
        }, 500);
    });
}

/*************************/
/* Ons verhaal functions */
/*************************/

function ons_verhaal() {
    /* ons verhaal effecten */
    $(".mensen .movie a").hover(function() {
        $(".fill", $(this).parent()).stop().animate({
            width: "310px",
            height: "174px",
            top: "1px",
            left: "26px"
        }, 80);
    }, function () {
        $(".fill", $(this).parent()).stop().animate({
            width: "298px",
            height: "162px",
            top: "7px",
            left: "32px"
        }, 100);
    });

    $(".office-movie").hover(function () {
        $(".left", this).stop().animate({
            left: "0px",
            width: "107px",
            height: "106px",
            top: "0px"
        }, 100);

        $(".right", this).stop().animate({
            right: "0px",
            width: "107px",
            height: "106px",
            top: "0px"
        }, 100);
    }, function () {
        $(".left", this).stop().animate({
            width: "83px",
            height: "82px",
            left: "12px",
            top: "12px"
        }, 100);

        $(".right", this).stop().animate({
            width: "83px",
            height: "82px",
            right: "12px",
            top: "12px"
        }, 100);
    });

    $(".beautiful").hover(function () {
        $(".over", this).stop().animate({
            top: "12px"
        }, 150);
    }, function () {
        $(".over", this).stop().animate({
            top: "36px"
        }, 150, function () {
            $(this).css("top", "-12px");
        });
    });
}

/**********************/
/* Diensten functions */
/**********************/

var movie = "right";

function replace_movie() {
    var vimeo_id = 0;
    if (movie == "left") {
        vimeo_id = 26261103;
    } else {
        vimeo_id = 26261254;
    }

    $(".overflowing ."+movie).html('<iframe src="http://player.vimeo.com/video/' + vimeo_id + '?api=1&amp;byline=0&amp;portrait=0&amp;color=ffffff&amp;autoplay=1&amp;player_id=vimeoplayer' + movie + '" width="628" height="348" frameborder="0" webkitAllowFullScreen allowFullScreen></iframe>');
    Froogaloop.init();
}

function slide_reel(left) {
    $(".motionreel .overflowing .content").stop().animate({
        marginLeft: left + "px"
    }, 400);
}

function selector_slideto(obj, animate) {
    var time = 0;
    if (animate == true) {
        time = 400;
    }

    var offset = $(obj).position();
    $(".motionreel p .selector").stop().animate({
        width: $(obj).innerWidth() + "px",
        left: offset.left + "px"
    }, time);

    $(".motionreel iframe").each(function () {
        this.api('pause');
    });
}

function diensten() {
    $(".monitor").cycle({
        random: 1,
        timeout: 5000
    });

    $(".motionreel .play").css("opacity", 0.8);
    $(".hit_area").hover(function () {
        $(".play", this).stop().animate({
            opacity: 1
        }, 95);

        $(".fill_bg", this).stop().animate({
            top: "0px",
            left: "0px",
            width: "638px",
            height: "358px"
        }, 95);
    }, function () {
        $(".play", this).stop().animate({
            opacity: 0.8
        }, 95);

        $(".fill_bg", this).stop().animate({
            top: "6px",
            left: "6px",
            width: "626px",
            height: "346px"
        }, 95);
    });

    $(".motionreel .content .play").click(function () {
        replace_movie();
    });
    
    selector_slideto($(".motionreel p .right"), false);

    //reels
    $(".motionreel p .right").click(function () {
        slide_reel(0);
        selector_slideto($(".motionreel p .right"), true);
        movie = "right";
    });
    $(".motionreel p .left").click(function () {
        slide_reel(628);
        selector_slideto($(".motionreel p .left"), true);
        movie = "left";
    });
    $("<div class='stripe'></div>").appendTo($(".motionreel p .selector"));

    //heart beat
    $(".facebook a").hover(function () {
        beating = true;
        beat_facebook_heart();
    }, function () {
        beating = false;
        unbeat_facebook_heart();
    });

    //portfolio
    $(".interactive .portfolio .item p a").hover(function () {
        $("img", $(this).parent().parent()).css({
            borderWidth: "0px",
            marginLeft: "0px",
            marginTop: "0px"
        }).stop().animate({
            borderWidth: "6px",
            marginLeft: "-6px",
            marginTop: "-6px"
        }, 80);
    }, function () {
        $("img", $(this).parent().parent()).stop().css({
            borderWidth: "0px",
            marginLeft: "0px",
            marginTop: "0px"
        });
    });
}

function beat_facebook_heart() {
    if (beating == true) {
        $(".facebook .beat").css({
            opacity: 0,
            display: "block"
        }).animate({
            opacity: 1
        }, 100, "linear", function () {
            $(this).animate({
                opacity: 0
            }, 100, "linear", function() {
                $(this).animate({
                    opacity: 1
                }, 100, "linear", function() {
                    $(this).animate({
                        opacity: 0
                    }, 800, "linear", function() {
                        beat_facebook_heart();
                    });
                });
            });
        });
    }
}

function unbeat_facebook_heart() {
    $(".facebook .beat").stop().animate({
        opacity: 0
    }, 400);
}

/***********************/
/* Portfolio functions */
/***********************/

function portfolio() {
    //Projects menu
    projects_menu();

    $("#pages").css("display", "none");

    var filter_cats = "";
    $("#top-menu .active span").each(function () {
        filter_cats += $(this).attr("class") + ",";
    });
    filter_cats = filter_cats.substr(0,(filter_cats.length - 1));

    if (filter_cats.length > 0) {
        categories = filter_cats;
    }

    //activating category
    $("#top-menu span").disableSelection().click(function () {
        var parent = $(this).parent();
        loading_items = false;
        if ($(parent).hasClass("active")) {
            deactivate_category(this);
        } else {
            activate_category(this);
        }
        filter_categories();
    });

    //check if loader is visible
    var items = $(".item");
    if (items.length < 5) {
        $("#loading-items").css("display", "none");
        loading_items = true;
    }
    
    $(document).scroll(function () {
        var offset = $("#loading-items").offset();
        items_load_height = offset.top;

        var offsetScroll = (items_load_height - $(window).height()) - $(document).scrollTop();
        if (offsetScroll < 200) {
            load_new_items();
        }
    });

    //init page
    fix_read_more();
    bind_links();
    lazy_load_images();

    if (!mobileDevice()) {
        //Cycle
        if ($.browser.msie && $.browser.version < 8) {

        } else {
            init_cycle();
        }

        //Header
        bind_header();
    } else {
        mobileSliderInit();
    }
}

function lazy_load_images() {
    if (!mobileDevice()) {
        $("img[data-src]").load(function () {
            $(this).css(
                "background-image", "none"
            );
        }).each(function () {
            $(this).attr("src", $(this).attr("data-src")).removeAttr("data-src");
        });
    } else {
        $("img[data-src]").load(function () {
            $(this).css(
                "background-image", "none"
            );
        });
    }
}

function fix_read_more() {
    $("a.readmore").removeAttr("href").click(function () {
        $(this).next().stop().css({
            display: "block",
            opacity: 0
        }).animate({
            opacity: 1
        });
        $(this).remove();
    });
}

function load_new_items() {
    if (loading_items == false) {
        loading_items = true;

        var temp_categories = "all";

        var filter_cats = "";
        $("#top-menu .active span").each(function () {
            filter_cats += $(this).attr("class") + ",";
        });
        filter_cats = filter_cats.substr(0,(filter_cats.length - 1));

        if (filter_cats.length > 0) {
            temp_categories = filter_cats;
        }

        if (categories != temp_categories) {
            current_page = 1;

            categories = temp_categories;
            $("#loading-items").css("display", "block");
            $("#portfolio-items").html("");
        } else {
            current_page++;
        }

        var url_part = categories+"/"+current_page;

        var result = $("<div></div>").load("/ons-werk/"+url_part+" #portfolio-items", function () {
            var items = $(".part", result);
            $(items).appendTo("#portfolio-items");

            if (items.length < 5) {
                $("#loading-items").css("display", "none");
            } else {
                loading_items = false;
            }

            fix_read_more();
            bind_links();
            lazy_load_images();

            if (!mobileDevice()) {
                init_cycle();
            } else {
                mobileSliderInit();
            }
        });
    }
}

function activate_category(obj) {
    $(obj).parent().addClass("active");
    loading_items = false;
    load_new_items();
}

function deactivate_category(obj) {
    $(obj).parent().removeClass("active");
    loading_items = false;
    load_new_items();
}

function filter_categories() {
    var filter_cats = new Array();
    $("#top-menu .active span").each(function () {
        filter_cats.push("." + $(this).attr("class"));
    });

    if (filter_cats.length > 0) {
        $("#portfolio-items .item").css("display", "none");

        for (var i = 0; i < filter_cats.length; i++) {
            $("#portfolio-items " + filter_cats[i]).css("display", "block");
        }
    } else {
        $("#portfolio-items .item").css("display", "block");
    }
}

function projects_menu() {
    //All projects extra yellow
    $("#all-projects").each(function () {
        var height = $(this).innerHeight();
        var offset = $(this).offset();
        var margin = 35;

        all_projects_height = height;

        var fake_bg = $("<div></div>").attr({
            id: "fake-bg"
        }).css({
            height: height + "px",
            top: offset.top + "px"
        }).prependTo("#container");
        $("<div id='top-shadow'></div>").css({
            opacity: 0.5,
            top: "0px"
        }).prependTo(fake_bg);
        $("<div id='bottom-shadow'></div>").css({
            opacity: 0.5,
            top: (height - 11) + "px"
        }).prependTo(fake_bg);
        $("<div id='shadow-overlay'></div>").css({
            top: "0px",
            left: (offset.left + 690) + "px"
        }).prependTo(fake_bg);
    }).css("visibility", "visible");

    $(window).resize(function () {
        var offset = $("#all-projects").offset();
        $("#shadow-overlay").css({
            top: "0px",
            left: (offset.left + 690) + "px"
        });
    });

    $("#all-projects, #fake-bg").css({
        "height": "0px"
    });

    $("#projects-button").click(function () {
        if (!menu_opened) {
            open_projects_menu();
        } else {
            close_projects_menu();
        }
    });
}

function open_projects_menu() {
    menu_opened = true;
    $("#all-projects, #fake-bg").stop().animate({
        "height": all_projects_height + "px"
    }, 200);
    $("#projects-button").addClass("open");
    $("#portfolio-items").stop().animate({
        marginTop: (all_projects_height + 20) + "px"
    }, 200);
}

function close_projects_menu() {
    menu_opened = false;
    $("#all-projects, #fake-bg").stop().animate({
        "height": "0px"
    }, 200);
    $("#portfolio-items").stop().animate({
        marginTop: "0px"
    }, 200);
    $("#projects-button").removeClass("open");
}

function init_cycle() {
    bind_vimeo();

    //bind button action
    $(".buttons .button").disableSelection().click(function () {
        cycle_goto($(this));
    });

    $(".arrow").each(function () {
        var new_height = $(".text-info", $(this).parent()).innerHeight();

        new_height = new_height + 20;

        $(this).css({
            top: (new_height / 2) + "px"
        });
    });

    $(".arrow").disableSelection().css({
        display: "block",
        opacity: 0
    });

    $(".left").unbind("click").click(function () {
        goto_left(this);
    });

    $(".right").unbind("click").click(function () {
        goto_right(this);
    });

    bind_footer_anims();
    
    resize_slider();

    $(window).resize(function () {
        resize_slider();
    });
}

function fix_image_positions() {
    if ($.browser.msie && $.browser.version < 8) {
        //no fix
    } else {
        $(".image-cycle").each(function () {
            var img_height = $(this).outerHeight();
            var text_height = $(".text-info", $(this).parent()).outerHeight();

            if (img_height > text_height) {
                var diff = ((img_height - text_height) / 2);

                $(this).css({
                   marginTop: (diff * -1) + "px"
                });
            }
        });
    }
}

//Slider functions
function resize_slider() {
    fix_image_positions();
    $("#portfolio-items .item").each(function () {
        if (!mobileDevice()) {
            $(this).mousemove(function (e) {
                var offset = $(this).offset();
                if ((e.pageX > (offset.left - 50)) && (e.pageX < ((offset.left + 680) + 50))) {
                    $(".arrow", this).stop().animate({
                        opacity: 1
                    }, 100);
                } else {
                    $(".arrow", this).stop().animate({
                        opacity: 0
                    }, 100);
                }
            }).mouseout(function () {
                $(".arrow", this).stop().animate({
                    opacity: 0
                }, 300);
            });
        }

        var width = $(".text-info", this).offset();
        width = width.left;

        width = width * 2;

        slideWidth = width;

        $(".image-cycle").css({
            width: width + "px",
            marginLeft: ((width - 658) * -1) + "px"
        });

        $(".images li", this).css({
            width: width + "px"
        });

        var photoNr = $(".buttons .active", this).text().replace(/^\s*/, "").replace(/\s*$/, "");
        var marginLeft = slideWidth;
        var newMargin = (marginLeft * (photoNr - 1)) * -1;

        $(".images", this).css({
            marginLeft: newMargin + "px"
        });
    });
}

function cycle_goto(obj) {
    $(".vimeo_iframe").each(function () {
        this.api('pause');
    });

    var photoNr = $(obj).text().replace(/^\s*/, "").replace(/\s*$/, "")

    $(".button", $(obj).parent()).removeClass("active");
    $(obj).addClass("active");

    var marginLeft = (slideWidth + 4);
    var newMargin = (marginLeft * (photoNr - 1)) * -1;

    $(".images", $(obj).parent().parent()).stop().animate({
        marginLeft: newMargin + "px"
    }, 800, "easeOutQuint");
}

function goto_left(obj) {
    cycle_goto($(".active", $(obj).parent()).prev());
}

function goto_right(obj) {
    cycle_goto($(".active", $(obj).parent()).next());
}

function mobileSliderInit() {
    bind_vimeo();

    $(".images-mobile .item:first-child img[data-src]").each(function () {
        $(this).attr("src", $(this).attr("data-src"));
    });

    $(".images-mobile:not(.lectric-slider)").each(function() {
        var slider = new Lectric();
        slider.init(this, {preventDefault: true});
        slider.subscribe('animationEnd', slidSlider);

        $(".button", $(this).parent()).unbind("click").click(function () {
            var item = $(".images-mobile .item:eq(" + ($(this).html() - 1) + ")", $(this).parent().parent());
            slider.toItem(item);
        });
    });
}

function printObject(o) {
  var out = '';
  for (var p in o) {
    out += p + ': ' + o[p] + '\n';
  }
  return out;
}

function slidSlider(e) {
    var pos = (e.position.x * -1) / 658;
    $(".button", $(e.element[0]).parent().parent()).removeClass("active");
    $(".button:eq(" + pos + ")", $(e.element[0]).parent().parent()).addClass("active");

    $(e.element[0]).find(".item img[data-src]").each(function () {
        $(this).attr("src", "/workspace/uploads/images/transparent.gif");
    });
    $(e.element[0]).find(".item:eq(" + pos + ") img[data-src]").each(function () {
        $(this).attr("src", $(this).attr("data-src"));
    });
}

//Header functions
function bind_header() {
    if (!mobileDevice()) {
        //header scrolling
        $(document).bind('mousemove scroll', function(e){
            if ($("body").scrollTop() > 140 && headerStick == false) {
                headerStick = true;
                stickHeader();
            } else if ($("body").scrollTop() < 140 && headerStick == true) {
                headerStick = false;
                unstickHeader();
            }
        });
    }
}

function stickHeader() {
    if (document.getElementById("stickHeader")) {
        var stickHeader = $("#stickHeader");
    } else {
        var headerHtml = $("#header").html();

        var stickHeader = $("<div id='stickHeader'></div>").prependTo("body");
        var fakeHeader = $("<div class='fake'></div>").html(headerHtml).appendTo(stickHeader);
    }

    $(stickHeader).stop().animate({
        top: "0px"
    }, 150);
}

function unstickHeader() {
    $("#stickHeader").stop().animate({
        top: "-70px"
    }, 150);
}

function bind_vimeo() {
    $("img.vimeo_movie").each(function () {
        $(this).removeClass("vimeo_movie");

        if (mobileDevice()) {
            var image = $("img", $(this).parent());
            var src = $(image).attr("data-src");
            var width = $(image).attr("width");
            var height = $(image).attr("height");
            var vimeo_hd = $(image).attr("data-vimeo");

            var video = $(document.createElement('video'))
                .attr('id', Math.random() * 100)
                .attr('poster', src)
                .attr('controls', 'controls')
                .attr('src', vimeo_hd) // Changed 'href' attribute to 'src'
                .css({
                  width: width,
                  height: height
                });
            $(this).parent().html("").append(video);
        } else {
            var play_button = $("<div class='vimeo_play'></div>").appendTo($(this).parent());
            $(play_button).css({
                opacity: 0.4
            }).hover(function () {
                $(this).stop().animate({
                    opacity: 0.7
                });
            }, function () {
                $(this).stop().animate({
                    opacity: 0.4
                });
            });
            $(play_button).click(function () {
                var image = $("img", $(this).parent());
                var vimeo_id = $(image).attr("id");

                var width = $(image).attr("width");
                var height = $(image).attr("height");

                var iframe = '<iframe class="vimeo_iframe" id="vimeoplayer' + vimeo_id + '" src="http://player.vimeo.com/video/' + vimeo_id + '?api=1&amp;autoplay=1&amp;player_id=vimeoplayer' + vimeo_id + '&amp;byline=0&amp;portrait=0&amp;color=ffffff" width="' + width + '" height="' + height + '" frameborder="0" webkitAllowFullScreen allowFullScreen></iframe>';
                $(this).parent().html("").append(iframe);
                master_frame = 'vimeoplayer' + vimeo_id;

                Froogaloop.init();
            });
        }

    });
}

/*********************/
/* Contact functions */
/*********************/

function contact() {
    //work at
    $("#workat").hover(function () {
       $("a", this).stop().animate({
           height: "126px"
       }, 70);
    }, function () {
       $("a", this).stop().animate({
           height: "106px"
       }, 200);
    });

    $(".social li a").hover(function () {
        $(this).stop().animate({
            marginTop: "4px"
        }, 90, "easeOutQuint");
    }, function () {
        $(this).stop().animate({
            marginTop: "0px"
        }, 200, "easeOutQuint");
    });

    $(".vacature").stop().css({
        opacity: 0
    });
    $(".items .active").stop().css({
        opacity: 1
    });

    $(".content .items").css("height", $(".items .active").innerHeight() + "px");

    gmaps_initialize();
}

//gmaps INIT
function gmaps_initialize() {
    var latlng = new google.maps.LatLng(51.440246, 5.475622);
    var myOptions = {
        zoom: 16,
        center: latlng,
        disableDefaultUI: false,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);

    var marker = new google.maps.Marker({
        position: latlng,
        title:"Twisted Interactive"
    });

    marker.setMap(map);
}

function load_vacature(obj, handle) {
    $("li", $(obj).parent().parent()).removeClass("active");
    $(obj).parent().addClass("active");

    $(".vacature").stop().animate({
        opacity: 0
    }, 300);
    $("#" + handle).stop().animate({
        opacity: 1
    }, 300);

    $(".content .items").stop().animate({
        height: $("#" + handle).innerHeight() + "px"
    }, 300);
}

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

function goTwitter() {
    $.getJSON("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=TwistedAgency&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 = $("<div></div>").addClass("tweet").html(text).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;
}

/* jQuery extenden */

$.fn.extend({
    disableSelection: function() {
		return this.bind( ( $.support.selectstart ? "selectstart" : "mousedown" ) +
			".ui-disableSelection", function( event ) {
				event.preventDefault();
			});
	}
});

function mobileDevice() {
    if(navigator.userAgent.match(/Android/i) ||
    navigator.userAgent.match(/webOS/i) ||
    navigator.userAgent.match(/iPhone/i) ||
    navigator.userAgent.match(/iPad/i) ||
    navigator.userAgent.match(/iPod/i)) {
        return true;
    } else {
        return false;
    }
}

jQuery.fn.anchorAnimate = function(settings) {

 	settings = jQuery.extend({
		speed : 1100,
        easing : "easeOutQuart"
	}, settings);

	return this.each(function(){
		var caller = this
		$(caller).click(function (event) {
			event.preventDefault()
			var locationHref = window.location.href
			var elementClick = $(caller).attr("href")

			var destination = $(elementClick).offset().top;
			$("html:not(:animated),body:not(:animated)").stop().animate({ scrollTop: destination}, settings.speed, settings.easing, function() {
				window.location.hash = elementClick
			});
		  	return false;
		})
	})
}
