var pn = 7;

$j(document).ready(function(){

	//create container div
	$j('#text1').html('<div id="container"></div>');
		
	imgLeft = new Array();
	imgRight = new Array();

	//preload the images
	for (var i=1; i <= pn; i++) {
		
		imgLeft[i] = new Image();
		imgLeft[i].src = "uploaded/home_photos/photo"+i+"_left.jpg?"+Math.random();
		imgRight[i] = new Image();

		if (i==pn) {imgRight[i].onload = function(){ procPhotos() }};

		imgRight[i].src = "uploaded/home_photos/photo"+i+"_right.jpg?"+Math.random();

	};
	
	//top news/calendar slider box
	
	var slideSpeed = 500;
	
	$j('#newsContainer').html( $j('.newsContent').html() );
	$j('#calContainer').html( $j('.calContent').html() );
	
	$j('#calBtn').click(function(){
		$j('#calBtns').animate({top:'19px'},slideSpeed);
		$j('#newsContainer').animate({height:'1px'},slideSpeed);
		$j('#calContainer').animate({height:'64px',top:'37px'},slideSpeed);
		return false;
	})

	$j('#newsBtn').click(function(){
			$j('#calBtns').animate({top:'82px'},slideSpeed);
			$j('#newsContainer').animate({height:'64px'},slideSpeed);
			$j('#calContainer').animate({height:'1px',top:'100px'},slideSpeed);
		return false;
	})
	
}); //end document ready

function procPhotos () {
	
	currentSet = 1;
	
	setHTML =  '<div id="set%n" class="photoSet">';
	setHTML += '<img src="uploaded/home_photos/photo%n_left.jpg" ><img src="uploaded/home_photos/photo%n_right.jpg" >';
	setHTML += '<div class="caption"><div class="captionbg"></div><div class="captiontext"></div></div>';
	setHTML += '</div>';
	
	for (var i=1; i <= pn; i++) {
		altHTML = setHTML.replace(/%n/g,i);
		$j('#container').append(altHTML);
		$j('#set'+i).hide();
		
		imgLw = imgLeft[i].width;
		imgRw = imgRight[i].width;
				
		if(imgLw > imgRw){
			$j('#set'+i+' .caption').width(imgLw + "px");	
			$j('#set'+i+' .captionbg').width(imgLw + "px");	
		}else{
			$j('#set'+i+' .caption').width(imgRw + "px");
			$j('#set'+i+' .caption').css('left' , (imgLw + 1 + "px"));
			$j('#set'+i+' .captionbg').width(imgRw + "px");
			// $j('#set'+i+' .captionbg').css('left' , (imgLw + 1 + "px"));
		}

	};

	// add caption text (unless content is empty)
	$j('#leftbanner .bannermodcontent').each( function(i){
		$j('#set'+(i+1)+' .captiontext' ).html( $j(this).html() );
		if( $j(this).find('div').html().length == 0 ){
			$j('#set'+(i+1)+' .caption' ).hide();
		} 
	});//end each function

	// finished loading, show buttons
	$j('#scrollLeft').show();
	$j('#scrollRight').show();

	$j('#scrollLeft').click(function(){
		var nextSet = currentSet > 1 ? currentSet - 1 : pn;
		$j('#set'+nextSet).css('left','-971px');
		$j('#set'+nextSet).show();
		$j('#set'+nextSet).animate({left:'0px'},1500);
		$j('#set'+currentSet).animate({left:'971px'},1500);
		currentSet = nextSet;
		createCookie('setNumber',nextSet,7);
	}).mouseover(function(){
			$j('#scrollRight').attr('src','uploaded/images/home/scroll_right.gif');
			$j('#scrollLeft').attr('src','uploaded/images/home/scroll_left_on.gif');
		})

	$j('#scrollRight').click(function(){
		var nextSet = currentSet < pn ? currentSet + 1 : 1;
		$j('#set'+nextSet).css('left','971px');
		$j('#set'+nextSet).show();
		$j('#set'+nextSet).animate({left:'0px'},1500);
		$j('#set'+currentSet).animate({left:'-971px'},1500);
		currentSet = nextSet;
		createCookie('setNumber',nextSet,7);		
	}).mouseover(function(){
		$j('#scrollRight').attr('src','uploaded/images/home/scroll_right_on.gif');
		$j('#scrollLeft').attr('src','uploaded/images/home/scroll_left.gif');
	})

	$j('#text1').append('<a href="#" id="mediaLink" onclick="javascript:popMedia(\'chnl=12\')">view in media gallery &gt;</a>');
	$j('#mediaLink').hide();
	$j('#text1').hover( function(){ $j('#mediaLink').show()}, function(){ $j('#mediaLink').hide() } )

	if(readCookie('setNumber')){
		setNum = Number(readCookie('setNumber')) + 1;
		setNum = setNum <= pn ? setNum : 1;
		$j('#set'+setNum).show();
		currentSet = setNum;
		createCookie('setNumber',setNum,7);
	}else{
		$j('#set1').show();
		currentSet = 1;
		createCookie('setNumber',1,7);
	}
	
}