// JavaScript Document

var curr_imgnum 	= 1;
var curr_enlnum 	= 1;
var curr_pgnum 		= 1;
var total_pgnum;
var tn_width	 	= 79;
var tn_perpg 		= 4;
var tn_pgwidth 		= tn_width * tn_perpg;
var tn_pgx 			= 0;

function dmag_nav_clickthumb ( imgnum ) {
	if ( pp_playing ) 	dmag_playpause ();
	dmag_loadimg ( imgnum );
}
function dmag_nav_clickbtn ( incr ) {
	if ( pp_playing ) 	dmag_playpause ();
	dmag_navtoimg ( Number ( incr ) );
}
function dmag_loadimg ( imgnum ) {
	//alert ( 'dmag_loadimg ( imgnum:' + imgnum + ' );' );
	var main_img 	= document.getElementById ( 'img_gallery-main_img' );
	var loader 		= document.getElementById ( 'img_gallery-loader' );
	var prev_tn 	= document.getElementById ( 'img_gallery-thumb' + curr_imgnum );
	var curr_tn 	= document.getElementById ( 'img_gallery-thumb' + imgnum );
	var caption 	= document.getElementById ( 'img_gallery-caption' );
	main_img.style.display 	= 'none';
	loader.style.display 	= 'block';
	prev_tn.className 		= 'img_gallery-thumb_old';
	caption.innerHTML 		= ( o_gimg[ imgnum ].caption ) ? urldecode ( o_gimg[ imgnum ].caption ) : urldecode ( o_gimg[ imgnum ].description );
	
	var img_url 	= o_gimg[ imgnum ].base_url + '/' + o_gimg[ imgnum ].medium.file;
	var tmp_img 	= new Image ();
	tmp_img.src 	= img_url;
	tmp_img.onload 	= function () {
		main_img.src 	= img_url;
		// ---	center the image
		marleft 	= String ( Math.round ( ( 410 - o_gimg[ imgnum ].medium.width ) /2 ) ) + 'px';
		martop 		= String ( Math.round ( ( 245 - o_gimg[ imgnum ].medium.height ) /2 ) );
		martop 		= ( martop < 0 ) ? '0px' : martop + 'px';
		loader.style.display 	= 'none';
		main_img.style.display 	= 'block';
		main_img.style.marginLeft 	= marleft;
		main_img.style.marginTop 	= martop;
		fade_img 	= new OpacityTween ( main_img, Tween.linear, 0, 100, 1 );
		fade_img.start ();
		curr_tn.className 	= 'img_gallery-thumb_curr';
		dmag_playpause_check ();
	}
	if ( !total_pgnum ) {
		var count = 0;
		for ( i in o_gimg ) count ++;
		total_pgnum 	= Math.ceil ( count / tn_perpg );
	}
	curr_imgnum 	= imgnum;
	curr_enlnum 	= imgnum;
	dmag_setpgnav ();
}
function dmag_setpgnav () {
	//alert ( 'dmag_setpgnav ()' );
	var prev_btn 	= document.getElementById ( 'img_gallery-prev_btn' );
	var next_btn 	= document.getElementById ( 'img_gallery-next_btn' );
	prev_btn.className 	= 'img_gallery-btn_on';
	next_btn.className 	= 'img_gallery-btn_on';
	if ( curr_pgnum <= 1 ) 				prev_btn.className 	= 'img_gallery-btn_off';
	if ( curr_pgnum >= total_pgnum ) 	next_btn.className 	= 'img_gallery-btn_off';
}
function dmag_navtoimg ( incr ) {
	//alert ( 'dmag_navtoimg ( incr:' + incr + ' );' );
	var next_pg 	= curr_pgnum + incr;
	if ( next_pg > 0 && next_pg <= total_pgnum ) {
		var tn_slide 	= document.getElementById ( 'img_gallery-thumb_slide' );
		tn_prevx 	= ( tn_pgwidth * ( curr_pgnum -1 ) * ( -1 ) );
		tn_pgx 		= ( tn_pgwidth * ( next_pg -1 ) * ( -1 ) );
		//alert ( 'tn_pgx = ' + tn_pgx );
		slide_tn 	= new Tween ( tn_slide.style, 'left', Tween.strongEaseOut, tn_prevx, tn_pgx, 1, 'px' );
		slide_tn.start ();
		curr_pgnum 	= next_pg;
		dmag_setpgnav ();
	}
}

