$.ajaxSetup({
cache: false
});

// Set-up

$(document).ready(function() {
section = '';
subSection = '';
thumbFilter = '';
project = '';
thumbCount = 0;
projectImageCount = 0;
pressID = '';
backstretchID = 0;
$('#follower').hide();
$('#logo').animate({opacity: 0.0}, 0);
$('ul#main-nav').hide()
.animate({opacity: 0.0, marginTop: '5px'}, 0);
$('ul#sub-nav').hide()
.animate({opacity: 0.0, marginTop: '15px'}, 0);
$('#bg').hide();
$('#section-loader').animate({opacity: 0.0}, 0);
$('#pop-up-background').animate({opacity: 0.0}, 0);
$('#pop-up-window').animate({opacity: 0.0}, 0);
$('#logo a img').imagesLoaded(function() {
$(this).unbind('load');
start();
});
if(swfobject.hasFlashPlayerVersion('10.0.0') && (navigator.userAgent.indexOf('Android') == -1)) {
flashEnabled = true;
} else {
flashEnabled = false;
}
$('a#site-credit').hide();
$('#header-bg').hide();
});

function start() {	
$('#logo').animate({opacity: 1.0}, {duration: 500, easing: 'jswing', complete:function() {
if ($.browser.msie) {
this.style.removeAttribute('filter');
}
$('ul#main-nav').show()
.animate({opacity: 1.0, marginTop: '0px'}, {duration: 250, easing: 'jswing', complete:function() {
if ($.browser.msie) {
this.style.removeAttribute('filter');
}
startAddress();
}
});
}
});
}

function loadBg() {
if(flashEnabled) {
//$('#container').css({'position': 'absolute', 'left': '50%', 'margin-left': '-475px'});
$('#bg').show();
var flashvars = {};
var params = {wmode: 'transparent'};
var attributes = {};
swfobject.embedSWF('content/swf/bg.swf', 'bg', '1500', '1000', '10.0.0',null, flashvars, params, attributes);
} else {
fadeInBackstrech()
}
}

function fadeInBackstrech() {
if(backstretchID == 0) {
backstretchID = Math.floor((Math.random()*15)+1);
} else if(backstretchID == 15) {
backstretchID = 1;
} else {
backstretchID++;
}
$.backstretch('content/img/home_bg_' + backstretchID + '.jpg', {speed: 500}, function() {
backstretchInterval = setInterval('fadeOutBackstrech()', 3000);
});
}

function fadeOutBackstrech() {
clearInterval(backstretchInterval);
$('#backstretch-wrap').animate({opacity: 0.0}, {duration: 250, complete:function() {
$(this).empty();
if (section == 'home') {
fadeInBackstrech();
} else {
backstretchID = 0;
}
}
});
}

function loadSection(i) {
if (i == 'projects') {
$('#section-loader').load(i + '.cfm', function() {
if (i != section) {
$(this).empty();
} else {
$(this).animate({opacity: 1.0}, {duration: 0});
}
});
} else {
$('#section-loader').load(i + '.cfm', function() {
if (i != section) {
$(this).empty();
} else {
$(this).animate({opacity: 1.0}, {duration: 250, easing: 'jswing', complete:function() {
if ($.browser.msie) {
this.style.removeAttribute('filter');
}
}
});
}
});
}
}

function startMouseLoader() {
$(document).mousemove(function(e){
$('#follower').show()
.css({top: (e.pageY + 15) + 'px', left: (e.pageX + 15) + 'px'});
});
}

function closeMouseLoader() {
$('#follower').hide();
$(document).unbind('mousemove');
}

function getThumbFilter(i) {
if (i == 'pop-up') {
var j = '1';
} else if (i == 'commercial') {
j = '2';
} else if (i == 'residential') {
j = '3';
} else if (i == 'exhibit') {
j = '4';
} else {
j = '5';
}
thumbFilter = j;
}

