// Set-up

$.ajaxSetup({
cache: false
});

function start() {
if (document.referrer) {
var page = document.referrer;
} else {
page = 'none';
}
insertAnalytics('http://www.monolith-nyc.com/mammoth/insert_analytics.cfm?datasource=wmartistmanagement&page=' + page + '&type=visit');
page = 'http://www.fredericmalle.com/%23' + $.address.value();
page = page.replace(/&/g,'%26')
insertAnalytics('http://www.monolith-nyc.com/mammoth/insert_analytics.cfm?datasource=fredericmalle&page=' + page + '&type=view');
section = '';
subSection = '';
subSubSection = '';
bg = '';
bgType = '';
scentsPerfumesBg = '';
xhr = 0;
$(window).resize(function() {
resizeBg();
});
resizeBg();
loadSection();
$('#footer-container img').imagesLoaded(function() {
$(this).unbind('load');
$('#footer-container').show()
.animate({opacity: 1.0}, {duration: 500, easing: 'jswing', complete:function() {
if ($.browser.msie) {
this.style.removeAttribute('filter');
}
}
});
});
$('#header-container').show()
.animate({opacity: 1.0}, {duration: 500, easing: 'jswing', complete:function() {
if ($.browser.msie) {
this.style.removeAttribute('filter');
}
}
});
$('p#main-nav-aboutus').show()
.animate({opacity: 1.0}, {duration: 500, easing: 'jswing', complete:function() {
if ($.browser.msie) {
this.style.removeAttribute('filter');
}
}
});
$('p#main-nav-scents').show()
.delay(100)
.animate({opacity: 1.0}, {duration: 500, easing: 'jswing', complete:function() {
if ($.browser.msie) {
this.style.removeAttribute('filter');
}
}
});
$('p#main-nav-perfumers').show()
.delay(200)
.animate({opacity: 1.0}, {duration: 500, easing: 'jswing', complete:function() {
if ($.browser.msie) {
this.style.removeAttribute('filter');
}
}
});
$('p#main-nav-yourscent').show()
.delay(300)
.animate({opacity: 1.0}, {duration: 500, easing: 'jswing', complete:function() {
if ($.browser.msie) {
this.style.removeAttribute('filter');
}
}
});
$('p#main-nav-shop').show()
.delay(400)
.animate({opacity: 1.0}, {duration: 500, easing: 'jswing', complete:function() {
if ($.browser.msie) {
this.style.removeAttribute('filter');
}
}
});
$('p#main-nav-press').show()
.delay(500)
.animate({opacity: 1.0}, {duration: 500, easing: 'jswing', complete:function() {
if ($.browser.msie) {
this.style.removeAttribute('filter');
}
}
});
$('p#main-nav-home').show()
.delay(600)
.animate({opacity: 1.0}, {duration: 500, easing: 'jswing', complete:function() {
if ($.browser.msie) {
this.style.removeAttribute('filter');
}
// Events
$.address.change(function(event) {  
address();
});
}
});
}

function loadPopUp(i, j) {
$('html, body').animate({scrollTop: 0}, 0);
//$('body').css({overflow: 'hidden'});
$('#pop-up-container').show();
$('#pop-up-content').load('pop_up.cfm?sectionid=' + i + '&subid=' + j, function() {
if ($('#pop-up-container').is(':visible') == true) {
$('#pop-up').stop()
.animate({opacity: 0.0}, 0)
.show()
.animate({opacity: 1.0}, {duration: 500, easing: 'jswing', complete:function() {
if ($.browser.msie) {
this.style.removeAttribute('filter');
}
}
});
}
});
}

function closePopUp() {
//$('body').css({overflow: 'auto', overflowX: 'hidden'});
$('#pop-up-container').hide();
$('#pop-up').hide();
$('#pop-up-content').empty();
}

function loadInfluencesPopUp() {
$('html, body').animate({scrollTop: 0}, 0);
//$('body').css({overflow: 'hidden'});
$('#influences-pop-up-container').show();
$('#influences-pop-up-content').load('influences.cfm', function() {
if ($('#influences-pop-up-container').is(':visible') == true) {
$('#influences-pop-up').stop()
.animate({opacity: 0.0}, 0)
.show()
.animate({opacity: 1.0}, {duration: 500, easing: 'jswing', complete:function() {
if ($.browser.msie) {
this.style.removeAttribute('filter');
}
}
});
}
});
}

function closeInfluencesPopUp() {
//$('body').css({overflow: 'auto', overflowX: 'hidden'});
$('#influences-pop-up-container').hide();
$('#influences-pop-up').hide();
$('#influences-pop-up-content').empty();
}