// ---	control functions for enlarged image view
function dmag_enlargeimg () {
	//alert ( 'dmag_enlargeimg ();' );
	if ( pp_playing ) 	dmag_playpause ();
	dmag_playpause_hide ();
	document.getElementById ( 'img_gallery-enlarged_container' ).style.display 	= 'block';
	var o_imginfo 	= o_gimg[ curr_enlnum ];
	var enl_cont 	= document.getElementById ( 'img_gallery-enlarged' );
	var enl_img 	= document.getElementById ( 'img_gallery-enlarged_img' );
	var enl_nav 	= document.getElementById ( 'img_gallery-enlarged_nav' );
	var enl_cap 	= document.getElementById ( 'img_gallery-enlarged_nav-caption' );
	var loader 		= document.getElementById ( 'img_gallery-enlarged_loader' );
	// ---	check for image higher than body
	var premarleft 	= o_imginfo.large.width /2;
	var premartop 	= o_imginfo.large.height /2;
	if ( document.body.clientHeight ) {
		var bodyhalf 	= document.body.clientHeight /2;
		if ( premartop > bodyhalf ) 	premartop 	= bodyhalf;
	}
	enl_nav.style.display 		= 'none';
	enl_cap.innerHTML 			= ( o_imginfo.caption ) ? urldecode ( o_imginfo.caption ) : urldecode ( o_imginfo.description );
	loader.style.display 		= 'block';
	
	var img_url 	= o_imginfo.base_url + '/' + o_imginfo.large.file;
	var tmp_img 	= new Image ();
	tmp_img.src 	= img_url;
	tmp_img.onload 	= function () {
		enl_cont.style.width 		= o_imginfo.large.width + 'px';
		enl_cont.style.height 		= o_imginfo.large.height + 'px';
		enl_cont.style.marginLeft 	= String ( Math.round ( premarleft ) * ( -1 ) ) + 'px';
		enl_cont.style.marginTop 	= String ( Math.round ( premartop ) * ( -1 ) ) + 'px';
		enl_img.src 	= img_url;
		loader.style.display 	= 'none';
		enl_img.style.display 	= 'block';
		enl_nav.style.display 	= 'block';
		dmag_ppenl_check ();
	}
	dmag_setenlnav ();
}
function dmag_enlargehide () {
	//alert ( 'dmag_enlargehide ()' );
	if ( ppenl_playing ) 	dmag_ppenl ();
	document.getElementById ( 'img_gallery-enlarged_container' ).style.display 	= 'none';
	document.getElementById ( 'img_gallery-enlarged_img' ).style.display 		= 'none';
}
function dmag_enlargenav ( incr ) {
	//alert ( 'dmap_enlargenav ( incr:' + incr + ' );' );
	if ( ppenl_playing ) 	dmag_ppenl ();
	var next_img 	= curr_enlnum + incr;
	if ( next_img > 0 && next_img <= o_gimg.total_img ) {
		curr_enlnum 	= next_img;
		dmag_enlargeimg ();
	}
}
function dmag_setenlnav () {
	var prev_btn 	= document.getElementById ( 'img_gallery-enlarged_nav-prev_btn' );
	var next_btn 	= document.getElementById ( 'img_gallery-enlarged_nav-next_btn' );
	prev_btn.className 	= 'img_gallery-enlarged_nav-btn';
	next_btn.className 	= 'img_gallery-enlarged_nav-btn';
	if ( curr_enlnum <= 1 ) 				prev_btn.className 	= 'img_gallery-enlarged_nav-btn_off';
	if ( curr_enlnum >= o_gimg.total_img ) 	next_btn.className 	= 'img_gallery-enlarged_nav-btn_off';
}
function urldecode ( str ) {
	str 	= str.replace ( /\+/g, ' ' );
	str 	= unescape ( str );
	return str;
}
function enlnav_over () {
	//alert ( 'enlnav_over ()' );
	document.getElementById ( 'img_gallery-enlarged_nav' ).style.height 	= 'auto';
}
function enlnav_out () {
	//alert ( 'enlnav_out ()' );
	document.getElementById ( 'img_gallery-enlarged_nav' ).style.height 	= '20px';
}

