// config the pixel perfect control to safari
var safariPixelPerfect = {
	enabled: false,	// enable/disable pixel perfect screens
	configs: {
		alpha: 50, 	// % of transparency of the screen
		range: 1 	// how many pixels to up/down
	},
	initialCoords: {
		x: 202, 	// initial left coord for pixel perfect screens
		y: 375		// initial top coord for pixel perfect screens
	},
	screens: {		// these screens must be stored inside the pperfect folder
		home: 'home.jpg',
		agency: 'agency.jpg',
		references: 'references.jpg',
		jobs: 'jobs.jpg',
//		jobs: 'jobs-opened.jpg',
		press: 'press.jpg',
		contact: 'contact.jpg'
	}
};

$(document).ready(function() {
	$('a[rel=external]').attr('target', '_blank');
	if (navigator.platform.indexOf("iPhone") != -1) {
		try
		{
			addEventListener("load", function() { setTimeout("window.scrollTo(0, 1)", 500); }, false);
		}
		catch(e){}
	}
	
	
	$('a.to-top').bind('click tap', function() {
		$('html, body').animate({scrollTop: $($(this).attr('href')).offset().top}, 1000);
		return false;
	});
	
	attachPixelPerfect();
});

function Box(parent, selector, row) {
	this.parent = parent;
	this.element = $(selector);
	this.row = row;
	this.init();
	this.bindEvents();
	this.setupHeightTimer;
}

Box.prototype = {
		init: function() {
			//this.position = (this.getX() == 0) ? 'left' : 'right';
			this.position = (parseInt(this.element.css('right')) == 0) ? 'right' : 'left';
			this.default_values = {
				top: this.getY()+'px',
				width: this.getWidth()+'px',
				height: this.getHeight()+'px'
			};
		},
		getX: function() {
			return parseInt(this.element.css('left')) || 0;
		},
		getY: function() {
			return parseInt(this.element.css('top')) || 0;
		},
		getWidth: function() {
			return parseInt(this.element.css('width'));
		},
		getHeight: function() {
			return parseInt(this.element.css('height'));
		},
		getBottom: function() {
			return this.getY() + this.getHeight();
		},
		getImageBottom: function() {
			return this.getY() + parseInt(this.element.find('img').css('height'));
		},
		bindEvents: function() {
			var _this = this;
			this.element.click(function(){
				_this.parent.startAnimation(_this);
			});
		},
		reset: function() {
			this.element.css(this.default_values);
		}
};

function PressSidebar(selector) {
	this.element = $(selector);
	this.init();
}

