var searchText = 'Enter a search term...';
var hideDelay = 300;
var menuSpeed = 100;
var autoRotateTimer;
var rotateSpeed = 8000;

function searchFOCUSR() {
	var $searchBox = $('#s');
	var searchValue = $searchBox.val();
	
	if(!searchValue)
		searchValue = searchText; 
	$searchBox.attr('value',searchValue).css('color', '#333');
	$searchBox.blur(function() {
		searchValue = $searchBox.attr('value');
		supportSearchHasValue(searchValue) == true ? $(this).css('color', '#06171E') : $(this).attr('value', searchText).css('color', '#333');
	});
	$searchBox.focus(function() {
		searchValue = $searchBox.attr('value');
		supportSearchHasValue(searchValue) == false ? $(this).attr('value', '').css('color', '#06171E') : $(this).css('color', '#06171E');
	});
	
	$('#s').submit(function(){
		
		//alert('start submit');
		
		var searchValue = $searchBox.val();
		
		//alert(searchValue);
		
		if( supportSearchHasValue(searchValue) ) {
			
		} else {
			$searchBox.focus();
			return false;
		}	
	});	
}

function supportSearchHasValue(myValue) {
	if (myValue == "" || myValue == searchText) {
		return false;
	}else{
		return true;
	};
}

function initCufon(){
	Cufon.replace('.cufon_harting', { fontFamily: 'Harting', hover: true });
}

function initDropdowns(){
	
	$('#page-bar > li').each(function(i){
		var $menuTrigger = $('> a', this);
		var $menu = $('> ul', this);
		
		var hideDelayTimer = null;
		var beingShown = false;
		var shown = false;	
	
		//alert( i );
		//alert( $menu.length );
	
		if($menu.length > 0){
			
			$menu.hide();
			
			$([$menuTrigger.get(0), $menu.get(0)]).hover(
				function(){
					//alert(beingShown+', '+shown);
					if (hideDelayTimer) clearTimeout(hideDelayTimer);
					
					if (beingShown || shown) {
					
					} else {
						// reset position of info box
						beingShown = true;
						
						$menu.slideDown(menuSpeed, function () {
							beingShown = false;
							shown = true;
						});
					}
					
					return false;
				},
				function(){
					if (hideDelayTimer) clearTimeout(hideDelayTimer);
					hideDelayTimer = setTimeout(function () {
						hideDelayTimer = null;
						$menu.slideUp(menuSpeed, function () {
							shown = false;
						});
			
					}, hideDelay);
			
					return false;
				}
			);
		}	
	});
	
}

function headlineSlider(){

	autoRotateTimer = setTimeout ( "nextSlide()", rotateSpeed);

	$(".sliderImages").jCarouselLite({
		btnNext: ".next",
		btnPrev: ".prev",
		visible: 1,
		easing: "easeinout",
		//auto: 5000,
		speed: 450,
		mouseWheel: true
	});
	captionHover();
}

function nextSlide(){
	//alert('slide');
	$('#headline .arrow-but .next').click();
	autoRotateTimer = setTimeout ( "nextSlide()", rotateSpeed);
}

function captionHover(){
		
	$('#home-slider').hover(
		function(){
			clearTimeout (autoRotateTimer);
		},
		function(){
			autoRotateTimer = setTimeout ( "nextSlide()", rotateSpeed);
		}
	);

	$('.boxgrid').each(function(){
	
		$('.details', this).hide();
		
		$(this).css({ cursor:'pointer' }).hover(function(){
			//alert('over');
			$(".details", this).slideDown(100);
		}, function() {
			//alert('out');
			$(".details", this).slideUp(100);
		});
		
		$(this).click(function(){
			//alert($('a', this).attr('href'));
			window.location =  $('.link a', this).attr('href');
		});
		
	});
}

function initColorbox(){
	$myLinks = $('.post a:not(".hiResDownload")');
	$myLinks.each(function(){
		if(
			( $(this).attr('href').indexOf('.jpg') > -1 ) ||
			( $(this).attr('href').indexOf('.jpeg') > -1 ) ||
			( $(this).attr('href').indexOf('.gif') > -1 ) ||
			( $(this).attr('href').indexOf('.png') > -1 )
		)
			//$(this).colorbox({scalePhotos:"true", maxWidth:"90%", maxHeight:"90%"});
			$(this).addClass('colorbox');
	});

	$('a.colorbox').colorbox({scalePhotos:"true", maxWidth:"90%", maxHeight:"90%"});
}


$(document).ready(function(){	
	initCufon();
	$(document).pngFix();
	initDropdowns();
	searchFOCUSR();
	headlineSlider();
	initColorbox();
	$(".mailSignUp").colorbox({width:"800x", height:"500px", iframe:true});
	$(".inlineLNCH").colorbox({inline:true, transition: "elastic", href:"#teaserWRAP"});
	$(".vimeoLNCH").colorbox({width:"700px", height:"450px", iframe:true});
	$(".QTframe").colorbox({width:"700px", height:"455px", scrolling:false,iframe:true});
});