function loadViewStorePopUp(i) {
$('html, body').animate({scrollTop: 0}, 0);
//$('body').css({overflow: 'hidden'});
$('#view-store-pop-up-container').show();
$('#view-store-pop-up-content').load('view_store.cfm?storeid=' + i, function() {
if ($('#view-store-pop-up-container').is(':visible') == true) {
$('#view-store-pop-up').stop()
.animate({opacity: 0.0}, 0)
.show()
.animate({opacity: 1.0}, {duration: 500, easing: 'jswing', complete:function() {
if ($.browser.msie) {
this.style.removeAttribute('filter');
}
}
});
}
});
}

function closeViewStorePopUp() {
//$('body').css({overflow: 'auto', overflowX: 'hidden'});
$('#view-store-pop-up-container').hide();
$('#view-store-pop-up').hide();
$('#view-store-pop-up-content').empty();
}

function deactivateLink(i) {
$(i).bind('click.deactivateLink', function(event) {
event.preventDefault();
})
.css({cursor: 'default'});
}

function activateLink(i) {
$(i).unbind('.deactivateLink')
.css({cursor: 'pointer'});
}

// Address

function address() {
var page = 'http://www.fredericmalle.com/%23' + $.address.value();
page = page.replace(/&/g,'%26')
insertAnalytics('http://www.monolith-nyc.com/mammoth/insert_analytics.cfm?datasource=fredericmalle&page=' + page + '&type=view');
if ($.address.value().indexOf('press') == -1 || $.address.value().indexOf('press') == 1 && ! $.address.parameter('sub')) {
loadSection();
}
}

function loadSection() {
if ($('#pop-up-container').is(':visible') == true) {
closePopUp();
} else if ($('#influences-pop-up-container').is(':visible') == true) {
closeInfluencesPopUp();
} else if ($('#view-store-pop-up-container').is(':visible') == true) {
closeViewStorePopUp();
}
var i = $.address.value();
var j = '';
if (i == '/') {
i = 'home';
} else if ($.address.parameter('sub')) {
i = i.substr(0, i.indexOf('/?sub'));
j = $.address.value().replace(i + '/', '');
i = i.replace('/', '');
} else {
i = $.address.value().replace('/', '');
}
if (section && section != i) {
$('p#main-nav-' + section + ' a').css({color: ''});
if (bgType == 'white') {
$('p#main-nav-' + section + ' a').addClass('white-bg-link');
}
activateLink('p#main-nav-' + section + ' a');
if (section == 'home') {
activateLink('a#footer-logo');
}
if (subSection || section == 'perfumers') {
$('p#main-nav-' + section).css({paddingBottom: '20px', marginBottom: '0px'});
$('#sub-nav-' + section).hide();
$('#sub-nav-' + section + '-bg').stop()
.animate({height: '0px'}, {duration: 500, easing: 'jswing', complete:function() {
$(this).hide();
}
});
}
}
if (subSection && ! $.address.parameter('sub') || subSection && $.address.parameter('sub') != subSection) {
activateLink('p#sub-nav-' + subSection + ' a');
$('p#sub-nav-' + subSection + ' a').css({color: ''});
if (subSection == '77') {
if (section == i) {
$('#sub-nav-' + section + '-bg').stop()
.animate({height: $('#sub-nav-' + section + '-bg').height() - $('#sub-sub-nav-' + subSection).height() + 'px'}, {duration: 500, easing: 'jswing'});
}
$('#sub-sub-nav-' + subSection).hide();
$('#sub-sub-nav-' + subSection + '-bg').stop()
.animate({height: '0px'}, {duration: 500, easing: 'jswing', complete:function() {
$(this).hide();
}
});
}
//subSection = '';
}
if (subSubSection) {
activateLink('p#sub-sub-nav-' + subSubSection + ' a');
$('p#sub-sub-nav-' + subSubSection + ' a').css({color: ''});
subSubSection = '';
}
if (i == 'aboutus' || i == 'scents' || i == 'perfumers') {
if (section == i && $.address.parameter('subsub') || section == i && $.address.parameter('news') || section == i && $.address.parameter('post') || section == i && $.address.parameter('cat') || section == i && $.address.parameter('search')) {
activateLink('p#sub-nav-' + $.address.parameter('sub') + ' a');
} else if (! $.address.parameter('subsub') && ! $.address.parameter('news') && ! $.address.parameter('post') && ! $.address.parameter('cat') && ! $.address.parameter('search')) {
deactivateLink('p#sub-nav-' + $.address.parameter('sub') + ' a');

}
$('p#sub-nav-' + $.address.parameter('sub') + ' a').css({color: '#ED1C24'});
if (section != i) {
loadSubNav(i);
}
if (i == 'perfumers') {
activateLink('p#main-nav-perfumers a');
}
if ($.address.parameter('sub') == '77') {
if ($.address.parameter('subsub')) {
if (! $.address.parameter('scentid')) {
deactivateLink('p#sub-sub-nav-' + $.address.parameter('subsub') + ' a');
}
$('p#sub-sub-nav-' + $.address.parameter('subsub') + ' a').css({color: '#ED1C24'});
if (! subSubSection) {
activateLink('p#sub-nav-' + $.address.parameter('sub') + ' a');
}
subSubSection = $.address.parameter('subsub');
}
if (subSection != $.address.parameter('sub')) {
loadSubSubNav($.address.parameter('sub'), i);
}
}
var k = subSection;
subSection = $.address.parameter('sub');
}
if (section != i && i != 'contact') {
$('p#main-nav-' + i + ' a').css({color: '#ED1C24'});
if (i != 'perfumers' || i == 'perfumers' && ! $.address.parameter('sub')) {
deactivateLink('p#main-nav-' + i + ' a');
}
} else if (section == 'perfumers' && i == 'perfumers' && ! $.address.parameter('sub')) {
deactivateLink('p#main-nav-' + i + ' a');
}
section = i;
if ($.address.parameter('subsub')) {
loadBg(window[i + 'Bg' + $.address.parameter('subsub')]);
} else if (j && i != 'press') {
loadBg(window[i + 'Bg' + $.address.parameter('sub')]);
} else {
loadBg(window[i + 'Bg']);
}
if (i == 'home') {
deactivateLink('a#footer-logo');
$('#section-loader').empty();
} else if ($.address.parameter('sub') && $.address.parameter('sub') == '72' && k == '72' || $.address.parameter('sub') && $.address.parameter('sub') == '74' && k == '74' || $.address.parameter('sub') && $.address.parameter('sub') && $.address.parameter('sub') == '75' && k == '75') {
//$('#section-loader').load(i + '.cfm' + j);
if (xhr) {
xhr.abort();
}
xhr = $.ajax({
url: i + '.cfm' + j,
success: function(data) {
$('#section-loader').html(data);
}
});
} else {
$('#section-loader').empty();
if (xhr) {
xhr.abort();
}
xhr = $.ajax({
url: i + '.cfm' + j,
success: function(data) {
$('#section-loader').html(data);
}
});
//$('#section-loader').empty()
//.load(i + '.cfm' + j);
}
}

