var relatedTitles = new Array();
var relatedTitlesNum = 0;
var relatedUrls = new Array();

function related_results_labels(json) {
	for (var i = 0; i < json.feed.entry.length; i++) {
		var entry = json.feed.entry[i];
		relatedTitles[relatedTitlesNum] = entry.title.$t;
		for (var k = 0; k < entry.link.length; k++) {
			if (entry.link[k].rel == 'alternate') {
				relatedUrls[relatedTitlesNum] = entry.link[k].href;
				relatedTitlesNum++;
				break;
			}
		}
	}
}

function removeRelatedDuplicates() {
	var tmp = new Array(0);
	var tmp2 = new Array(0);
	for(var i = 0; i < relatedUrls.length; i++) {
		if(!contains(tmp, relatedUrls[i])) {
			tmp.length += 1;
			tmp[tmp.length - 1] = relatedUrls[i];
			tmp2.length += 1;
			tmp2[tmp2.length - 1] = relatedTitles[i];
		}
	}
	relatedTitles = tmp2;
	relatedUrls = tmp;	
}
	
function contains(a, e) {
	for(var j = 0; j < a.length; j++) if (a[j]==e) return true;
	return false;
}

function printRelatedLabels() {
	var r = Math.floor((relatedTitles.length - 1) * Math.random());
	var i = 0;
	document.write('<ul>');
	while (i < relatedTitles.length && i < 20) {
		document.write('<li><a href="' + relatedUrls[r] + '">' + relatedTitles[r] + '</a></li>');
		if (r < relatedTitles.length - 1) {
			r++;
		} else {
			r = 0;
		}
		i++;
	}
document.write('</ul>');
}

var scrollSpeed = 100;
var step = 1;
var current = 0;
var imageWidth = 2247;
var headerWidth = 560;		
var restartPosition = -(imageWidth - headerWidth);

function scrollBg(){
	current -= step;
	if (current == restartPosition){
		current = 0;
	}
	$('#animate-area').css("background-position",current+"px 0");
}

var init = setInterval("scrollBg()", scrollSpeed);



Number.random = function(min, max){
			return Math.floor(Math.random() * (max - min + 1) + min);
		};

		var zeros = {x:0, y:0, z:0};

		jQuery.extend(jQuery.fn, {

			scatter: function(){
				return this.translate({
					x: Number.random(-1000, 1000),
					y: Number.random(-1000, 1000),
					z: Number.random(-500, 500)
				}).rotate({
					x: Number.random(-720, 720),
					y: Number.random(-720, 720),
					z: Number.random(-720, 720)
				});
			},

			unscatter: function(){ 
				return this.translate(zeros).rotate(zeros);
			},

			frighten: function(d){
				var self = this;
				this.setTransition('timing-function', 'ease-out').scatter();
				setTimeout(function(){
					self.setTransition('timing-function', 'ease-in-out').unscatter();
				}, 500);
				return this;
			},

			zoom: function(delay){
				var self = this;
				this.scale(0.01);
				setTimeout(function(){
					self.setTransition({
						property: 'transform',
						duration: '250ms',
						'timing-function': 'ease-out'
					}).scale(1.2);
					setTimeout(function(){
						self.setTransition('duration', '100ms').scale(1);
					}, 250)
				}, delay);
				return this;
			},

			makeSlider: function(){
				return this.each(function(){
					var $this = $(this),
						open = false,
						next = $this.next(),
						height = next.attr('scrollHeight'),
						transition = {
							property: 'height',
							duration: '500ms',
							transition: 'ease-out'
						};
					next.setTransition(transition);
					$this.bind('click', function(){
						next.css('height', open ? 0 : height);
						open = !open;
					});
				})
			},

			fromChaos: (function(){
				var delay = 0;
				return function(){
					return this.each(function(){
						var element = $(this);
						//element.scatter();
						setTimeout(function(){
							element.setTransition({
								property: 'transform',
								duration: '500ms',
								'timing-function': 'ease-out'
							});
							setTimeout(function(){
								element.unscatter();
								element.bind({
									mouseenter: jQuery.proxy(element.frighten, element),
									touchstart: jQuery.proxy(element.frighten, element)
								});
							}, delay += 100);
						}, 1000);
					})
				}
			}())

		});
		
		
		// When the DOM is ready...
		$(document).ready(function() {
			
			// Get the proper CSS prefix
			var cssPrefix = false;
			if(jQuery.browser.webkit) {
				cssPrefix = "webkit";
			}
			else if(jQuery.browser.mozilla) {
				cssPrefix = "moz";
			}
			else if(jQuery.browser.opera) {
				cssPrefix = "webkit";
			}
			else if(jQuery.browser.msie) {
				cssPrefix = "ms";
			}
				
			// If we support this browser
			if(cssPrefix) {
				var cols = 15; // Desired columns
				var rows = 15; // Desired rows
				var totalWidth = 700; // Logo width
				var totalHeight = 698; // Logo height
				var singleWidth = Math.ceil(totalWidth / cols); // Shard width
				var singleHeight = Math.ceil(totalHeight / rows); // Shard height
				
				// Remove the text and background image from the logo
				var logo = jQuery("#homeLogo").css("backgroundImage","none").html("");
				
				// For every desired row
				for(x = 0; x < rows; x++) {
					var last;
					//For every desired column
					for(y = 0; y < cols; y++) {
						// Create a SPAN element with the proper CSS settings
						// Width, height, browser-specific CSS
						last = jQuery("<span />").attr("style","width:" + (singleWidth) + "px;height:" + (singleHeight) + "px;background-position:-" + (singleHeight * y) + "px -" + (singleWidth * x) + "px;-" + cssPrefix + "-transition-property: -" + cssPrefix + "-transform; -" + cssPrefix + "-transition-duration: 200ms; -" + cssPrefix + "-transition-timing-function: ease-out; -" + cssPrefix + "-transform: translateX(0%) translateY(0%) translateZ(0px) rotateX(0deg) rotateY(0deg) rotate(0deg);");
						// Insert into DOM
						logo.append(last);
					}
					// Create a DIV clear for row
					last.append(jQuery("<div />").addClass("clear"));
				}
				
				// Chaos!
				jQuery("#homeLogo span").fromChaos();
				
			}
		
		
		});


 
	$(document).ready(function() {
			moveBgAround();
	});
		
		function moveBgAround() {
			var x = Math.floor(Math.random()*401);
			var y = Math.floor(Math.random()*401);
			var time = Math.floor(Math.random()*1001) + 2000;
		
			$('.scrollBg').animate({
				backgroundPosition: '(' + (x * -1) + 'px ' + (y * -1) + 'px)'
			}, time, "swing", function() {
				moveBgAround();
			});
		}


var TabbedContent = {
	init: function() {	
		$(".tab_item").mouseover(function() {
		
			var background = $(this).parent().find(".moving_bg");
			$(background).stop().animate({
				left: $(this).position()['left']
			}, {
				duration: 300
			});
			
			TabbedContent.slideContent($(this));
			
		});
	},
	
	slideContent: function(obj) {
		var margin = $(obj).parent().parent().find(".slide_content").width();
		margin = margin * ($(obj).prevAll().size() - 1);
		margin = margin * -1;
		$(obj).parent().parent().find(".tabslider").stop().animate({
			marginLeft: margin + "px"
		}, {
			duration: 300
		});
	}
}

$(document).ready(function() {
	TabbedContent.init();
});


