var home;
var news;
var contact;
var awards;
var about;
var people;
var projectcontainer;
var closebutton;
var menu_onlinefilms;
var menu_commercials;
var menu_directors;
var menu_info;
var menu_project;
var activeNavButton;
var functionDelay;
var activeMenu = undefined;
var cubeReady = false;
var puzzleMode = false;
var isLoading = false;
var loadingProject;
var language = "en";
var baseURL = "http://www.wefilm.nl";
$("document").ready(function () {
    if (isMobile()) {
        window.location.href = "http://www.wefilm.nl/m/";
        return
    }
    home = $("aside#home");
    news = $("section#news");
    contact = $("section#contact");
    awards = $("section#awards");
    about = $("section#about");
    people = $("section#people");
    projectcontainer = $("section#projectcontainer");
    menu_onlinefilms = $("ul#nav-online-films");
    menu_commercials = $("ul#nav-commercials");
    menu_directors = $("ul#nav-directors");
    menu_info = $("ul#nav-info");
    menu_project = $("ul#nav-project");
    closebutton = $("nav.closenav");
    menu_onlinefilms.hide();
    menu_commercials.hide();
    menu_info.hide();
    menu_directors.hide();
    menu_project.hide();
    $.address.init(function (a) {
        updateContent(a)
    });
    $.address.change(function (a) {
        updateContent(a)
    });
    $(".goldblock").css({
        opacity: "0",
        display: "block"
    });
    menu_directors.accordion();
    $("#form-response-success").hide();
    $("#form-response-error").hide();
    $("div.more-txt").hide();
    $(".menubtn").mouseenter(function (a) {
        $("#cubeswf").get(0).hilightThumbnail(this.href)
    }).mouseleave(function (a) {
        $("#cubeswf").get(0).unhilightThumbnail(this.href)
    });
    $(".navbtn").mouseenter(function (a) {
        $(".goldblock", this).stop().animate({
            opacity: "1",
            "margin-top": "6px"
        }, 300)
    }).mouseleave(function (a) {
        if (activeNavButton != a.currentTarget.id) {
            $(".goldblock", this).stop().animate({
                opacity: "0",
                "margin-top": "8px"
            }, 300)
        }
    });
    if (is_iPad()) {
		$('#footerbar').hide();
        setCubeLoaded()
    }
});
$(window).load(function () {
    if (!is_iPad()) {
        showMain(baseURL + "/wp-content/themes/wefilm", $.cookie("introwatched"))
    }
    $("#submitbtn").mousedown(function (f) {
        f.preventDefault();
        var b = $("input#fullname").val();
        var a = $("input#email").val();
        var d = $("textarea#message").val();
        var c = "fullname=" + b + "&email=" + a + "&message=" + d;
        $.ajax({
            type: "POST",
            url: "/wp-content/themes/wefilm/inc/php/postform.php",
            data: c,
            success: function () {
                $("#contactform").hide();
                $("#form-response-success").show()
            },
            error: function () {
                $("#contactform").hide();
                $("#form-response-error").show()
            }
        });
        return false
    });
    $("nav#language-selector").click(function (a) {
        if ($(this).hasClass("activeselector")) {
            $(this).removeClass("activeselector");
            $(this).delay(200).css({
                "background-position": "top center"
            });
            $(this).stop().delay(160).animate({
                "background-color": "#2b2f31"
            }, 200);
            $(".settingspanel").slideUp(200, "easeInExpo")
        } else {
            $(this).addClass("activeselector");
            $(this).delay(200).css({
                "background-position": "bottom center"
            });
            $(this).stop().animate({
                "background-color": "#a3b5ba"
            }, 200);
            $(".settingspanel").delay(200).slideDown(200, "easeOutExpo")
        }
    });
    $("a.sharebtn").click(function (a) {
        a.preventDefault();
        openPopup(a.currentTarget.href, "socialshare", 800, 500)
    })
    
    $("nav.newsclose").click(function (a) {
        a.preventDefault();
        $(this).fadeOut(400, function () {
            $(this).parent().slideUp(400, function () {
                $("a.newsbtn").stop().animate({
                    'opacity': "1"
                }, 400);
            })
        })
    });
    $("a.newsbtn").click(function (a) {
        a.preventDefault();
        $("div.more-txt").stop().slideUp(400);
        $("a.newsbtn").stop().animate({
            'opacity': "1"
        }, 400);
        //$("nav.newsclose").show();
        $("div.more-txt", $(this).parent()).stop().slideDown(400, "easeOutCubic");
        $(this).stop().animate({
            'opacity': "0"
        }, 400);
    });
    $("div.more-txt").hide();
    $("a.newsbtn").css({'opacity': "1"});
});

