// JavaScript Document

//general
function listNextAfter($list,$item){
	var index = $list.index($item);
	if(index < $list.length - 1)
		return $list.eq(index+1);
	else 
		return $([]);
}

function listPrevBefore($list,$item){
	var index = $list.index($item);
	if(index > 0)
		return $list.eq(index-1);
	else 
		return $([]);
}



function sifr1_replace(target){
	sIFR.replaceElement(target, named({sFlashSrc: "sifr/be_imago_light.swf", sColor: "#E01F00",sBgColor: "#151515"/*, sWmode: "transparent", sHoverColor: "#eb1996"  ,sFlashVars: "offsetTop=0"*/}));	
}

$(document).ready(function(){
	//sifr			
	if(typeof sIFR == "function"){
		sIFR.setup();
		sifr1_replace('.sifr1');
		sIFR.replaceElement('.sifr4', named({sFlashSrc: "sifr/be_imago_book.swf", sColor: "#E01F00", sLinkColor: "#BBBBBB",sBgColor: "#151515"/*, sWmode: "transparent"*/}));			
		sIFR.replaceElement('.sifr2', named({sFlashSrc: "sifr/be_imago_book.swf", sColor: "#BBBBBB",sBgColor: "#151515"/*, sWmode: "transparent"*/}));	
		sIFR.replaceElement('.sifr3', named({sFlashSrc: "sifr/be_imago_book.swf", sColor: "#E01F00",sBgColor: "#151515"/*, sWmode: "transparent"*/}));			
	};

	
	$('.topBoxLink').click(function(){
		$(this).css('visibility','hidden');
		$topForm = $(this).prev('.topForm')
		$topForm.animate({top:4},500,function(){
			$topForm.find('input').focus()[0].select();											   
		});
		return false;
	});

	$('#topFormButtonSearch').click(function(){
		$input = $(this).prev('input'); 
		location.href = 'http://www.google.com/search?q='+escape($input.val()+' site:www.hornetinc.com');
		return false;												
	});
	
	$('#topFormButtonNewsletter').click(function(){
		$input = $(this).prev('input'); 
		$.get('get.php?newsletter_email='+escape($input.val()),function(data){
			if(data){																		
				data = eval('('+data+')');
				if(data.error){
					$input[0].select();
				}else{
					$('#topFormNewsletter').css({top:-100}).next('.topBoxLink').css('visibility','visible');	
				}
				
				alert(data.message);
			}
		});
		return false;												
	});	
	
	if(window.location.search == '?newsletter_subscribe') setTimeout( function(){$('.topBoxLink').click();},100); 
	
	$('.topFormButton').mouseover(function(){
		$topForm = $(this).parent('.topForm');
		$topForm.css('background-image','url(images/'+$topForm[0].id+'_over.gif)');
	}).mouseout(function(){	
		$topForm = $(this).parent('.topForm');
		$topForm.css('background-image','url(images/'+$topForm[0].id+'.gif)');
	
	});
	
	$('.pictureImage').load(function(){
		if($(this)[0].width > 600) $(this).attr('width',600);
	    $(this).css('visibility','visible');
		$gallerySibling(this,'#pictureActivity').hide();
									 
	});
	
	$('.grayedOut').css('opacity',0.40);
	
});


function pictureMove(obj,move){
	$a = $gallerySibling(obj,'.pictureBoxLink.current');
	$pictureBox = $a.parents('.pictureBox');
	if(-1 == move)
		$a_sibling = $pictureBox.prevAll('.pictureBox:first').find('.pictureBoxLink');	
	else
		$a_sibling = $pictureBox.nextAll('.pictureBox:first').find('.pictureBoxLink');	
	if($a_sibling.length) 
		pictureBoxClick($a_sibling[0]);
	return false;	
}

//finds a sibling in same gallery
function $gallerySibling(child,siblingLocator){
	return $(child).parents('.gallery').find(siblingLocator);
}

function pictureBoxClick(a){
	$a = $(a);
	
	$gallerySibling(a,'.pictureImage').removeAttr('width').css('visibility','hidden').attr('src',$a.attr('href'));
	
	$gallerySibling(a,'#pictureActivity').css('padding-top',Math.round(($gallerySibling(a,'#pictureFrame').height()-32)/2)).show();
	$gallerySibling(a,'#pictureZoom').attr('href',$a.attr('href'));	
	$gallerySibling(a,'#pictureDesc').text($a.find('img').attr('alt'));
	$gallerySibling(a,'#pictureFrame').fadeIn('slow');
	$gallerySibling(a,'.pictureBoxLink.current').removeClass('current');
	$a.addClass('current');
	$pictureBox = $a.parents('.pictureBox');
	$gallerySibling(a,'#pictureLeft' )[ $pictureBox.prevAll('.pictureBox:first').length ? 'removeClass' : 'addClass' ]('disabled');
	$gallerySibling(a,'#pictureRight')[ $pictureBox.nextAll('.pictureBox:first').not('.pictureAdd').length ? 'removeClass' : 'addClass' ]('disabled');
}

function frameSlideshow(frame_list_json){
	//alert(frame_list_json);
	var frame_list = eval('('+	frame_list_json + ')');
	if(frame_list && frame_list.length > 1){
		var frameIndex = 0;
		var imageIndex = 0;
		var expectLoad = 0;
		var $imageA = null;
		var $imageB = null;
		
		$('#frameImage1').css('opacity',0);
		
		function scheduleNextFrame(){
			setTimeout(function(){
				frameIndex = (frameIndex + 1 ) % frame_list.length;
				//alert("scheduleNextFrame:" + frameIndex)												
				imageIndex = 1 - imageIndex;
				$imageA = $('#frameImage' + imageIndex);
				$imageB = $('#frameImage' + (1-imageIndex));
				expectLoad = 1;
				$imageA[0].src = frame_list[frameIndex];
			},3000);
		}
		
		$('.frameImage').load(function(){
			if(expectLoad){	
				expectLoad = 0;
				//alert("loaded:" + frameIndex);	
				$imageA.animate({opacity:1},1000);
				$imageB.animate({opacity:0},1000);				
				
				scheduleNextFrame();
			}
		});		
		
		scheduleNextFrame();
		
	}
}