PressSidebar.prototype = {
		init: function() {
			var _this = this;
			this.matrix = [];
			this.openedBox = [];
			var temp_list = [];
			var row = 0;
			
			this.element.find('.sidebar-item').each(function(i, item) {
				if (temp_list.length > 1) {
					_this.matrix.push(temp_list);
					temp_list = [];
					row+=1;
				}
				temp_list.push(new Box(_this, item, row));
			});
			
			_this.matrix.push(temp_list);
			this.animating = false;
		},
		
		startAnimation: function(box) {
			if (!this.animating) {
				this.animating = true;
				
				if (this.openedBox.length > 0) {
					this.resetAnimation();
				}
				
				if (this.openedBox.length == 0 || this.matrix[this.openedBox[0]][this.openedBox[1]] != box) {
					this.animate(box);
				}
				else {
					this.openedBox = [];
				}
				
				var _this = this;
				
				this.element.queue('animation', function() {
					_this.animating = false;
				});
				
				this.element.dequeue('animation');
			}
		},
		
		animate: function(box) {
			var _this = this;
			var sideBoxes = [];
			var side = (box.position == 'right') ? 0 : 1;
			var infoDiv = box.element.find('div.info');
			var margin = 7;
			var duration = 300;
			this.openedBox = [box.row, (box.position == 'right') ? 1 : 0];
			var previousBottom = box.getImageBottom();
			
			for (var i=box.row;i<this.matrix.length;i++) {
				sideBoxes.push(this.matrix[i][side]);
			}
			
			$('.press-sidebar').css('height', 1000);
			
			this.element.queue('animation', function(next) {//slide side boxes down
				$.each(sideBoxes, function(i, sideBox) {
					sideBox.element.animate({'top': (parseInt(sideBox.default_values.top) + parseInt(sideBox.default_values.height) + margin)+'px'}, duration, 'linear', function() {
						if (i==sideBoxes.length-1){
							next();
						}
					});

				});
			});
			
			this.element.queue('animation', function(next) {//grow box width
				previousBottom = box.getHeight();
				box.element.find('.sidebar-item-wrap').animate({'width': (box.getWidth()*2)+(margin-1)+'px'}, duration, 'linear', function() {
					next();
//					_this.setupHeight();
				});
			});
			
			this.element.queue('animation', function(next) {//grow box height and slide other boxes down
				sideBoxes = [];
				for (var i=box.row;i<_this.matrix.length;i++) {//slide boxes down
					for (var j=0;j<2;j++) {
						if (j == side || i != box.row){
							sideBoxes.push(_this.matrix[i][j]);
						}
//						_this.setupHeight();
					}
				}
				//console.log(parseInt(box.element.find('img').css('height')) + parseInt(infoDiv.css('height')));
				box.element.find('.sidebar-item-wrap').animate(
						{'height': parseInt(box.element.find('img').css('height')) + parseInt(infoDiv.css('height')) + 'px'},
						duration,
						function() {
							_this.setupHeight();
						}
					);
				
				$.each(sideBoxes, function(i, sideBox) {
					sideBox.element.animate({top: ((parseInt(box.element.find('img').css('height')) + parseInt(infoDiv.css('height')) + margin) + (sideBox.getY() - previousBottom)) + 'px' }, duration, 'linear', function() {
						if (i == sideBoxes.length - 1 ) {
							next();
						}
					});
				});
			});
			
		},
		
//		getFirstEl: function(elements) {
//			var top = $(elements.get(0)).position().top;
//			elements.each(function() {
//				if (top > $(this).position().top) {
//					top = $(this).position().top;
//				}
//			});
//			
//			return top;
//		},
		
		getLastEl: function(elements) {
			var veryLastEl = $(elements.get(elements.length - 1));
			var bottom = veryLastEl.position().top + veryLastEl.outerHeight();
			elements.each(function() {
				var el = $(this);
				var elPos = el.position().top + el.outerHeight();
				if (bottom < elPos) {
					bottom = elPos;
				}
			});
			
			return bottom;
		},
		
		setupHeight: function(ms) {
			var _this = this;
			var timeout = ms ? ms : 0;
			clearTimeout(this.setupHeightTimer);
			this.setupHeightTimer = setTimeout(function() {
				var els = _this.element.find('.sidebar-item');
				var lastEl = _this.getLastEl(els);
				$('.press-sidebar').css('height', lastEl);
			}, timeout);
		},
		
		resetAnimation: function() {
			var _this = this;
			var box = this.matrix[this.openedBox[0]][this.openedBox[1]];
			var sideBox;
			var side = (box.position == 'right') ? 0 : 1;
			var duration = 300;
			var sideBoxes = [];
			var bottom = box.getY() + box.getHeight();

			for (var i=box.row;i<_this.matrix.length;i++) {//slide boxes up
				for (var j=0;j<2;j++) {
					if (j == side || i != box.row){
						sideBox = _this.matrix[i][j];
						sideBoxes.push(sideBox);
					}
				}
			}
			
			this.element.queue('animation', function(next) {
				var first = true;
				box.element.find('.sidebar-item-wrap').animate({height: (parseInt(box.default_values.height) - 8) + 'px'}, {duration: duration, step: function() {
						if (first) {
							first = false;
							next();
						}
					}
				});
			});
			
			this.element.queue('animation', function(next) {
				$.each(sideBoxes, function(i, sideBox) {
					sideBox.element.animate({top: ((parseInt(box.default_values.top) + parseInt(box.default_values.height)) + (sideBox.getY() - bottom)) + 'px'}, duration, 'linear', function() {
						if (i == sideBoxes.length - 1)
							next();
					});
				});
			});
			
			this.element.queue('animation', function(next) {
				//box.element.find('.sidebar-item-wrap').animate({width: box.default_values.width}, duration, 'linear');
				box.element.find('.sidebar-item-wrap').animate({width: (parseInt(box.default_values.width) - 8) + 'px', height: (parseInt(box.default_values.height) - 8) + 'px'}, duration, 'linear', function() {
					next();
				});
			});
			
			this.element.queue('animation', function(next) {
				$.each(sideBoxes, function(i, sideBox) {
					sideBox.element.animate({top: sideBox.default_values.top}, duration, 'linear', function() {
						if (i == sideBoxes.length - 1)
							next();

						_this.setupHeight(1000);
					});
				});
			});
		}
};

