// =========================================================================
// CHANGE LOG (most recent first)
// =========================================================================
//
// -------------------------------------------------------------------------
//
// 22 Apr 2010.	 First version
//
// -------------------------------------------------------------------------

document.write('<style type="text/css">');
document.write('.hide 				    { display: none   } ');
document.write('.hide-noscript  		    { display: block  } ');
document.write('a.hide-noscript, span.hide-noscript { display: inline } ');
document.write('</style>');

// -----------------------
// images to preload first
// -----------------------
var images  = new Array(); 
$.each( preload_first, function(i) {
  images[i] = new Image();
  images[i].src = this;
  //alert( 'first:' + this + ' ' + images[i].width );
});

$( function() {	
        // ---------------------------
	// images to preload for later
        // ---------------------------
	$.each( preload_after, function(i) {
	  images[i] = new Image();
	  images[i].src = this;
	  //alert( 'after:' + this + ' ' + images[i].width );
	});

        // -----------------
	// apply arrows
        // -----------------
        apply_arrows();
        
        function apply_arrows() {
		$('a.arrow-right').each( function() {
			$(this).after('<a class="img-arrow img-arrow-right" href="' + $(this).attr('href') + '">&nbsp;</a>')
		} );
		$('a.arrow-right-big').each( function() {
			$(this).after('<a class="img-arrow img-arrow-right-big" href="' + $(this).attr('href') + '">&nbsp;</a>')
		} );
		$('a.arrow-down').each( function() {
			$(this).after('<a class="img-arrow img-arrow-down ' + $(this).attr('class') + '" href="' + $(this).attr('href') + '">&nbsp;</a>')
		} );
		$('a.arrow-up').each( function() {
			$(this).after('<a class="img-arrow img-arrow-up ' + $(this).attr('class') + '" href="' + $(this).attr('href') + '">&nbsp;</a>')
		} );
	}
	
	// ---------------------
	// profile pic highlight
	// ---------------------
	$('#about-us span.profile-pic').hover( function() {
		$(this).css('cursor','pointer');
		$(this).css('background-position','0 -118px');
	}, function() {		
		// only grey out if 'more' content not open
		if ( $(this).parent().find('.arrow-down').size() ) {
			$(this).css('background-position','0 0');
		}
	} );

	$('#about-us span.profile-link,#about-us a.profile-link').click( function() {
		// hide show already handled by presence of these classes (see 'regular hide-show')			
		var do_hide_show = ( $(this).hasClass('arrow-down') || $(this).hasClass('arrow-up') ) 
			? 0 // click on 'Read more' link 
			: 1 // click on profile pic
		;

		if ($(this).parent().find('.arrow-down').size()) {
			$(this).parent().find('.profile-pic').css('background-position','0 -118px');
			if (do_hide_show) $(this).parent().find('.arrow-down').first().trigger('click');
		}
		else {
			$(this).parent().find('.profile-pic').css('background-position','0 0');		
			if (do_hide_show) $(this).parent().find('.arrow-up').first().trigger('click');
		}
		return false;
	} );

        // -----------------
	// regular hide-show
        // -----------------
	$('div.hide-show a.arrow-down').click( function() {	
		if ( $(this).parent().find('.arrow-down').size() ) {
			$(this).parent().find('.hide').show();
			toggle_more_link( $(this), 'show' );
		}
		else {
			$(this).parent().find('.hide').hide();
			toggle_more_link( $(this), 'hide' );
		}
		return false;				
	} );

        // -----------------
	// hide-show replace
        // -----------------
	$('div.hide-show-replace a.arrow-down').click( function() {
		if ( $(this).parent().find('.arrow-down').size() ) {
			$(this).parent().find('.short').hide();
			$(this).parent().find('.long').show()	
			toggle_more_link( $(this), 'show' );
		}
		else {
			$(this).parent().find('.long').hide()
			$(this).parent().find('.short').show();		
			toggle_more_link( $(this), 'hide' );
		}
		return false;
	} );	

	function toggle_more_link( jq_el, mode ) {
		if ( mode == 'show') {
			jq_el.parent().find('.arrow-down').each( function() {
				$(this).removeClass('arrow-down').addClass('arrow-up');
				$(this).text( $(this).text().replace(/more/i,'less') );
				if ($(this).hasClass('img-arrow-down')) {
					$(this).removeClass('img-arrow-down').addClass('img-arrow-up');
				}
			} );
		}
		else {
			jq_el.parent().find('.arrow-up').each( function() {
				$(this).removeClass('arrow-up').addClass('arrow-down');
				$(this).text( $(this).text().replace(/less/i,'more') );
				if ($(this).hasClass('img-arrow-up')) {
					$(this).removeClass('img-arrow-up').addClass('img-arrow-down');
				}
			} );
		}
	}

	// -----------------
	// email replacement
	// -----------------
	$('a.email').each( function() {
		var address = $(this).text().replace(/\[at\]/,'@');
		$(this).attr('href','mailto:' + address); 
		$(this).text(address);
	});	
	
	// -----------------
	// forms
	// -----------------
	// highlight fields
	$('input,textarea').focus( function() {
		$(this).css('background-color','#FFFEA2');
	} );
	$('input,textarea').blur( function() {
		$(this).css('background-color','#FFFFFF');
	} );
	
	// validate emails	
	$('form').submit( function() {
		return check_email( $(this).find('.email'), 1 );
	});
	
	// Note: applying blur to the newsletter form prevents it submitting following an invalid email
	// Since it's a one liner it's not needed there so don't worry.
	$('form#request-demo .email').blur( function() {
		return check_email( $(this) );
	} );
	
	function check_email( jq_el, is_required ) {
		if (!jq_el) return;
		var val =  jq_el.val();
		var error_msg_id = ('#' + jq_el.attr('id') + '-error');
		if ( 	( !val && is_required ) ||
			(  val && val.search( /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/ ) == -1 ) ) { 
			$( error_msg_id ).text('Please enter a valid e-mail');
			$( error_msg_id ).show();
			return false;
		}
		else {
			$( error_msg_id ).css('display','none');
			return true;
		}
	}

	// -----------------
	// quote rotation
	// -----------------
	var fade_total  = 0;
	var fade_count = 1;	
	var fade_last;
	var fade_max;
	var fade_tallest = 0;
	fade_max = $('blockquote.fade-inner').size(); // more efficient than looking for '.fade'
	if (fade_max) { 
		fade_rotate();
	}

	function fade_rotate() {	
		var interval_id = setInterval( function() {		

			fade_last = fade_count;
			fade_count++;
			fade_total++;
			if (fade_count>fade_max) { 
				fade_count = 1; 
				fade_last = fade_max; 		
			};

			var fade = $('#fade-'+fade_count);
			var fade_last = $('#fade-'+fade_last);

			fade.css('z-index',2);		// on top
			fade_last.css('z-index',1);	// underneath

			// set the minimum height to the running tallest 
			// (prevent a smaller quote shifting content below upwards)
			var fade_height = fade_last.height();
			if ( fade_height > fade_tallest ) fade_tallest = fade_height;
			if($.browser.msie && $.browser.version=="6.0")	{
				fade.css('height', fade_tallest+'px');			
			}
			else {
				fade.css('min-height', fade_tallest+'px');
			}
			
			fade.fadeIn(500, function() { 
				if ($.browser.msie) {
					$(this).get(0).style.removeAttribute('filter');
				} 
				$(this).css('position','static');     // retain height e.g. if text resized
				fade_last.css('position','absolute');
				fade_last.css('display','none');
			} );

			// don't run forever - leaks small amount of memory
			if (fade_total > 100) clearInterval( interval_id );			

		}, 18000);
	}

	// -----------------
	// product tour ajax
	// -----------------
	$.ajaxSetup({ cache: false }); // important	

	init_tour_links();

	function init_tour_links() {
		$('#tour .tour-link').unbind('click');
		$('#tour .tour-link').click( function() {		
			var url = $(this).attr('href');
			$('#tour').load( (url + ' #tour-inner'), function() { init_tour_links(); apply_arrows() }); // init on loaded fragment
			return false;
		});
	}    
} );