function filterThumbs() {
if (thumbCount != 0) {
for (var i=1;i<=thumbCount;i++) {
var j = $('#thumb' + i +'Container a').attr('id');
var k = 'img#thumb' + i;
var l = '-' + thumbFilter;
if (j.indexOf(l) != -1 && $(k).is(':visible')) {
$(k).animate({opacity: 1.0}, {duration: 250, easing: 'jswing'});
} else if ($(k).is(':visible')) {
$(k).animate({opacity: 0.25}, {duration: 250, easing: 'jswing'});
}
}
}
}

function loadPopUp(i) {
$('#pop-up-background').stop()
.show()
.animate({opacity: 0.75}, {duration: 500, easing: 'jswing', complete:function() {
$('#pop-up-content-loader').load(i, function () {
$('#pop-up-window').stop()
.show()
.animate({opacity: 1.0}, {duration: 250, easing: 'jswing', complete:function() {
if ($.browser.msie) {
this.style.removeAttribute('filter');
}
}
});
});
}
});
}

function closePopUp() {
var i = $.address.value();
if ($.address.parameter('projectid')) {
var j = $.address.parameter('projectid');
i = i.replace('?projectid=' + j, '');
} else {
j = $.address.parameter('pressid');
i = i.replace('?pressid=' + j, '');
}
//window.location = '#' + i;
$.address.value(i);
}

// Address

