
function alert_dump(o) {
	var s = "";
	for(var i in o) {
		s += i + ': ' + o[i] + "\n";
	}
	alert(s);
}

var heroshots = null;

$(document).ready(function() {
	$('body').append(jQuery('<div></div>').attr({id: 'overlay'}));
	$('body').append(jQuery('<div></div>').attr({id: 'lightbox'}));
	$('#overlay').css({
		width: $(document).width() + 'px',
		height: ($(document).height() * 3) + 'px',
		display: 'none'
	});
	$('#overlay').click(
		function() {
			$('#lightbox').fadeOut();
			setTimeout(function(){
				$('#overlay').hide();
			}, 500);
		}
	);
	$('#lightbox').css({
		display: 'none'
	});
	$('#lightbox').click(
		function() {
			$('#lightbox').stop();
			$('#overlay').stop();
		}
	);
	$('#content').find('div.hero').ajaxComplete( 
		function() {
            // cxflow($('#imageFadeContainer')); LB
			portfolio();
			portfolio_ajax();
		}
	);
	
	$('#imageFadeContainer').addClass('loading')
	heroshots = $('#imageFadeContainer a');
	heroshots.each(
		function() {
			$(this).remove()
		}
	)
	expandables();
	readmore();
	on_blur();
	portfolio();
	portfolio_ajax();
	contact_section();
	contact_popup();
});

$(window).load(
	function() {
		$('#imageFadeContainer').removeClass('loading')
		if(heroshots.size()) {
			heroshots.each(
				function() {
					$(this).appendTo($('#imageFadeContainer'))
				}
			)
		}
	}
)

function readmore(){
	$("a.readmore, div.paged_navigation a").each(
		function(){
			if($(this).attr("href")!="javascript:void(0)"){
				var read_more=$(this).attr("href");
					$(this).click(
						function(){
							$("#mainstory").load(read_more+" #story",{},function(){readmore();})
							if($(this).parents('div.newsteaser').length > 0) {
								if($(this).parents('div.expand').next('div.contentToChange').css('display') != 'block') {
									$('div.newsteaser').slideUp('slow')
									$(this).parents('div.expand').next('div.contentToChange').slideToggle('slow')
									$(this).parents('div.expand').addClass('expanded')
								}
							}
						}	
					);
					$(this).attr({href: "javascript:void(0)"});
			}
		}
	);
}

function on_blur(){
	$("a").each(
		function(){
			$(this).click(
				function(){
					$(this).blur()
				}
			)
		}
	)
}

function expandables() {
	$('div.newsteaser').each(
		function() {
			$(this).appendTo($(this).parents('div.newswrapper').find('div.expand'))
		}
	)
	$("div.expand").each(
		function(){
			$(this).css({cursor: 'pointer'});
			if($(this).attr('href') != '') {
				$(this).attr({href: 'javascript:void(0)'});
			}

			var header = false;
			if($(this).find('div.header').length > 0) {
				header = $(this).find('div.header');
			} else if($(this).parents('div.expand').find('div.header').length > 0) {
				$(this).click(function(){
					$(this).parents('div.expand').find('div.header').click();
				});
			}
			if(header) {
				header.click(
					function(e) {
						$(this).parents('.block').siblings('.block').each(
							function() {
								$(this).removeClass('expanded');
								$(this).find('div.expanded').removeClass('expanded')
							}
						)
						
						$(this).parents('.block').siblings('.block').find('div.contentToChange').each(
							function() {
								if($(this).css('display') != 'none') {
									$(this).slideUp();
									$(this).prev(".expand").children("div.newsteaser").slideDown();
								}
							}						
						)

						$(this).parent('div.expand').toggleClass('expanded');

						e.preventDefault();

						$(this).parent(".expand").next("div.contentToChange").slideToggle("slow");
						$(this).parent(".expand").children("div.newsteaser").slideToggle();
					}
				);
			}
			$(this).next("div.contentToChange").slideUp();
		}
	);
}

