// 
//  basic.js
//  
//  Created by Jeremy Tredway on 2010-04-25.
//  Copyright 2010 ThoughtMatrix. All rights reserved.
// 


// extend browser object to define IE6
$.browser.msie6 = $.browser.msie && /MSIE 6\.0/i.test(window.navigator.userAgent) && !/MSIE 7\.0/i.test(window.navigator.userAgent);

// extend browser object to define IE7
$.browser.msie7 = $.browser.msie && /MSIE 7\.0/i.test(window.navigator.userAgent);

// synchronize height function
(function($){$.fn.syncHeight=function(settings){var max=0;var browser_id=0;var property=[['min-height','0px'],['height','1%']];if($.browser.msie&&$.browser.version<7){browser_id=1;}
$(this).each(function(){$(this).css(property[browser_id][0],property[browser_id][1]);var val=$(this).height();if(val>max){max=val;}});$(this).each(function(){$(this).css(property[browser_id][0],max+'px');});return this;};})(jQuery);

// extend jquery with preload images function
(function($) {
	var cache = [];
	$.preLoadImages = function() {
		var args_len = arguments.length;
		for (var i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			cache.push(cacheImage);
			load_count++;
			if (load_count >= args_len && cycle) {
				setTimeout($.cycleHomePage, cycle_delay);
			}
		}
	};
})(jQuery)


// obfuscate address function
function obfsMail(s,e,h,d,c) {
	//if (e == "" || h == "") { return; }
	if (d == "") { d = "com"; }
	var x = e + "&#x40;" + h + "&#x2E;" + d;
	if (c == "") { c = x; }
	var y = "href=\"ma" + "ilto:" + x + "\"";
	var z = (s == "") ? ("<a " + y + ">" + c + "</a>") : ("<a class=\"" + s + "\" " + y + ">" + c + "</a>");
	return z;
}


// validate email
function checkEmail(val) {
	var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (regex.test(val)) { return true; }
	else { return false; }
}


// validate form
function validateForm(elem) {
	var submit = true;
	var this_form = '#' + $(elem).parents('form').attr('id');
	// reset errors
	$(this_form).find('.error').hide();
	// check for errors in input and textareas
	$(this_form).find('.required').each(function() {
		var pre_pop = '';
		if ($(this).hasClass('pre_pop')) {
			pre_pop = $(this).prev('label').text();
		}
		if ($(this).val() == '' || $(this).val() == pre_pop) {
			submit = false;
			$(this).next('.error').fadeIn(400);
		}
		if ($(this).attr('type') == 'text' && $(this).attr('alt') == 'email_address') {
			check_email = checkEmail($(this).val());
			if (!check_email) {
				submit = false;
				$(this).next('.error').fadeIn(400);
			}
		}
	});
	return submit;
}


// onload routines
$(document).ready(function() {

	// init left-hand navigation active state
	$('#left-nav').find('a').each(function() {
		if (location.href == this.href) {
			$(this).parent().addClass('on');
		}
	});

	// init top header navigation active state
	$('#header-nav').find('a').each(function() {
		if (location.href.indexOf(this.href) != -1) {
			$(this).parent().addClass('on');
		}
	});

	// init twitter import
	if ($('#twitter_import').length != 0) {
		$.ajax({
			url: '/twitter.php',
			success: function(data) {
				if (data != '') {
					$('#twitter_import').html('<p>'+data+' <a href="http://twitter.com/thoughtmatrix" target="_blank" class="external">Read&nbsp;More&nbsp;on&nbsp;Twitter&nbsp;&gt;</a></p>');
					if ($('body').hasClass('home')) {
						$("#information .info-col").syncHeight();
					}
				}
			}
		});
	}

	// init toggles
	$('.toggle').hide();
	$('.toggle_trigger').click(function() {
		$(this).toggleClass('toggle_trigger_open');
		if ($(this).hasClass('toggle_trigger_open')) {
			$(this).text('less');
		} else {
			$(this).text('more');
		}
		$(this).parents('.toggle_wrap').find('.toggle').each(function() {
			$(this).toggle();
		});
		return false;
	});

	// init slideshow (w/ api)
	$('#slideshow_overlay .overlay_scrollable').scrollable({
		next: '#ovs-next',
		prev: '#ovs-prev',
		size: 1,
		onSeek: function() {
			$('#slideshow_overlay .scrollable_counter span').text(this.getIndex() + 1);
		}
	});
	var scroll_api = $("#slideshow_overlay .overlay_scrollable").data("scrollable");

	// init slideshow overlays
	$('.slideshow_overlay').overlay({
		onLoad: function() {
			var init_slide = this.getTrigger().index();
			scroll_api.seekTo(init_slide, 400);
		}
	});

	// init contact overlays
	$('.contact_overlay').overlay({
		onBeforeLoad: function() {
			var params = this.getTrigger().attr("title").split(":");
			if (params[0] == "other") {
				$('#contact_overlay #other').val(params[1]);
				$('#contact_overlay #check-Other').attr('checked', 'checked');
			}
		}
	});

	// init standard overlays
	$('.overlay').overlay();

	// initialize obfuscated addresses
	$(".obfmail").each(function() {
		var $this = $(this);
		var params = $this.attr("rel").split(":");
		if (params[0] == "mail") {
			var addr = obfsMail("",params[1],"thought-matrix","","");
		}
		$this.html(addr);
	});

	// set external links
	$('a[href^="http"]').each(function () {
		if(this.href.indexOf(location.hostname) == -1) {
			$(this).attr('target', '_blank');
			$(this).addClass('external');
		}
	});


	// show errors returned by server
	$('.show_error').fadeIn(400);

	// form validation
	$('.validate_form input[type="submit"]').click(function() {
		return validateForm(this);
	});

	// clear error messages upon blur
	$('.validate_form .required').blur(function() {
		if ($(this).val() != '') {
			var next_error = $(this).next('.error');
			$(next_error).fadeOut(400, function() {
				$(next_error).removeClass('show_error');
			});
		}
	});

});

$(window).load(function(){
	$('input.pre_pop').each(function (type) {

		$(this).attr('autocomplete', 'off');

		// var init_val = $(this).val();
		// setTimeout(function(){
		// 	if (init_val != '') {
		// 		$(this).prev('label.pre_pop').addClass('has-text');
		// 	}
		// 	alert(init_val);
		// }, 500);

		$(this).focus(function () {
			$(this).prev('label.pre_pop').addClass('focus');
		});

		$(this).keypress(function () {
			$(this).prev('label.pre_pop').addClass('has-text').removeClass('focus');
		});

		$(this).blur(function () {
			if ($(this).val() == '') {
				$(this).prev('label.pre_pop').removeClass('has-text').removeClass('focus');
			}
		});
	});
});
