$(document).ready(function(){

    $.history.init(function(hash){
        hash = hash.toLowerCase();
        hashpart = hash.split('/');
        if(hashpart[1] == "project" && hashpart[2] !== undefined && hashpart[2] != "") {
            viewProject(hashpart[2]);
        } else if(hashpart[1] == "tag" && hashpart[2] !== undefined && hashpart[2] != "") {
            viewTag(hashpart[2]);
        } else if(hashpart[1] == "over-ons"){
            viewOverOns();
        } else if(hashpart[1] == "nieuws"){
            viewNieuws();
        } else if(hashpart[1] == "contact"){
            viewContact();
        } else {
            viewHome();
        }
        _gaq.push(['_trackPageview', hash]);
    },
    { unescape: ",/" });

    updateWallpaperBox();
    $(window).resize(updateWallpaperBox);
	
});


window.onload = function(){ loadFancybox() };

function loadFancybox(){
	$.fancybox(
            '/tableaux/requests/kerst2011.php',
            {
                'width'             : 701,
                'height'            : 550,
                'autoScale'     	: false,
                'padding'           : 0,
                'centerOnScroll'    : true,
                'modal'             : true,
                'transitionIn'  	: 'none',
                'transitionOut' 	: 'none',
                'type'      		: 'iframe',
                'overlayOpacity'	: 0.7,
                'overlayColor'		: '#000000'
            }
        );
}


function updateWallpaperBox(){
    $("#fullscreenbg img").each(function(){
        $("#fullscreenbg").height($(window).height());
        $("#fullscreenbg").width($(window).width());
        var winRatio = $(window).height()/$(window).width();
        var imgRatio = 1;
        if(winRatio > imgRatio){
            $(this).height($(window).height());
            $(this).width($(window).height()/imgRatio);
        }else{
            $(this).width($(window).width());
            $(this).height($(window).width()*imgRatio);
        }
        $(this).css({"left":(($(window).width()-$(this).width())/2)});
        $(this).css({"top":(($(window).height()-$(this).height())/2)});
    });
}

firstProject = true;

function viewTag(id){
    switchBlock('tag');

    // LOADING
    if($("#tagcontent").is(":visible")){
        $("#tagcontent").hide();
        $("#tagprojects").hide();
        $("#tagloading").fadeIn(500);
    }

    $.getJSON("/tableaux/requests/getTag.php?tagId="+id+"&callback=?", function(data) {

        $("#tagloading").fadeOut(500,function(){

            $("#tagcontent").html("<h1>"+data.naam+"</h1>"+data.beschrijving);

            $("#tagprojects").fadeIn(500);
            $("#tagcontent").fadeIn(500);

            $("#tagprojects .topslider li").remove();

            $(data.projecten).each(function(i, item){
                var slide = document.createElement("li");
                if(item.empty == "0"){
                    $(slide).html("<div class=\"sliderimg\" onclick=\"location.href='/#/project/"+item.id+"/"+item.permalink+"';\"><img src=\""+item.photo+"\" border=\"0\" width=\"144\" height=\"120\" /><div class=\"topslider_overlay\"><div class=\"topslider_overlay_bg\"></div><div class=\"topslider_overlay_text\">"+item.project+"</div></div></div>");
                    $(slide).hover(function(){
                        $(".topslider_overlay", this).fadeIn(100);
                    },function(){
                        $(".topslider_overlay", this).fadeOut(100);
                    });
                }
                $("#tagprojects .topslider").append(slide);
            });

        });

    });

    restoreWallpaper();
}