function portfolio() {
	
	var large = $("ul.all-attachments li.even");

	if(large.length > 1){
		large.each(
			function(){
				$(this).css({display: "none"})
			}
		)
		$(large[0]).css({display: "block"})
	}
	
	if($('ul.all-attachments li.odd.selected').length == 0) {
		$(large[0]).next("li.odd").addClass("selected");
	}
	
	if($('#foldergroup')) { // This is a section page so make the large images link to their portfolios
		large.each(
			function() {
				var link = $($(this).find('a'));
				if(link.length == 0) {
					return;
				}
				var page = link.attr('href');
				
				link.attr({ href: 'javascript:void(0)' });
				$(this).append(
					$(jQuery('<div />'))
						.addClass('portfolio_label')
						.html($(this).attr('title'))
				)
				$(this).hover(
					function() {
						var hover_div = jQuery('<div />');
						hover_div.attr({ id: 'portfolio_hover' });
						hover_div.appendTo($(this));
					},
					function() {
						$(this).find('#portfolio_hover').remove();
					}
				);
				$(this).click(
					function() {
						//if(link.attr('dragging') == 'true') {
						//	return false;
						//}
						$('#lightbox').load(page + ' #projectwrapper', {}, 
							function() {
								if($('#overlay').css('display') == 'none') {
									$('#overlay').show();
								}
								if($('#lightbox').css('display') == 'none') {
									$('#lightbox').append(jQuery('<div />').attr({id: 'close_box'}));
									$('#close_box').click(
										function() {
											setTimeout(function(){
												$('#lightbox').fadeOut();
											}, 10);
											setTimeout(function(){
												$('#overlay').hide();
											}, 510);
										}
									);
									$('#lightbox ul.all-attachments li').hide();
									$('#lightbox').fadeIn('normal',function(){
										
										$('#lightbox').find('li:first-child + li').click();
										$('#lightbox').animate({
											marginTop: '-' + Math.round($('#lightbox').height() / 2) + 'px'
										});
										if(jQuery.browser.msie && jQuery.browser.version < 7) {
											window.scrollTo($('#lightbox').offset().left, ($('#lightbox').offset().top - $('#lightbox').width()));
										}
										
										$('#lightbox ul.all-attachments li.even:first').fadeIn('slow', function(){
										    $('#lightbox ul.all-attachments li.odd').fadeIn('slow');
										});
										
										
                                    	$("ul.all-attachments li.odd").each(
                                    		function(){
                                    			var toshow = $(this);
                                    			var show_large = toshow.prev('li.even');

                                    			if(toshow.attr('processed') == 'true') {
                                    				return;
                                    			}
                                    			toshow.attr({processed: 'true'})

                                    			if(toshow.find("a").length > 0){
                                    				toshow.find("a").attr({href: "javascript:void(0)"});
                                    			}
                                    			toshow.css({cursor: "pointer"})
                                    			toshow.hover(
                                    				function(){
                                    					toshow.addClass("hover")
                                    				},
                                    				function(){
                                    					toshow.removeClass("hover")
                                    				}
                                    			)
                                    			toshow.unbind('click')
                                    			toshow.click(
                                    				function(){
                                    					toshow.siblings('li.selected').each(
                                    						function() {
                                    							$(this).removeClass('selected')
                                    						}
                                    					)
                                    					toshow.parents('.portfoliohero').find('ul.all-attachments li.even').each(
                                    						function() {
                                    							if($(this).css('display') != 'none') {
                                    								$(this).fadeOut();
                                    							}
                                    						}
                                    					)

                                    					show_large.fadeIn()
                                    					toshow.addClass("selected")
                                    				}
                                    			)
                                    		}
                                    	)
									});
								}
							}
						);
					}
				);
			}
		);
	}
	
	$("ul.all-attachments li.odd").each(
		function(){
			var toshow = $(this);
			var show_large = toshow.prev('li.even');
			
			if(toshow.attr('processed') == 'true') {
				return;
			}

			toshow.attr({processed: 'true'})

			if(toshow.find("a").length > 0){
				toshow.find("a").attr({href: "javascript:void(0)"});
			}
			toshow.css({cursor: "pointer"})
			toshow.hover(
				function(){
					toshow.addClass("hover")
				},
				function(){
					toshow.removeClass("hover")
				}
			)
			toshow.unbind('click')
			toshow.click(
				function(){
					toshow.siblings('li.selected').each(
						function() {
							$(this).removeClass('selected')
						}
					)
					toshow.parents('.portfoliohero').find('ul.all-attachments li.even').each(
						function() {
							if($(this).css('display') != 'none') {
								$(this).fadeOut();
							}
						}
					)

					show_large.fadeIn()
					toshow.addClass("selected")
				}
			)
		}
	)

	if($('ul.all-attachments li.odd').size() > 6 && !large.parents('#lightbox').size()) {
		portfolioThumbs($('ul.all-attachments li.odd'));
	}

	if($('div#navwrapper').find('div.portfolioheader')) {
		if($('div#navwrapper').find('div.portfolioheader').parent('a')) {
			$('div#navwrapper').find('div.portfolioheader').parent('a').attr({href: 'javascript:void(0)'});
		}
	}
}

