﻿var totalItems = 0;

$(document).ready(function () {
    totalItems = $('.slidingObject > li').length;
    resizeBackground(true);
    $('div.menu > ul > li').bind('mouseenter', function () { toggleHover(this.id, true) }).bind('mouseleave', function () { toggleHover(this.id, false) });
    $('#ancContact').click(function () { handleContact(); });
    $('#ancCareers').click(function () { handleCareers(); });
    setActiveNav();
    $('body .container').removeClass('hide');
    $('#background').removeClass('hide');
    $('.slidingObject').width(totalItems * itemWidth);
});

function setActiveNav() {
    if (window.location.pathname.match('/project/') != null) {
        var anchor = $('.menu > ul > li#menArtFormed a')
        $(anchor).css('display', 'block');
        $(anchor).removeClass('hover');
        $(anchor).addClass('active');
    } else {
        $('.menu > ul > li a').each(function () {
            if ($(this).attr('href') == window.location.pathname) {
                $(this).css('display', 'block');
                $(this).removeClass('hover');
                $(this).addClass('active');
            }
        });
    }
}

var imgWidth = 0;

function easterEgg() {
    $('#fullFadePopup').removeClass('hide');
    $('.content > div.logo').addClass('hide');

    var imgDot = new Image();
    var container = $('#easterEgg');
    if (imgWidth == 995) {
        $('#imgDot').attr('src', '/assets/easteregg_dot.png');
        $('#imgArchitecture').attr('src', '/assets/easteregg_architecture.png');
        $('#imgNeoteric').attr('src', '/assets/easteregg_neoteric.png');
        $('#imgText').attr('src', '/assets/easteregg_text.png');
    } else {
        $('#imgDot').attr('src', '/assets/easteregg_dot_1546.png');
        $('#imgArchitecture').attr('src', '/assets/easteregg_architecture_1546.png');
        $('#imgNeoteric').attr('src', '/assets/easteregg_neoteric_1546.png');
        $('#imgText').attr('src', '/assets/easteregg_text_1546.png');
    }

    $('#easterEgg').removeClass('hide');
}

function closeEasterEgg() {
    $('#fullFadePopup').addClass('hide');
    $('.content > div.logo').removeClass('hide');
    $('#easterEgg').addClass('hide');
}

function handleContact() {
    //Todo get object serializing to JSON and a webservice created
    var logoDiv;
    if (imgWidth > 995)
        logoDiv = '<img class=\'logoText\' alt=\'neoteric\' src=\'/assets/logo_1546_nodot.png\' /><img class=\'logoDot\' alt=\'neoteric\' src=\'/assets/logo_1546_dot.png\' />';
    else
        logoDiv = '';

    var at = '@';
    var html = "<div class='contact'>\
    <div class='map'>&nbsp;" + logoDiv + "</div>\
    <a href='javascript:closePopup();' class='close'><img src='/assets/popup_close.png' alt='Close Project' /></a>\
    <div class='address'><h2>contact</h2><span>p 403.984.2862 | f 403.452.2822<br />Suite 101, 224 11 Avenue SW<br />Calgary, AB T2R 0C3<br /><a href='javascript:showFTPInfo();'>ftp</a> | <a href='javascript:showEML();'>eml</a></span>\
    <div class='ftpInfo hide'>To connect to the ftp <a href='ftp.neotericarchitecture.ca'>click here</a>.</div><div class='emailInfo hide'><a href='mailto:info" + at + "neotericarchitecture.ca'>info" + at + "neotericarchitecture.ca</a></div></div>\
    </div>";
    showPopup(html);
    //setTimeout(new function () { $('.logoDot').animate({ left: 230, bottom: 180}, 500); }, 4000);
    $('.logoDot').delay(1000).animate({ left: 245, bottom: 200, width: 20 }, 500);
}

