
var flashMovie;

$( document ).ready( function() {

	posSite();

	if (document.getElementById) {

		flashMovie = document.getElementById( "flashGallery" );
	}

	fadeOutMainMenu();

	updateGalleryListMargin();
	galleryListItemInit();
	galleryNav();

	galleryItem();

	initScroll();
});

function posSite() {

//	tmpX = $( document ).width() / 2 - 692 / 2;
//	tmpY = $( document ).height() / 2 - 454 / 2;
//	tmpY -= 40;

	tmpX = 0;
	tmpY = 0;

	$( 'div#content' ).css( 'left', tmpX ).css( 'top', tmpY );

	$( 'div#head' ).css( 'left', tmpX ).css( 'top', tmpY - 45 );

	$( 'div#mainMenuCont' ).css( 'left', tmpX - 9 - 100 ).css( 'top', tmpY );

	$( 'div#madeBy' ).css( 'left', tmpX );

	$( 'body' ).css( 'display', 'block' );
}

function fadeOutMainMenu() {

	$( 'div#mainMenuCont img' ).css( 'opacity', '0.3' );
}

function updateGalleryListMargin() {

	if ( $( 'div#galleryList' ).length == 0 ) return;

	$( 'div#galleryList div.galleryListItem' ).css( 'margin-right', '16px' );

	$( 'div#galleryList div.galleryListItem:nth-child(3n+3)' ).css( 'margin-right', '0' );

	$( 'div#galleryList div.galleryListItem:nth-child(9n+7)' ).css( 'margin-bottom', '0' );
	$( 'div#galleryList div.galleryListItem:nth-child(9n+8)' ).css( 'margin-bottom', '0' );
	$( 'div#galleryList div.galleryListItem:nth-child(9n+9)' ).css( 'margin-bottom', '0' );
}

function galleryListItemInit() {

	if ( $( 'div#galleryList' ).length == 0 ) return;

	$( 'div.galleryListItem a img' ).mouseover( function() {

		$( this ).animate( { opacity: 0.75 }, 350 );
	}).mouseout( function() {

		$( this ).animate( { opacity: 1 }, 200 );
	});
}

var galleryCurPage;
function galleryNav() {

	if ( $( 'div#galleryList' ).length == 0 ) return;

	if ( ! galleryCurPage )
				galleryCurPage = 1;

	var count = parseInt( $( '#numGalItem' ).val() );
	var maxPage = Math.ceil( count / 9 );

	$( 'span.site' ).html( galleryCurPage + '&nbsp;/&nbsp;' + maxPage );

	$( 'span.nav img' ).click( function() {

		if ( count < 10 ) return;

		if ( $( this ).hasClass( 'next' ) ) {

			if ( galleryCurPage == maxPage ) return;

			galleryCurPage++;
		} else {

			if ( galleryCurPage == 1 ) return;

			galleryCurPage--;
		}

		//
		$( 'div#galleryList' ).fadeOut( 500,
			function() {

				$( 'div#galleryList div.galleryListItem' ).css( 'display', 'none' );

				for ( i = 1; i < 10; i++ )
					$( 'div#galleryList div.galleryListItem:nth-child(' + (((galleryCurPage - 1) * 9) + i) + ')' ).css( 'display', 'block' );

				$( 'div#galleryList' ).fadeIn( 300 );
		});



		$( 'span.site' ).html( galleryCurPage + '&nbsp;/&nbsp;' + maxPage );
	});
}

