﻿var cpOpen = false;

var contactz = function(){
    //contact panel
    $('#contactsPanel').animate({
            left: '+=28px'
        },1000, 'swing');
    $('#contactsTab').click(function(){
    if (!cpOpen){
        cpOpen = true;
        $('#contactsPanel').animate({
            left: '+=320px'
        },600);
        $(this).css('background-position','0px 0px');
    } else {
        cpOpen = false;
        $('#contactsPanel').animate({
            left: '-=320px'
        },600, 'swing');
        $(this).css('background-position','0px -120px');
    }
    });
    
    //piggy-back architects map link here
    $('.callMap').each(function(){
        $(this).wrap('<div class="callMapPanel"></div>');
        $('.callMapPanel').append('<div class="callMapBtn"></div>').click(function(){
            $('#panelAMap').load('/design/architectsMap.htm').fadeIn();
            $('#closeAMap').fadeIn('slow').click(function(){
                $('#panelAMap').fadeOut();
                $(this).hide();
            });
        });
        return false;
    });
}

//Map in Contact Section
var contactMap = function(){
      //check if contact section is open
      var isViz = $('#contentBlock5').is(":visible");
      if(isViz){
        //$('#contentBlock5').addClass('mapBG');
        $('#mapData ul').show().children().children('ul').hide();
        $('#mapData li.hotspot').each(function(){
        $(this).hoverIntent(function(){
            $(this).children('ul').fadeIn();
            $(this).children('a').addClass('mapSqrActive');
            return this;
        },function(){
            $(this).children('ul').fadeOut();
            $(this).children('a').removeClass('mapSqrActive');
            return this;
        });

        });
      } else {
        //case for when contact section closed
        $('#mapData ul').hide();
      }
      //remove map if in subnav
      $('#contentBlock5 .subNav li').click(function(){
        //$('#contentBlock5').removeClass('mapBG');
        $('#mapData ul').hide();
      });
      
      $('h2.menuheader:last').click(function(){
      //reset if header is clicked
       //$('#contentBlock5').addClass('mapBG');
       $('#mapData ul').show().children().children('ul').hide();
      });

}
//'Contact' Regions Map //'Who For' Logo Cloud
var specialsOpen = function () {
    //check if contact section is open
    var whoForViz = $('#contentBlock2').is(":visible");
    var newsViz = $('#contentBlock4').is(":visible");
    var contactViz = $('#contentBlock5').is(":visible");
    if (contactViz) {
        //get img path
        var img = $('#cMap').attr('src');
        $('#contentBlock5 .itemMain').css('background-image', 'url(' + img + ')').css('background-repeat', 'no-repeat');
        $('#contentBlock5 .subNav:first').hide();
        //build left-hand nav
        $('#contactNav ul.subNav').appendTo($('#contentBlock5 .itemNav')).children().children('a').each(function () {
            $(this).attr('target', '_blank');
        });
        //create map hotspots
        $('#contentBlock5 ul.subNav a').each(function () {
            var href = $(this).attr('href');
            var id = $(this).text();
            $('#contentBlock5').append('<div class="mapSpotLink" id="' + id + '"><a href="' + href + '" title="' + id + '" target="_blank"></a>');
        });

    } else {
        //case for when contact section closed
        //$('#mapData ul').hide();
    }
    if (whoForViz) {
        $('#contentBlock2 .itemMain .mainContent .col').css('width', '100%').css('padding', '0');
    }
    if (newsViz) {
        $('#contentBlock4 .itemMain .mainContent').append('<div class="tweetCol col hidden"></div>');
        var allTweets = $('#tweetFeed').html();
        $('.tweetCol:first').html(allTweets).fadeIn('slow');
    }
}


//TWITTER FEED FUNCTIONS---------------------------------------------------------
function filterCallback(twitter_json) {

    var result = [];
    for (var index in twitter_json) {
        if (twitter_json[index].in_reply_to_user_id == null) {
            result[result.length] = twitter_json[index];
        }
        if (result.length == 4) break; // Edit this to change the maximum tweets shown
    }
    twitterCallback2(result); // Pass tweets onto the original callback. Don't change it!
}
function twitterCallback2(twitters) {
    var statusHTML = [];
    for (var i = 0; i < twitters.length; i++) {
        var username = twitters[i].user.screen_name;
        var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function (url) {
            return '<a href="' + url + '">' + url + '</a>';
        }).replace(/\B@([_a-z0-9]+)/ig, function (reply) {
            return reply.charAt(0) + '<a href="http://twitter.com/' + reply.substring(1) + '">' + reply.substring(1) + '</a>';
        });
        statusHTML.push('<div class="tweet">' + status + '<p><a class="tweetTime" style="font-size:10px" href="http://twitter.com/' + username + '/statuses/' + twitters[i].id_str + '">' + relative_time(twitters[i].created_at) + '</a></p></div>');
        //alert(statusHTML);
    }
    document.getElementById('tweetFeed').innerHTML = statusHTML.join('');
}

function relative_time(time_value) {
    var values = time_value.split(" ");
    time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
    var parsed_date = Date.parse(time_value);
    var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
    var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
    delta = delta + (relative_to.getTimezoneOffset() * 60);

    if (delta < 60) {
        return 'less than a minute ago';
    } else if (delta < 120) {
        return 'about a minute ago';
    } else if (delta < (60 * 60)) {
        return (parseInt(delta / 60)).toString() + ' minutes ago';
    } else if (delta < (120 * 60)) {
        return 'about an hour ago';
    } else if (delta < (24 * 60 * 60)) {
        return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
    } else if (delta < (48 * 60 * 60)) {
        return '1 day ago';
    } else {
        return (parseInt(delta / 86400)).toString() + ' days ago';
    }
}
//----------------------------------------------------------------------------
