
window.addEvent('load', function(){

	// facebook like button fix
    if($$('.addthis_button_facebook_like iframe')[0]){
    	if($$('.summary')[0].hasClass('black')){
	    	modifyFBlike('button_count','true','80','like','dark');
    	}else{
    		modifyFBlike('button_count','true','80','like','light');
    	}
    }
    
});

// facebook like button fix
function modifyFBlike(layout,faces,width,action,colorScheme) {
	var frames = $$('.addthis_button_facebook_like iframe');
	
	// set iframe width
	frames.each(function(frame){
		frame.set({
			'allowtransparency': 'true',
			'styles': {
				'width': width,
				'background': 'transparent'
			}
		});	
	
		// set height for "faces"
		if(faces == 'true') frame.setStyle('height', 22);
		
		var frameSrc = "//www.facebook.com/plugins/like.php?href="+addthis_share.url+"&layout="+layout+"&show_faces="+faces+"&width="+width+"&action="+action+"&colorscheme="+colorScheme;
		frame.set('src', frameSrc);
	});
	
}

window.addEvent('domready', function() {

	window.uri = new URI();
	
	// global CSS3 support variable - which browsers do we want to support?
	window.css3 = false;
	if(Browser.Engine.webkit) window.css3 = true;
	
	// boot up our environment
	if($$('.fltr-content').length > 1 || $('body').hasClass('search-results') || $('body').hasClass('category')){
		window.sidebar = new Sidebar();
		window.list = new Posts();
	}else{
		window.single = new Single();
	}
	
	// form validation
	if($('commentform')) window.commentVal = new FormValidation($('commentform'));
	if($('content').getElement('.contactform')) window.contactVal = new FormValidation($('content').getElement('.contactform form'));
	
    // start the slideshow if there's a cookie
	if(window.list && Cookie.read('slideshow') && Cookie.read('slideshow') == 'true'){
		window.list.toggleSlideshow();
	}
	
	
});

// form validation helper class - useless right now but may add intelligence here later
var FormValidation = new Class({

	initialize: function(form){
		var that = this;
		
		this.form = form;
		this.validation = new Form.Validator(this.form);
	
	}

});

// sets up our scripting for single pages
var Single = new Class({
	
	initialize: function(){
		var that = this;
		
		this.footer = $('footer');
		this.html = $('html');
		this.sidebar = $('sidebar');
		this.img = $('content').getElement('.img');
		
		if($('footer-bg')) this.footer.setStyle('background-color', $('footer-bg').get('value'));
		//this.html.setStyle('background-color', '#fff');
		//this.sidebar.addClass('white');
		if(this.img){
		
			// if page has flash, load it and set it up
			this.img.flash = this.img.getElement('.bg-flash');
			this.img.flashBg = this.img.getElement('.bg-img-flash');
			this.img.bg = this.img.getElement('.background');
			
			if(this.img.flash){
				this.img.flashBg ? this.img.bg.setStyle('background-image', this.img.flashBg.get('value')) : this.img.bg.setStyle('background-image', 'none');
				var flash = new Swiff(this.img.flash.get('value'), {
				    width: 1350,
				    height: 445,
				    container: this.img.getElement('.flash-content')
				});
			}
		
		}
		
	}

});

