// js support detect
//document.getElementById( 'nojs' ).id = 'js';

$( document ).ready( function() {
	// loading
	$( 'body' ).append( '<div id="pageload">Loading...</div>' );
	$( '#pageload' )
		.css({
			'position'		: 'absolute',
			'top'			: '1px',
			'right'			: '4px',
			'font-size'		: '9px',
			'color'			: '#FFFFFF',
			'text-shadow'	: 'none'
		});

	// global navigation
	var $nav_item = $( '#global_menu li' );
	$nav_item.each( function() {
		if ( !$( this ).hasClass( 'active' ) ) {
			$( this ).children( 'a' ).hover( function() {
				$( this )
					.stop()
					.animate({
						'background-position' : '50% 100%'
					}, 300);
			}, function() {
				$( this )
					.stop()
					.animate({
						'background-position' : '50% 200%'
					});
			});
		}
	});

	// input file styling
	$( 'input:file' ).each(function() {
		var $file_wrap = $( '<div class="input-file" title="Select file..."><input type="text" class="input-text" readonly="readonly" value="" /><button type="button"><span>Browse</span></button></div>' );

		$( this )
			.replaceWith( $file_wrap )
			.appendTo( $file_wrap );

		var $file_text		= $( '.input-file' ).children( 'input:text' ),
			$file_button	= $( '.input-file' ).children( 'button:button' ),
			$file_pseudo	= $( '.input-file' ).children( 'input:file' );

		$file_text.css({
			'width' : ( $file_text.outerWidth() - $file_button.outerWidth() - 6 ) + 'px'
		});

		$file_pseudo
			.attr( 'size', 100 )
			.addClass( 'input-pseudo' )
			.css({
				'opacity'	: 0,
				'width'		: $file_wrap.outerWidth() + 'px',
				'height'	: $file_wrap.outerHeight() + 'px'
			});

		$( this ).change( function() {
			var $file_text_val = $( this ).val().replace( /^([^\\\/]*(\\|\/))*/, '' );

			$file_wrap.attr({ 'title' : 'File: ' + $file_text_val });

			$file_text.val( $file_text_val );

			if ( $file_text.val().length >= 38 ) {
				$file_text.val( $file_text_val.slice( 0, 36 ) + '...' );
			}
		});
	});

	// carousel
	var $carousel = $( '.scrollbox' );

	$carousel.each(function() {
		var $carousel			= $( this ),
			$carousel_content	= $carousel.children( '.scrollbox-content' ),
			$carousel_controls	= $carousel.children( '.scrollbox-navigation' ),
			$carousel_prev		= $carousel_controls.children( '.prev' ),
			$carousel_next		= $carousel_controls.children( '.next' ),
			$carousel_play		= $carousel_controls.children( '.play' ),
			$carousel_pause		= $carousel_controls.children( '.pause' ),

			$carousel_param_fx	= $carousel_content.attr( 'data-carousel-fx' ),
			$carousel_param_time= Number( $carousel_content.attr( 'data-carousel-time' ) );

		$carousel_play.click( function() {
			$carousel_content.cycle( 'resume' );
			$carousel_pause.show();
			$carousel_play.hide();
		});
		$carousel_pause.click( function() {
			$carousel_content.cycle( 'pause' );
			$carousel_pause.hide();
			$carousel_play.show();
		});

		$carousel_content.cycle({ 
			fx				: $carousel_param_fx,
			timeout			: $carousel_param_time,
			speed			: 'slow',
			cleartype		: !$.support.opacity,
			cleartypeNoBg	: true,
			next			: $carousel_next,
			prev			: $carousel_prev
		});
	});

	// more images
	$( '#slideshow' ).cycle({
		fx					: 'cover',
		speed				: 'fast',
		timeout				: 0,
		pager				: $( '.more-list' ),
		activePagerClass	: 'active',
		pagerAnchorBuilder	: carouselPager
	});
	function carouselPager( idx, slide ) {
		var small_src = $( slide ).attr( 'data-carousel-src' );
		return '<li><a href="javascript:;"><span class="zoom"><!-- zoom --></span><img src="' + small_src + '" width="144" height="102" alt="" /></a></li>';
	}

	// team nav
	/*$( '#team_list_main' ).cycle({
		fx					: 'scrollVert',
		speed				: 600,
		timeout				: 0,
		pager				: '#team_list_global',
		pagerAnchorBuilder	: function( idx, slide ) {
			return '#team_list_global LI:eq(' + idx + ') A';
		}    
	});
	
	$( '#team_list_global LI A' ).click( function( e ) {
		$.scrollTo( 346, 600 );
	});*/

	// to top page
	var $link_top = $( '<a href="#top" class="link-top" id="link_top" title="Scroll to Top">Top ▲</a>' );

	$( 'body' ).append( $link_top );
	$link_top
		.hide()
		.css({
			'position'				: 'fixed',
			'bottom'				: '5px',
			'right'					: '5px',
			'display'				: 'block',
			'padding'				: '2px 4px',
			'border'				: '1px solid #CCCCCC',
			'background'			: '#FFFFFF',
			'z-index'				: '123456789',
			'-moz-border-radius'	: '2px',
			'-webkit-border-radius'	: '2px',
			'border-radius'			: '2px'
		});

	$( window ).scroll( function() {
		if ( $.browser.msie && $.browser.version == '6.0' ) {
			var offset = $link_top.height() + 8;

			$link_top.css({
				'position'	: 'absolute',
				'bottom'	: 'auto',
				'top'		: $( window ).scrollTop() + $( window ).height() - offset + 'px'
			});
		}

		if ($( window ).scrollTop() >= 200) {
			$link_top.fadeIn( 200 );
		}
		else {
			$link_top.fadeOut( 200 );
		}
	});

	$link_top.click( function( e ) {
		e.preventDefault();
		$.scrollTo( 0, 300 );
	});

	// 404
	var $page_not_found = $( '#page_not_found' );

	$page_not_found
		.append( '<div class="gradient-top" />' )
		.stop()
		.mouseover( function() {
			$( this )
				.stop()
				.animate({
					'background-position'	: '0 -300px',
					'padding-left'			: 0
				}, {
					duration				: 500
				});
		})
		.mouseout( function() {
			$( this )
				.stop()
				.animate({
					'background-position'	: '0 0',
					'padding-left'			: '300px'
				}, {
					duration				: 500
				});
		});

	// slider home
	$('.slidedeck').slidedeck();
});

$( window ).load( function() {
	$( '#pageload' )
		.text( 'Complete.' )
		.fadeOut( 1000, function() {
			$( this ).remove();
		});
});

$(document).ready(function() {
	$("a.fancy-block").fancybox({
		'titlePosition'		: 'inside',
		'frameWidth': '1000',
		'frameHeight' : '600', 
		'overlayShow' : 'true', 
		'overlayOpacity' : '0.8',	
		'hideOnContentClick' :'false',	
		'centerOnScroll' : 'false' 	
	});
});