function showEML() {
    if ($('div.ftpInfo').is(':visible'))
        $('div.ftpInfo').fadeOut('fast', function () { $('div.emailInfo').fadeIn(); });
    else
        $('div.emailInfo').fadeIn();
}

function showFTPInfo() {
    if ($('div.emailInfo').is(':visible'))
        $('div.emailInfo').fadeOut('fast', function () { $('div.ftpInfo').fadeIn(); });
    else
        $('div.ftpInfo').fadeIn();
    
}

function handleCareers() {
    var links = "<a href='/assets/uploaded/NEOTERIC%20Careers.pdf' class='currentPositions' target='_blank'><img src='/assets/current_postings.png' alt='Current Positions' /></a><a href='mailto:careers@neotericarchitecture.ca' class='submitResume'><img src='/assets/submit_resume.png' alt='Submit Resume' /></a></div>"

    if (!showCareers)
        links = "<a href='mailto:careers@neotericarchitecture.ca' class='currentPositions'><img src='/assets/submit_resume.png' alt='Submit Resume' /></a></div>"

    var html = "<div class='careers'>\
    <div class='careersContent'><h2>careers</h2><div><span>neoteric</span> is a young, design-oriented firm located in the heart of Inglewood.  We welcome resume submissions from qualified and enthusiastic individuals.</div>" + links + "\
    <img src='/assets/logo_1546.png' alt='neoteric' class='logo' />\
    <a href='javascript:closePopup();' class='close'><img src='/assets/popup_close.png' alt='Close Project' /></a>\
    </div>";
    showPopup(html);
}

function closePopup() {
    //$('#fullFadePopup').fadeOut();
    $('#fullFadePopup').addClass('hide');
    $('#popupContainer').fadeOut(300, function() {
        if (projectVisible)
            $('#project').fadeIn(300);
    });

    if (imgWidth == 995)
        $('.content > div.logo').removeClass('hide');
}

var projectVisible = false;

function showPopup(html) {
    if ($('#project').is(':visible')) {
        projectVisible = true;
        $('#project').fadeOut(100);
    }
    
    if ($.browser.msie) {
        //$('#fullFadePopup').fadeTo(300, .70);
        $('#fullFadePopup').removeClass('hide');
        $('#popupContainer').fadeTo(300, .90);
    } else {
        $('#fullFadePopup').removeClass('hide');
        //$('#fullFadePopup').fadeTo(300, 1);
        $('#popupContainer').fadeTo(300, 1);
    }

    //$('div.logo').fadeOut();
    $('#popupContainer').html(html);

    if (imgWidth == 995)
        $('.content > div.logo').addClass('hide');
}

function toggleHover(itemId, show)
{
    if (show)
        $('#' + itemId + ' > .hover').fadeIn('fast');
    else
        $('#' + itemId + ' > .hover').fadeOut('fast');
}

var resizeTimer;

$(window).resize(function () {
    clearTimeout(resizeTimer);
    resizeTimer = setTimeout('resizeBackground()', 500);
});

var windowWidth = 0;
var windowHeight = 0;
var aryWidths = new Array(995, 1546);
var aryHeights = new Array(520, 808);
var curSizeIndex = 0;

var curIndex = 0;
var itemWidth = 180;
var inView = 5;

var curGalleryIndex = 0;
var totalGalleryItems = 0;
var galleryItemWidth = 79;
var galleryInView = 5;

var windowResizer = {
    windowWidth: 0,
    windowHeight: 0,
    aryWidths: new Array(995, 1546),
    aryHeights: new Array(520, 808),
    curIndex: 0,
    totalItems: 0,
    itemWidth: 180,
    inView: 5,
    resizeBackground: function (noAnimation) {

    }
}