var FeedParser = Class.extend({
	init: function(selector, count) {
		this.element = $(selector);
		this.count = count;
		this.data = [];
		this.load();
	},

	load: function() {

	},

	writeHTML: function() {
		var _this = this;
		_this.element.html('');
		$.each(this.data, function(i, item) {
			_this.element.append(
					'<dt>' + item.user + ' ­— ' + _this.formatDate(item.date) + '</dt>' +
					'<dd>' + _this.formatText(item.text) + '</dd>'
					);
					
			_this.element.find('dd:last a').attr('target', '_blank');
		});
	},

	formatDate: function(date) {
		return humanized_time_span(date);
	},

	formatText: function(text) {
		return text
	}
});

var TwitterFeed = FeedParser.extend({
	init: function(selector, count, username) {
		this.username = username;
		this.inherited().init(selector, count);
	},

	load: function() {
		var _this = this;
		$.getJSON("http://twitter.com/status/user_timeline/" + this.username + ".json?count=" + this.count + "&callback=?", function( data ) {
			$.each(data, function(i, tweet) {
				if ($.browser.msie) {
					tweet.created_at = Date.parse(tweet.created_at.replace(/( \+)/, ' UTC$1'));
		        }
	        	_this.data.push({
	        		date: tweet.created_at,
	        		user: tweet.user.name,
	        		text: tweet.text
	        	});
	        });
	        _this.writeHTML();
	    });
	},

	formatText: function(text) {
		// replace links
		var pattern = /(http:\/\/\S+)/ig;
		text = text.replace(pattern, '<a href="$1" target="_blank">$1</a>');

		// replace mentions
		text = text.replace(/(@)(\w+)/ig, '<a href="http://twitter.com/$2" target="_blank">$1$2</a>');

		// replace searches
		text = text.replace(/(#)(\w+)/ig, '<a href="http://search.twitter.com/search?q=%23$2" target="_blank">$1$2</a>');

		return text;
	}
	
});

var FacebookFeed = FeedParser.extend({
	load: function() {
		var _this = this;
		$.jGFeed(encodeURIComponent('http://www.facebook.com/feeds/page.php?id=130876673640902&format=rss20'),
		function(feeds) {
		  if(!feeds){
		    return false;
		  }

		  for(var i=0; i<feeds.entries.length; i++){
		    var entry = feeds.entries[i];
		    _this.data.push({
        		date: entry.publishedDate,
        		user: entry.author,
        		text: entry.content
        	});
		  }
		  _this.writeHTML();
		}, this.count);
	}
});

var BlogFeed = FeedParser.extend({
	init: function(selector, count, url) {
		this.url = url;
		this.inherited().init(selector, count);
	},
	load: function() {
		var _this = this;
		$.jGFeed(encodeURIComponent(this.url),
		function(feeds) {
		  if(!feeds){
		    return false;
		  }

		  for(var i=0; i<feeds.entries.length; i++){
		    var entry = feeds.entries[i];
		    _this.data.push({
        		date: entry.publishedDate,
        		title: entry.title,
        		text: entry.contentSnippet,
        		link: entry.link
        	});
		  }
		  _this.writeHTML();
//		  resizeListItems();
		}, this.count);
	},
	writeHTML: function() {
		var _this = this;
		_this.element.html('');
		$.each(this.data, function(i, item) {
			_this.element.append(
					'<li>' +
					'	<span class="blue-text">' + _this.formatDate(item.date) + '</span>' +
					'	<span class="break"><!-- --></span>' +
					'	<h3><a href="' + item.link + '" target="_blank">' + item.title + '</a></h3>' +
					'	<p>' + item.text + '</p>' +
					'</li>'
					);
		});
	},
	formatDate: function(date) {
		var date = new Date(date);
		var month_names = {'en':
						['January',
						'February',
						'March',
						'April',
						'May',
						'June',
						'July',
						'August',
						'September',
						'October',
						'November',
						'December'],
					'de':
						['January',
						'February',
						'March',
						'April',
						'May',
						'June',
						'July',
						'August',
						'September',
						'October',
						'November',
						'December']
		};

		return month_names[LANG][date.getMonth()] + ', ' + date.getDate() + ', ' + date.getFullYear();
	}
});

var Accordion = Class.extend({
	init: function(selector) {
		this.element = $(selector);
		this.opened = null;
		this.bindEvents();
	},
	bindEvents: function() {
		var _this = this;
		this.element.find('h3').click(function(ev) {
			ev.preventDefault();
			if (_this.opened){
				_this.opened.slideUp('slow', function(){
					$(this).hide();
				});
			}
			if (!_this.opened || !(_this.opened[0] == _this.getNext(this)[0])) {
				_this.opened = _this.getNext(this);
				_this.opened.slideDown('slow');
			} else {
				_this.opened = null;
			}
		});
	},
	getNext: function(el) {
		return $(el).next();
	}
});

var AccordionWithPreview = Accordion.extend({
	getNext: function(el) {
		return $(el).next().next();
	}
});

(function( $ ){

	$.fn.dropdown = function() {
		//this.find('dd ul li:first').before('<li class="first" style="display:none;">' + this.find('dt').html() + '</li>');
		this.find('dt').unbind('click');
		this.find('dt').click(function(ev) {
			ev.stopPropagation();
			var next = $(this).next();
			if (next.hasClass('dropdown-opened')){
				next.hide();
				next.removeClass('dropdown-opened');
			} else {
				var opened = $('dd.dropdown-opened');
				opened.removeClass('dropdown-opened');
				opened.hide();
				next.show();
				next.addClass('dropdown-opened');
			}
		});
		//this.find('dd ul li').unbind('click');
		this.find('dd ul li').click(function(ev) {
			//ev.stopPropagation();
			$(this).parents('dl').find('dt').html($(this).html());
			$(this).parents('dd').hide();
			$(this).parents('dd').removeClass('dropdown-opened');
		});
		$(document).click(function(){
			$(this).find('dd').hide();
			$(this).find('dd').removeClass('dropdown-opened');
		});
	};
})( jQuery );

var Filter = Class.extend({
	init: function(filter, list) {
		this.filter = $(filter);
		this.element = $(list);
		this.filters = [];
		this.observers = [];
		this.bindEvents();
		this.filterList([]); // Start with filter empty
	},

	bindEvents: function() {
		var _this = this;
		this.filter.find('dd ul li').click(function() {
			// We can have more than one class of filters
			var filter = $(this).parents('dl')[0].className.split(' ')[0];
			for (var i=0;i<_this.filters.length;i++) {
				if (_this.filters[i].match('^.'+filter)) {
					_this.filters.splice(i, 1);
				}
			}

			if (this.className != filter + '-all')
				_this.filters.push('.' + this.className);

			_this.filterList();
		});
	},

	filterList: function(filter) {
		var filters = this.filters.join('');
		if (filters != ''){
			this.element.children().removeClass('filter-pass');
			this.element.children().each(function(i, element) {
				if ($(element).is(filters)){
					$(element).addClass('filter-pass');
				}
			});
		} else {
			this.element.children().addClass('filter-pass');
		}
		this.notifyObservers();
	},

	registerObserver: function(obj) {
		this.observers.push(obj);
	},

	notifyObservers: function() {
		$.each(this.observers, function(idx, obj) {
			obj.filterCallback();
		});
	}
});

Paginator = Class.extend({
	init: function(items_container_selector, items_selector, controls_selector, items_per_page) {
		this.items_per_page = (typeof(items_per_page) === 'undefined') ? 10 : items_per_page;
		this.$items_container = $(items_container_selector);
		this.items_selector = items_selector;
		this.$controls_selector = $(controls_selector);
		this.$previous_button = this.$controls_selector.find('a.back');
		this.$next_button = this.$controls_selector.find('a.next');
		this.$paginate_links = this.$controls_selector.find('ul.paginate-links');

		this.reset();
		this.bindEvents();
	},

	bindEvents: function() {
		var _this = this;

		this.$previous_button.click(function (e) {
			_this.previous();
			e.preventDefault();
		});

		this.$next_button.click(function (e) {
			_this.next();
			e.preventDefault();
		});
	},

	reset: function() {
		this.page = 1;
		this.$items_container.find(' > *').each(function(i, item) {$(item).hide();})
		this.updateItems();
		this.showCurrentPage();
	},

	pagesTotal: function() {
		return Math.ceil(this.$items_cache.length / this.items_per_page);
	},

	updateItems: function() {
		this.$items_cache = this.$items_container.find(this.items_selector);
	},

	updateControls: function() {
		var _this = this;
		var pages_total = this.pagesTotal();
		if (pages_total > 1) {
			// Update page number links
			this.$paginate_links.children().remove();

			for (var i = 1; i <= pages_total; i++ ) {
				var html = this.getPageLinkHTML(i);

				var link = html.find('a');
				if (i === this.page) {
					link.addClass('current');
				} else {
					link.click(function(e) {
						_this.setCurrentPage($(this).text());
						e.preventDefault();
					});
				}

				this.$paginate_links.append(html);
			}

			// Update before / next page links
			if (this.page === this.pagesTotal()) {
				this.$next_button.parent().addClass('disable');
			} else {
				this.$next_button.parent().removeClass('disable');
			}

			if (this.page === 1) {
				this.$previous_button.parent().addClass('disable');
			} else {
				this.$previous_button.parent().removeClass('disable');
			}

			this.$controls_selector.show();
		} else {
			this.$controls_selector.hide();
		}
	},

	getPageLinkHTML: function(page) {
		var slash = (page === 1) ? '' : '/ ';
		return $('<li>'+ slash +'<a class="pagenumber">'+ page +'</a></li>')
	},

	previous: function() {
		if (this.page > 1) {
			this.page -= 1;
			this.showCurrentPage();
		}
	},

	next: function() {
		if (this.page < this.pagesTotal()) {
			this.page += 1;
			this.showCurrentPage();
		}
	},

	setCurrentPage: function(num) {
		num = parseInt(num);
		if (num <= this.pagesTotal() && num >= 1) {
			this.page = num;
			this.showCurrentPage();
		}
	},

	showCurrentPage: function() {
		var _this = this;
		this.updateControls();
		this.$items_cache.each(function(i, item) {
			if (_this.page === parseInt(i / _this.items_per_page) + 1) {
				$(item).show();
			} else {
				$(item).hide();
			}
		});
	},

	filterCallback: function() {
		this.reset();
	}
});

ShareItem = Class.extend({
	init: function(item, title_selector, text_selector, mounted_url) {
		this.$item = $(item);
		this.title = this.$item.find(title_selector).text();
		this.text  = this.$item.find(text_selector).text();
		this.bindEvents();
		this.mountedURL = mounted_url;
	},

	bindEvents: function() {
		var _this = this;
		_this.$item.find('a.twitter').click(function () {
			_this.shareByTwitter();
		});

		_this.$item.find('a.facebook').click(function () {
			_this.shareByFacebook();
		});

		_this.$item.find('a.email').click(function () {
			_this.shareByEmail();
		});
	},

	shareByTwitter: function() {
		var message = TWITTER_MESSAGE.replace('#{position}', this.title);//.replace('#{url}', this.mountUrl());
		var text = encodeURIComponent(message);
		this.showPopup('https://twitter.com/share?related=conradcaine&text=' + text + '&url=' + this.mountUrl());
	},

	shareByFacebook: function() {
		var link = encodeURIComponent(this.mountUrl());
		// Facebook ignores all params except url, then grab the data directly from the
		// page, so it will only work in production
		this.showPopup('http://www.facebook.com/sharer.php?u=' + link);
		
	},

	shareByEmail: function() {
		var subject = encodeURIComponent(this.title);
//		var message = encodeURIComponent(document.location.href +"\n\n "+ this.text);
		var message = EMAIL_MESSAGE.replace('#{position}', this.title).replace('#{url}', this.mountUrl());
		var mailto_link = 'mailto:?subject='+ subject +'&body='+ message;
		
		window.open(mailto_link, '_self');
	},

	showPopup: function(url) {
		var height = 436;
		var width = 626;
		var left = (screen.width - width) / 2;
		var top = (screen.height - height) / 2;
		var popup_params = 'toolbar=0, status=0, width=' + width + ', height='+ height +', left=' + left + ', top=' + top;

		window.open(url, 'sharer', popup_params);
	},
	
	mountUrl: function() {
		if (!this.mountedURL) {
			var url = document.location.href;
			var splitUrl = document.location.href.split('/');
			var kind = splitUrl[splitUrl.length - 1].replace('.html', '');
			var newUrl = document.location.href.replace(splitUrl[splitUrl.length - 1], kind + '/' + this.$item.attr('id') + '.html');
			
			return newUrl;
		}
		
		return document.location.href;
		
	}
});

ShareItem.createShareItems = function(items_selector, title_selector, text_selector, mounted_url) {
	$(items_selector).each(function (i, elem) {
		new ShareItem(elem, title_selector, text_selector, mounted_url);
	});
};

function refreshImage() {
	if (!refreshImageLoading) {
		refreshImageLoading = true;
		var img = new Image();
		img.src = 'http://test.conrad-caine.com/ccwebsite/ccrooftop.jpeg?' + Math.random();
		img.onload = function() {
			clearTimeout(refreshTimeout);
			homeBackground.attr('src', img.src);
			refreshTimeout = setTimeout(function() {
				refreshImage();
			}, 10000);
			refreshImageLoading = false;
		};
	}
}

var AnimatedBoxes = function(selector) {
	this.boxes = $(selector).children();
	this.selector = selector;
	this.originalSizes = {width: 128, height: 128};
	this.width = 267;
	this.init();
	this.currenElement;
}

AnimatedBoxes.prototype = {
	init: function() {
		this.bindEvents();
	},
	
	bindEvents: function() {
		var _this = this;
		this.boxes.each(function() {
			$(this).bind('click', function() {
				_this.resetAnimation($(this));
			});
		});
	},
	
	animate: function(currEl) {
		var _this = this;
		this.boxes.each(function() {
			var element = $(this);
			if (element.index() == currEl.index()) {
				if (element.index() % 2 == 0) {
					_this.currentElement = element;
					_this.currentElement.addClass('opened');
					_this.resizeWidth(_this.currentElement);
				}
				else {
					currEl.hide();
					_this.currentElement = currEl.clone().insertBefore(currEl.prev()).show();
					_this.currentElement.addClass('clone');
					_this.resizeWidth(_this.currentElement);
					_this.currentElement.bind('click', function() {
						_this.resetAnimation(_this.currentElement);
					});
				}
			}
		});
	},
	
	resetAnimation: function(currEl) {
		var _this = this;
		var opened = $('.opened');
		
		if (opened.length > 0) {
			this.resetHeight(opened, currEl);
		}
		else {
			this.animate(currEl);
		}
	},
	
	resizeWidth: function(element) {
		var _this = this;
		element.find('.sidebar-item-wrap').animate({width: this.width}, {
			complete: function() {_this.resizeHeight(element)},
			queue: true
		});
	},
	
	resizeHeight: function(element) {
		var _this = this;
		var height = element.find('.sidebar-item-wrap').find('img').height() + element.find('.sidebar-item-wrap .info').height();
		element.find('.sidebar-item-wrap').animate({height: height}, {
			complete: function() {
				$(this).parent().addClass('opened');
			},
			queue: false
		});
	},
	
	resetWidth: function(element, currEl) {
		var _this = this;
		element.find('.sidebar-item-wrap').animate({width: this.originalSizes.width}, {
			complete: function() {
				$('.clone').remove();
				currEl.removeClass('opened');
				_this.boxes.show();
				if (_this.currentElement.get(0) != currEl.get(0)) {
					_this.animate(currEl);
				}
			},
			queue: false
		});
	},
	
	resetHeight: function(element, currEl) {
		var _this = this;
		element.find('.sidebar-item-wrap').animate({height: this.originalSizes.height}, {
			complete: function() {
				_this.resetWidth(element, currEl);
			},
			queue: false
		});
	}
}

function resizeListItems() {
	var news = $('.column-news');
	var blogLis = $('.column-blog li');
	
	news.find('li').each(function(i) {
		var newsLi = $(this);
		var blogLi = $(blogLis[i]);
		var newsLiHeight = newsLi.height();
		var blogLiHeight = blogLi.height();
		
		var property = ($.browser.msie && parseInt($.browser.version) === 6) ? 'height' : 'min-height';
		
		if (newsLiHeight > blogLiHeight) {
			blogLi.css(property, newsLiHeight + 'px');
		} else {
			newsLi.css(property, blogLiHeight + 'px');
		}
	});
}

function attachPixelPerfect() {
	if (!safariPixelPerfect.enabled || !$.browser.safari) {
		return;
	}
	
	var contentClass = $('title').text();
	var page = contentClass === 'CONRAD CAINE' ? 'home' : contentClass.replace('CONRAD CAINE - ', '').toLowerCase();
	var screen = safariPixelPerfect.screens[page];

	var pperfect = $('<div class="pperfect" style="top:' + safariPixelPerfect.initialCoords.y + 'px; left:' + safariPixelPerfect.initialCoords.x + 'px; opacity:' + safariPixelPerfect.configs.alpha / 100 + ';"><img src="pperfect/' + screen + '"></div>');
	var controls = $('<div class="pperfect-controls"><strong>PP Controls</strong></div>');
	
	var upButton = $('<button class="pp-up">U</button>');
	upButton.bind('click', function() {
		pperfect.css('top', parseInt(pperfect.css('top')) - safariPixelPerfect.configs.range + 'px');
	});
	
	var downButton = $('<button class="pp-down">D</button>');
	downButton.bind('click', function() {
		pperfect.css('top', parseInt(pperfect.css('top')) + safariPixelPerfect.configs.range + 'px');
	});
	
	var leftButton = $('<button class="pp-left">L</button>');
	leftButton.bind('click', function() {
		pperfect.css('left', parseInt(pperfect.css('left')) - safariPixelPerfect.configs.range + 'px');
	});
	
	var rightButton = $('<button class="pp-right">R</button>');
	rightButton.bind('click', function() {
		pperfect.css('left', parseInt(pperfect.css('left')) + safariPixelPerfect.configs.range + 'px');
	});
	
	var toggleButton = $('<button class="pp-toggle">Disable</button>');
	toggleButton.bind('click', function() {
		var $this = $(this);
		
		if ($this.text() == 'Disable') {
			$this.text('Enable');
			
			controls.find('button').not($this).attr('disabled', 'disabled');
		} else {
			$this.text('Disable');
			
			controls.find('button').not($this).removeAttr('disabled');
		}
		
		pperfect.toggle();
	});
	
	controls.append(upButton)
			.append(leftButton)
			.append(rightButton)
			.append(downButton)
			.append(toggleButton);
	
	$('body').prepend(controls).prepend(pperfect);
}