// Scripts for the Filterific list
var Posts = new Class({

	initialize: function(){
		var that = this;
		
		window.fx = new Fx.Scroll(window, {
			link:'cancel'
		});
		
		this.shadow = $('shadow');
		this.shadow.fx = new Fx.Morph(this.shadow, {
			link: 'cancel'
		});
		
		this.nav = $$('.navigation a');
		this.nav.next = $('body').getElement('.navigation.bottom a');
		this.nav.prev = $('body').getElement('.navigation.top a');
		this.nav.each(function(nav){
			nav.fx = new Fx.Morph(nav, {
				link: 'cancel'
			});
		});
		
		this.html = $('html');
		this.html.fx = new Fx.Tween(this.html, {
			link: 'cancel'
		});
		//this.html.fx.set('background-color', '#ffffff');
		
		this.footer = $('footer');
		this.footer.fx = new Fx.Morph(this.footer, {
			link: 'cancel'
		});
		this.footer.offset = 0;
		
		this.posts = $$('.fltr-content');
		this.posts.current = 'none';
		this.posts.allClosed = true;
		
		this.current = this.posts[0].get('id');
		
		// set up global options
		this.options = {
			colors: {
				bg: this.posts[0].getElement('.img').getStyle('background-color'),
				summary: this.posts[0].getElement('.summary').getStyle('background-color'),
				h2: /* this.posts[0].getElement('.summary h2').getStyle('color') */ '#ffffff',
				footer: this.footer.getStyle('background-color'),
				html: this.html.getStyle('background-color')
			},
			logoSize: $('logo').getSize()
		};
		
		this.posts.each(function(post, index){
			that.setUpPost(post, index);
		});
		
		// navigation buttons
		this.up = $('sidebar').getElement('#up');
		this.up.fx = new Fx.Morph(this.up, {
			link: 'cancel'
		});
		this.up.addEvent('click', function(e){
			e.preventDefault();
			that.openPrevPost();
		});
		this.down = $('sidebar').getElement('#down');
		this.down.fx = new Fx.Morph(this.down, {
			link: 'cancel'
		});
		this.down.addEvent('click', function(e){
			e.preventDefault();
			that.openNextPost();
		});
		
		// arrow key navigation
		this.keyEvents = new Keyboard({
		    defaultEventType: 'keyup', 
		    events: { 
		        'left': function(){
		        	that.openPrevPost();
		        },
		        'right': function(){
	        		that.openNextPost();
		        }
		    }
		});
		this.keyEvents.activate();
		
		// slideshow
		this.slideshow = $('sidebar').getElement('#slideshow');
		this.slideshow.fx = new Fx.Morph(this.slideshow, {
			link: 'cancel'
		});
		this.slideshow.on = false;
		this.slideshow.sprite = this.slideshow.getElement('.progress');
		this.slideshow.sprite.height = 15; //TODO - detect this height
		this.slideshow.sprite.frame = 1;
		this.slideshow.sprite.nextFrame = function(){
			if(that.slideshow.sprite.frame > 5) that.slideshow.sprite.frame = 1;
			var offset = that.slideshow.sprite.frame * that.slideshow.sprite.height;
			that.slideshow.sprite.setStyle('background-position', '0 -'+ offset +'px');
			that.slideshow.sprite.frame = ++that.slideshow.sprite.frame;
		}
		this.slideshow.addEvent('click', that.toggleSlideshow.bind(that));
		this.slideshow.setStyle('display', 'block');
		
		// begin listening for hash links
		if(!Browser.Engine.trident4){
			this.startHashListener();
		}else{
			this.toggle(this.posts[0])
		}
	
	},
	
	startHashListener: function(){
		var that = this;
		
		// listener opens post if there is a hash link
		window.anchors = new HashListener();
		window.anchors.start();
		window.anchors.addEvent('hash-changed', function(anchor){
			
			var parts = anchor.split('/');
			parts[0] = null;
			parts = parts.clean();
			
			// parse the uri to get the category
			var category = window.uri.parsed.directory.split('/').filter(function(item){
				return item != '';
			})[0];

			if(category && category != 'page'){
			
				// category page anchor links don't have the category in them, so get it from the uri
				var id = parts[0];
				
			}else{
			
				// homepage anchor links include category
				category = parts[0];
				var id = parts[1];
				
			}
			
			// build the url
			var url = '/' + category + '/' + id + '/';
			var post;
			if($(id)) post = $(id).getParent('.fltr-content');
			
			if(id && $(id) && !post.open){
				
				// if there's an anchor link & the post is present, open it
				window.anchors.stop();
				that.toggle(post);
				
			}else if(id && !$defined($(id))){
				
				// if there's an anchor link but the post is not present, auto-redirect to post's url
				window.location = url;
				
			}else if(!id && !$('body').hasClass('search-no-results')){
			
				// if no anchor link, open the 1st post
				window.anchors.stop();
				that.toggle(that.posts[0]);
				
			}
			
		});
		
	},
	
	openNextPost: function(){
		if(!this.posts.opening){
        	if(this.posts[this.posts.current+1]){
        		this.open(this.posts[this.posts.current+1]);
        	}else if(this.nav.next){
        		window.fx.toElement(this.nav.next);
        		this.nav.next.set('text', 'Loading next page...');
        		window.location = this.nav.next.get('href');
        	}else{
        		window.location = '/'
        	}
		}
	},
	
	addFBLike: function(post){
		post.fblike = new Element('iframe', {		 
		    src: 'http://www.facebook.com/plugins/like.php?href='+ post.readMore[0].get('href') +'&layout=button_count&show_faces=false&width=350&action=like&font=arial&colorscheme=light&height=21',
		    styles: {
		        width: 350,
		        height: 21,
		        border: 'none',
		        overflow: 'hidden'
		    },
		    allowtransparency: 'true',
		    scrolling: 'no',
		    frameborder: '0'
		});
		post.fblike.inject(post.more[1].getElement('.fb-like'));
	},
	
	startSlideshow: function(){
		var that = this;
		
		this.slideshow.on = true;
		this.slideshow.cookie = Cookie.write('slideshow', 'true', {
			domain: window.uri.parsed.host,
			path: '/'
		});
		this.slideshow.sprite.periodical = this.slideshow.sprite.nextFrame();
		this.resumeSlideshow();
	},
	
	stopSlideshow: function(){
		var that = this;
		
		this.slideshow.on = false;
		this.slideshow.cookie = Cookie.write('slideshow', 'false', {
			domain: window.uri.parsed.host,
			path: '/'
		});
		this.pauseSlideshow();
		this.slideshow.sprite.frame = 1;
		this.slideshow.sprite.setStyle('background-position', '0 0');	
	},
	
	pauseSlideshow: function(){
		var that = this;
		
		$clear(this.slideshow.periodical);
		$clear(this.slideshow.sprite.periodical);
	},
	
	resumeSlideshow: function(){
		var that = this;
		
		this.slideshow.periodical = this.openNextPost.periodical(6000, that);
		this.slideshow.sprite.periodical = this.slideshow.sprite.nextFrame.periodical(1000);
	},
	
	toggleSlideshow: function(e){
		var that = this;
		
		if(e) e.preventDefault();
		
		if(this.slideshow.on){
			this.stopSlideshow();
		}else{
			this.startSlideshow();
		}
	
	},
	
	openPrevPost: function(){
		if(!this.posts.opening){
        	if(this.posts[this.posts.current-1]){
        		this.open(this.posts[this.posts.current-1]);
        	}else if(this.nav.prev){
        		window.location = this.nav.prev.get('href');
        		window.fx.toTop();
        		this.nav.prev.set('text', 'Loading previous page...');
        	}
    	}
	},
	
	setUpPost: function(post, index){
		var that = this;

		post.index = index;
		post.open = false;
		post.colors = {};
		post.size = post.getSize();
		post.addClass('closed');
		
		post.fx = new Fx.Morph(post, {
			link: 'cancel',
			transition: Fx.Transitions.Quad.easeInOut
		});
		
		post.category = post.getElement('.categories strong');
		
		if(post.getElement('.read-more a')) post.readMore = post.getElements('.read-more a');
		if(post.readMore){
			post.readMore.each(function(readMore){
				readMore.set({
					events: {
						'mouseenter': function(){
							this.addClass('hover');
						},
						'mouseleave': function(){
							this.removeClass('hover');
						}
					}
				});			
			});
		}
		
		post.heading = post.getElement('h2.title');
		if(post.heading && post.readMore){
			post.heading.set({
				events: {
					'mouseenter': function(){
						this.addClass('hover');
					},
					'mouseleave': function(){
						this.removeClass('hover');
					},
					'click': function(){
						if(post.open){
							window.location = post.readMore[0].get('href');
						}
					}
				}
			});		
		}
		
		post.summary = post.getElement('.summary');
		post.summary.setStyle('height', 'auto');
		post.summary.h2 = post.summary.getElement('h2');
		post.summary.fx = new Fx.Morph(post.summary, {
			link:'cancel',
			transition: Fx.Transitions.Quad.easeInOut
		});
		post.summary.set({
			'events': {
				'mouseenter': function(){
					post.addClass('hover');
					if(window.css3 && that.posts.open != post){
						post.summary.temp = post.summary.getStyle('background-color');
						post.summary.setStyle('background-color', post.colors.summary);
					}
				},
				'mouseleave': function(){
					post.removeClass('hover');
					if(window.css3 && that.posts.open != post) post.summary.setStyle('background-color', post.summary.temp);
				},
				'click': function(){
					if(post.open && post.heading && post.heading.hasClass('hover') || post.open && post.readMore && post.readMore[0].hasClass('hover') || post.open && post.readMore && post.readMore[1].hasClass('hover')){
						// do nothing
					}else{
						that.toggle(post);
					}
				}
			}
		});
		
		post.img = post.getElement('.img');
		
		// if flash content is present, prepare it
		post.img.flash = false;
		if(post.img.getElement('input.bg-flash')){
			post.img.flash = post.img.getElement('input.bg-flash');
			post.img.src = post.img.getElement('input.bg-img-flash');
		}else{
			post.img.src = post.img.getElement('input.bg-img');
		}
		
		post.img.getElement('input.bg-color') ? post.colors.bg = post.img.getElement('input.bg-color').get('value') : post.colors.bg = that.options.colors.bg;
		post.img.getElement('input.list-bg') ? post.colors.summary = post.img.getElement('input.list-bg').get('value') : post.colors.summary = that.options.colors.summary;
		post.img.getElement('input.footer-bg') ? post.colors.footer = post.img.getElement('input.footer-bg').get('value') : post.colors.footer = that.options.colors.footer;
		
		post.more = [];
		post.getElements('.more').each(function(more, index2){
			more.size = more.measure(function(){
			    return this.getSize();
			});
			more.fx = new Fx.Morph(more, {
				link:'cancel',
				transition: Fx.Transitions.Quad.easeInOut
			});
			more.setStyles({
				'height': 0,
				'display': 'block'
			});
			post.more[index2] = more;
		});
	
	},
	
	toggle: function(post){
		var that = this;
	
		if(!post.open){
			this.open(post);
		}
		
		// can put more intelligence here later if needed
	},
		
	open: function(post){
		var that = this;
		
		post.open = true;
		this.posts.opening = true;
		this.posts.allClosed = false;
		this.posts.current = post.index;
		post.addClass('open');
		post.removeClass('closed');
		
		// if this is the first post, hide the up button
    	if(!this.nav.prev && !this.posts[that.posts.current-1]){
			this.up.setStyle('display', 'none');
    	}else{
    		this.up.setStyle('display', 'block');
    	}
    	
		// if this is the last post, hide the down button
		if(!this.nav.next && !this.posts[that.posts.current+1]){
			this.down.setStyle('display', 'none');
    	}else{
    		this.down.setStyle('display', 'block');
    	}
    	
    	// pause slideshow to wait for image load
    	if(this.slideshow.on) this.pauseSlideshow();
    	
    	// if post has a Facebook Like button, add it
    	if(post.more[1].getElement('.fb-like')){
    		if(!post.fblike) this.addFBLike(post);
    	}
		
		// if post has an image, load it via ajax
		if(post.img.src){
			var img = new Asset.images(post.img.src.get('value'), {
			    onComplete: function(){
			        post.img.setStyle('background-color', post.colors.bg);
			        post.img.getElement('.background').setStyle('background',  'url('+ post.img.src.get('value') +') center top no-repeat');
			        if(that.slideshow.on) that.resumeSlideshow();
			    }
			});
		}
		
		// if post has flash, load it and set it up
		if(post.img.flash){
			var flash = new Swiff(post.img.flash.get('value'), {
			    width: 1280,
			    height: 450,
			    container: post.img.getElement('.flash-content')
			});
			post.img.getElement('.flash-content').setStyle('position', 'relative'); // <-- avoids IE7 flash underlay bug
			post.img.setStyle('background-color', post.colors.bg);
		}
		
		// calculate future position of post
		var sumPos = post.summary.getPosition($('page')).y;
		
		var prevSize;
		this.posts.open ? prevSize = this.posts.open.getSize().y : prevSize = 0;
		
		var imgHeight = post.more[0].size.y;
		var offset = 9 + this.options.logoSize.y;

		var pos;
		if(this.posts.open && this.posts.open.index < post.index){
			// next post is after currently open one
			pos = sumPos - (prevSize - this.posts.open.size.y) + imgHeight - (offset);
		}else if(!this.posts.open || this.posts.open && this.posts.open.index > post.index){
			// next post is before currently open one
			pos = sumPos + imgHeight - offset;
		}
		
		// calculate future position of sidebar
		var sidebarBottom = pos + window.sidebar.sidebar.size.y;
		
		// calculate difference in size between current and future summary areas
		var openSumSize;
		this.posts.open ? openSumSize = this.posts.open.more[1].size.y : openSumSize = 0;
		var sizeDiff = openSumSize - post.more[1].size.y;
		if(sizeDiff < 0) sizeDiff = 0;
		
		// calculate future position of footer
		var footerPos = this.footer.getPosition($('page')).y - this.footer.offset - sizeDiff - 20;
		if(!this.posts.open) footerPos = footerPos + imgHeight + post.more[1].size.y - 30;
		
		// footer dodges sidebar to prevent overlap
		if(sidebarBottom > footerPos){
			this.footer.offset = sidebarBottom - 1500;
		}else{
			this.footer.offset = 0;
		}
		
		// move the footer
		if(Browser.Engine.trident || window.css3){
			this.footer.setStyles({
				'-webkit-transition': 'all 0.5s ease',
				'-moz-transition': 'all 0.5s ease',
				'-o-transition': 'all 0.5s ease',
				'transition': 'all 0.5s ease'
			});
			this.footer.fx.set({
				'top': this.footer.offset,
				'background-color': post.colors.footer
			});
		}else{
			this.footer.fx.start({
				'top': this.footer.offset,
				'background-color': post.colors.footer
			});
		}
		
		// scroll sidebar to post
		if(window.css3){
			window.sidebar.sidebar.setStyles({
				'-webkit-transition': 'all 0.5s ease',
				'-moz-transition': 'all 0.5s ease',
				'-o-transition': 'all 0.5s ease',
				'transition': 'all 0.5s ease'
			});
			window.sidebar.sidebar.fx.set({'top': pos});
		}else{
			window.sidebar.sidebar.fx.start({'top': pos});
		}
		
		// scroll window to post
		window.fx.start(0, pos - (imgHeight - offset - 1)).chain(function(){
			//window.fireEvent('scroll'); // for iPhone
			var frag;
			var id = post.getElement('div[class^=post-]').get('id');
			
			if($('body').hasClass('home') || $('body').hasClass('search-results')){
				frag = '/' + post.category.get('class') + '/' + id + '/';
			}else{
				frag = '/' + id + '/';
			}
			window.uri.set('fragment', frag);
			window.uri.go();
			_gaq.push(['_trackPageview', '/#'+frag]);
			that.current = post.get('id');
			if(!Browser.Engine.trident4 && !Browser.Engine.trident5) window.anchors.start();
		});
		
		// scroll bg shadow into new pos
		if(window.css3){
			this.shadow.setStyles({
				'-webkit-transition': 'top 0.5s ease',
				'-moz-transition': 'top 0.5s ease',
				'-o-transition': 'top 0.5s ease',
				'transition': 'top 0.5s ease'
			});
			this.shadow.fx.set({'top': pos});
		}else{
			this.shadow.fx.start({'top': pos});
		}
		
		// open all 'more' content
		post.more.each(function(more){
			if(window.css3){
				more.setStyles({
					'-webkit-transition': 'height 0.5s ease',
					'-moz-transition': 'height 0.5s ease',
					'-o-transition': 'height 0.5s ease',
					'transition': 'height 0.5s ease'
				});
				more.fx.set({'height': more.size.y});
			}else{
				more.fx.start({'height': more.size.y});
			}
		});
		
		// change colors
		[this.up, this.down, this.slideshow].each(function(button){
			if(window.css3){
				button.setStyles({
					'-webkit-transition': 'all 0.5s ease',
					'-moz-transition': 'all 0.5s ease',
					'-o-transition': 'all 0.5s ease',
					'transition': 'all 0.5s ease'
				});
				button.fx.set({'background-color': post.colors.summary});
			}else{
				button.fx.start({'background-color': post.colors.summary});
			}
		});
		
		if(window.css3){
			[post.summary.h2, post.summary].each(function(el){
				el.setStyles({
					'-webkit-transition': 'all 0.5s ease',
					'-moz-transition': 'all 0.5s ease',
					'-o-transition': 'all 0.5s ease',
					'transition': 'all 0.5s ease'
				});
			});
			post.summary.fx.set({
				'margin-top': -30,
				'background-color': /* post.colors.summary */ '#eeeeee'
			});			
			// can't chiain with CSS3...
			var done = function(){
				that.posts.opening = false;
				post.summary.setStyle('display', 'block');
			}
			done.delay(500);
		}else{
			post.summary.fx.start({
				'margin-top': -30,
				'background-color': /* post.colors.summary */ '#eeeeee'
			}).chain(function(){
				that.posts.opening = false;
				post.summary.setStyle('display', 'block');
			});
		}
		
		post.summary.h2.setStyle('color', post.colors.summary);
		
		// close the other posts
		this.posts.each(function(post2, index){
			if(index !== post.index && post2.open){
				that.close(post2, post.colors);
			}else if(index !== post.index && !post2.open){
				if(window.css3 || Browser.Engine.trident){
					post2.summary.setStyles({
						'-webkit-transition': 'all 0.5s ease',
						'-moz-transition': 'all 0.5s ease',
						'-o-transition': 'all 0.5s ease',
						'transition': 'all 0.5s ease'
					});
					post2.summary.fx.set({'background-color': post.colors.summary})
				}else{
					post2.summary.fx.start({'background-color': post.colors.summary});
				}
			}
		});
		
		this.nav.each(function(nav){
			nav.fx.start({'background-color': post.colors.summary});
		});
		
		this.posts.open = post;
	},
	
	close: function(post, colors){
		var that = this;
		
		post.open = false;
		post.removeClass('open');
		post.addClass('closed');
		post.more.each(function(more){
			window.css3 ? more.fx.set({'height': 0}) : more.fx.start({'height': 0});
		});
		post.summary.h2.setStyle('color', this.options.colors.h2);
		var color;
		this.posts.allClosed ? color = this.options.colors.summary : color = colors.summary;
		
		if(window.css3){
			post.summary.fx.set({
				'margin-top': 0,
				'background-color': color
			});			
			// can't chiain with CSS3...
			var done = function(){
				if(post.img.flash) post.img.getElement('.flash-content').empty();
			}
			done.delay(500);		
		}else{
			post.summary.fx.start({
				'margin-top': 0,
				'background-color': color
			}).chain(function(){
				if(post.img.flash) post.img.getElement('.flash-content').empty();
			});
		}
		
		this.nav.each(function(nav){
			nav.fx.start('background-color', color);
		});
		
	}

});