function loadSubNav(i) {
if (! section) {
var j = 500;
} else {
j = 0;
}
$('#sub-nav-' + i).stop()
.animate({opacity: 0.0}, 0)
.show();
if ($.address.parameter('sub') == '77') {
if ($('#sub-sub-nav-' + $.address.parameter('sub') + '-bg').is(':animated') == true) {
$('#sub-sub-nav-' + $.address.parameter('sub') + '-bg').stop()
.animate({height: '0px'}, 0);
}
var k = $('#sub-sub-nav-' + $.address.parameter('sub')).height();
} else {
k = 0;
}
$('#sub-nav-' + i + '-bg').stop()
.animate({height: '0px'}, 0)
.delay(j)
.show()
.animate({height: $('#sub-nav-' + i).height() + 17 + k + 'px'}, {duration: 500, easing: 'jswing', complete:function() {
$('p#main-nav-' + i).css({paddingBottom: '10px', marginBottom: '0px'});
$(this).animate({height: $(this).height() + 10 + 'px'}, 0);
$('#sub-nav-' + i).animate({opacity: 1.0}, {duration: 500, easing: 'jswing', complete:function() {
if ($.browser.msie) {
this.style.removeAttribute('filter');
}
}
});
}
});
}

function loadSubSubNav(i,j) {
if (! section) {
k = 500;
} else {
k = 0;
}
$('#sub-sub-nav-' + i).stop()
.animate({opacity: 0.0}, 0)
.show();
$('#sub-sub-nav-' + i + '-bg').stop()
.animate({height: '0px'}, 0)
.delay(k)
.show()
.animate({height: $('#sub-sub-nav-' + i).height() + 'px'}, {duration: 500, easing: 'jswing', complete:function() {
$('#sub-sub-nav-' + i).animate({opacity: 1.0}, {duration: 500, easing: 'jswing', complete:function() {
if ($.browser.msie) {
this.style.removeAttribute('filter');
}
}
});
}
});
if (section && section == 'scents') {
$('#sub-nav-' + j + '-bg').delay(k)
.animate({height: $('#sub-nav-' + j + '-bg').height() + $('#sub-sub-nav-' + i).height() + 'px'}, {duration: 500, easing: 'jswing'});
}
}

// Bg

