// JavaScript Document

document.observe( "dom:loaded", function() { // initiate page
	$$( '.lnk', '.arrow' ).each( function( element ) {
		setRollOver( element );
	} );
	$$( '.side_bar' ).each( function( element ) { // disable user selection in navigations
		element.onselectstart = function() { return false; };
	} );
} );

function initprojpage( allImg ) { // initiate project page
	var imgnumsin = '', containertxtin = $( 'proj_container_txt' ).innerHTML;
	for ( i = 1; i <= allImg; i ++ ) {
		if( i == 11 ) imgnumsin += '<br>';
		imgnumsin += '<div class="lnk">' + i + '</div>';
	}
	with( top ) {
		$( 'proj_img_nums' ).innerHTML = imgnumsin;
		$$( '.lnk', '.arrow' ).each( function( element ) {
			setRollOver( element );
		} );
		var ar = $$( '#proj_img_nums > div' );
		ar.each( function( element ) {
			element.observe( 'click', function() { if( !element.className.endsWith( '-on' ) ) fr.turn_image( element ); } );
		} );
		$( 'proj_container_txt' ).innerHTML = containertxtin;
		Effect.Queues.get( 'proj_dtls' ).each( function( effect ) { effect.cancel(); } );
		with(  $( 'proj_dtls' ) ) {
			if( allImg > 10 ) addClassName( 'btmfix' );
			else removeClassName( 'btmfix' );
		}
		new Effect.Appear( $( 'proj_dtls' ), {
			duration: 1.0,
			queue: { position: 'front', scope: 'proj_dtls', limit: 1 }
		} );
		prev_title = $( 'proj_img_prev' ).readAttribute( 'title' );
		next_title = $( 'proj_img_next' ).readAttribute( 'title' );
	}
	big_img_ctrl( ar.first() );
}

function setRollOver( element ) { // set rollover
	var cl = element.className + '-over';
	element.observe( 'mouseenter', function() { if( !element.className.endsWith( '-on' ) && !element.className.endsWith( '-off' ) ) element.addClassName( cl ); } )
	.observe( 'mouseleave', function() { if( !element.className.endsWith( '-on' ) && !element.className.endsWith( '-off' ) ) element.removeClassName( cl ); } );
}

function killRollOver( element, original_class ) { // kill rollover
	element.stopObserving( 'mouseenter' ).stopObserving( 'mouseleave' ).removeClassName( original_class + '-over' );
}

function goToURL( el ) { // general nav bar navigation action
	with( el ) if( !className.endsWith( '-on' ) && !className.endsWith( '-off' ) ) MM_goToURL( 'top', readAttribute( 'href' ) );
}

function goToProj( el ) { // projests nav bar navigation action
	if( $( el ).hasClassName( 'lnk-off' ) ) return;
	if( typeof( current_on ) == 'undefined' ) var current_on = $( 'proj_img_navbar' ).down( '.lnk-on' );
	if( el == current_on ) return;
	$( 'proj_dtls' ).hide();
	killRollOver( el, 'lnk' );
	el.addClassName( 'lnk-on' );
	current_on.removeClassName( 'lnk-on' ).addClassName( 'lnk' );
	setRollOver( current_on );
	current_on = el;
	$( 'proj_frame' ).src = el.readAttribute( 'href' );
}

function ctrl_subnav_out( el, subnav ) { // sub nav bar collapse behavior
	new Effect.BlindUp( subnav, {
		duration: 0.3,
		queue: { position: 'front', scope: subnav.id, limit: 2 },
		afterFinish: function() {
			el.removeClassName( 'lnk-live-on' );
			setRollOver( el );
		}
	} );
}

function set_subnav_timeout( el, subnav ) { // set sub nav bar collapse timeout
	to = window.setTimeout( function() { ctrl_subnav_out( el, subnav ); } , 500 );
}

function clear_subnav_timeout() { // clear sub nav bar collapse timeout
	if( window.to ) window.clearTimeout( window.to );
}
function ctrl_subnav( el, subnav ) { // sub nav bar expand & collapse control
	subnav = $( subnav );
	if( !subnav.visible() ) {
		killRollOver( el, 'lnk' );
		el.addClassName( 'lnk-live-on' );
		[ el, subnav ].each( function( element ) {
			element.observe( 'mouseenter', function() { clear_subnav_timeout(); } )
			.observe( 'mouseleave', function() {
				clear_subnav_timeout();
				set_subnav_timeout( el, subnav );
			} );
		} );
		new Effect.BlindDown( subnav, {
			duration: 0.6,
			queue: { position: 'end', scope: subnav.id, limit: 2 },
			beforeStart: function() {
				Effect.Queues.get( subnav.id ).each( function( effect ) { effect.cancel(); } );
				subnav.hide().setStyle( { height: '' } );
			}
		} );
	} else {
		el.stopObserving( 'mouseleave' );
		clear_subnav_timeout();
		ctrl_subnav_out( el, subnav );
	}
}