function portfolioThumbs(thumbs) {
	if(!thumbs.size()) {
		return
	}
	var height = 47
	var wrap = $(jQuery('<div />'))
	wrap.addClass('portfolio-slider-wrap')
	var slider = $(jQuery('<ul />'))
	slider.addClass('portfolio-slider')
	wrap.append(slider)
	thumbs.parents('.portfoliohero').append(wrap)
	var up = $(jQuery('<a />'))
	up.attr({href: '#up', title: 'Scroll Thumbnails Up'})
	up.addClass('up')
	up.css({opacity: 0.25, cursor: 'default'})
	up.click(function(c){
		c.preventDefault()
		up.blur()
		if(slider.is(':animated')) {
			return
		}
		var slider_offset = parseInt(slider.css('top'),10)
		if(slider_offset + height <= 0) {
			slider.animate({
				top: slider_offset + height
			}, 1000)
			down.css({opacity: 1, cursor: ''})
			if(slider_offset + height + height > 0) {
				up.css({opacity: 0.25, cursor: 'default'})
			}
		}
	})
	var down = $(jQuery('<a />'))
	down.attr({href: '#down', title: 'Scroll Thumbnails Down'})
	down.addClass('down')
	down.click(function(c){
		c.preventDefault()
		down.blur()
		if(slider.is(':animated')) {
			return
		}
		var slider_offset = parseInt(slider.css('top'),10)
		if(slider_offset - height >= 0 - (height * (thumbs.size() - 5))) {
			slider.animate({
				top: slider_offset - height
			}, 1000)
			up.css({opacity: 1, cursor: ''})
			if(slider_offset - height - height < 0 - (height * (thumbs.size() - 5))) {
				down.css({opacity: 0.25, cursor: 'default'})
			}
		}
	})
	wrap.parents('.portfoliohero').prepend(up).append(down)
	slider.append(thumbs)
}

function portfolio_ajax() {
	$('div.mainnav ul.navigation li a, #imageFadeContainer a').each(
		function() {
			if($(this).attr('processed') == 'true') {
				return;
			}
			var url = $(this).attr("href");
			if(url[url.length -1] != '/') {
				url = url + '/';			
			}
             // $(this).attr({href: 'javascript:void(0)', processed: 'true'});

			$(this).click(
				function() {
                    // clearTimeout(cxflow_loop) LB
					$('div.expanded').each(
						function() {
							$(this).removeClass('expanded');
						}
					);
					
					$('div.contentToChange').each(
						function() {
							if($(this).css('display') == 'block') {
								$(this).slideUp('fast')
								$(this).prev('.expand').children("div.newsteaser").slideDown('fast');
							}
						}
					)
					$('div.portfolioheader').parent('div.ph_wrap').removeClass('expanded').addClass('expanded');
					$('#content').find('div.hero').load(url + ' #foldergroup', {}, 
						function() { 
						}
					);
						if(!$('#back_button').size()) {
							var back_btn = $(jQuery('<div />'));
							back_btn.attr({id: 'back_button'})
							$('#content').find('div.hero').after(back_btn)
						}
						$('#back_button').click(
								function() {
									$('#content').find('div.hero').load('/ #imageFadeContainer', {}, 
										function() {
										}
									);
							        setTimeout(
										function() {
											$('#back_button').remove()
										}, 1
									);
								}
							)
				}
			);
		}
	);
}