function resizeBg() {
if (section != 'perfumers' || section == 'perfumers' && $.address.parameter('sub')) {
if ($(window).width() < 1008 && $(window).height() < 630) {
$('#bg-container img').width(1008);
$('#bg-container img').height(630);
$('#bg-container img').css({top: '0px', left: '-50px'});
} else {
if ($(window).height() > $('#bg-container img').height() || $(window).width() < $('#bg-container img').width()) {
$('#bg-container img').height($(window).height());
var j = $('#bg-container img').height() / 750 * 100;
j = 1200 / 100 * j;
$('#bg-container img').width(j);
}
if ($(window).width() > $('#bg-container img').width() || $(window).height() < $('#bg-container img').height()) {
$('#bg-container img').width($(window).width());
j = $('#bg-container img').width() / 1200 * 100;
j = 750 / 100 * j;
$('#bg-container img').height(j);
}
var k = $(window).width() - $('#bg-container img').width();
k = Math.round(k / 2);
j = $(window).height() - $('#bg-container img').height();
j = Math.round(j / 2);
$('#bg-container img').css({top: j + 'px', left: k + 'px'});
}
}
}

function loadBg(i) {
if (i && i != bg) {
$('#bg-container').stop()
.hide()
.animate({opacity: 0.0}, 0);
if (i != 'bg_perfumers.jpg') {
$('#bg-container img').attr('src', 'content/img/' + i)
.imagesLoaded(function() {
$(this).unbind('load');
if (bgType == 'image') {
$(this).show();
$('#bg-container').css({backgroundColor: ''})
.stop()
.show()
.animate({opacity: 1.0}, {duration: 1000, easing: 'jswing', complete:function() {
resizeBg();
if ($.browser.msie) {
this.style.removeAttribute('filter');
}
}
});
}
});
}
if ($.address.parameter('sub') && $.address.parameter('sub') == '76' && $.address.parameter('subsub') && $.address.parameter('subsub') != '87' && $.address.parameter('subsub') != '95' && $.address.parameter('subsub') != '97' && $.address.parameter('subsub') != '403' || $.address.parameter('sub') && $.address.parameter('sub') == '322') {
swapColorWhite();
scentsPerfumesBg = 'true';
} else if (bgType == 'white' && ! scentsPerfumesBg || scentsPerfumesBg) {
swapColorImage();
scentsPerfumesBg = '';
}
bgType = 'image';
bg = i;
} else if (! i && bgType != 'white') {
$('#bg-container').stop()
.hide()
.animate({opacity: 0.0}, 0);
if (! scentsPerfumesBg) {
swapColorWhite();
} else {
scentsPerfumesBg = '';
}
$('#bg-container img').hide();
if (! bgType) {
var j = 0;
} else {
j = 1000;
}
$('#bg-container').css({backgroundColor: '#FFF'})
.stop()
.show()
.animate({opacity: 1.0}, {duration: j, easing: 'jswing', complete:function() {
if ($.browser.msie) {
this.style.removeAttribute('filter');
}
}
});
bgType = 'white';
bg = '';
}
}

function swapColorWhite() {
$('a#footer-logo img').attr('src', 'content/img/logo_footer_2.png');
$('body').css({color: '#000'});
$('html').css({backgroundColor: '#FFF'});
$('.sub-nav').css({borderColor: '#000'});
$('a').addClass('white-bg-link');
}

function swapColorImage() {
$('a#footer-logo img').attr('src', 'content/img/logo_footer.png');
$('body').css({color: '#FFF'});
$('html').css({backgroundColor: '#231F20'});
$('.sub-nav').css({borderColor: '#FFF'});
$('a').removeClass('white-bg-link');
}

/* Forms */

function validateEmail(i) {
with (i) {
var j = value.indexOf('@');
var k = value.lastIndexOf('.');
if (j < 1 || k-i < 2) {
return false;
} else {
return true;
}
}
}

function validateRequired(i, j) {
with (i) {
if (value == null || value == '' || value == j) {
return false;
} else {
return true;
}
}
}

function clearText(i, j) {
$(i).css({color: '#000'});
if (i.value == j) {
i.value = "";
}
}

function insertValue(i) {
if (i.value == "") {
i.value = i.defaultValue;
$(i).css({color: '#FFF'});
}
}

/* Share */

function sharePost(i,j,k,l) { 
var url = i;
var username= 'monolithnyc'; // bit.ly username
var key = 'R_30b9a0072420fc5fbaa6a6251e244354';
$.ajax({
url: 'http://api.bit.ly/v3/shorten',
data: {longUrl: url, apiKey: key, login: username},
dataType: 'jsonp',
success: function(v) {
var bit_url = v.data.url;
if (j == 'facebook') {
$('a#share-facebook-' + l).attr({href: 'http://www.facebook.com/share.php?u=' + bit_url});
} else {
$('a#share-twitter-' + l).attr({href: 'http://twitter.com/home?status=' + k + ' ' + bit_url});
}
//alert(bit_url);
}
});
}

/* Mammoth */

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