var kROTATE_TIME_IN_MS			= ( isIE() ) ? 3500: 3500;
var kROTATE_BLEND_TIME_IN_MS	= 1000;
var kNUMBER_ss_IMAGES			= arr_ss.length;

function init() {
	if( !window.print )
		getObj( 'button_print' ).style.display = 'none';
}

function slideshow() {
	ss = $( 'slideshow' );
	ss_bg = $( 'slideshow_bg' );
	
	var hasLB = false;
	arr_ss.each( function( slide ) {
		slide = eval( slide );
		if( slide._full ) {
			hasLB = true;
			$( document.body ).grab(
				slide._link = new Element( 'a', { 'href': slide._full, 'class': 'mb', 'styles': { 'display': 'none' }, 'rel': 'type:image' } )
					.grab( new Asset.image( slide.src ) )
			);
		}
	} );
	
	if( hasLB ) {
		mb = new multiBox( 'mb', { 'useOverlay': true, 'openFromLink': false, '_onOpen': function( ) { ssPause( ); }, '_onClose': function( ) { ssResume( ); } } );
		
		var firstSlide = eval( arr_ss[0]+'._link' );
		firstSlide.getFirst( ).set( 'id', ss.get( 'id' ) );
		
		ss_bg.empty( )
			.grab( firstSlide.clone( true, true ).cloneEvents( firstSlide ) )
			.getFirst( ).setStyle( 'display', 'block' );
		
		ss = $( ss.get( 'id' ) );  // Relink to attached node.
	}
	
	if( arr_ss.length == 1 )
		return;
	
	first_slide = eval( arr_ss[0] );
	
	if( first_slide && first_slide.complete ) {
//		alert( 'Go!' );
		setTimeout( updateSsImage, kROTATE_TIME_IN_MS );
	}
}

var uhThread = 1;
var uhCurrSlide = 0;
function updateSsImage( ) {
	if( uhThread > 1 ) {
		uhThread--;
		return;
	}
	
	if( ss ) {
		if( ss._hold ) {
			--uhThread;
			return;
		}
		else {
			if( !uhCurrSlide ) {
				uhCurrSlide = 1;
			}
			else {
				uhCurrSlide = ( uhCurrSlide + 1 ) % kNUMBER_ss_IMAGES;
			}
			
//			uhCurrSlide = ss._n;
			
			slide = eval( arr_ss[ uhCurrSlide ] );
			
			if( slide.complete ) {
				// See comment in luxury.php about viddler.
				var isie = false;
				try {
					isie = isIE( );
				} catch( e ) {
					isie = isIE;
				}
				
				if( isie ) {
					ss.setStyle( 'visibility', 'hidden' );
					applyProperty( ss_bg.id, 'style.backgroundImage', 'url(' + slide.src + ')' );
				} else
					blendimage( ss_bg.id, ss.id, slide.src, kROTATE_BLEND_TIME_IN_MS );
				
				if( slide._full ) {
					ss.getParent( ).href = slide._link.href;
					/** Having trouble cloning the click event w/out it calling the previously added events (removing the click event first didn't have an effect either). For now, just hard map the current content index to `uhCurrSlide' in multiBox.open( ).
					*/
				}
			}
			
//			alert( arr_ss + "\n" + 'arr_ss[' + ss._n + '] = ' + arr_ss[ ss._n ] + '.src' );
			
//			ss.style.width	= eval( arr_ss[ ss._n ] + '.width + "px"' );
//			ss.style.height	= eval( arr_ss[ ss._n ] + '.height + "px"' );
//			ss.src			= eval( arr_ss[ ss._n ] + '.src' );
		}
	}

	if( --uhThread == 0 ) {
		++uhThread;
		setTimeout(
			updateSsImage,
			kROTATE_TIME_IN_MS
			);
		}
	}

var ss_paused = false;
function toggleSS( button ) {
	if( ss ) {
		if( ss._hold ) {
			src		= 'slideshow_pause.gif';
			alt		= 'Pause Slideshow';
			title	= 'Pause Slideshow';
			
			ss_paused = false;
			ssResume( 0 );
		} else {
			src = 'slideshow_play.gif';
			alt		= 'Play Slideshow';
			title	= 'Play Slideshow';
			
			ssPause();
			ss_paused = true;
		}
		
		button.src		= AURI + 'resource/image/' + src;
		button.alt		= alt;
		button.title	= title;
	}
}

function ssPause() {
	if( ss ) {
		ss._hold = true;
	}
}

function ssResume( start_time ) {
	if( ss_paused )
		return;
	
	if( typeof(start_time) != 'number' )
		start_time = kROTATE_TIME_IN_MS;
	
	if( ss ) {
		ss._hold = false;
		uhThread++;
		setTimeout(
			updateSsImage,
			start_time
		);
	}
}

