$j = jQuery.noConflict();

function __d(m) {var u='undefined';if (typeof console != u){var c=console;if ((typeof c.log != u) && c.log) c.log(m);}}


var BP = (function($) {
	var bp = {
		slides: [],
    text_images: { },
    has_more_info: false,

    track_event: function(url) {
      if (typeof pageTracker !== 'undefined') { pageTracker._trackPageview(url); }
    },
		init: function() {
			try{
				$('#load-animation').hide();
				$('#width').css('display', 'none').css('visibility', 'visible').fadeIn(300);
				if($('#slideshow.gallery').length > 0) {
					this.gallery.init();
				} else {
					this.more_info.init();
					this.slideshow.init();
				}
			} catch (e) {
			}
		},
		more_info: {
			info_div: '#moreinfo',
			slideshow_div: '#slideshow',
			pages: [],
			page: 0,
			scroll_offset: 16,
			nav_move_duration: 600,

			init: function() {
				var ap = (function(m) {
					return function(i, p) {
						m.pages.push(p)
					}
				})(this);
				$('#moreinfo .pages .page').each(ap);

				var pr = (function(m) {
					return function() {
						m.page_right($(this).attr('href'));
						return false;
					}
				})(this)
				var pl = (function(m) {
					return function() {
						m.page_left($(this).attr('href'));
						return false;
					}
				})(this);
				var rt = (function(m) {
					return function() {
						m.hide();
						return false;
					}
				})(this);
        this.enable_read_more();
				if (this.single_page_mode()) {
					$('a.textpage.return').bind('click', rt)
					$('a.textpage.right').bind('click', pr)
					$('a.textpage.left').bind('click', pl)
				}
        window.setTimeout(function() {
          $.each(BP.text_images, function(id, src) {
            $('#'+id).attr('src', src);
          })
        }, 1000);
			},
      enable_read_more: function() {
        if (this.single_page_mode()) {
          var sm = (function(m) {
            return function() {
              m.show($(this).attr('href'));
              return false;
            }
          })(this);
					$('a.showmoreinfo').bind('click', sm);
        }
      },

			single_page_mode: function() {
				return ($(this.slideshow_div).length > 0);
			},
			use_simple_transition: function() {
				if ($.browser.msie) {
					if (parseInt($.browser.version.substring(0,1)) < 8) {
						return true;
					}
				}
				return false;
			},
			use_simple_paging: function() {
				if ($.browser.msie) {
					if (parseInt($.browser.version.substring(0,1)) < 8) {
						return true;
					}
				}
				return false;
			},

			show: function(url) {
        try {
          BP.track_event(url);
        } catch (e) { }
				BP.slideshow.pause();
				this.viewport().scrollLeft(0)
				if (this.use_simple_transition()) {
					this.swap(this.info_div, this.slideshow_div);
					sifferate();
					$('#nav').addClass('top');
				} else {
					var s = this.nav_move_duration, show = this.info_div, hide = this.slideshow_div;
					$(show).eq(0).css('opacity', 0);
					var cb = function() {
						$(show).eq(0).css('display', 'block')
						$(show).eq(0).animate({opacity: 1}, 200);
						$('#nav').addClass('top');
						sifferate();
					}
					$(hide).eq(0).animate({height: "hide"}, s, 'swing', cb)
				}

			},

			viewport: function() {
				if (!this.__viewport) {
					this.__viewport = $('#pages')
				}
				return this.__viewport;
			},
			page_right: function(url) {
        try {
          BP.track_event(url);
        } catch (e) { }
				this.move_page(1)
			},

			page_left: function(url) {
        try {
          BP.track_event(url);
        } catch (e) { }
				this.move_page(-1)
			},

			move_page: function(direction) {
				var p = this.page + direction, current = this.page, page, fade_in, s, o = 0;
				var scroll_faded = !$.browser.msie,  animated_scroll = this.use_simple_paging();
				if (animated_scroll) {
					o = 0.5;
				}
				if (p != this.page) {
					if (!animated_scroll || scroll_faded) $(this.pages[p]).css('opacity', o);
					var callback = (function(m) {
						return function() {
							m.page = p;
							var g = $(m.pages[m.page]);
							var l = m.page_scroll(g);

							fade_in = function() {
								$(window).scrollTop(0);
								g.animate({'opacity': 1}, 400, 'swing', function() {
								})};
							if (animated_scroll) {
								if (scroll_faded) {
									m.viewport().animate({'scrollLeft': l}, 400, 'swing', fade_in)
								} else {
                  m.viewport().animate({'scrollLeft': l}, 400, 'swing', function() {
                    $(window).scrollTop(0);
                  });
								}
							} else {
								m.viewport().scrollLeft(l);
								fade_in()
							}
						}
					})(this);
					page = $(this.pages[current]);
					page.css('opacity', 1);
					if (!animated_scroll || scroll_faded) {
						page.animate({opacity:o}, 100, "swing", callback);
					} else {
						callback()
					}
				}
			},

			page_scroll: function(page) {
				if (typeof page == 'undefined') page = this.pages[this.page];
				return $(page).position().left + this.viewport().scrollLeft() - this.scroll_offset;
			},

			hide: function() {

				if (this.use_simple_transition()) {
					this.swap(this.slideshow_div, this.info_div);
					$('#nav').removeClass('top');
					BP.slideshow.unpause();
				} else {
					var s = this.nav_move_duration, hide = this.info_div, show = this.slideshow_div;
					var cb2 = (function(m) {
						return function() {
							BP.slideshow.unpause();
							m.reset_pages()
						}
					})(this);
					var cb = function() {
						$('#nav').removeClass('top');
						$(hide).eq(0).css('display', 'none')
						$(show).eq(0).animate({height: "show"}, s, 'swing', cb2)
					};
					$(hide).eq(0).animate({opacity: 0}, 200, 'swing', cb)
				}
			},
			reset_pages: function() {
				if (!$.browser.mozilla) { // firefox seems to keep its scroll pos correctly
					this.page = 0;
					this.viewport().scrollLeft(0);
					for (var i = 0; i < this.pages.length; i++) {
						$(this.pages[i]).css('opacity', 1);
					};
				}
			},
			swap: function(show, hide) {
				$(hide).eq(0).hide()
				$(show).eq(0).show()
			}
		},

		slideshow: {
			position: 0,
			period: 4000,   // time slide is displayed for
			duration: 1000, // duration of transition
			steps: 30, // no of updates of transition
			fade_step: 0,
			timer: null,
			slides: [],
			current: null,
			next: null,
			paused: false,
			fading: false,
			caption: null,
			debug: true,
			// title and subtitle parameters
			title_pause_duration: 900, // pause between image load and title fade start
			title_fade_duration: 1500,
			strapline_pause_duration: 2500, // pause between main title appearing and strapline fade start
			caption_fade_duration: 400,
      image_urls: {},



			init: function() {
				//this.slides = BP.slides;
        this.assign_images();
				this.load_slides();
				this.caption = $('#caption')
				this.position = 0;
				this.set_slides();
				if(this.has_titles()) {
					var t = (function(s) {
						return function() {
							s.reveal_titles();
						};
					})(this);
					window.setTimeout(t, this.title_pause_duration);
				}
				if (this.slides.length > 1) {
					this.start();
				}
			},
      assign_images: function() {
        $.each(BP.slideshow.image_urls, function(id, url) {
          $('#'+id).css('background-image', 'url('+url+')');
        });
      },
			slide_list: function() {
				return $('#slides div.fading');
			},
			load_slides: function() {
				var __slideshow = this;

				this.slide_list().each(function(i, d) {
					var s = $(d);
					d.slide_index = i;
					d.slide_ready = false;
					d.slide_next = function() {
						if ((this.slide_index + 1) < BP.slideshow.slides.length) {
							return BP.slideshow.slides[this.slide_index + 1];
						} else {
							return false;
						}
					};
					d.chain_preload = function() {
						var n = this.slide_next();
						if (n) {
							n.start_preload();
						}
					}
					s.start_preload = function() {
						var n = this[0];
						if (n.slide_ready) return;
						var src = /^url\(['"]?([^\)"']+)['"]?\)/.exec(this.css('background-image'))[1];
						n.image = new Image();
						n.image.slide = d;
						n.image.onload = function() {
							if (this.debug) __d('loaded ' + this.slide.id);
							this.slide.slide_ready = true;
							// this.slide.chain_preload()
						}
						n.image.src = src;
					}
					s.bind('click', function(event) {__slideshow.toggle()})
					__slideshow.slides.push(s);
				});
				// chain preloading fails on ie for unknown and weird reasons
				// BP.slideshow.slides[0].start_preload()
			},

			start: function(immediate) {
				if (this.slides.length <= 1) return;
				this.next.start_preload();
				var sf = (function(s) {
					return function() {
						s.check_loaded();
					}
				})(this);
				if (immediate) {
					sf();
				} else {
					this.timer = setTimeout(sf, this.period)
				}
			},

			toggle: function() {
				if (this.paused) {
					this.unpause();
				} else {
					this.pause();
				}
			},

			pause: function() {
				this.paused = true;
				if (this.fading) {
					this.clear_fade_interval();
				} else {
					this.clear_view_interval()
				}
			},

			unpause: function() {
				this.paused = false;
				if (this.fading) {
					this.set_fade_interval()
				} else {
					this.start(true);
				}

			},
			set_slides: function() {
				this.current = this.slides[this.position];
				this.next = this.slides[(this.position+1) % this.slides.length]
			},

			check_loaded: function() {
				this.clear_view_interval();
				this.ready_check();
			},

			wait_for_ready: function() {
				var cl = (function(s) {
					return function() {
						s.ready_check();
					}
				})(this);
				this.timer = setTimeout(cl, 500)
			},

			ready_check: function() {
				if (this.next[0].slide_ready){
					clearTimeout(this.timer);
					if(!this.paused) { this.start_fade(); }
				} else {
					this.wait_for_ready();
				}

			},

			start_fade: function() {
				this.before_fade_start();
        if (this.has_titles()) { this.titles().add(this.sub_titles()).css('display', 'none'); }
				this.fading = true;
				this.fade_step = 0;
				this.next.css('opacity', 0);
				this.next.css('display', '')
        if ($.browser.msie) { this.caption.hide(); }
				this.set_fade_interval();
			},

			set_fade_interval: function() {
				var w = (this.duration) / this.steps;
				var uf = (function(s){
					return function() {
						s.update_fade();
					}
				})(this);
				this.timer = setInterval(uf, w);
			},

			clear_fade_interval: function() {
				clearInterval(this.timer);
			},

			clear_view_interval: function() {
				clearTimeout(this.timer);
			},

			update_fade: function() {
				// if (this.paused) return;
				this.fade_step++;
				if (this.fade_step <= this.steps) {
					var co, no;
					co = 1.0 - this.easing(this.fade_step / this.steps);
					no = 1.0 - co;
					this.current.css('opacity', co);
					this.next.css('opacity', no);
					if (!$.browser.msie) { this.caption.css('opacity', 0.5*co*co); }
				} else {
					this.fade_complete();
				}
			},

			easing: function(p) {
				var e = 1-(1.0/(0.62 + 1.1* p * (-Math.pow(4, -10*p)+1) ) - 0.62);
				return e;
			},

			stop_fade: function() {
				this.clear_fade_interval();
				this.fading = false;
			},

			fade_complete: function() {
				// if (this.paused) return;
				this.stop_fade();
				this.current.css('display', 'none');
				this.titles().add(this.sub_titles()).css('display', 'none');
				this.position = ((++this.position) % this.slides.length);
				this.set_slides();
				if(this.has_titles()) {
					var t = (function(s) {
						return function() {
							s.reveal_titles();
						};
					})(this);
					window.setTimeout(t, this.title_pause_duration);
				}
				this.after_fade_complete();
				this.reveal_caption();
				this.start();
			},
			reveal_caption: function() {
        var read_more = '';
        if (BP.has_more_info) {
          // read_more = ' <a class="showmoreinfo inline" href="#moreinfo">» Read more</a>';
        }
				this.caption.html(this.current.attr('title') + read_more + ' <span>(image '+(this.position+1)+' of '+this.slides.length+')</span>');
        BP.more_info.enable_read_more();
        if (!$.browser.msie) {
          this.caption.animate({opacity: 1}, this.caption_fade_duration)
        } else {
          this.caption.show();
        }
			},
			has_titles: function() {
				if(!this.current) { return false; }
				return ( this.titles().length > 0 );
			},
			reveal_titles: function(){
				var cb = (function(s) {
					return function() {
						window.setTimeout(function() {s.reveal_sub_titles()}, s.strapline_pause_duration);
					};
				})(this);
				this.titles().fadeIn(this.title_fade_duration, cb);
			},
			reveal_sub_titles: function(){
				this.sub_titles().fadeIn(this.title_fade_duration);
			},
			titles: function(){
				return $('h1, h2', this.current);
			},
			sub_titles: function(){
				return $('h3', this.current);
			},
			before_fade_start: function() { },
			after_fade_complete: function() { }
		}
	};
	bp.gallery = $.extend(true, {}, bp.slideshow, {
		slide_list: function() {
			$.each(this._slide_list, function(i, s) {
				s._slide_index = i;
				if(i > 0) {
					$('#slides').append('<div class="fading" title="'+s[1]+'" id="image_'+(i+1)+'" style="display: none; background-image:url('+s[0]+')"></div>');
				}
			});

			this.draw_active();
			//
			// $.each(this._slide_list, function(i, s) {
			// 	var css_class="", css_id = "g-thumbnail_"+i
			// 	if(i == (BP.gallery._slide_list.length - 1)) { css_class='last'; }
			// 	if (i === 0) { css_class='first'; }
			// 	$('#slideshow.gallery .subtitle .thumbnails').append('<img id="'+css_id+'" src="'+s[0]+'" width="42" height="21" alt="" class="'+css_class+'" title="Click to view this image" />');
			// 	var c = (function(g, n) {
			// 		return function() {
			// 			g.go_to(n);
			// 		}
			// 	})(BP.gallery, i);
			// 	$('#'+css_id).click(c);
			// });
			return BP.slideshow.slide_list();
		},
		go_to: function(n) {
			var t = this;
			if(n === t.position) { return; }
			// t.default_duration = t.duration;
			// t.duration = 600;
			t.pause();
			t.pause_now = true;
			t.next = this.slides[n]
			t.position = n-1;
			t.start_fade();
		},
		reveal_caption: function() { },
		before_fade_start: function() {
			$('#gallery-window').animate({'opacity': 0}, 200);
			// $('#thumb-active').fadeOut(200)
		},
		after_fade_complete: function() {
			this.draw_active();
		},
		showing_list: function() {
			var n = 17, t = this, s = Math.floor(t.position - ((n-1) / 2)) , e = Math.floor(t.position + ((n-1)/2) + 2), l = [], p = [], i;
			if(s < 0) {
				for (i = (t._slide_list.length + s); i < t._slide_list.length; i++) {
					l.push(t._slide_list[i]);
				}
				s = 0;
			}
			for (i = s; i < Math.min(e, t._slide_list.length); i++) {
				l.push(t._slide_list[i]);
			}
			if(e >= t._slide_list.length) {
				for (i = 0; i < (e - t._slide_list.length); i++) {
					l.push(t._slide_list[i]);
				}
			}
			return l;
		},
		draw_active: function() {
			var p = this.thumbnails(), l = this.showing_list();
			p.html('');
			$.each(l, function(i, s){
				var css_class="", css_id = "g-thumbnail_"+i
				if(i == (l.length - 1)) { css_class='last'; }
				if (i === 0) { css_class='first'; }
				p.append('<img id="'+css_id+'" src="'+s[0]+'" width="42" height="21" alt="" class="'+css_class+'" title="Click to view this image" />');
				var c = (function(g, n) {
					return function() {
						g.go_to(n);
					}
				})(BP.gallery, s._slide_index);
				$('#'+css_id).click(c);
			});
			$('#gallery-window').animate({'opacity': 1}, 400);
		},
		thumbnails: function() {
			return $('#slideshow.gallery .subtitle .thumbnails');
		}
	});
	return bp;
})(jQuery);

// sifr config
var title_font = { src: '/swf/myriad-pro-light-ita.swf' };
var sub_title_font = { src: '/swf/myriad-pro-light.swf' };
var quote_font = { src: '/swf/myriad-pro-light-ita.swf' };
sIFR.activate(quote_font, title_font, sub_title_font);
$j(window).load(function($) {
	BP.init();
	sifferate();
});
function sifferate() {
	sIFR.replace(quote_font, {
		selector: '.quote blockquote',
		wmode: 'transparent',
		css: '.sIFR-root { color: #777;font-size: 20px; leading: 5; }'
	});

	sIFR.replace(title_font, {
		selector: '.page h2',
		wmode: 'transparent',
		css: '.sIFR-root { color: #777;font-size: 28px; leading: 5; }'
	});

	sIFR.replace(sub_title_font, {
		selector: '.page h3',
		wmode: 'transparent',
		css: '.sIFR-root { color: #777;font-size: 20px; leading: 5; }'
	});
};