
$( document )
.ready(
	function(){
		var w=0,it=null;
		var li = $('#gallery section:not( :eq(0) )')
		.hide()
		.parent()
			.append( '<ul><li rel="0"></li><li rel="1"></li><li rel="2"></li></ul>')
		.find('li')
			.click(
				function(){
					clearInterval( it );
					var p = $(this).parent();
					if( $(this).attr('rel') == '1' ){
						p.css( 'background-position', '-40px 0' );
						w = 1;
					}
					else if( $(this).attr('rel') == '2' ){
						p.css( 'background-position', '-80px 0' );
						w = 2;
					}
					else{
						p.css( 'background-position', '0 0' );
						w = 0;
					}
					
						p.siblings('section:visible')
							.fadeOut()
						.end()
							.siblings('section:eq('+w+')')
							.fadeIn();
							
					runit();
				}
			);
			
		function runit(){
			it = setInterval(
				function(){
					var n = ++w;
					
					if( n == 3 ){
						w = n = 0;
					}
					
					li.eq( n ).click();
				},
				7000
			);
		}
		
		runit();
		
	
		var usermessagea = $('#usermessagea');
		
		if( usermessagea.size() ){
			if( usermessagea.hasClass('success') ){
				usermessagea.siblings().remove();
			}
		}
	
		$('#main-nav > div > ul > li')
		.mouseenter(
			function(){
				var el = $( this ).children(':first'), 
					li = el.parent(), W
					ul = li.parent(), 
					bgp = '0 0';
					
				li.siblings().removeClass( 'opened' )
					
				if( el.hasClass('link_1') ){
					bgp = '0 -40px';
				}
				else if( el.hasClass('link_2') ){
					bgp = '0 -80px';
				}
				else if( el.hasClass('link_3') ){
					bgp = '0 -120px';
				}
				else if( el.hasClass('link_4') ){
					bgp = '0 -160px';
				}

				ul.css( 'background-position', bgp );
				li.addClass( 'opened' );
			}
		)
		.mouseleave(
			function(){
				$( this ).removeClass( 'opened' ).parent().css( 'background-position', '0 0' );
			}
		);
		
		$('#child-navigation .has_children:not(.opened)')
		.bind('mouseenter',
			function( e ){
				e.preventDefault();
				$(this).addClass('opened');
			}
		)
		.bind('mouseleave',
			function( e ){
				e.preventDefault();
				$(this).removeClass('opened');
			}
		);
		
		$('.ie7 nav li:last-child, .ie8 nav li:last-child').addClass('last');
		
		$('.btn_print')
		.click(
			function( e ){
				e.preventDefault();
				window.print();
			}
		);
	}
);