function contact_section() {
	var links = $('div.contactwrapper h2');
	if(links.length < 1) {
		return;
	}
	var list = jQuery('<ul />');
	$(links[0]).addClass('selected');
	$(links[links.length - 1]).css({ borderRight: 'none' });
	links.each(
		function() {
			$(this).css({pointer: 'cursor'});
			$(this).attr('rel', $(this).html());
			$(this).next('div.location').attr('rel', $(this).html());
			$(this).click(
				function() {
					$('div.contactwrapper h2.selected').each(
						function() {
							$(this).removeClass('selected');
						}
					);
					$('div.expanded').each(
						function() {
							$(this).removeClass('expanded')
						}
					)
					$('.block1 div.contentToChange, .block2 div.contentToChange').each(
						function() {
							if($(this).css('display') != 'none') {
								$(this).slideUp();
								$(this).prev(".expand").children("div.newsteaser").slideDown();
							}
						}
					)
					$(this).addClass('selected');

					var header = $(this);
					$('div.contactwrapper div.contentToChange').children('div.location').each(
						function() {
							if($(this).attr('rel') == header.attr('rel')) {
								if(jQuery.browser.msie && jQuery.browser.version >= 7) {
									$(this).siblings('div.location:visible').hide('fast')
									$(this).show('fast').parent('div.contentToChange').show('fast')
								} else {
									$(this).siblings('div.location:visible').slideUp('fast')
									$(this).slideDown('fast').parent('div.contentToChange').slideDown('fast')
								}
							}
						}
					)

					$('div.contactexpand').addClass('expanded');
				}
			);
			list.append(jQuery('<li />').append($(this)));
		}	
	);
	
	$('div.contactwrapper div.contactexpand .header').after(list);

	$('div.contactwrapper div.location').each(
		function(i) {
			if(i > 0) {
				$(this).hide();
			}
		}
	);
	
}

function contact_popup() {
	$('div.contactwrapper div.location div.left div.page-content a[@href~=mailto]').each(
		function() {
			$(this).attr({href: 'javascript:void(0)'});
			$(this).click(
				function() {
					$('#lightbox').load('/page/contact-form #contact_form', {}, 
						function() {
							$('#lightbox').append(jQuery('<div />').attr({id: 'close_box'}));
							$('#close_box').click(
								function() {
									setTimeout(function(){
										$('#lightbox').fadeOut();										
									}, 10);
									setTimeout(function(){
										$('#overlay').hide();
									}, 510);
								}
							);
						}
					);
					$('#overlay').show();
					$('#lightbox').css({marginTop: '-140px'});
					$('#lightbox').fadeIn();
					if(jQuery.browser.msie && jQuery.browser.version < 7) {
						window.scrollTo($('#lightbox').offset().left, ($('#lightbox').offset().top - $('#lightbox').width()));
					}
				}
			);
		}
	);
}

function sleep(seconds) {
	d = new Date();
	while(1) {
		mill = new Date();
		diff = mill - d;
		if(diff > (seconds * 1000)) {
			break;
		}
	}
}

var finPos = false;
var cxflow_loop = null;