// ---	play/pause functions
var pp_vis 		= false; // play/pause visible
var pp_playing 	= true;
var pp_timeout;
var pp_time 	= 3000;
function dmag_playpause_init () {
	var playpause 	= document.getElementById ( 'img_gallery-playpause' );
	var mainimg 	= document.getElementById ( 'img_gallery-main_container' );
	var rollout 	= document.getElementById ( 'img_gallery-rollout' );
	playpause.onclick 		= function () { dmag_playpause (); };
	mainimg.onmouseover 	= function () { dmag_playpause_show (); };
	rollout.onmouseover 	= function () { dmag_playpause_hide (); };
	playpause.style.display 	= 'none';
	rollout.style.display 		= 'none';
	dmag_ppenl_init ();
}
function dmag_playpause_check () {
	if ( !pp_playing )	return;
	//alert ( 'dmag_playpause_check ()' );
	// ---	this function is called after image loads.
	//		this is to keep the slideshow timeout from 
	//		running while the image is downloading
	pp_timeout 	= setTimeout ( 'dmag_playpause_next()', pp_time );
}
function dmag_playpause_next () {
	if ( !pp_playing ) 	return;
	//alert ( 'dmag_playpause_next ()' );
	// ---	get current image number and increment 
	imgnum 	= curr_imgnum +1;
	// ---	check for last image and set to 1
	if ( imgnum > o_gimg.total_img ) 	imgnum = 1;
	// ---	call load image function
	dmag_loadimg ( imgnum );
	// ---	check if we need to move the nav to the next page or back to beginning: 1 -currpg
	var setpg 	= Math.ceil ( imgnum / tn_perpg );
	if ( setpg != curr_pgnum ) {
		// ---	find increment from current page
		var incr 	= setpg -curr_pgnum;
		dmag_navtoimg ( incr );
	}
}
function dmag_playpause () {
	//alert ( 'playpause ()' );
	// ---	toggle pp_playing value
	pp_playing 		= ( pp_playing ) ? false : true;
	var pp_btn 		= document.getElementById ( 'img_gallery-playpause-btn' );
	var btnimgpath 	= '/wp-content/themes/rowepalettes/css/templates/';
	if ( pp_playing ) 	pp_btn.src 	= btnimgpath + 'pause_btn.gif';
	else 				pp_btn.src 	= btnimgpath + 'play_btn.gif';
	dmag_playpause_next ();
}
function dmag_playpause_show () {
	if ( pp_vis ) 	return;
	//alert ( 'playpause_show ()' );
	var playpause 	= document.getElementById ( 'img_gallery-playpause' );
	var rollout 	= document.getElementById ( 'img_gallery-rollout' );
	playpause.style.display 	= 'block';
	rollout.style.display 		= 'block';
	pp_vis 	= true;
}
function dmag_playpause_hide () {
	//alert ( 'playpause_hide ()' );
	var playpause 	= document.getElementById ( 'img_gallery-playpause' );
	var rollout 	= document.getElementById ( 'img_gallery-rollout' );
	playpause.style.display 	= 'none';
	rollout.style.display 		= 'none';
	pp_vis 	= false;
}


// ---	play/pause functions for enlarged view
var ppenl_vis 		= false; // play/pause visible
var ppenl_playing	= true;
var ppenl_timeout;
var ppenl_time 		= 3000;
function dmag_ppenl_init () {
	// ---	this function called from dmag_playpause_init
	var playpause 	= document.getElementById ( 'img_gallery-enl_pp-btn' );
	var rollon 		= document.getElementById ( 'img_gallery-enl_pp-rollon' );
	var rollout 	= document.getElementById ( 'img_gallery-enl_pp-rollout' );
	playpause.onclick 		= function () { dmag_ppenl (); };
	rollon.onmouseover 		= function () { dmag_ppenl_show (); };
	rollout.onmouseover 	= function () { dmag_ppenl_hide (); };
	playpause.style.display 	= 'none';
	rollout.style.display 		= 'none';
}
function dmag_ppenl_check () {
	//alert ( 'dmag_playpause_check () \n ppenl_playing = ' + ppenl_playing );
	if ( !ppenl_playing )	return;
	// ---	this function is called after image loads.
	//		this is to keep the slideshow timeout from 
	//		running while the image is downloading
	ppenl_timeout 	= setTimeout ( 'dmag_ppenl_next()', ppenl_time );
}
function dmag_ppenl_next () {
	if ( !ppenl_playing ) 	return;
	//alert ( 'dmag_playpause_next ()' );
	// ---	get current image number and increment 
	curr_enlnum ++;
	// ---	check for last image and set to 1
	if ( curr_enlnum > o_gimg.total_img ) 	curr_enlnum = 1;
	// ---	call load image function
	dmag_enlargeimg ();
}
function dmag_ppenl () {
	//alert ( 'dmag_ppenl ()' );
	// ---	toggle pp_playing value
	ppenl_playing 		= ( ppenl_playing ) ? false : true;
	var ppenl_btn 		= document.getElementById ( 'img_gallery-enl_pp-btn' );
	var btnimgpath 		= '/wp-content/themes/rowepalettes/css/templates/';
	if ( ppenl_playing ) 	ppenl_btn.src 	= btnimgpath + 'pause_btn.gif';
	else 					ppenl_btn.src 	= btnimgpath + 'play_btn.gif';
	dmag_ppenl_next ();
}
function dmag_ppenl_show () {
	if ( ppenl_vis ) 	return;
	//alert ( 'dmag_ppenl_show ()' );
	var playpause 	= document.getElementById ( 'img_gallery-enl_pp-btn' );
	var rollout 	= document.getElementById ( 'img_gallery-enl_pp-rollout' );
	playpause.style.display 	= 'block';
	rollout.style.display 		= 'block';
	ppenl_vis 	= true;
}
function dmag_ppenl_hide () {
	//alert ( 'dmag_ppenl_hide ()' );
	var playpause 	= document.getElementById ( 'img_gallery-enl_pp-btn' );
	var rollout 	= document.getElementById ( 'img_gallery-enl_pp-rollout' );
	playpause.style.display 	= 'none';
	rollout.style.display 		= 'none';
	ppenl_vis 	= false;
}