var galleryCurPageGalItem=1;
var galItemcurPos = 0;
var clickDisabledGalItem = false;
var clickDisabledGalItem_2 = false;
var waitGalItemLoad = true;
function galleryItem() {

	if ( $( 'div#galleryItem' ).length == 0 || clickDisabledGalItem ) return;

	$( 'div.galleryItemNavCont img:nth-child(6n+6)' ).css( 'margin-right', 0 );

	var count = parseInt( $( '#numGalItem' ).val() );
	var maxPage = Math.ceil( count / 6 );

	$( 'div.galleryItemNavBar img' )
		.mouseover( function() {
			if ( $( this ).hasClass( 'disabled' ) ) return;
			$( this ).attr( 'src', 'assets/img/btn_' + $( this ).attr( 'alt' ) + '_over.gif' );
		})
		.mouseout( function() {
			if ( $( this ).hasClass( 'disabled' ) ) return;
			$( this ).attr( 'src', 'assets/img/btn_' + $( this ).attr( 'alt' ) + '_out.gif' );
		});

	$( 'img.prev' ).attr( 'src', 'assets/img/btn_prev_disabled.gif' );
	$( 'img.prev' ).addClass( 'disabled' );

	$( 'div.galleryItemNavBar img' ).click( function() {

		if ( count < 7 ) return;

		if ( $( this ).hasClass( 'next' ) ) {

			if ( galleryCurPageGalItem == maxPage ) return;

			galleryCurPageGalItem++;

			if ( galleryCurPageGalItem == maxPage ) {
				$( this ).attr( 'src', 'assets/img/btn_' + $( this ).attr( 'alt' ) + '_disabled.gif' );
				$( this ).addClass( 'disabled' );
			}

			if ( $( 'img.prev' ).hasClass( 'disabled' ) ) {
				$( 'img.prev' ).attr( 'src', 'assets/img/btn_prev_out.gif' );
				$( 'img.prev' ).removeClass( 'disabled' );
			}
		} else if ( $( this ).hasClass( 'prev' ) ) {

			if ( galleryCurPageGalItem == 1 ) return;

			galleryCurPageGalItem--;

			if ( galleryCurPageGalItem == 1 ) {
				$( this ).attr( 'src', 'assets/img/btn_' + $( this ).attr( 'alt' ) + '_disabled.gif' );
				$( this ).addClass( 'disabled' );
			}

			if ( $( 'img.next' ).hasClass( 'disabled' ) ) {
				$( 'img.next' ).attr( 'src', 'assets/img/btn_next_out.gif' );
				$( 'img.next' ).removeClass( 'disabled' );
			}
		}

		clickDisabledGalItem = true;

		$( 'div.galleryItemNavCont' ).fadeOut( 500,
			function() {

				$( 'div.galleryItemNavCont img' ).css( 'display', 'none' );

				for ( i = 1; i < 7; i++ )
					$( 'div.galleryItemNavCont img:nth-child(' + (((galleryCurPageGalItem - 1) * 6) + i) + ')' ).css( 'display', 'inline' );

				$( 'div.galleryItemNavCont' ).fadeIn( 300, function() { clickDisabledGalItem = false; });
		});
	});

	$( 'div.galleryItemNavCont img.active' ).css( 'opacity', '0.5' );
	$( 'div.galleryItemNavCont img.active:nth-child(1)' ).css( 'opacity', '1' );



	$( 'div.galleryItemNavCont img.active' ).mouseover( function() {

		if ( $( this ).attr( 'alt' ) == galItemcurPos ) return;

		$( this ).animate( { opacity: 1 }, 350 );
	}).mouseout( function() {

		if ( $( this ).attr( 'alt' ) == galItemcurPos ) return;

		$( this ).animate( { opacity: 0.5 }, 200 );
	}).click( function() {

		if ( clickDisabledGalItem || clickDisabledGalItem_2 || waitGalItemLoad ) return;
		clickDisabledGalItem_2 = true;

		galItemcurPos = parseInt( $( this ).attr( 'alt' ) );

		$( 'div.galleryItemNavCont img.active' ).css( 'opacity', '0.5' );
		$( this ).css( 'opacity', '1' );
		flashMovie.galleryMoveTo( $( this ).attr( 'alt' ) );
	});
}

function galleryLoadFinishd() {

	waitGalItemLoad = false;
}

function galleryMoveFinishd() {

	clickDisabledGalItem_2 = false;
}

function lockGallery() {

	clickDisabledGalItem_2 = true;
}

function galleryMoved( value ) {

	if ( value == '1' ) {

		galItemcurPos++;

		if ( galItemcurPos % 6 == 0 ) $( 'div.galleryItemNavBar img.next' ).trigger( 'click' );
	} else {

		galItemcurPos--;

		if ( (galItemcurPos + 1) % 6 == 0 ) $( 'div.galleryItemNavBar img.prev' ).trigger( 'click' );
	}

	$( 'div.galleryItemNavCont img.active' ).css( 'opacity', '0.5' );
	$( 'div.galleryItemNavCont img.active:nth-child(' + (galItemcurPos + 1) + ')' ).css( 'opacity', '1' );

	clickDisabledGalItem_2 = false;
}

var yTmp;
var scrollTop;
function initScroll() {

	if( $( 'div#textContentDesc' ).length == 0 ) return;

	if ( $( 'div#textContentCont' ).height() >= $( 'div#textScrollBox div' ).height() ) {

		$( 'div#textScrollBox' ).width( 286 );
		return;
	}

		$( 'div#textScrollBox' ).width( 264 );
		$( 'div#scrollBar' ).css( 'display', 'block' );

		$( 'div#scrollHandle' ).mouseover( function() {

			$( 'body' ).bind( 'mousedown', function( E ) {

				yTmp = E.pageY;
				scrollTop = $( 'div#scrollHandle' ).position().top - 30;

				$( 'body' ).bind( 'mousemove', onScrollMouseMove );
			}).mouseup( function() {

				$( 'body' ).unbind( 'mousemove', onScrollMouseMove );
				$( 'body' ).focus();
			});
		}).mouseout( function() {

			$( 'body' ).unbind( 'mousedown' );
		});
}

function onScrollMouseMove( E ) {

	var dist = E.pageY - yTmp;

	if ( scrollTop + dist <= 0 ) dist = -1 * scrollTop;
	if ( scrollTop + dist > 393 - $( 'div#scrollHandle' ).height() ) dist = 393 - $( 'div#scrollHandle' ).height() - scrollTop;

	$( 'div#scrollHandle' ).css( 'top', scrollTop + dist );

	updatetextScrollBoxTop( ( scrollTop + dist ) / (393 - $( 'div#scrollHandle' ).height()) );
}

function updatetextScrollBoxTop( pos ) {

	var newTop = -1 * ( $( 'div#textScrollBox div' ).height() - $( 'div#textContentCont' ).height() ) * pos;
	$( 'div#textScrollBox div' ).css( 'top', newTop );
}