function big_img_ctrl( clicked ) { // big image presentation control
	fr = top.frames['proj_frame'];
	if ( typeof( target ) == 'undefined' ) target = fr.$( 'proj_img' );
	target.hide().src = '';
	cl = clicked;
	killRollOver( cl, 'lnk' );
	cl.addClassName( 'lnk-on' );
	if ( typeof( current ) != 'undefined' ) {
		current.removeClassName( 'lnk-on' );
		setRollOver( current );
	}
	current = cl;
	var img_total = top.$( 'proj_img_nums' ).select( 'div' ).size() - 1;
	var img_ind = top.$( 'proj_img_nums' ).select( 'div' ).indexOf( clicked );
	var sp = fr.$( 'proj_container' );
	sp.addClassName( 'spinner' );
	var pre_image = new Image();
	pre_image.onload = function() {
		sp.removeClassName( 'spinner' );
		target.writeAttribute( 'width', pre_image.width );
		target.src = pre_image.src;
		new Effect.Appear( target, {
			duration: 1.5,
			queue: { position: 'end', scope: 'proj_img', limit: 1 }
		} );
	};
	pre_image.src = 'images/' + ( img_ind + 1 ) + '.jpg';
	with( top ) {
		if( img_total > 0 && img_ind > 0 ) {
			$( 'proj_img_prev' ).removeClassName( 'arrow-off' ).writeAttribute( 'title', prev_title );
			setRollOver( $( 'proj_img_prev' ) );
		} else {
			killRollOver( $( 'proj_img_prev' ), 'arrow' );
			$( 'proj_img_prev' ).addClassName( 'arrow-off' ).writeAttribute( 'title', false );		
		}
		if( img_total > 0 && img_ind < img_total ) {
			$( 'proj_img_next' ).removeClassName( 'arrow-off' );
			setRollOver( $( 'proj_img_next' ) );
			target.removeClassName( 'off' );
			[target, $( 'proj_img_next' ) ].each( function( element ) { element.writeAttribute( 'title', next_title ); } );
		} else {
			killRollOver( $( 'proj_img_next' ), 'arrow' );
			$( 'proj_img_next' ).addClassName( 'arrow-off' );
			target.addClassName( 'off' );
			[target, $( 'proj_img_next' ) ].each( function( element ) { element.writeAttribute( 'title', false ); } );
		}
	}
}

function turn_image( ind ) { // big image refresh command
	if( ind == 'next' ) {
		if( top.$( 'proj_img_next' ).className.endsWith( '-off' ) ) return;
		else cl = current.next( 'div.lnk' );
	} else if( ind == 'prev' ) {
		if( top.$( 'proj_img_prev' ).className.endsWith( '-off' ) ) return;
		else cl = current.previous( 'div.lnk' );
	} else cl = ind;
	Effect.Queues.get( 'proj_img' ).each( function( effect ) { effect.cancel(); } );
	new Effect.Fade( target, {
		duration: 0.5,
		queue: { position: 'front', scope: 'proj_img', limit: 1 },
		afterFinish: function() { big_img_ctrl( cl ); } 
	} );
}

function home_images_init() { // initiate homepage image rotation
	home_ind = 0;
	home_total = 12;
	home_target = $( 'home_img' );
	sp = $( 'content' );
	home_images_rotate();
	to = window.setTimeout( function() { pe = new PeriodicalExecuter( function() { home_images_rotate(); }, 5.0 ); } , 450 );
}

function home_images_preload() { // preload rotated homepage image
	home_ind %= home_total;
	home_ind ++;
	sp.addClassName( 'spinner' );
	var pre_image = new Image();
	pre_image.onload = function() {
		sp.removeClassName( 'spinner' );
		home_target.writeAttribute( 'width', pre_image.width );
		home_target.src = pre_image.src;
		new Effect.Appear( home_target, {
			duration: 1.5,
			queue: { position: 'end', scope: 'home_img', limit: 1 }
		} );
	};
	pre_image.src = 'images/home_img_' + home_ind + '.jpg';
}

function home_images_rotate() { // rotate homepage image
	if( !home_target.visible() ) {
		home_images_preload();
		return;
	}
	new Effect.Fade( home_target, {
		duration: 0.5,
		queue: { position: 'front', scope: 'home_img', limit: 1 },
		afterFinish: function() { home_images_preload(); }
	} );
}

function open_pdf( element ) {
	MM_openBrWindow( element.readAttribute( 'href' ), '', 'status=yes,resizable=yes' );
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&top.frames.length) {
    d=top.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