(function($){  

  var heroshot = $.extend(new Object(), {
    selector: 'a',
    totalx: 0,
    scrollx: 0,
    n: 0,
    cur: 0,
    moving: false,
    dragging: false,
    widths: [],
    
    init: function(){
      var self = this;
      this.el = $('.heroshots:first').css('overflow','hidden');
      this.el.append($('<div class="heroshot-wrap"></div>').css({position:'absolute', top:0, left:0}));
      this.wrap = $('.heroshots .heroshot-wrap');
      this.slides = this.el.find(this.selector).each(function(i,tem){ $(tem).remove().appendTo(self.wrap) });
      this.fadetime = 982;
      this.waittime = 9724;
      // Uncomment the following to use CMS-controlled values
      this.fadetime = (f = this.el.find('input[name=fadetime]'), f.size()) ?
                        Math.floor(parseFloat(f.val())*1000) : 982;
      this.waittime = (f = this.el.find('input[name=showtime]'), f.size()) ?
                        Math.floor(parseFloat(f.val())*1000+this.fadetime) : 9724; 
      
      this.slides.each(function(i,tem){
        var kore = $(tem),
            w = kore.outerWidth() + parseInt(kore.css('margin-left')) + parseInt(kore.css('margin-right'));
        self.widths.push(w);
        kore.css({position:'absolute', left:self.totalx}).show();
        self.totalx += w;
      });
      this.slides.filter(':last').clone().css({'left':-this.widths[this.widths.length-1]}).prependTo(this.wrap);
      this.el.find(this.selector).click(function(e){ e.preventDefault(); })
                                  .mousedown(function(e){ self.dragstart.apply(self, [e]); });
      
      for(i=1;i<this.widths;i++) this.widths[i] = this.widths[0];
      
      $(document).mousemove(function(e){ self.drag.apply(self,[e]); });
      $(document).mouseup(function(e){ self.dragstop.apply(self,[e]); });
      
      this.interval = setInterval(function(){ self.next.apply(self,[]); }, this.waittime);
	

      return this;
    },
    
    next: function(d){

    if (!($('html#home').size())) {return;}    
        
        
      var dir = d || 1;
      if(this.dragging) return;
      this.moving = true;
      var kore = this,
          o = this.n % this.slides.length,
          n = (a = this.n + dir, (a >= 0)) ? a % this.slides.length : a = this.slides.length - Math.abs(a) % this.slides.length,
          nu = $(this.slides[o]).clone()
                 .css('left', ((dir<0)? parseInt(this.el.find(this.selector).filter(':first').css('left')) - this.widths[n] : this.totalx));
          if(dir>0) nu.appendTo(this.wrap);
          else nu.prependTo(this.wrap);
          nu.mousedown(function(e){ kore.dragstart.apply(kore, [e]); })
            .click(function(e){ e.preventDefault(); });
      this.scrollx += this.widths[o] * dir;
      this.totalx += this.widths[n] * dir;
      var kore = this;
      this.wrap.animate({left: -this.scrollx}, this.fadetime, 'easeOutQuad', function(){
        kore.moving = false;
        if(dir > 0)
          kore.el.children(kore.selector).filter(':first').remove();
        else
          kore.el.children(kore.selector).filter(':last').remove();
        if(kore.scrollx > 10000)
          kore.rewind.apply(kore,[]);
      });
      this.cur = o;
      this.n++;
    },
    
    rewind: function(){
      var n = 10000;
      this.scrollx-=n;
      this.totalx-=n;
      this.el.find(this.selector).each(function(i,tem){
        var dis = $(tem);
        dis.css({left: parseInt(dis.css('left')) - n});
      });
      this.wrap.css({left: -this.scrollx });
    },
    
    drag: function(e){
      if(! this.dragging) return;      
      e.preventDefault();
      if(this.moving) return;
      
      var diff = e.pageX - this.dragorigin;
      if(Math.abs(diff) > 0) this.hasmoved = true;
      if(diff > 470) diff = 470;
      if(diff < -1000) diff = -1000;
      this.diff = diff;
      
      this.wrap.css({left: -this.scrollorigin + diff});
      
      var kore = this;
      if(!this.dragpoints) this.speedpoints = this.dragpoints = [e.pageX, e.pageX];
      else {
        this.dragpoints.push(e.pageX);
      }
      if(!this.speedinterval)
        this.speedinterval = setInterval(function(){ kore.trackspeed.apply(kore); }, 20);
    },
    
    dragstop: function(e){
      if(! this.dragging) return;
      e.preventDefault();
      var self = this;
      this.interval = setInterval(function(){ self.next.apply(self,[]); }, this.waittime);
      this.dragging = false;
      
      if((this.speed < -20) || (this.diff < -400))
        this.next();
      else if ((this.speed > 20) || (this.diff > 400))
        this.next(-1);
      else
        this.wrap.animate({left: -this.scrollx}, 420, 'easeOutQuad');
      
      clearInterval(this.speedinterval);
      this.speedinterval = null;
      this.speedpoints = null;
      this.dragpoints = null;
      this.speed = 0;
      
      if(! this.hasmoved){
        var cur = this.slides[this.cur];
        window.location = ((e.target.tagName.match(/^a$/i)) ? 
            $(e.target) :
            // $(e.target).closest(this.selector).find('a:first'))
            $(e.target).parent())
          .attr('href') || '#';
      }
      this.hasmoved = false;
    },
    
    dragstart: function(e){
      e.preventDefault();
      this.dragorigin = e.pageX;
      this.scrollorigin = this.scrollx;
      this.dragging = true;
      clearInterval(this.interval);
      this.interval = null;
    },
    
    trackspeed: function(){
      this.speedpoints[0] = this.speedpoints[1];
      this.speedpoints[1] = this.dragpoints[this.dragpoints.length-1];
      this.speed = this.speedpoints[1] - this.speedpoints[0];
    }
    
  });
  $(window).load(function(){
    window.HERO = heroshot.init();
  });
})(jQuery);