function viewProject(id){
    switchBlock('project');

    if(firstProject){

        $(".project_topslider .topslider .sliderimg").each(function(){
            $(this).hover(function(){
                $(".topslider_overlay", this).fadeIn(100);
            },function(){
                $(".topslider_overlay", this).fadeOut(100);
            });
        });

        firstProject = false;

    }

    // LOADING
    if($("#projectcontent").is(":visible")){
        $("#projectcontent").fadeOut(500);
        $("#projectloading").fadeIn(500);
    }

    $.getJSON("/tableaux/requests/getProject.php?projectId="+id+"&callback=?", function(data) {

        $("#fullscreenbg img").fadeOut(500);

        $("#projectloading").fadeOut(500,function(){
            var website = "";
            if(data.url != ""){
                website = "<br/>bezoek: <a href=\""+data.url+"\" target=\"_blank\">"+data.url+"</a>";
            }
            $("#project_content_name").html("<strong>"+data.klant+"</strong><br />"+data.project+website);

            tags = "";
            $(data.tags).each(function(i,item){
                tags = tags + "<a href=\"/#/tag/"+item.id+"/"+item.name.replace(/ /,"-")+"\">" + item.name + "</a>";
                if(i != data.tags.length-1){
                    tags = tags + ", ";
                }
            });

            $("#project_content_tags").html("tagged: "+tags);

            $("#project_content_twitter").click(function(e){ 
                window.open("http://www.twitter.com/share?related=tableaux&url="+data.shorturl+"&text=Bekijk+dit+project+van+tableaux+eens:&related=tableaux", "twitter", "toolbar=no, width=630, height=375"); 
            });

            $("#project_content_facebook").click(function(e){ 
                window.open("http://www.facebook.com/sharer.php?u="+data.shorturl+"&t="+data.klant+" / "+data.project, "facebook", "toolbar=no, width=630, height=375"); 
            });

            $("#project_content_hyves").click(function(e){ 
                window.open("http://www.hyves-share.nl/button/tip/?tipcategoryid=12&rating=5&title=tableaux - project bekijken&body="+data.shorturl, "hyves", "toolbar=no, width=550, height=450"); 
            });

            $("#project_content_email").click(function(e){ 
                location.href="mailto:?subject="+data.klant+" / "+data.project+"&body=Bekijk deze link eens: "+data.shorturl; 
            });

            $("#fullscreenbg img").attr("src",data.background).fadeIn(500);
            $("#projectcontent").fadeIn(500);

            $("#project_content_images img").remove();
            $("#project_content_navigation div").remove();
            $("#project_content_images").css({"marginLeft":"0px"});
            $(data.images).each(function(i, item){
                $("#project_content_images").append("<img src=\""+item.url+"\" width=\"800\" height=\"388\" />");
                var navItem = document.createElement("div");
                $(navItem).addClass("navitem");
                $(navItem).attr("id","projectnavitem_"+i);
                if(i == 0){ $(navItem).addClass("active"); }
                $(navItem).click(function(){
                    $("#project_content_navigation .active").removeClass("active");
                    $(this).addClass("active");
                    $("#project_content_images").animate({"marginLeft":(i*-800)+"px"},250);
                    checkImageArrows();
                });
                $("#project_content_navigation").append(navItem);
            });
            $("#project_content_images").width(data.images.length*800);
            $("#project_content_navigation").width(data.images.length*15);

            checkImageArrows();

        });

    });

}

firstHome = true;

function viewHome(){
    switchBlock('home');

    if(firstHome){

        $(".home_topslider").hide();

        var i = 0;
        $(".home_topslider .topslider .sliderimg").each(function(){ i++;
            $(this).attr("delay",i).hide();
        });
        $(".home_topslider .topslider").shuffle();
        $(".home_topslider .topslider .sliderimg").delay(250).each(function(){ i++;
            $(this).delay($(this).attr("delay")*50).fadeIn(250,function(){
                $(this).hover(function(){
                    $(".topslider_overlay", this).fadeIn(100);
                },function(){
                    $(".topslider_overlay", this).fadeOut(100);
                });
            });
            $(this).click(function(){
                $(".topslider_overlay", this).fadeOut(100);
            });
        });

        $(".home_topslider").show();

    firstHome = false;

    }

    restoreWallpaper();
}

firstContact = true;

function viewContact(){
    switchBlock('contact');

    if(firstContact){
        initGMap();
        firstContact = false;
    }

    restoreWallpaper();
}

function viewOverOns(){
    switchBlock('over-ons');

    restoreWallpaper();
}

firstNews = true;

function viewNieuws(){
    switchBlock('nieuws');

    if(firstNews){
        
        $.get('/tableaux/requests/getTwitter.php', function(data) {
            $('#nieuwstwitter').html(data);
        });
        
        $.get('/tableaux/requests/getFacebook.php', function(data) {
            $('#nieuwsfacebook').html(data);
        });
        
        firstNews = false;
    }

    restoreWallpaper();
}

function switchBlock(type){
    if(type == 'home'){
        showProjects();
        showAddress();
    }else{
        hideProjects();
        hideAddress();
    }

    if(type == 'project'){
        showProject();
        showProjectNav();
    }else{
        hideProject();
        hideProjectNav();
    }

    if(type == 'contact'){
        showContact();
    }else{
        hideContact();
    }

    if(type == 'tag'){
        showTag();
    }else{
        hideTag();
    }
    
    if(type == 'over-ons'){
        showOverOns();
    }else{
        hideOverOns();
    }
    
    if(type == 'nieuws'){
        showNieuws();
    }else{
        hideNieuws();
    }
}

function showAddress(){
    if($("#addresscontainer").is(":hidden")){
        $("#addresscontainer").slideDown(250);
    }
}

function hideAddress(){
    if($("#addresscontainer").is(":visible")){
        $("#addresscontainer").slideUp(250);
    }
}

function showNieuws(){
    if($("#nieuwscontainer").is(":hidden")){
        $("#nieuwscontainer").slideDown(250);
    }
}

function hideNieuws(){
    if($("#nieuwscontainer").is(":visible")){
        $("#nieuwscontainer").slideUp(250);
    }
}

function showContact(){
    if($("#contactcontainer").is(":hidden")){
        $("#contactcontainer").slideDown(250);
    }
}

function hideContact(){
    if($("#contactcontainer").is(":visible")){
        $("#contactcontainer").slideUp(250);
    }
}

