/*
 *
 *	jQuery Timer plugin v0.1
 *		Matt Schmidt [http://www.mattptr.net]
 *
 *	Licensed under the BSD License:
 *		http://mattptr.net/license/license.txt
 *
 */
jQuery.timer=function(interval,callback)
{var interval=interval||100;if(!callback)
return false;_timer=function(interval,callback){this.stop=function(){clearInterval(self.id);};this.internalCallback=function(){callback(self);};this.reset=function(val){if(self.id)
clearInterval(self.id);var val=val||100;this.id=setInterval(this.internalCallback,val);};this.interval=interval;this.id=setInterval(this.internalCallback,this.interval);var self=this;};return new _timer(interval,callback);};

$(document).ready(function() {

	/* hide/show labels */
	$('#search input:not(:submit)').each(function() {
		if($(this).attr('value') != '') {
			$(this).parent().addClass('on');
		}
	}).focus(function() {
		$(this).parent().addClass('on');
	}).blur(function() {
		if($(this).attr('value') == '') {
			$(this).parent().removeClass('on');
		}
	});

	/* homepage portfolio */
	$('#home #portfolio li:first-child').addClass('on');
	Array.prototype.removeValue = function (str) {
		var arr = this;
		for (var i = 0; i < this.length; i++) {
			if (arr[i] === str) {
				arr = arr.slice(0, i).concat(arr.slice(i+1));
			}
		}
		return arr;
	};
	

	function makeNewGrid(items, i) {
		
		var i = parseInt(i, 10);
		
		var grid = {};
		for (var j = 0; j < 15; j++) {
			grid[j] = true;
		}
		
		if (i % 5 == 4) {
			i -= 1;
		}
		
		if (i >= 10) {
			i -= 5;
		}
		
		
		grid[i] = false;
		grid[i+1] = false;
		grid[i+5] = false;
		grid[i+6] = false;
		
		var arr = [];
		for (var j in grid) {
			if (grid.hasOwnProperty(j) && grid[j] === true) {
				arr.push(parseInt(j, 10));
			}
		}
		
		return [i, arr];
	};
	
	function pos (el, i) {
		$(el).css({
			'top': 90 * Math.floor(i / 5),
			'left': 130 * (i%5)
		});
	}
	
	var positionItems = function (i) {
		
		var items = $('#portfolio li');
		
		var grid = makeNewGrid(items, i);
		
		items.filter('[data-list="' + i + '"]').addClass('on').siblings().removeClass('on');
		
		items.not('.on').each(function (j) {
			$(this).attr('data-list', grid[1][j]);
		});
		
		items.filter('.on').each(function () {
			$(this).attr('data-list', grid[0]);
		});
		
		for (var i = 0; i < 15; i++) {
			var item = items.filter('[data-list="' + i + '"]');
			if (item) {
				
				pos(item, i);
			}
		}
		
	};
	
	$('#portfolio').css('position', 'relative');
	$('#portfolio li:gt(11)').css({'top':'0','left':'-99999px'});
	$('#portfolio li').css('position', 'absolute').each(function (i) {
		$(this).find('a').live('click', function(event) {
			positionItems($(this).parent().attr('data-list'));
			return false;
		});
	});
	
	positionItems(0);
	$('#home #portfolio .box').attr("title","Opens new window").live('click', function() {
		theLink = $(this).parent().find('a').attr('href');
		if(theLink) {
			window.open(theLink);
		}
	});
	numRand = 0;
	$.timer(3000, function (timer) {
		if(numRand > 11) { numRand = 0; }
		var itemNo=["4","2","11","9","8","1","5","7","10","6","3","0"];
		$('#home #portfolio li:eq(' + itemNo[numRand] + ') a.thumb').click();
		$('#home #portfolio').live('mouseover mouseout', function(event) {
			if (event.type == 'mouseover') {
				timer.stop();
			} else {
				timer.reset(3000);
			}
		});
		numRand ++;
	});
	$.timer(9000, function (timer) {
		var numLow = 0;
		var numHigh = 11;
		var adjustedHigh = (parseFloat(numHigh) - parseFloat(numLow)) + 1;
		var numRand = Math.floor(Math.random()*adjustedHigh) + parseFloat(numLow);
		var chosenItem = $('#portfolio li:not(.on):eq(' + numRand + ')').html();
		$('#portfolio ul').append('<li style="top: 0; left: -99999px;">' + chosenItem + '</li>');
		var itemToMove = $('#portfolio li:eq(12)').html();
		$('#portfolio li:not(.on):eq(' + numRand + ')').html(itemToMove);
		$('#portfolio li:eq(12)').remove();
		$('#home #portfolio').live('mouseover mouseout', function(event) {
			if (event.type == 'mouseover') {
				timer.stop();
			} else {
				timer.reset(9000);
			}
		});
	});

	/* our awards */
	$('#content .our-awards ul li img').reflect({'height': '45', 'opacity': '0.15'}).mouseover(function() {
		description = $(this).attr('title');
		$(this).parent().parent().append('<span class="description"><span>' + description + '</span></span>');
	}).mouseout(function() {
		$('#content .our-awards ul li span.description').remove();
	});

	/* key points */
	$('#content .key-points:eq(0), #content .key-points .right ul li:first-child').addClass('first');

	/* sectors */
	$('#content ul.sectors li.item:odd').addClass('alt');
	$('#content ul.sectors li.item h2').mouseover(function() {
		$(this).parent().addClass('hover');
	}).mouseout(function() {
		$(this).parent().removeClass('hover');
	});

	/* our team */
	$('#content table.listing tbody tr').mouseover(function() {
		$(this).addClass('hover');
	}).mouseout(function() {
		$(this).removeClass('hover');
	}).click(function() {
		var theLink = $(this).find('th a').attr('href');
		window.location = theLink;
	});

	/* our team rollovers */
	$('#content ul.team-members li').mouseover(function() {
		$(this).addClass('hover');
		description = $(this).find('img').attr('alt');
		if(description != '') {
			$(this).append('<span class="description"><span>' + description + '</span></span>');
		}
	}).mouseout(function() {
		$(this).removeClass('hover');
		$('#content ul.team-members li span.description').remove();
	});

	/* open external links in new windows */
	$("a[href*='http://']:not([href*='"+location.hostname+"']),[href*='https://']:not([href*='"+location.hostname+"'])").addClass("external").attr("target","_blank").attr("title","Opens new window");
	$('#home #portfolio a.thumb').attr('title','');

	// email links
	$('#content img.wp-image-2021').click(function() {
		window.location = 'mailto:projects@dfjesprit.com';
	});

	// run google maps
	initialize();

});