function updateContent(a) {
    if (is_iPad()) {
        $(".ipadcontent").hide()
    }
    home.stop().hide();
    news.stop().hide();
    contact.stop().hide();
    awards.stop().hide();
    about.stop().hide();
    people.stop().hide();
    projectcontainer.stop().hide();
    projectcontainer.html("");
    closebutton.hide();
    $("#contactform").show();
    $("#form-response-success").hide();
    $("#form-response-error").hide();
    $("a.menubtn").removeClass("activebutton");
    switch (a.pathNames[0]) {
    case "home":
        showHome();
        break;
    case "online-films":
        showOnlineFilms();
        break;
    case "commercials":
        showCommercials();
        break;
    case "directors":
        showDirector(a.pathNames[1]);
        break;
    case "contact":
        showContact();
        break;
    case "news":
        if (a.pathNames[1] != undefined) {
            showNews(a.pathNames[1])
        } else {
            showNews()
        }
        break;
    case "about":
        showAbout();
        break;
    case "awards":
        showAwards();
        break;
    case "project":
        showProject(a.pathNames[1]);
        break;
    case "puzzle":
        showPuzzle();
        break;
    case "people":
        showPeople();
        break;
    default:
        unknownAddressValue();
        break
    }
}
function showHome() {
    if (cubeReady == true) {
        if (is_iPad()) {
            $(".ipadcontenthome").delay(100).fadeIn(500, "easeOutCubic")
        } else {
            $("#cubeswf").get(0).positionForHome()
        }
    } else {
        functionDelay = showHome;
        return
    }
    hilightButton("#home");
    showMenu(false);
    home.delay(100).fadeIn(500, "easeOutCubic")
}
function showPuzzle() {
    if (cubeReady == true) {
        if (is_iPad() == false) {
            $("#cubeswf").get(0).playPuzzle()
        }
    } else {
        functionDelay = showPuzzle;
        return
    }
    puzzleMode = true;
    hilightButton("#home");
    showMenu(false)
}
function stopPuzzle() {
    if (cubeReady == true) {
        if (is_iPad() == false) {
            $("#cubeswf").get(0).stopPuzzle()
        }
    } else {
        functionDelay = stopPuzzle;
        return
    }
    puzzleMode = false
}
function showOnlineFilms() {
    if (cubeReady == true) {
        if (is_iPad()) {
            $(".ipadcontentonlinefilms").delay(100).fadeIn(500, "easeOutCubic")
        } else {
            $("#cubeswf").get(0).positionForOnlineFilms()
        }
    } else {
        functionDelay = showOnlineFilms;
        return
    }
    showMenu(menu_onlinefilms);
    hilightButton("#online-films")
}
function showCommercials() {
    if (cubeReady == true) {
        if (is_iPad()) {
            $(".ipadcontentcommercials").delay(100).fadeIn(500, "easeOutCubic")
        } else {
            $("#cubeswf").get(0).positionForCommercials()
        }
    } else {
        functionDelay = showCommercials;
        return
    }
    showMenu(menu_commercials);
    hilightButton("#commercials")
}
function showProject(a) {
    if (a == undefined) {
        unknownAddressValue()
    } else {
        loadingProject = a;
        if (cubeReady == false && is_iPad() == false) {
            functionDelay = loadProject;
            return
        }
        if ($("a#commercial-" + loadingProject) != undefined) {
            $("a#commercial-" + loadingProject).addClass("activebutton")
        }
        if ($("a#online-film-" + loadingProject) != undefined) {
            $("a#online-film-" + loadingProject).addClass("activebutton")
        }
        if ($("a#roel-" + loadingProject) != undefined) {
            $("a#roel-" + loadingProject).addClass("activebutton")
        }
        loadProject()
    }
}
function loadProject() {
    if (is_iPad() == false) {
        $("#cubeswf").get(0).positionForPages()
    }
    isLoading = true;
    //alert('icl_home '+icl_home);
    projectcontainer.load(icl_home + "/index.php/" + loadingProject, function () {
        $("iframe", ".video").css({
            opacity: "0"
        });
        $("iframe", ".video").load(function () {
            $(this).stop().animate({
                opacity: "1"
            }, 400)
        });
        $("section#project").show();
        isLoading = false;
        closebutton.show();
        if (activeMenu == undefined || activeMenu == false) {
            showMenu(menu_project)
        }
        $("a.sharebtn").click(function (a) {
            a.preventDefault();
            openPopup(a.currentTarget.href, "socialshare", 800, 500)
        });
        if (is_iPad() == true) {
            projectcontainer.show()
        } else {
            projectcontainer.fadeIn(300, "easeOutCubic")
        }
    })
}
function showDirector(a) {
    switch (a) {
    case "roel-welling":
        showRoel();
        break;
    case "martijn-de-jong":
        showMartijn();
        break;
    case "lennart-verstegen":
        showLennart();
        break
    }
    if (a == undefined && cubeReady == true && is_iPad() == false) {
        $("#cubeswf").get(0).positionForHome()
    }
    showMenu(menu_directors);
    hilightButton("#directors")
}
function showRoel() {
    if (cubeReady == true) {
        if (is_iPad()) {
            $(".ipadcontentroel").delay(100).fadeIn(500, "easeOutCubic")
        } else {
            $("#cubeswf").get(0).positionForRoelsProjects()
        }
    } else {
        functionDelay = showRoel;
        return
    }
    $("a#submenu-roel").delay(500).trigger("activate-node")
}
function showMartijn() {
    if (cubeReady == true) {
        if (is_iPad()) {
            $(".ipadcontentmartijn").delay(100).fadeIn(500, "easeOutCubic")
        } else {
            $("#cubeswf").get(0).positionForMartijnsProjects()
        }
    } else {
        functionDelay = showMartijn;
        return
    }
    $("a#submenu-martijn").delay(500).trigger("activate-node")
}
function showLennart() {
    if (cubeReady == true) {
        if (is_iPad()) {
            $(".ipadcontentlennart").delay(100).fadeIn(500, "easeOutCubic")
        } else {
            $("#cubeswf").get(0).positionForLennartsProjects()
        }
    } else {
        functionDelay = showLennart;
        return
    }
    $("a#submenu-lennart").delay(500).trigger("activate-node")
}
function showContact() {
    if (cubeReady == true) {
        if (is_iPad() == false) {
            $("#cubeswf").get(0).positionForPages()
        }
    } else {
        functionDelay = showContact;
        return
    }
    showMenu(menu_info);
    $("a#submenu-contact").addClass("activebutton");
    hilightButton("#info");
    closebutton.show();
    contact.delay(100).fadeIn(500, "easeOutCubic", function () {
        showMaps()
    })
}
function showMaps() {
    $("#gmap").gMap({
        zoom: 16,
        icon: {
            image: baseURL + "/wp-content/themes/wefilm/inc/img/contact/marker.png",
            iconsize: [42, 56],
            iconanchor: [42, 56],
            infowindowanchor: [0, 0]
        },
        markers: [{
            latitude: 52.35529,
            longitude: 4.864272
        }],
        mapTypeControl: false,
        zoomControl: true,
        panControl: true,
        scaleControl: false,
        streetViewControl: true,
    })
}
function showNews(a) {
    if (cubeReady == true) {
        if (is_iPad() == false) {
            $("#cubeswf").get(0).positionForPages()
        }
    } else {
        functionDelay = showNews;
        return
    }
    if (a != undefined) {
//        $(window).scrollTop($('#news-'+a));
        $("div.more-txt", '#news-'+a).show();
        $("a.newsbtn", '#news-'+a).css({'opacity': "0"});
    }
    showMenu(menu_info);
    $("a#submenu-news").addClass("activebutton");
    hilightButton("#info");
    
    $("img", news).hide();
    news.delay(100).fadeIn(500, "easeOutCubic", function () {
        $("img", news).delay(100).fadeIn(500, "easeOutCubic")
    });
    closebutton.show()
}
function showAbout() {
    if (cubeReady == true) {
        if (is_iPad() == false) {
            $("#cubeswf").get(0).positionForPages()
        }
    } else {
        functionDelay = showAbout;
        return
    }
    showMenu(menu_info);
    $("a#submenu-about").addClass("activebutton");
    hilightButton("#info");
    about.delay(100).fadeIn(500, "easeOutCubic");
    closebutton.show()
}
function showPeople() {
    if (cubeReady == true) {
        if (is_iPad() == false) {
            $("#cubeswf").get(0).positionForPages()
        }
    } else {
        functionDelay = showPeople;
        return
    }
    showMenu(menu_info);
    $("a#submenu-people").addClass("activebutton");
    hilightButton("#info");
    
    $("img", people).hide();
    people.delay(100).fadeIn(500, "easeOutCubic", function () {
        $("img", people).delay(100).fadeIn(500, "easeOutCubic");
    });
    closebutton.show()
}
function showAwards() {
    if (cubeReady == true) {
        if (is_iPad() == false) {
            $("#cubeswf").get(0).positionForPages();
        }
    } else {
        functionDelay = showAwards;
        return
    }
    showMenu(menu_info);
    $("a#submenu-awards").addClass("activebutton");
    awards.delay(100).fadeIn(500, "easeOutCubic");
    hilightButton("#info");
    closebutton.show();
}
function hilightButton(a) {
    activeNavButton = a.replace("#", "");
    $(".goldblock").stop().animate({
        opacity: "0",
        "margin-top": "8px"
    }, 300);
    if (a != "#home" && a != undefined) {
        $(".goldblock", a).stop().animate({
            opacity: "1",
            "margin-top": "6px"
        }, 300);
    }
}
function showMenu(a) {
    if (activeMenu == undefined && a == menu_project) {
        activeMenu = a;
        menu_project.delay(100).fadeIn(500, "easeOutCubic");
        return
    }
    if (activeMenu == a) {
        return
    }
    menu_onlinefilms.hide();
    menu_commercials.hide();
    menu_info.hide();
    menu_directors.hide();
    menu_project.hide();
    activeMenu = a;
    if (a == false) {
        return
    }
    a.delay(100).fadeIn(500, "easeOutCubic")
}
function unknownAddressValue() {
    $.address.value("home")
}
function setCubeLoaded() {
    $.cookie("introwatched", "yes", {
        expires: 7
    });
    cubeReady = true;
    $("header#wefilm").slideDown(500, "easeOutCubic", function () {
        $("figure#logo").fadeIn(700, "easeOutCubic");
        $("nav#main-nav").delay(100).fadeIn(700, "easeOutCubic");
        $("nav#language-selector").delay(200).fadeIn(700, "easeOutCubic");
        $("section#content").delay(300).fadeIn(700, "easeOutCubic");
        $("nav#navcontent").delay(300).fadeIn(700, "easeOutCubic", function () {
            if (functionDelay != undefined) {
                functionDelay();
            }
        })
    });
}
function is_iPad() {
    if (useHTML) {
        return true;
    }
    return ((navigator.platform.indexOf("iPad") != -1));
}
function isMobile() {
    return ((navigator.platform.indexOf("Blackberry") != -1) || (navigator.platform.indexOf("Android") != -1) || (navigator.platform.indexOf("iPhone") != -1) || (navigator.platform.indexOf("iPod") != -1));
}
function openPopup(c, b, a, d) {
    var f = (screen.availWidth - a) / 2;
    var e = (screen.availHeight - d) / 2;
    service = window.open(c, b, "menubar=no,toolbar=no,resizeable=no,scrollbars=no,width=" + a + ",height=" + d + ",screenX=" + f + ",screenY=" + e + ",left=" + f + ",top=" + e + ",screenX=" + f + ",screenY=" + e + ",left=" + f + ",top=" + e);
    service.focus();
};