function showOverOns(){
    if($("#overonscontainer").is(":hidden")){
        $("#overonscontainer").slideDown(250);
    }
}

function hideOverOns(){
    if($("#overonscontainer").is(":visible")){
        $("#overonscontainer").slideUp(250);
    }
}

function showTag(){
    if($("#tagcontainer").is(":hidden")){
        $("#tagcontainer").slideDown(250);
    }
}

function hideTag(){
    if($("#tagcontainer").is(":visible")){
        $("#tagcontainer").slideUp(250);
    }
}

function showProjects(){
    if($("#projectscontainer").is(":hidden")){
        $("#projectscontainer").slideDown(250);
    }
}

function hideProjects(){
    if($("#projectscontainer").is(":visible")){
        $("#projectscontainer").slideUp(250);
    }
}

function showProjectNav(){
    if($("#projectnavcontainer").is(":hidden")){
        $("#projectnavcontainer").slideDown(250);
    }
}

function hideProjectNav(){
    if($("#projectnavcontainer").is(":visible")){
        $("#projectnavcontainer").slideUp(250);
    }
}

function showProject(){
    if($("#projectcontainer").is(":hidden")){
        $("#projectcontainer").slideDown(250);
    }
}


function hideProject(){
    if($("#projectcontainer").is(":visible")){
        $("#projectcontainer").slideUp(250);
    }
}

function previousTopslider(){
    var maxTopPage = ($(".project_topslider .topslider li").length+1)/10;
    var curTopPage = $(".project_slidernav .active").attr("id").replace(/topnavitem_/,"");
    if(curTopPage > 0){
        var newTopPage = (curTopPage*1) - 1;
        $(".project_slidernav .active").removeClass("active");
        $("#topnavitem_"+newTopPage).addClass("active");
        $(".project_topslider .topslider").animate({"marginLeft":(newTopPage*-770)+"px"},250);
        checkTopArrows();
    }
}

function nextTopslider(){
    var maxTopPage = ($(".project_topslider .topslider li").length+1)/10;
    var curTopPage = $(".project_slidernav .active").attr("id").replace(/topnavitem_/,"");
    if(curTopPage < (maxTopPage-1)){
        var newTopPage = (curTopPage*1) + 1;
        $(".project_slidernav .active").removeClass("active");
        $("#topnavitem_"+newTopPage).addClass("active");
        $(".project_topslider .topslider").animate({"marginLeft":(newTopPage*-770)+"px"},250);
        checkTopArrows();
    }
}

function checkTopArrows(){
    var maxTopPage = ($(".project_topslider .topslider li").length+1)/10;
    var curTopPage = $(".project_slidernav .active").attr("id").replace(/topnavitem_/,"");
    if(curTopPage == 0){
        $("#topslider_image_left").hide();
    }else{
        $("#topslider_image_left").show();
    }
    if(curTopPage == (maxTopPage-1)){
        $("#topslider_image_right").hide();
    }else{
        $("#topslider_image_right").show();
    }
}

function nextImageslider(){
    var maxTopImage = $("#project_content_images img").length;
    var curTopImage = $("#project_content_navigation .active").attr("id").replace(/projectnavitem_/,"");
    if(curTopImage < (maxTopImage-1)){
        var newTopPage = (curTopImage*1) + 1;
        $("#project_content_navigation .active").removeClass("active");
        $("#projectnavitem_"+newTopPage).addClass("active");
        $("#project_content_images").animate({"marginLeft":(newTopPage*-800)+"px"},250);
        checkImageArrows();
    }
}

function previousImageslider(){
    var maxTopImage = $("#project_content_images img").length;
    var curTopImage = $("#project_content_navigation .active").attr("id").replace(/projectnavitem_/,"");
    if(curTopImage > 0){
        var newTopPage = (curTopImage*1) - 1;
        $("#project_content_navigation .active").removeClass("active");
        $("#projectnavitem_"+newTopPage).addClass("active");
        $("#project_content_images").animate({"marginLeft":(newTopPage*-800)+"px"},250);
        checkImageArrows();
    }
}

function checkImageArrows(){
    var maxTopImage = $("#project_content_images img").length;
    var curTopImage = $("#project_content_navigation .active").attr("id").replace(/projectnavitem_/,"");
    if(curTopImage == 0){
        $("#project_image_left").hide();
    }else{
        $("#project_image_left").show();
    }
    if(curTopImage == (maxTopImage-1)){
        $("#project_image_right").hide();
    }else{
        $("#project_image_right").show();
    }
}

function initGMap() {
    var latlng = new google.maps.LatLng(51.37269124464958, 5.582084655761719);
    var myOptions = {
      zoom: 14,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("contactmap"),myOptions);

    var image = '/tableaux/tableaux/images/marker-map.png';
    var myLatLng = new google.maps.LatLng(51.36579230241763, 5.585174560546875);
    var tblxMarker = new google.maps.Marker({
        position: myLatLng,
        map: map,
        icon: image
    });
}

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-16345520-2']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
