// JavaScript Document

var public = new Array( 'plant_sciences_center', 'foreign_students_dwellings', 'aleh_science_school', 'students_dormitories', 'multi_functional_building', 'animal_sciences_center', 'weitz_institute', 'agamon_hakhula' );
var residential = new Array( 'tsahala_roses', 'paglin', 'north_tel_baruch', 'kfar_saba', 'kfar_shalem', 'bnei_moshe', 'einstein', 'kaplan_st', 'yaari_st', 'north_tel_aviv' );
var commercial = new Array( 'yediot_ahronot', 'bilu_power_center', 'yaacobi_steel_factory', 'haomanim_st', 'bank_hapoalim' );
var urban = new Array( 'faculty_of_agriculture', 'jabotinski_raziel_schools', 'bavli', 'north_telaviv', 'rimon_school', 'kiryat_ono' );

document.observe( 'dom:loaded', function() { // initiate rollover behaviours attachments on mouseenter and mouseleave
	$$( '.topNavBtn', '.sideNavBtn', '.profileNavBtn', '.profileImgCtrlBtn', '.catProj', '.projCtrlBtn' ).each( function( element ) {
		element.observe( 'mouseenter', function() { this.down().appear( { duration:0.3 } ) } )
			.observe('mouseleave', function() { this.down().fade( { duration:0.6 } ) } );
	} );
} );

function extractPath() {
	var docpath = location.pathname, slash;
	if ( docpath.indexOf( '\u005C' ) < 0 ) slash = '/';
	else slash = '\u005C';
	var p = docpath.split( slash );
	return p;
}

function initCatProjPage() {
	var p = extractPath();
	cat_array = window[ p[ p.length - 2 ] ];
	$$( '.catProj' ).each( function( element ) {
		var targeturl = cat_array[ $$( '.catProj' ).indexOf( element ) ] + '/default.html';
		element.observe( 'click', function() { MM_goToURL('parent',targeturl);return document.MM_returnValue } );
	} );
}

// START of project page image gallery control
function initProjPage() {
	
	// START of project navigation controls
	var p = extractPath();
	var cat_array = window[ p[ p.length - 3 ] ];
	var proj_name = p[ p.length - 2 ];
	var proj_index = cat_array.indexOf( proj_name );
	if ( proj_index > 0 ) {
		var targeturlBack = '../' + cat_array[ proj_index - 1 ] + '/default.html';
		$( 'projCtrl_back' ).observe( 'click', function() { MM_goToURL('parent',targeturlBack);return document.MM_returnValue } ).show();
	} else $( 'projCtrl_backOff' ).setOpacity( 0.3 ).show();
	if ( proj_index < cat_array.length - 1 ) {
		var targeturlFwd = '../' + cat_array[ proj_index + 1 ] + '/default.html';
		$( 'projCtrl_fwd' ).observe( 'click', function() { MM_goToURL('parent',targeturlFwd);return document.MM_returnValue } ).show();
	} else $( 'projCtrl_fwdOff' ).setOpacity( 0.3 ).show();
	// END of project navigation controls

	target = $( 'projBigImgTbl' ).down( 'img' );
	cur_img = null;
	thumbs = $$( '.projImg' );
	thumbs.each( function( element ) {
		with ( element ) {
			observe( 'click', function() { flipProjImg( this ) } );
			observeMouse( element );
		}
	} );
	flipProjImg( thumbs[ 0 ] );
}

function flipProjImg( element ) {
	if ( thumbs.indexOf( element ) == cur_img ) return;
	else {
		element.addClassName( 'projImgOn' ).stopObserving( 'mouseenter' ).stopObserving( 'mouseleave' );
		new Effect.Opacity( element, { from: 0.7, to: 0.2, duration: 0.5 } );
		if ( cur_img != null ) {
			thumbs[ cur_img ].removeClassName( 'projImgOn' );
			observeMouse( thumbs[ cur_img ] );
			new Effect.Opacity( thumbs[ cur_img ], { from: 0.2, to: 1.0, duration: 0.5 } );
			new Effect.Fade( target, { duration:0.5, afterFinish: function() { preloadProjImg( element ) } } );
		} else 
		preloadProjImg( element );
		cur_img = thumbs.indexOf( element );
	}
}

function flipProjImgClick() {// advance image when image is clicked
	flipProjImg( thumbs[ ( cur_img + 1 ) % thumbs.size() ] );
}

function preloadProjImg( element ) {
	var sp = $( 'projBigImgTbl' );
	sp.addClassName( 'spinner' );
	var pre_image = new Image();
	pre_image.onload = function() {
		sp.removeClassName( 'spinner' );
		target.src = pre_image.src;
		new Effect.Appear( target, { duration:1.5, queue: 'end' } );
	};
	pre_image.src = element.down( 'img' ).src.gsub( 't.jpg' , '.jpg' );
}

function observeMouse( element ) {
	element.observe( 'mouseenter', function() { new Effect.Opacity( this, { from: 1.0, to: 0.7, duration: 0.3 } ) } )
	.observe( 'mouseleave', function() { new Effect.Opacity( this, { from: 0.7, to: 1.0, duration: 0.3 } ) } );
}
// END of project page image gallery

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_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];}}
}

