// non-js filter
if (document.documentElement) {
	document.documentElement.className += "js";
}
document.writeln('<style type="text/css">.js-hide, .js-dropdown { display: none; }</style>');

jQuery(document).ready(function(){
	jsToggle.init();	//Expand headers
	jsDropdown.init();	//Convert Dropdown
	jsCarousel.init();	//Load carousel photos
	
	socialTabs('#net');
	
	//is gmaps enabled?
	if (typeof Gmap !== 'undefined') Gmap.init('.gmap');
	
	/* to stop script errors with console calls when Firebug is not installed or running */
	var k = function(){},
		console = {log:k,debug:k,info:k,warn:k,error:k,assert:k,
			dir:k,dirxml:k,trace:k,group:k,groupCollapsed:k,groupEnd:k,
			time:k,timeEnd:k,profile:k,profileEnd:k,count:k};
	if (typeof window.console === 'undefined') window.console = console;
});

// Tab effect on the social asset
function socialTabs(divHolder) {
	// $(divHolder + ' div').addClass('js-hide');
	$(divHolder + ' div:first').removeClass('js-hide');
	$(divHolder + ' ul li:first').addClass('active');
	$(divHolder + ' ul li a').click(function(){
		$(divHolder + ' ul li').removeClass('active');
		$(this).parent().addClass('active');
		var currentTab = $(this).attr('href');
		$(divHolder + ' div').addClass('js-hide');
		$(currentTab).removeClass('js-hide');
		$(currentTab + ' div').removeClass('js-hide')
		return false;
	});
}

//Expand headers
var jsToggle = function() {
	var config = { showSpeed: 350, hideSpeed: 100 };
	
	function appendClickEvent() {
		$('.js-toggle').each(function() {
			if ($(this).parent('#clients').length < 1) appendToggleEvent(this);
			else appendClientsToggle(this);
		});
	};
	function appendToggleEvent(el) {
		$(el).click(function() {
			var targetEl = $(this).next();
			if (targetEl.css('display') == 'none') targetEl.stop(true, true).slideDown(config.showSpeed);
			else targetEl.stop(true, true).slideUp(config.hideSpeed);
			return false;
		});
	};
	function appendClientsToggle(el) {
		var targetEl = $(el).next();
		var hideList = $(el).outerHeight();
		var clientsList = targetEl.outerHeight();
		$(el).css({ position: 'absolute', bottom: 0 });
		targetEl.css({ position: 'relative', top: '-33px' });
		$('#container').css('margin-top', hideList + 'px');
		$('#clients').css({ position: 'absolute', top: 0, height: hideList + 'px' });
		$('#search').css({ top: '-' + hideList + 'px' });
		$(el).click(function() {
			if (targetEl.css('display') == 'none') {
				targetEl.stop(true, true).slideDown(config.showSpeed);
				$('#clients').animate({ height: clientsList + 'px' }, config.showSpeed);
				$('#container').animate( {marginTop: clientsList + 'px' }, config.showSpeed);
				$(this).addClass('collapse');
			}
			else {
				targetEl.stop(true, true).slideUp(config.hideSpeed);
				$('#clients').animate({ height: hideList + 'px' }, config.hideSpeed);
				$('#container').animate({ marginTop: hideList + 'px' }, config.hideSpeed);
				$(this).removeClass('collapse');
			}
			return false;
		});
	};
	return {
		init: function() {
			appendClickEvent();
		}
	};
}();

var jsDropdown = function() {
	function convertDropdown() {
		$('.js-dropdown').each(function() {
			var dropdown = $('<p><select name="office"></select></p>');
			var options = [];
			
			$(this).children('h3').each(function(j) {
				options.push('<option value="' + j + '">' + $(this).text() + '</option>');
			});
			
			for (i in options) {
				dropdown.children('select').append(options[i]);
			};
			
			$(this).children().hide();
			$(this).prepend(dropdown);
			dropdownChange(this);
			$(this).show();
		});
	};
	function dropdownChange(el) {
		var headers = $(el).children('h3');
		$(headers[0]).show().next().show();
		
		$(el).find('select').change(function() {
			$(headers).hide().next().hide();
			var i = $(this).val();
			$(headers[i]).fadeIn().next().fadeIn();
		});
	};
	return {
		init: function() {
			convertDropdown();
		}
	};
}();

var jsCarousel = function() {
	function uniqueRandom(arg) {
		var uniqueNum = [];
		var i = 0;

		if (isset(arg.firstItem)) {
			uniqueNum.push(arg.firstItem);
		}

		while (i < arg.nums) {
			var ranNum = Math.round(Math.random() * arg.nums);
			if (!in_array(ranNum, uniqueNum)) {
				uniqueNum.push(ranNum);
				i++;
			}
		};

		return uniqueNum;
	}

	function isset() {
		var a = arguments, l = a.length, i = 0;

		if (l === 0) {
			throw new Error('Empty isset');
		}

		while (i !== l) {
			if (typeof (a[i]) == 'undefined' || a[i] === null) {
				return false;
			} else {
				i++;
			}
		}
		return true;
	}

	function in_array(needle, haystack, argStrict) {
		var key = '', strict = !!argStrict;

		if (strict) {
			for (key in haystack) {
				if (haystack[key] === needle) {
					return true;
				}
			}
		} else {
			for (key in haystack) {
				if (haystack[key] == needle) {
					return true;
				}
			}
		}
		return false;
	}
	
	return {
		init: function() {
			if ($('#carousel').length > 0) {
				$.get("/services/flickrcarousel.ashx", function(data) {
					var list = $(data);
					var randSet = { nums: list.length };
					var randomNumber = uniqueRandom(randSet);
					for (var counter = 0; counter < 14; counter++) {
						$('#carousel ul').append('<li class="members"></li>');
						$('#carousel ul li:last').append(list[randomNumber[counter]]);
					}
				});
			}
		}
	};
}();

function log(a) {
	console.log(a);
};