/**
 * Clear form input
 */
function ClearInput(value, id){ // This calls our function ClearInput, and the two variables we will need for it to function the original value and the id.
var input = document.getElementById(id); // Gets the input field based on its id.

if(value == input.value){ // If the default value is equal to the current value.
input.value = ''; // Empty It.
}else{ // Else the value is not equal to the current input field value.
input.value = input.value; // Leave it the same.
} // End Else.
} // Close Function.

/**
 * Compact labels plugin
 */
(function($){$.fn.compactize=function(){return this.each(function(){var label=$(this),input=$('#'+label.attr('for'));input.focus(function(){label.hide();}).blur(function(){if(input.val()===''){label.show();}});window.setTimeout(function(){if(input.val()!==''){label.hide();}},50);});};})(jQuery);

/*
 * hrefID jQuery extention - returns a valid #hash string from link href attribute in Internet Explorer
 */
(function($){$.fn.extend({hrefId:function(){return $(this).attr('href').substr($(this).attr('href').indexOf('#'));}});})(jQuery);

/*
 * Scripts
 *
 */
jQuery(function($) {
 
	var Engine = {
		utils : {
			links : function(){
				$('a[rel*=external]').click(function(e){
					e.preventDefault();
					window.open($(this).attr('href'));						  
				});
			},
			mails : function(){
				$('a[href^=mailto:]').each(function(){
					var mail = $(this).attr('href').replace('mailto:','');
					var replaced = mail.replace('/at/','@');
					$(this).attr('href','mailto:'+replaced);
					if($(this).text() == mail) {
						$(this).text(replaced);
					}
				});
			},
			labels : function(){
				$('#top label, div.find-a label.compact, div.find-b label.compact').compactize();
			}
		},
		enhancements : {
			resources : function(){
				$('.cols-b .col > li > a').click(function(e){
					e.preventDefault();
					$(this).next('ul').toggle();
				});
			},
			slideshow : function(options){
				$('#slideshow').each(function(){
					var 
						move,
						$slideshow = $(this),
						$slides = $slideshow.find('.slide'),
						$nav = $('<ul class="nav"/>'),
						current;					

					//navigation
					$slides.each(function(index){
						var $slide = $(this);
						var $trigger = $('<a href="#'+$slide.attr('id')+'">'+$slide.find('h3').html()+'</a>');

						$trigger.data('index',index);
						
						$trigger.click(function(e){
							e.preventDefault();
							move($(e.target).closest('a').data('index'));
						});
						
						$nav.append($('<li/>').append($trigger));
						
						//preload image
						var $img = $slide.find('img').clone();
					});
					
					var $triggers = $nav.find('a');
					
					//previous slide trigger
					var $prev = $('<a href="#">Previous</a>').click(function(e){
						e.preventDefault();
						move(current - 1);
					});
					
					$nav.prepend($('<li class="prev"/>').append($prev));
					
					//next slide trigger
					var $next = $('<a href="#">Next</a>').click(function(e){
						e.preventDefault();
						move(current + 1);
					});

					$nav.append($('<li class="next"/>').append($next));
								
					//floating pointer image		
					var $pointer = $('<span id="slideshow-pointer" />');
					
					//content holders	
										
					var $media = $('<div class="media-holder" />');
					var $description = $('<div class="description" />');

					$slideshow.find('.wrapper').append($media, $description, $nav, $pointer);

					//autorotate
					var interval;
					
					var stopAutorotate = function(){
						window.clearInterval(interval);
					};					

					
					//this is checking if user is using keyboard to navigate elements in the slideshow
					var isFocused = false;
					
					$slideshow.find('a').focus(function(){
						isFocused = true;
					}).blur(function(){
						isFocused = false;
					});
					
					var restartAutorotate = function(){						
						interval = window.setInterval(function(){							
							//keyboard navigation
							if (isFocused === true) {
								return;
							}
							move(current + 1);
						}, options.timerInterval);
					};					
					
					$slideshow.mouseenter(function(){
						stopAutorotate();
					}).mouseleave(function(){
						restartAutorotate();
					});
										
					move = function(index){
						
						if (current === index) {
							return;
						}
						
						//determine position of the new slide
						if (index < 0) {
							current = $slides.length - 1;
						}
						else if (index >= $slides.length) {
							current = 0;
						}
						else {
							current = index;
						}						
						
						var instant = arguments[1] || false;						
						var speed = instant ? 0 : options.transitionSpeed;
												
						//image transition
						var $img = $slides.eq(current).find('p.media img').clone(true).addClass('active').hide();						
						var $oldImg = $media.find('img');
						$media.append($img);
						$img.fadeIn(speed,function(){
							$oldImg.remove();
							$(this).removeClass('active');
						});

						//description transition
						var $desc = $slides.eq(current).find('p.desc').clone(true).addClass('active').hide();
						var $oldDesc = $description.find('p.desc');
						$description.append($desc);
						$desc.fadeIn(speed,function(){
							$(this).removeClass('active');
						});
						
						$oldDesc.fadeOut(speed,function(){
							$(this).remove();
						});
						
						//pointer transition
						var activeTrigger = $triggers.eq(current);

						//find the center of active trigger and offset by half of pointer width - 30px
						var pointerPosition = activeTrigger.position().left + (activeTrigger.width()/2) - 30;
																		
						$pointer.stop().animate({'left' : pointerPosition }, speed);						
					};
					
					//initialize on the first slide
					move(0,true);		
					
					if (options.autorotate === true) {
						restartAutorotate();
					}
				});
			}
		}
	};

	Engine.utils.links();
	Engine.utils.mails();
	Engine.utils.labels();
	
	Engine.enhancements.resources();
	Engine.enhancements.slideshow({
		transitionSpeed : 1000,
		autorotate : true,		
		timerInterval : 6500
	});
});