function getIframe(){
	f=document.getElementById('text_editFrame');
	return (f.contentDocument.body.innerHTML);
}



$(function() {


	$('#header h1').css({ opacity:0 }).fadeTo(2000,1);

	$('.thesisinfo1 .layer1').css({ opacity:0 });
	$('.thesisinfo1 .layer2').css({ opacity:0 });
	$('.thesisinfo1 .layer3').css({ opacity:0 });

    $('.thesiswrap').append('<div id="_menuhover_" style="position:absolute; top:1px; z-index:0; height:64px; background:#e8eaec; overflow:hidden; left:-100px;">');
    
	var self=this;

    var $menuhover = $('#_menuhover_'),
        $menuItems = $('.thesiswrap ul li'),
		$lenta = $('.lenta', '.thesiswrap2'),
		$arleft = $('.prev','.thesiswrap2'),
		$arRight = $('.next','.thesiswrap2'),
		cl=0,
		running=false,
		sLmain =0,
		
		$cabinet = $('.auth .cabinet'),
		$enter = $('.auth .enter');
	

	
	if ($('.thesiswrap').length){
		sLmain = $('.thesiswrap').offset().left;
	}
		


/*ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½*/
	this.rubber = function(){
		for (i=1; i<6; i++ ){
			if ($menuItems.hasClass('act')){
				$menuItems.removeClass('act');
			}
		}
	}


	this.hidePic = function(){
		for (i=1; i<6; i++ ){

			$('.thesisinfo'+i+' .layer1').stop().css({ opacity:0 });
			$('.thesisinfo'+i+' .layer2').stop().css({ opacity:0 });
			$('.thesisinfo'+i+' .layer3').stop().css({ opacity:0 });
		}
	}
	
	this.showPic = function(h){
		$('.thesisinfo'+h+' .layer1').fadeTo(500,1,function(){
			$('.thesisinfo'+h+' .layer2').fadeTo(500,1,function(){
				$('.thesisinfo'+h+' .layer3').fadeTo(500,1);
			});
		});
	}



/*ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½*/
	if ($('.thesiswrap li.act').length){
		var $sItem = $('.thesiswrap li.act'),
			sW = $sItem.width(),
			sL = $sItem.offset().left- sLmain;
	} else {
		sW = 0;
		sL = -100;
	}

	$menuhover.animate( { width:sW, left:sL }, 500, null, null);
	self.showPic(1);




/*ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½*/
	$menuItems.hover(function(){
		var $this = $(this),
			W = $this.width(),
			sLmain = $('.thesiswrap').offset().left,
			L = $this.offset().left - sLmain;

		$menuhover.stop().animate({ width:W, left:L }, 500, null, null);

	}, function(){
		var $sItem = $('.thesiswrap li.act'),
			sW = $sItem.width(),
			sLmain = $('.thesiswrap').offset().left,
			sL = $sItem.offset().left- sLmain;

		$menuhover.stop().animate({ width:sW, left:sL }, 500, null, null);
	});



	/*ï¿½ï¿½ï¿½ï¿½*/
	$menuItems.each(function(k){
 
		$('.t'+(k+1)).click(function(){
			var $this=$(this);
			
			self.rubber();
			self.hidePic();

			$this.addClass('act');
			
			if(k==0){
				$lenta.animate( { left: '400%' }, 500, null, function(){
					self.showPic((k+1));
				});
				$arleft.hide();
				$arRight.show();
				$arRight.html("<b>"+(k+2)+"</b>");
				cl=0;
			} else {
				$lenta.animate( { left: (400-k*100)+'%' }, 500, null, function(){
					self.showPic((k+1));
				});
				$arleft.show();
				$arRight.show();
				if(k==4){
					$arRight.hide();
				} else {
					$arRight.html("<b>"+(k+2)+"</b>");
				}
				$arleft.html("<b>"+k+"</b>");
				cl=k;
			}
			return false;
		});

	});

	

	$arRight.click(function(){
		
		if (!running)
		{
			running=true;
			self.rubber();
			self.hidePic();
			cl++;

			$arRight.html("<b>"+(cl+2)+"</b>");
			$arleft.html("<b>"+cl+"</b>");
			$('.thesiswrap ul .t'+(cl+1)).addClass('act');
		
			var $sItem = $('.thesiswrap li.act'),
				sW = $sItem.width(),
				sLmain = $('.thesiswrap').offset().left
				sL = $sItem.offset().left- sLmain;		
		
			$menuhover.stop().animate({ width:sW, left:sL });			

			if (cl==4)
			{
				$arRight.hide();
			}			
			
			$lenta.stop().animate( { left: "-=100%" }, 500, null, function(){
				self.showPic((cl+1));
				running=false;
			});
			$arleft.show();

		}



		return false;
	});




	$arleft.click(function(){

		if (!running)
		{
			running=true;
			self.rubber();
			self.hidePic();
			cl--;
			
			$arRight.html("<b>"+(cl+2)+"</b>");
			$arleft.html("<b>"+cl+"</b>");
			$('.thesiswrap ul .t'+(cl+1)).addClass('act');
		
			var $sItem = $('.thesiswrap li.act'),
				sW = $sItem.width(),
				sLmain = $('.thesiswrap').offset().left
				sL = $sItem.offset().left- sLmain;		
		
			$menuhover.stop().animate({ width:sW, left:sL });

			if (cl==0)
			{
				$arleft.hide();
			} 

			$lenta.stop().animate( { left: "+=100%" }, 500, null, function(){
				self.showPic((cl+1));
				running=false;
			});
			$arRight.show();
		}

		return false;
	});




	$(window).resize(function(){
	
		if ($('.thesiswrap').length){

			var $sItem = $('.thesiswrap li.act'),
				sW = $sItem.width(),
				sLmain = $('.thesiswrap').offset().left,
				sL = $sItem.offset().left-sLmain;
					
			$menuhover.stop().css({ width:sW, left:sL });
		}

	});
	
	
	
	

    $('#do-it').click(function(){
	    
		var theme = $('#theme').val();
		var text  = getIframe();
		
		var options = {
			    type       : 'post',			   
			    dataType   : 'json',
				url        : '../mail.php',
				data       : 'theme='+theme+'&text='+text,
				beforeSend : function(){
					$('.darkening').fadeIn('slow');
					$('#process').fadeIn('fast');
					$('#process  span').html('Èäåò ðàññûëêà...');
					$('#process  img').show();												
				},
                success: function(data) {				
								
					if (data.success==true){						                       
						$('#process  span').html('Ðàññûëêà óñïåøíî çàâåðøåíà');														
					}else{
						$('#process  span').html(data.message);						
					}
					$('#process  img').hide();
					$('#process > center > input').show().click(function(){
							$('#process, .darkening').hide();
							$(this).hide();																		
					});	
                }
        };
		
		$.ajax(options);						
		
	});
	
	
	$('#forget_lnk').click(function(){
		$('#auth').hide();
		$('#forget').show();
		$('#message').html('Ââåäèòå e-mail óêàçàííûé ïðè ðåãèñòðàöèè');		
	});
	
	$('#return_lnk').click(function(){
		$('#forget').hide();	
		$('#auth').show();
		$('#message').html('');				
	});
	
	
	$cabinet.click(function(){ 
		$enter.toggle();		
	});	
	
	
	//ÎÍëàéí çàïðîñ
	$('#form').submit(function(){
		
		var options = {              
               dataType  : "html",
			   clearForm : true,
               success   : function(data) {
                   $('#message').html(data);
				   
               }
        }; 
        
        $(this).ajaxSubmit(options);
		return false;
	})
	
	$('#forget').submit(function(){		
		
		var options = {              
               dataType  : "html",
			   clearForm : true,
               success   : function(data) {
                   $('#message').html(data);				   
               }
        }; 
        
        $(this).ajaxSubmit(options);
		
		return false;
	})
		
	$('#auth').submit(function(){		
		
		var options = {              
               dataType  : "json",
			   clearForm : true,
               success   : function(data) {
			   	   if (data.success){				   
				   	   location.reload(true);
				   }else{				   	   
				   	   $('#message').html(data.message);
				   }				   
               }
        }; 
        
        $(this).ajaxSubmit(options);
		
		return false;
	})
	
	
	$('#auth .inp').focus(function(){
		$(this).val('');		
	})
	
	

})
