/*
 * jQuery Timer Plugin
 * http://www.evanbot.com/article/jquery-timer-plugin/23
 *
 * @version      1.0
 * @copyright    2009 Evan Byrne (http://www.evanbot.com)
 */ 

jQuery.timer = function(time,func,callback){
	var a = {timer:setTimeout(func,time),callback:null}
	if(typeof(callback) == 'function'){a.callback = callback;}
	return a;
};

jQuery.clearTimer = function(a){
	clearTimeout(a.timer);
	if(typeof(a.callback) == 'function'){a.callback();};
	return this;
};

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

	$.ajaxSetup({
		type: 'POST'
	});
	
	config = {
		sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
		interval: 200, // number = milliseconds for onMouseOver polling interval    
		over: function(){
			$(this).addClass('hover');
		}, 
		// timeout: 500, // number = milliseconds delay before onMouseOut    
		out: function() {
			$(this).removeClass('hover');		
		}
	}
	
	$('#locationWrap').hoverIntent(config);
	
	$('.sitemapList li:last-child').addClass('last');
	
	/* !AUTOSUGGEST */
	/**********************/
	$('#searchField').livequery('keyup', function() {
		if ($('#searchField').val().length > 0) {
			the_url = $('#searchForm').attr('action');
			the_url = the_url.replace('search_results','auto_suggest/');
			the_url += $('#searchField').val();
			$.ajax({
				beforeSend: function(){$('.autoSuggest').addClass('loading');},
				url: the_url,
				success: function(data) {
					$('.autoSuggest').addClass('show').removeClass('loading').html(data);
				}
			});
		}
	}).livequery('blur', function() {
		// console.log('blur');
		// var myTimer = {};
		// myTimer = 
		$.timer(1000,function(){
			$('.autoSuggest').removeClass('loading').removeClass('show');
		});
	});
	
	// http://stackoverflow.com/questions/1319126/prevent-highlight-of-text
	$.extend($.fn.disableTextSelect = function() {
		return this.each(function(){
			if($.browser.mozilla){//Firefox
				$(this).css('MozUserSelect','none');
			}else if($.browser.msie){//IE
				$(this).bind('selectstart',function(){return false;});
			}else{//Opera, etc.
				$(this).mousedown(function(){return false;});
			}
		});
	});
	
	/* http://www.vancelucas.com/blog/fixing-ie7-z-index-issues-with-jquery/comment-page-2/ */
	/* FYI this has broken stuff before */
	$(function() {
		var zIndexNumber = 901;
		$('div').each(function() {
			$(this).css('zIndex', zIndexNumber);
			zIndexNumber -= 10;
		});
	});
	
	/* !CYCLE */
	/********************************/
	$('.neSection .items').cycle({
		fx: 'scrollUp',
		pause: 1,
		height: '30px',
		cleartypeNoBg: true,
		speed: 2000,
		timeout: 5000
	});
	
	/* !INPUT BLANKING */
	/********************************/
	$.fn.input_blank = function() {
		return this.focus(function() {
			if( this.value == this.defaultValue ) {
				this.value = "";
			}
		}).blur(function() {
			if( !this.value.length ) {
				this.value = this.defaultValue;
			}
		});
	};
	$("input").input_blank();
	
	/* !SUCKERFISH SHOAL */
	/********************************/
	
	$("#menu li").hover(
		function() {$(this).addClass("hover");},
		function () {$(this).removeClass("hover");}
	);
	
	/* !TIGER STRIPE TABLES */
	/********************************/
	$('.column tbody tr:odd td').addClass('alt');
	
	/* !VIDEO */
	/*******************************/
	$.fn.media.defaults.flvPlayer = 'assets/scripts/mediaplayer/player.swf';
	$.fn.media.defaults.flashvars = {skin: "assets/scripts/mediaplayer/modieus/modieus.swf"};
	$.fn.media.defaults.params = {allowfullscreen: "true", allowscriptaccess: "always", wmode: "transparent"};
	
	$('a.media').empty().media();
	// $('a.media').flowplayer('assets/scripts/flowplayer/flowplayer-3.1.5.swf');
	
	/* !SIDEBAR BOX DROPDOWNS */
	/******************************/
	
	$('.box.open h5').live('click', function() {
		$(this).parents('.box').removeClass('open')
		.addClass('closed')
		.disableTextSelect()
		.children('.boxContent').css('display','block').slideUp();
	});
	$('.box.closed h5').live('click', function() {
		$(this).parents('.box').removeClass('closed')
		.addClass('open')
		.disableTextSelect()
		.children('.boxContent').css('display','none').slideDown();
	});
	
	// content boxes
	$('.contentBox.open h5').live('click', function() {
		$(this).parents('.contentBox').removeClass('open')
		.addClass('closed')
		.disableTextSelect()
		.children('.boxContent').css('display','block').slideUp();
	});
	$('.contentBox.closed h5').live('click', function() {
		$(this).parents('.contentBox').removeClass('closed')
		.addClass('open')
		.disableTextSelect()
		.children('.boxContent').css('display','none').slideDown();
	});
	
	// $("a[rel^='popup']").prettyPhoto({theme: 'dark_square'});
	$("a[rel^='popup'], a.popup").prettyPhoto();
	
	/* !CONTENT IMAGE CHOOSER
	********************************/
	$(".smPreviews a").mouseover( function() {
		linky = $(this).attr("href");
		medurl = linky.replace('-lg','-med');
		// console.log(medurl);
		$(this).siblings().removeClass("selected");
		$(this).addClass("selected");
		$(this).parents('.previewBox').children('.medPreview').children('a')
		.attr('href',linky)
		.children('img').attr("src",medurl);
	});
	
	$('.logoutLink a').live('click', function() {
		// console.log($(this).attr('href'));
		$.ajax({
			url: $(this).attr('href'),
			dataType: 'json',
			success: function(data) {
				$.each(data.notices, function(key, value) {
					$.gritter.add({
						title: 'Success',
						text: value
					});
				});
				page_id = $('#loginWrap').metadata().id;
				// console.log('page id: '+page_id);
				$.get($('body').attr('class')+'/login_header_only/'+page_id, function(data) {
					$('#loginWrap').replaceWith(data);
				});
			}
		});
		return false;
	});

	$('input, textarea, .mceLayout').livequery('focus', function() {
		$(this).removeClass('errorField')
		.addClass('focus');
	})
	.livequery('blur', function() {
		$(this).removeClass('focus');
	});
	
	/* !LOGIN FORM SUBMIT */
	/***************************************/
	$('form.loginForm').livequery('submit',function(theform) {
		theform.preventDefault();
		// console.log('test');
		thisid = $(this).attr('id');
		$.ajax({
			url: this.action,
			dataType: 'json',
			data: $(this).serialize(),
			success: function(data) {
				$('.errorField').removeClass('errorField');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					// page_id = $('#loginWrap').attr('title');
					page_id = $('#loginWrap').metadata().id;
					$.get($('body').attr('class')+'/login_header_only/'+page_id, function(data) {
						$('#loginWrap').replaceWith(data);
					});
				} else {
					$.each(data.errors, function(key, value) {
						console.log('#'+thisid+' .'+key+'Field');
						$('#'+thisid+' .'+key+'Field').addClass('errorField');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
	});
	
	/* !CONTACT SUPPORT FORM SUBMIT */
	/***************************************/
	$('form.supportForm').livequery('submit',function(theform) {
		theform.preventDefault();
		// console.log('test');
		$.ajax({
			url: this.action,
			dataType: 'json',
			data: $(this).serialize(),
			success: function(data) {
				$('.errorField').removeClass('errorField');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$('#'+key+'FieldSupport').addClass('errorField');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
	});
	
	/* !CONTACT SALES FORM SUBMIT (only need separate for field names) */
	/***************************************/
	$('form.salesForm').livequery('submit',function(theform) {
		theform.preventDefault();
		// console.log('test');
		$.ajax({
			url: this.action,
			dataType: 'json',
			data: $(this).serialize(),
			success: function(data) {
				$('.errorField').removeClass('errorField');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$('#'+key+'FieldSales').addClass('errorField');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
	});
	
	/* !REQUEST DEMO FORM SUBMIT (only need separate for field names) */
	/***************************************/
	$('form.demoForm').livequery('submit',function(theform) {
		theform.preventDefault();
		// console.log('test');
		$.ajax({
			url: this.action,
			dataType: 'json',
			data: $(this).serialize(),
			success: function(data) {
				$('.errorField').removeClass('errorField');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$('#'+key+'FieldDemo').addClass('errorField');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
	});
	
	/* !CUSTOMER / SUPPORT SIGNUP FORM SUBMIT (only need separate for field names) */
	/***************************************/
	$('form.customerSignupForm').livequery('submit',function(theform) {
		theform.preventDefault();
		// console.log('test');
		$.ajax({
			url: this.action,
			dataType: 'json',
			data: $(this).serialize(),
			success: function(data) {
				$('.errorField').removeClass('errorField');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$('#'+key+'FieldCs').addClass('errorField');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
	});
	
	/* !PARTNER SIGNUP FORM SUBMIT (only need separate for field names) */
	/***************************************/
	$('form.partnerSignupForm').livequery('submit',function(theform) {
		theform.preventDefault();
		// console.log('test');
		$.ajax({
			url: this.action,
			dataType: 'json',
			data: $(this).serialize(),
			success: function(data) {
				$('.errorField').removeClass('errorField');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$('#'+key+'FieldPs').addClass('errorField');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
	});
	
	/* !PROSPECT SIGNUP FORM SUBMIT (only need separate for field names) */
	/***************************************/
	$('form.prospectSignupForm').livequery('submit',function(theform) {
		theform.preventDefault();
		// console.log('test');
		$.ajax({
			url: this.action,
			dataType: 'json',
			data: $(this).serialize(),
			success: function(data) {
				$('.errorField').removeClass('errorField');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$('#'+key+'FieldPs').addClass('errorField');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
	});
	
	/* !NEWSLETTER SLIDE DOWN */
	/**************************************/
	
	$('.newsletterForm button.continueNl').live('click', function() {
		$(this).parents('form')
		.children('.hide')
		.slideDown();
		$(this).parent().remove();
	});
	
	/* !NEWSLETTER SUBSCRIBE FORM SUBMIT (only need separate for field names) */
	/***************************************/
	$('form.newsletterForm').livequery('submit',function(theform) {
		theform.preventDefault();
		// console.log('test');
		$.ajax({
			url: this.action,
			dataType: 'json',
			data: $(this).serialize(),
			success: function(data) {
				$('.errorField').removeClass('errorField');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$('#'+key+'FieldNl').addClass('errorField');
						// console.log('#'+key+'FieldNl');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
	});
	
	/* !NEWSLETTER UNSUBSCRIBE BUTTON CLICK */
	/********************************/
	the_href = $('a#unsubscribeLink').attr('href');
	the_href += $.url.param("email_address");
	// the_href = the_href.replace('_at_','@');
	$('a#unsubscribeLink').attr('href',the_href);
	
	$('a#unsubscribeLink').click(function() {
		$.ajax({
			url: $(this).attr('href'),
			dataType: 'json',
			data: $(this).serialize(),
			success: function(data) {
				$('.errorField').removeClass('errorField');
				if (data.success == 'yes') {
					msg = 'You have been unsubscribed. You should receive a confirmation in your email shortly.';
					$('a#unsubscribeLink').replaceWith(msg);
				} else {
					$.each(data.errors, function(key, value) {
						$('#'+key+'Field').addClass('errorField');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
		return false;
	});
	
	/* GOOGLE ANALYTICS ASYNCHRONOUS SNIPPET
	*************************************/
	/* http://code.google.com/apis/analytics/docs/tracking/asyncTracking.html */
	// doesn't work! don't know why.
	/*
var _gaq = _gaq || [];
	the_host = $.url.attr('host');
	reg_ex = /nditech.net$/;
	check = the_host.search(reg_ex);
	if(check != -1) {
		// it's nditech.net - use the primary google tracking code
		_gaq.push(['_setAccount', 'UA-4500327-1']);
		// console.log('nditech.net');
	} else {
		// it's probably ndi-rs.com so use the secondary
		_gaq.push(['_setAccount', 'UA-4500327-2']);
		// console.log('ndi-rs.com');
	}
	_gaq.push(['_trackPageview']);
	
	(function() {
		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
	})();
*/

	
	
	
}); // document ready