function ss_shift( places ) {
	if( arr_ss.length < 2 )
		return;
	
	if( uhCurrSlide + places < 0 )
		place = kNUMBER_ss_IMAGES + ( uhCurrSlide + places );
	else
		place	= ( uhCurrSlide + places ) % kNUMBER_ss_IMAGES;
	
	slide = eval( arr_ss[ place ] );
	
//	alert( '( uhCurrSlide + places ) % kNUMBER_ss_IMAGES = ' + '( ' + uhCurrSlide + ' + ' + places + ' ) % ' + kNUMBER_ss_IMAGES + ' = ' + place );
//	alert( 'arr_ss[ ' + place + ' ] = ' + source );
	
	setCurrSlide( place );
	applyStyle( ss_bg.id, 'backgroundImage', 'url(' + slide.src + ')' );
	changeOpac( 0, ss.id );
	
	if( slide._full ) {
		ss.getParent( ).href = slide._link.href;
		ss.src = slide.src;
	}
}

function setCurrSlide( slide ) {
	uhCurrSlide = slide;
}


function applyStyle( p_id, p_style, p_value ) {
	eval( 'getObj(p_id).style.' + p_style + ' = "' + p_value + '"');
}

function applyProperty( p_id, p_property, p_value ) {
	eval( 'getObj(p_id).' + p_property + ' = "' + p_value + '"');
}

function pop( id, img_width, img_height, name ) {
	
	win_width	= img_width + 40;
	win_height	= img_height + 100;
	
	if( document.all )
		win_height += 20;
	
	var pop = window.open( '', 'ss_pop', 'width=' + win_width + ', height=' + win_height + ', resizable' );
	
	pop.resizeTo( win_width, win_height );
	
	pop.document.open();
	
	pop.document.write( '' +
		'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' +
		'<html xmlns="http://www.w3.org/1999/xhtml">' +
		'	<head>' +
		'		<title>Town & Shore | ' + name + ' (full)</title>' +
		'		<link type="text/css" href="' + AURI + 'resource/style/master.all.css" rel="stylesheet" />' +
		'	</head>' +
		'	<body style="text-align:center; margin:0px; padding:10px;" class="BodyText">' +
		'		<p style="margin:0px;">' +
		'			<img	src="' + AURI + 'resource/image/image_viewer.php?id=' + id + '&cache"' +
		'					style="width:' + img_width + 'px; height:' + img_height + 'px;"' +
		'					class="outline"' +
		'					alt="Full size of ' + name + '" />' +
		'		</p>' +
		'		<p>' +
		'			<a href="javascript: self.close();">close</a>' +
		'		</p>' +
		'	</body>' +
		'</html>'
	);
	
	pop.document.close();
	pop.focus();
}

function ss_print() {
	ssPause();
	
	ss.src = extract_css_url( ss_bg.id );
	changeOpac( 100, ss.id );
	
//	alert( ss_bg.style.backgroundImage + ' = ' + ss.src + ' at ' + ss.style.opacity + ' opacity' );
	
	if( window.print )
		window.print();
	else
		alert( 'Unfortunately, this browser does not support our function. Please use your browser\'s Print button.' );
	
	ssResume();
}

function extract_css_url( id ) {
	src = getObj( id ).style.backgroundImage;
    src = src.match( /^url\([\s"']*([^\)]*?)[\s"']*\)$/ );
    return src[1];
}


// http://www.brainerror.net/scripts_js_blendtrans.php
function blendimage(divid, imageid, imagefile, millisec) { 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 
    
    bg	= getObj( divid );
    img	= getObj( imageid );
    
//    if( (bg.style.width == img.style.width) && (bg.style.height == img.style.height) )
//    	same_size = true;
//    else
//    	same_size = false;
    
    
/*    //set the current image as background 
    bg.style.backgroundImage = "url(" + img.src + ")"; 
    
    //make image transparent 
    changeOpac(0, imageid);
    
    //make new image 
    img.src = imagefile; 
*/
     
    //make new image
    img.src = extract_css_url( bg.id );
	
    //make image opaque 
    changeOpac( 99, imageid );
    
//    alert( bg.style.backgroundImage + ' (bg) = ' + img.src + ' (img) @ ' + img.style.filter );
     
    //set the current image as background
    bg.style.backgroundImage = "url(" + imagefile + ")"; 

    //fade out image
    for(i = 99; i >= 0; i--) { 
        setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed)); 
        timer++; 
    } 
}

function opacity(id, opacStart, opacEnd, millisec) { 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } 
} 

//change the opacity for different browsers 
function changeOpac(opacity, id) { 
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
}