var Sidebar = new Class({

	initialize: function(){
		var that = this;
		
		this.sidebar = $('sidebar');
		this.sidebar.margin = this.sidebar.getStyle('top').toInt();
		this.sidebar.size = this.sidebar.getSize();
		this.sidebar.up = this.sidebar.getElement('#up');
				
		this.sidebar.fx = new Fx.Morph(this.sidebar, {
			link:'cancel',
			transition: Fx.Transitions.Quad.easeInOut
		});
		
		this.sidebar.pos = this.sidebar.getPosition($('page'));
		
	}

});





/*
---
description: A Class that provides a cross-browser history-management functionaility, using the browser hash to store the application's state

license: MIT-style

authors:
- Arieh Glazer
- Dave De Vos
- Digitarald

requires:
- core/1.2.4: Class
- core/1.2.4: Class.Extras
- core/1.2.4: Element

provides: [HashListner]

...
*/

var HashListener = new Class({
	Implements : [Options,Events],
	options : {
		blank_page : 'blank.html',
		start : false
	},
	iframe : null,
	currentHash : '',
	firstLoad : true,
	handle : false,
	useIframe : (Browser.Engine.trident && (typeof(document.documentMode)=='undefined' || document.documentMode < 8)),
	ignoreLocationChange : false,
	initialize : function(options){
		var s_f, 
			self=this,
			cached_storage;
			
		this.setOptions(options);
		
		// Disable Opera's fast back/forward navigation mode
		if (Browser.Engine.presto && window.history.navigationMode) {
			window.history.navigationMode = 'compatible';
		}

		
		 // IE8 in IE7 mode defines window.onhashchange, but never fires it...
        if (
			window.onhashchange &&
            (typeof(document.documentMode) == 'undefined' || document.documentMode > 7)
		   ){
				// The HTML5 way of handling DHTML history...
				window.onhashchange = function () {
					var hash = self.getHash();
					if (hash == self.currentHash) {
						return;
					}
					self.fireEvent('hash-changed',hash);
				}
        } else  {
			if (this.useIframe){
				this.initializeHistoryIframe();
			} 
        } 
		
		window.addEvent('unload', function(event) {
			self.firstLoad = null;
		});
		
		if (this.options.start) this.start();
	},
	initializeHistoryIframe : function(){
		var hash = this.getHash(), self = this, doc;
		this.iframe = new IFrame({
			src		: this.options.blank_page,
			styles	: { 
				'position'	: 'absolute',
				'top'		: 0,
				'left'		: 0,
				'width'		: '1px', 
				'height'	: '1px',
				'visibility': 'hidden'
			}
		}).inject(document.body);
    	
		doc	= (this.iframe.contentDocument) ? this.iframe.contentDocumnet  : this.iframe.contentWindow.document;
		doc.open();
		doc.write('<html><body id="state">' + hash + '</body></html>');
		doc.close();
		return;
	},
	checkHash : function(){
		var hash = this.getHash(), ie_state, doc;
		if (this.ignoreLocationChange) {
			this.ignoreLocationChange = false;
			return;
		}

		if (this.useIframe){
			doc	= (this.iframe.contentDocument) ? this.iframe.contentDocumnet  : this.iframe.contentWindow.document;
			ie_state = doc.body.innerHTML;
			
			if (ie_state!=hash){
				this.setHash(ie_state);
				hash = ie_state;
			} 
		}		
		
		if (this.currentLocation == hash) {
			return;
		}
		
		this.currentLocation = hash;
		
		this.fireEvent('hash-changed',hash);
	},
	setHash : function(newHash){
		window.location.hash = this.currentLocation = newHash;
		
		this.fireEvent('hash-changed',newHash);
	},
	getHash : function(){
		var m;
		if (Browser.Engine.geko){
			m = /#(.*)$/.exec(window.location.href);
			return m && m[1] ? m[1] : '';
		}else{
			return window.location.hash.substr(1);
		}
	},
	setIframeHash: function(newHash) {
		var doc	= (this.iframe.contentDocument) ? this.iframe.contentDocumnet  : this.iframe.contentWindow.document;
		doc.open();
		doc.write('<html><body id="state">' + newHash + '</body></html>');
		doc.close();
		
	},
	updateHash : function (newHash){
		if ($type(document.id(newHash))) {
			this.debug_msg("Exception: History locations can not have the same value as _any_ IDs that might be in the document,"
					 	+ " due to a bug in IE; please ask the developer to choose a history location that does not match any HTML"
						+ " IDs in this document. The following ID is already taken and cannot be a location: " + newLocation);
		}
		
		this.ignoreLocationChange = true;
		
		if (this.useIframe) this.setIframeHash(newHash);
		else this.setHash(newHash);
	},
	start : function(){
		this.handle = this.checkHash.periodical(100, this);
	},
	stop : function(){
		$clear(this.handle);
	}
});