/*
function cxflow(container) {

	if(container.length == 0) {
		return false;
	}

	var images = container.children('img, a');

	var duration = $('input[name=showvalue]').val();
	if(duration == undefined || duration == 0) {
		duration = 5;
	}
	duration = duration * 1000;

	var fade = $('input[name=fadevalue]').val();
	if(fade == undefined || fade == 0) {
		fade = 2;
	}
	fade = fade * 1000;

	container.css({
		position: 'relative'
	});

	var img_starts = new Array();
	var img_widths = 0;
	images.each(function(i) {
			var _this = $(this);
			img_starts[i] = _this.position().left;
			img_widths += _this.outerWidth();
	});

	container.children('a').click(function() {
		 clearTimeout(cxflow_loop);
	});
	
	container.append(
		$(jQuery('<div />'))
			.addClass('container')
			.css({
				position: 'relative',
				left: '0px',
				width: img_widths + 'px',
				overflow: 'hidden'
			})
		    .append(images)
	);

	var _container = container.find('div.container');
	_container.Draggable({
		axis: 'horizontally',
		insideParent: true,
		autoSize: true,
		ghosting: true,
		onStart: function(cloneEl, x, y) {
			// Stop any current animation and imitate ghosting (doesn't flicker)
			_container.stop().hide();
			clearTimeout(cxflow_loop);
		},
		onStop: function() {
			_container.show();
			var current_left = parseInt(_container.css('left'));
			var max_right = (0 - Number($(images[images.length - 1]).position().left + $(images[images.length - 1]).outerWidth() - container.innerWidth()));
			var flow_img = null;
			if(current_left > 0) { // We've scrolled to the left of the first image
				_container.animate({
					left: '0px'
				});
				flow_img = 0;
			} else if(current_left < 0 && current_left < max_right) { // We've scrolled to the right of the last image
				_container.animate({
					left: max_right + 'px'
				});
				flow_img = images.length - 1;
			} else { // We're in the middle so let's slide to the nearest image
				var dist = Math.abs(current_left);
				var p = container.find('img').outerWidth() + 9;
				var diff = dist % p;
				if(diff > p/2) {
					var rightp = _container.outerWidth() - container.outerWidth();
					_container.animate({
						left: Math.max(current_left - (p - diff), -rightp)
					});
				}
				else {
					_container.animate({
						left: current_left + diff
					});
				}

				flow_img = Math.floor(dist / p);
			}

			
			cxflow_loop = setTimeout(function() {
					           cxflow_auto(container.find('div.container'), flow_img);
				          }, duration);
		}
	});

	cxflow_loop = setTimeout(function() {
			           cxflow_auto(container.find('div.container'), 0);
		          }, duration + fade);
}

function cxflow_auto(container, current) {
	clearTimeout(cxflow_loop);

	images = container.children('a,img');
	if(images.length < 1) {
		return;
	}

	var duration = $('input[name=showvalue]').val();
	if(duration == undefined || duration == 0) {
		duration = 5;
	}
	duration = duration * 1000;

	var fade = $('input[name=fadevalue]').val();
	if(fade == undefined || fade == 0) {
		fade = 2;
	}
	fade = fade * 1000;

	if(current > images.length - 2) { // We're at the last image now
		container.fadeOut(fade / 2)
		setTimeout(
			function() {
				container.css({ left: '0px' })
			}, fade / 2
		)
		setTimeout(
			function() {
				container.fadeIn(fade / 2)
			}, (fade / 2) + 10
		)
		current = -1
	} else if(current == images.length - 2) {
		container.animate({
			left: '-' + (container.width() - $(container).parent().width() - 9) + 'px'
		}, fade)
	} else {
		container.animate({
			left: '-' + $(images[current + 1]).position().left + 'px'
		}, fade)
	}
	
	cxflow_loop = setTimeout(function() {
		cxflow_auto(container, current + 1)
	}, duration + fade)
}
*/