function resizeBackground(noAnimation) {
    if ($(window).width() == windowWidth && ($(window).height() == windowHeight || $(window).height() == windowHeight + 17))
        return;
            
    var imgHeight = 0;
    windowWidth = $(window).width();
    windowHeight = $(window).height();
    var percentage = 100;
    var menuRMar = 61;
    var menuTMar = 95;
    var menuWidth = 334;
    var itemHeight = 50;

    var widthIndex = checkWidth();
    var heightIndex = checkHeight(widthIndex);
    if (widthIndex > heightIndex)
        widthIndex = heightIndex;

    imgWidth = aryWidths[widthIndex];
    imgHeight = aryHeights[heightIndex];

    if (curSizeIndex != widthIndex && !noAnimation) {
        $('.noAnimate, #bgFade').css('display', 'none');
        $('.popup').addClass('hide');
    }

    if (noAnimation) {
        postProcessing();
        $('#background > img#bg, .container').css('margin-left', ($(window).width() - imgWidth) / 2).css('width', imgWidth).css('height', imgHeight);
    } else {
        $('#background > img#bg, .container').animate({ "margin-left": ($(window).width() - imgWidth) / 2, "width": imgWidth, 'height': imgHeight }, "slow", function () { postProcessing(imgWidth); fadeBackIn(); });
    }

    curSizeIndex = widthIndex;
}

function fadeBackIn() {
    if ($('.noAnimate, #bgFade').css('display') != 'none')
        return;
    
    $('.noAnimate').fadeIn()
    if ($.browser.msie)
        $('#bgFade').fadeTo(100, .35);
    else
        $('#bgFade').fadeTo(100, 1);
}

function checkWidth() {
    if (windowWidth >= aryWidths[aryWidths.length - 1]) {
        return (aryWidths.length - 1);
    } else if (windowWidth < aryWidths[0] || windowHeight < aryHeights[0]) {
        return 0;
    } else {
        for (var i = 0; i < aryWidths.length; i++) {
            if (windowWidth > aryWidths[i] && windowWidth < aryWidths[i + 1]) {
                return i;
                break;
            }
        }
    }
}

function checkHeight(widthIndex) {
    if (windowHeight >= aryHeights[widthIndex])
        return widthIndex;
    else {
        for (var i = 0; i < aryHeights.length; i++) {
            if (windowHeight > aryHeights[i] && windowHeight < aryHeights[i + 1]) {
                if (i > widthIndex)
                    return widthIndex;
                else
                    return i;
            }
        }
    }
    return 0;
}


function postProcessing() {
    if (imgWidth > 995) {
        inView = 8;
        $('.slidingObject').width(totalItems * itemWidth);
        if ($('#project') != null)
            $('#project > .projectConatiner > .imageContent > .close').attr('href', 'javascript:closeProject();');
        
        $('a.bios img').attr('src', '/assets/bios_1546.png');
    } else {
        inView = 5;
        if ($('#project') != null)
            $('#project > .projectConatiner > .imageContent > .close').attr('href', '/artformed');

        $('a.bios img').attr('src', '/assets/bios.png');
    }

    if ($.cookie('res') != imgWidth)
        loadStyleSheet();

    $('.content > div.logo').removeClass('hide');

    if ($('.slidingObject > li').length <= inView && $('.slidingObject > li').length > 0)
        $('.galleryRootContainer > .control > a').addClass('hide');
    else
        $('.galleryRootContainer > .control > a').removeClass('hide');

    if ($('.galleryRootContainer').length == 1)
        moveSlider(0, true);
}

function loadStyleSheet() {
    if (imgWidth > 995) {
        $('head').append('<link type="text/css" class="overrideStyleSheet" rel="stylesheet" href="/library/css/' + imgWidth + '.css">');
        if ($.browser.msie) { $('.overrideStyleSheet').clone().appendTo('head') };
        if ($.browser.msie) { $('.overrideStyleSheet').clone().appendTo('head') };
        $.cookie('res', '1546');
    } else {
        $('.overrideStyleSheet').remove();
        $('#cssHighRes').remove();
        $.cookie('res', '995');
    }
}
