$(function(){
	var $width = $( 'body' ).css( 'width' ).replace( 'px', '' ),
		$height = document.compatMode=='CSS1Compat' && document.documentElement.clientHeight,
		$curpos = $( '#curpos' ), $curpos2 = $( '#curpos2' ),
		$ottop = (($height-550)/2).toFixed(0);
	$( '#main-overlay-block' ).css( 'top', $ottop+'px' );
	getFleft();
	$( '#button' ).click( function(){
		if ( $( this ).attr( 'stop' ) == '0' ){
			var $getid = $( '#getid' ).val();
			$( this ).attr( 'stop', 2 );
			var $top = 750 - ( $curpos.offset().top-20 ) + ( $height-20 );
			$( '#overlay' ).fadeIn( 500 ); $( '#onbutxt' ).fadeOut( 50 ); $( '#page-text , #page-title' ).hide(); $( '#page-text' ).css( 'top','-320px' ); $( '#page-title' ).css( 'top','-130px' ); $( '#triangle' ).fadeOut( 50 ); $( '#triangleback' ).fadeOut( 50 );
			$( this ).css( { position:'fixed', left:$( '#fleft' ).val()+'px', top:$top+'px' } );
			$( this ).find( 'img:first' ).animate( { width:'150px', height:'150px' }, 100 );
			$( this ).animate( { top:$ottop-20+'px', width:'157px', height:'157px' }, 300 ).animate( { top:$ottop+'px' }, 350, function(){ 
				$( '#button' ).attr( 'stop', 1 ); 
				if ( $getid*1 < 1){
					$( '#outbutxt' ).fadeIn(); 
				}
			} );
			if ( $getid*1 > 1 )
				getVideo( 'one', $getid, '' );
			else
				getVideo( 'stock', '', '' ); 
		} else if ( $( this ).attr( 'stop' ) == '1' ){
			$( 'title' ).html( 'Начни новый день с улыбки.' );
			$( '#button img' ).attr( 'src', '/images/button.png' );
			$( this ).attr( 'stop', 2 );
			var $top = $( '#button' ).offset().top;
			$( '#overlay' ).fadeOut(); $( '#outbutxt' ).fadeOut( 50 ); $( '#page-text, #page-title' ).show(); $( '#triangleback' ).fadeOut( 50 ); $( '#triangle' ).fadeOut( 50 ); $( '#response' ).fadeOut( 50 ); $( '#response' ).html('');

			$( this ).css( { position:'absolute', left:'20px', top:$top+'px' } );
			$( this ).find( 'img:first' ).animate( { width:'219px', height:'219px' }, 100 );
			$( this ).animate( { top:'790px' }, 400 ).animate( { top:'730px' }, 350 ).animate( { top:'750px' }, 400, function() { $( '#onbutxt' ).fadeIn( 200 ); } );
			setTimeout( function() { $( '#page-title' ).animate( {top:'170px'}, 300 ).animate( {top:'110px'}, 350 ).animate( {top:'130px'}, 400 ); }, 400 );
			setTimeout( function() { $( '#page-text' ).animate( {top:'420px'}, 300 ).animate( {top:'375px'}, 350 ).animate( {top:'410px'}, 400, function(){ $( '#button' ).attr( 'stop', 0 ); } ); }, 500 );
		}
	} );

	$( '#face img, #vk img, #twi img' ).hover( function() { $( this ).attr( 'src', '/images/'+$( this ).attr( 'divname' )+'a.png' ); } , function() { $( this ).attr( 'src', '/images/'+$( this ).attr( 'divname' )+'.png' ); } );
	$( '#onbutxt' ).hover( function() { $( '#triangle' ).fadeIn( 200 ); } , function() { $( '#triangle' ).fadeOut( 200 ); } );
	$( '#outbutxt' ).hover( function() { $( '#triangleback' ).fadeIn( 200 ); }, function() { $( '#triangleback' ).fadeOut( 200 ); } );

	$(window).load(function () {
		$( '#hand' ).show();
		$( '#page-title' ).animate( {top:'170px'}, 300 ).animate( {top:'110px'}, 350 ).animate( {top:'130px'}, 400 );
		setTimeout( function() { $( '#page-text' ).animate( {top:'450px'}, 300 ).animate( {top:'390px'}, 350 ).animate( {top:'410px'}, 400 ); }, 300 );
		setTimeout( function() { 
			$( '#fade-wrap' ).fadeIn( 1500, function(){
				if ( $( '#getid' ).val()*1 > 1 )
					$( '#button' ).click();
			} );
		}, 1500 );
		VK.init({
			apiId:2479883
		});
		VK.Auth.getLoginStatus(authInfo);
		VK.UI.button('login_button');
	} );
	$(window).bind('resize', function(){ document.location.href='/'; } );
} );
function getFleft(){
	var $width = $( 'body' ).css( 'width' ).replace( 'px', '' ),
		tmp = (($width-1000)/2+27).toFixed( 0 ),
		$height = document.compatMode=='CSS1Compat' && document.documentElement.clientHeight,
		$ottop = (($height-550)/2).toFixed(0);
	$( '#fleft' ).val( tmp );
	if ( $( '#button' ).attr( 'stop' ) == '0' ){
		$( '#face' ).css( { position:'fixed', top:$ottop*1+369+'px', right:$( '#fleft' ).val()+'px'} );
		$( '#vk' ).css( { position:'fixed', top:$ottop*1+409+'px', right:$( '#fleft' ).val()-1+'px'} );
		$( '#twi' ).css( { position:'fixed', top:$ottop*1+449+'px', right:$( '#fleft' ).val()+'px'} );
	}
	setTimeout( function() { getFleft( $width ); }, 1000 );
}
function getVideo( $func, $id, $page ){
	$( '#response' ).html( '<br/><br/><center><img src="/images/ajax-loader.gif" title="Пожалуйста, подождите."/></center>' );
	$( '#getid' ).val ( '' );
	$.ajax({
		url: "/ajax/getvideo.php",
		global: false,
		type: "POST",
		data: ( {'myid':$( '#myid' ).val(), 'func':$func, 'id':$id, 'page':$page }),
		dataType: "html",
		async:true,
		success: function(msg){
			if ( msg == 'error:close' ){
				document.location.href='/';
			} else {
				msg = msg.split( '--++--' );
				$( '#response' ).html( msg[0] ).fadeIn( 500, function() {
					if ( $func == 'stock' ){
						$( '#outbutxt' ).fadeIn( 50 );
						$( 'title' ).html( 'Начни новый день с улыбки.' );
						$( '#button img' ).attr( 'src', '/images/button.png' );
						$( '#response td' ).each( function(indx,element){
							if ( $( this ).attr( 'rel' ) != 'nan' ){
								var img = $( this ).find( 'img:first' ).attr( 'load' ),
									name = $( this ).find( 'img:first' ).attr( 'lname' ),
									$parent = $( this ),
									image = new Image();
								image.src = img; image.style.display = 'none';
								$parent.find( 'div:first' ).append( image );
								$parent.find( 'img:hidden' ).load( function() {
									$parent.find( 'img:first' ).fadeOut( function() {
										$( this ).remove();
										$parent.find( 'img:hidden' ).fadeIn( function() {
											$( this ).attr( {'on':0,'title':name,'alt':name} );
											$parent.find( 'div:first' ).append( '<div class="objtext dn"><nobr>'+name+'</nobr></div>' );
										} );
									} );
								} );
							}
						} );
						$( '.objblock img' ).hover( function() {
							if ( $( this ).attr( 'on' )*1 <= 1 ){
								$( this ).attr( 'on', 1 );
								$(this).stop().animate( { width:'120px', height:'120px' }, 150, function() {
									if ( $( this ).attr( 'on' ) == 1 )
										$( this ).parent().find('.objtext:first').fadeIn();
								} );
							}
						}, function() { 
							if ( $( this ).attr( 'on' )*1 == 1 ){
								$( this ).attr( 'on', 0 ); $( this ).parent().find('.objtext:first').hide().fadeOut();
								$( this ).stop().animate( { width:'180px', height:'180px' }, 150 ).animate( { width:'157px', height:'157px' }, 250 );
							}
						} );
					} else if ( $func == 'one' ){
						$( '#outbutxt' ).hide();
						$( 'title' ).html( msg[2] );
						if ( $( '#button' ).attr( 'stop' ) == '1' )
							$( '#button img' ).attr( 'src', msg[1] );
						$( '#likevk img' ).hover( function() { $( this ).attr( 'src', '/images/like-vk-red.png' ); }, function() { $( this ).attr( 'src', '/images/like-vk-normal.png' ); } );
						$( '#likeface img' ).hover( function() { $( this ).attr( 'src', '/images/like-facebook-red.png' ); }, function() { $( this ).attr( 'src', '/images/like-facebook-normal.png' ); } );
					}
				} );
			}
		}
	} );
}
function authInfo(response) {
	//$( '#login-form' ).animate( {top:'0px'}, 400 );
/*
	if (response.session) {
		alert('user: '+response.session.mid);
	} else {
		alert('not auth');
	}
*/
}