function startAddress() {
address($.address.value());
$.address.change(function(event) {  
address(event.value);
});

function insertAnalytics(x) {
if ('withCredentials' in new XMLHttpRequest()) {
$.post(x);
}
}

function address(i) {
if (! section) {
if (document.referrer) {
var j = document.referrer;
} else {
j = 'none';
}
insertAnalytics('http://www.monolith-nyc.com/mammoth/insert_analytics.cfm?datasource=architectureatlarge&page=' + j + '&type=visit');
}
j = 'http://www.architectureatlarge.com/%23' + $.address.value();
insertAnalytics('http://www.monolith-nyc.com/mammoth/insert_analytics.cfm?datasource=architectureatlarge&page=' + j + '&type=view');
i = i.replace('/', '');
if (i != '') { // Load section
if (pressID && $.address.parameter('pressid')) {
j = $.address.parameter('pressid');
$('#pop-up-content-loader').load('press_item.cfm?pressid=' + j);
pressID = j;
} else if ($('#pop-up-background').is(':visible') == true) {
$('#pop-up-background').hide();
$('#pop-up-window').hide();
$('#pop-up-content-loader').empty();
project = '';
pressID = '';
} else if ($.address.parameter('pressid')) {
j = $.address.parameter('pressid');
loadPopUp('press_item.cfm?pressid=' + j);
i = i.replace('?pressid=' + j, '');
if (! section ) {
$('li#press a').hide();
$('li#press span').show();
loadSection('press');
section = 'press';
}
pressID = $.address.parameter('pressid');
} else if ($.address.parameter('projectid')) {
j = $.address.parameter('projectid');
loadPopUp('project.cfm?projectid=' + j);
i = i.replace('?projectid=' + j, '');
if (! section ) {
if (i.indexOf('projects') == 0 && i != 'projects') {
i = i.replace('projects', '');
i = i.replace('/', '');
$('li#' + i + ' a').hide();
$('li#' + i + ' span').show();
$('li#projects a').addClass('grey-link');
getThumbFilter(i);
subSection = i;
} else {
$('li#projects a').hide();
$('li#projects span').show();
}
loadSection('projects');
$('ul#sub-nav').show() // Load Projects sub nav
.animate({opacity: 1.0, marginTop: '10px'}, {duration: 250, easing: 'jswing', complete:function() {
if ($.browser.msie) {
this.style.removeAttribute('filter');
}
}
});
section = 'projects';
}
project = $.address.parameter('projectid');
} else if (section && section == 'projects' && i == 'projects') {
$('li#projects a').hide();
$('li#projects span').show();
$('li#projects a').removeClass('grey-link');
$('li#' + subSection + ' a').show();
$('li#' + subSection + ' span').hide();
for (var k=1;k<=thumbCount;k++) {
$('img#thumb' + k).animate({opacity: 1.0}, {duration: 250, easing: 'jswing'});
}
} else if (i.indexOf('projects') == 0 && i != 'projects') { // Load Projects sub section
i = i.replace('projects', '');
i = i.replace('/', '');
$('li#' + i + ' a').hide();
$('li#' + i + ' span').show();
if (subSection) {
$('li#' + subSection + ' a').show();
$('li#' + subSection + ' span').hide();
} else {
if (! section || section && section != 'projects') { 
$('li#projects a').addClass('grey-link');
loadSection('projects');
$('ul#sub-nav').show() // Load Projects sub nav
.animate({opacity: 1.0, marginTop: '10px'}, {duration: 250, easing: 'jswing', complete:function() {
if ($.browser.msie) {
this.style.removeAttribute('filter');
}
}
});
if (section && section == 'home') {
if(flashEnabled) {
$('#bg').hide()
.empty();
} else {
$('#backstretch-wrap').animate({opacity: 0.0}, {duration: 250, complete:function() {
$(this).empty();
}
});
}
$('a#site-credit').hide();
$('#header-bg').hide();
} else if (section) {
$('li#' + section + ' a').show();
$('li#' + section + ' span').hide();
}
}
section = 'projects';
}
if ($('li#projects a').is(':visible') == false) {
$('li#projects a').show();
$('li#projects span').hide();
$('li#projects a').addClass('grey-link');
}
getThumbFilter(i);
filterThumbs(); // Filter thumbs
subSection = i;
} else {
$('li#' + i + ' a').hide();
$('li#' + i + ' span').show();
if (section && section == 'home') {
if(flashEnabled) {
$('#bg').hide()
.empty();
} else {
$('#backstretch-wrap').animate({opacity: 0.0}, {duration: 250, complete:function() {
$(this).empty();
}
});
}
$('a#site-credit').hide();
$('#header-bg').hide();
} else if (section) {
$('li#' + section + ' a').show();
$('li#' + section + ' span').hide();
}
if (i == 'projects' && $('ul#sub-nav').is(':visible') == false) { // Load Projects sub nav
$('ul#sub-nav').show()
.animate({opacity: 1.0, marginTop: '10px'}, {duration: 250, easing: 'jswing', complete:function() {
if ($.browser.msie) {
this.style.removeAttribute('filter');
}
}
});
} else if (section && section == 'projects') {
$('ul#sub-nav').animate({opacity: 0.0, marginTop: '15px'}, {duration: 250, easing: 'jswing', complete:function() {
$(this).hide();
if (subSection) {
$('li#' + subSection + ' a').show();
$('li#' + subSection + ' span').hide();
subSection = '';
thumbFilter = '';
$('li#projects a').removeClass('grey-link');
}
}
});
}
if (section && section != 'home') {
if($('#follower').is(':visible') == true){
closeMouseLoader();
}
$('#section-loader').animate({opacity: 0.0}, {duration: 250, easing: 'jswing', complete:function() {
$(this).empty();
loadSection(i);
}
});
} else {
loadSection(i);
}
j = i.substr(0, 1).toUpperCase() + i.substr(1);
$.address.title($.address.title() + ' | ' + j);
section = i;
}
if($('img#home-logo').is(':visible') == true){
$('#logo a').show();
$('img#home-logo').hide();
}
} else { // Load home
if (section) {
$('img#home-logo').show();
$('#logo a').hide();
$('li#' + section + ' a').show();
$('li#' + section + ' span').hide();
if (section == 'projects') {
$('ul#sub-nav').animate({opacity: 0.0, marginTop: '15px'}, {duration: 250, easing: 'jswing', complete:function() {
$(this).hide();
if (subSection) {
$('li#' + subSection + ' a').show();
$('li#' + subSection + ' span').hide();
subSection = '';
thumbFilter = '';
$('li#projects a').removeClass('grey-link');
}
}
});
}
$('#section-loader').animate({opacity: 0.0}, {duration: 250, easing: 'jswing', complete:function() {
$(this).empty();
}
});
if($('#follower').is(':visible') == true){
closeMouseLoader();
}
$.address.title('ARCHITECTURE AT LARGE | Rafael de Cárdenas, Ltd.');
}
loadBg();
$('a#site-credit').show();
$('#header-bg').show();
section = 'home';
}
}

$('#nav a').click(function() {
$.address.value($(this).attr('href'));
});
}

