// JavaScript Document
$.fn.extend({ 
	hasMovie : function(){
		var size = this.size();
		var tr = 0;
		this.each(function(){ if (this.movie) { if(this.movie==true) { return tr++ } } });
		if (tr==size) { return true } else { return false }
	},
	toggleMovie : function(){
		this.each(function(){ if (this.movie) { if(this.movie==true) { this.movie=false; } else { this.movie=true; } } else { this.movie=true; } })
	},
	getMovie : function(e){
		var _elements = [];
		this.each(function(){
			var elem = this;
			if (elem.movie==true) { return _elements.push(elem) }
		});
		if (typeof e=='number') { retur = $(_elements).eq(e) } else { retur = $(_elements) }
		return retur
	}
});

function getRand(min, max){
	return Math.round(Math.random()*(max-min))+min;
}

$.fn.rotation = function(options){
	var options = jQuery.extend({
		container: $(this),
		cont_class: '.' + $(this).attr('class'),
		elements: 'li',
		activator: 'li',
		rand: '.bum',
		close: 'li',
		toRotate: 'img',
		speed: 1
	},options);
	 
	return this.each(function() {
							  
		var container = options.container;
		var cont_class = options.cont_class;
		var elements = options.elements;
		var activator = options.activator;
		var rand = options.rand;
		var close = options.close;
		var toRotate = options.toRotate;
		var spd = options.speed;
		
		var hid_anim = 200/spd;
		var li_anim = 400/spd;
		var op_anim = [300/spd,600/spd];
		var cl_anim = 350/spd;
		var hei_anim = 250/spd;
		
		container[0].pos = new Array();
		container[0].Dep = new Array();
		container[0].defH = container.height();
		container.css({height:container.height(),width:container.width()});
		
		$('#content').prepend('<xml:namespace ns="urn:schemas-microsoft-com:vml" prefix="v">');
		
		container.find(elements).each(function(e){
			if ($(this).parent().hasClass('users') && !$(this).hasClass('new_line') && !$(this).hasClass('last_li')) {
				var thiOff = $(this).offset();
				if (($('body').width() - $('#content').width())/2>110) { var baKr = (($('body').width() - $('#content').width())/2)+32; } else { var baKr = 105; }
				this.Ttop = thiOff.top - 280;
				this.Tleft = thiOff.left - baKr;
				this.Bus = e;
				
				
				var t = 0;
				var z = 0;
				if (container[0].Dep.length>0) {
					for (i=0;i<container[0].Dep.length;i++) {
						if (container[0].Dep[i].name==$(this).attr("title")) { t = i } 
						else { z++ }
					}
					if (z<container[0].Dep.length) { 
						if (!container[0].Dep[t].pos) { container[0].Dep[t].pos = new Array(); }
						container[0].Dep[t].pos[e] = { elem:this, Ttop:this.Ttop, Tleft:this.Tleft, Bus:0 }
					} else {
						container[0].Dep[z] = { name:$(this).attr("title") };
						if (container[0].Dep[z].name==$(this).attr("title")) {
							if (!container[0].Dep[z].pos) { container[0].Dep[z].pos = new Array(); }
							container[0].Dep[z].pos[e] = { elem:this, Ttop:this.Ttop, Tleft:this.Tleft, Bus:0 }
						}
					}
				} else {
					container[0].Dep[0] = { name:$(this).attr("title") };
					if (container[0].Dep[0].name==$(this).attr("title")) {
						if (!container[0].Dep[0].pos) { container[0].Dep[0].pos = new Array(); }
						container[0].Dep[0].pos[e] = { elem:this, Ttop:this.Ttop, Tleft:this.Tleft, Bus:0 }
					}
				}
				
				
				container[0].pos[e] = { Ttop:this.Ttop, Tleft:this.Tleft, Bus:1, elem:this };
			}
		});
		
		var depBus = 0;
		var coBus = 0;
		var outTopMrg = 0;
		
		$(container[0].Dep).each(function(e){
			var thisLi = container.find('.new_line').eq(e);
			if(thisLi.length) { thisLi.append('<h2>'+this.name+'</h2>'); } else { container.find('.last_li').before('<li class="new_line"><h2>'+this.name+'</h2></li>'); }
			thisLi = container.find('.new_line').eq(e);
			container[0].Dep.h2Height = thisLi.find('h2').height();
			var depTtop = container[0].pos[1].Ttop;
			var depTleft = container[0].pos[1].Tleft;
			var frst = 0;
			depTtop += container[0].Dep.h2Height+10;
			$(this.pos).each(function(i){
				if (this.Tleft) {
					if (frst==0) { frst=i }
					this.Tleft = depTleft+((container.find(elements).eq(1).width()+parseInt(container.find(elements).eq(1).css('margin-right')))*depBus);
					this.Ttop = depTtop+((container[0].Dep.h2Height+container.find(elements).eq(1).height()+parseInt(container.find(elements).eq(1).css('margin-top')))*coBus)+outTopMrg;
					depBus++;
					container[0].lastDepElemH = this.Ttop+container[0].Dep.h2Height+container.find(elements).eq(1).height()+parseInt(container.find(elements).eq(1).css('margin-top'));
					if (depBus==5) { depBus=0; outTopMrg-=container[0].Dep.h2Height+5; coBus++ }
				}
			});
			if (depBus!=0) { depBus=0; coBus++ }
			var h2top = this.pos[frst].Ttop-container[0].Dep.h2Height;
			thisLi.find('h2').css({top:h2top});
		});
		
		$(container[0].pos).each(function(e){ 
			if (this.elem) {
				var offs = { top:this.Ttop, left:this.Tleft };
				$(this.elem).css({position:'absolute', top:offs.top, left:offs.left});
			}
		});
		
		var noLag = 0;
		
		function getPos(e, bt, num){
			if (bt.hasClass('random')) {
				var maxAr = container[0].pos.length;
				var lod = 0;
				while (lod < 1) {
					num = getRand(1, maxAr);
					if (container[0].pos[num]) { 
						if (container[0].pos[num].Bus==0) lod++;
					}
				}
				if (noLag==0) {
					noLag=1;
					container.find('.new_line h2').stop().animate({left:-2000}, 450);
					container.stop().animate({height:container[0].defH},500);
				}
				return container[0].pos[num];
			} else if (bt.hasClass('normal')) {
				num = container.find(elements)[e].Bus;
				if (noLag==0) {
					noLag=1;
					container.find('.new_line h2').stop().animate({left:-2000}, 450);
					container.stop().animate({height:container[0].defH},500);
				}
				return container[0].pos[num];
			} else if (bt.hasClass('dep')) {
				tt = false;
				$(container[0].Dep).each(function(i){
					if (this.name==$(container[0].pos[e].elem).attr('title')){
						tt = this.pos[e];
					}
				});
				if (noLag==0) {
					noLag=1;
					container.stop().animate({height:container[0].lastDepElemH},500);
					container.find('.new_line h2').stop().animate({left:0}, 750);
				}
				return tt;
			}
		};
		
		$(rand).click(function(){
			var btn = $(this);
			if(!$('body').hasMovie() && container.find('.cont_rot span').getMovie().size()<1) {
				$(rand).removeAttr('disabled');
				$(this).attr({disabled:'disabled'});
				$('.random').removeAttr('disabled');
				$('body').toggleMovie();
				var cHeight = (container.height()/2)-80;
				var cWidth = (container.width()/2);
				noLag = 0;
				$(container[0].pos).each(function(e){
					if (this.elem) {
						container[0].pos[e].Bus = 0;
						$(this.elem).animate({top:cHeight+getRand(-50, 50),left:cWidth+getRand(-50, 50)}, getRand(450, 750), function(){
							var newPos = 0;
							newPos = getPos(e, btn, newPos);
							newPos.Bus = 1;
							var offs = {top:newPos.Ttop,left:newPos.Tleft};
							$(this).animate({top:offs.top, left:offs.left}, getRand(450, 750));
							$(this)[0].Ttop = offs.top;
							$(this)[0].Tleft = offs.left;
						});
						
					}
				});
				setTimeout("$('body').toggleMovie()", 1500)
			}
		});
		
		
							  
		container.find(activator).click(function(){
			if(!$('body').hasMovie()) {
				$('body').toggleMovie();
				if (!$(this).parent().hasMovie()) {
						$(this).parent().toggleMovie();
						if (container.find('.cont_rot span').getMovie().size()>0) {
							$(this).parent().toggleMovie();
							$('body').toggleMovie();
						} else {
							$(this).parent().addClass('goo');
							if (jQuery.browser.msie) {$(this).parent().find("span").eq(1).click();}
							$(this).parent().find("canvas").click();
						}
				} else {
					$(this).parent().addClass('goo');
					if (jQuery.browser.msie) {$(this).parent().find("span").eq(1).click();}
					$(this).parent().find("canvas").click();
				}
			}
		});
		
		container.find(close).click(function(){$(this).parent().click(); return false});
		
		
		
		
		
		
		
		
		var animRot = function(e){
					if (e.parent().parent().hasClass("goo")) {
						if (!e.hasMovie()) {
							e.toggleMovie();
							var thiLi = e.parent().parent().parent();
							window.thiLiOff = { top:thiLi[0].Ttop, left:thiLi[0].Tleft };
							if (($('body').width() - $('#content').width())/2>110) { var baKr = (($('body').width() - $('#content').width())/2)+32; } else { var baKr = 105; }
							var haKr = (thiLi.parent().height()/2) - (thiLi.height()/2) - 80;
							thiLi.toggleMovie();
							var toHide = e.parent().parent().parent().parent().find('li:visible');
							for (i=0;i<toHide.size();i++) { if (!toHide.eq(i).hasMovie()) { toHide.eq(i).addClass('hidd').animate({opacity:'0.3'}, hid_anim); } }
							thiLi.css({/*position:'absolute',*/ zIndex:500/*, top:thiLiOff.top - 244, left:thiLiOff.left - baKr*/, height:'550px'});
							var thiMove = (thiLi.parent().width() / 2) - (180);
							/*thiLi.next().css({marginLeft:'105px'});*/
							var scMo = haKr + thiLi.parent().find('li').eq(2).offset().top;
							if (scMo+200>$(window).height()){ $(window).scrollTop(300); }
							thiLi.animate({width:'445px',left:thiMove, top:haKr}, li_anim);
							e.rotateAnimation(-135);
							e.find('canvas').animate({opacity:'1'},op_anim[0]);
							if (!jQuery.browser.msie) {e.find('*').css({opacity:1})}
							window.tops = parseInt(e.parent().css('top'));
							e.parent().animate(
							{top:"-391px", left:"-435px"}, li_anim,
							function(){ 
								var tThis = $(this).parent().find('.userscribe');
								tThis.css({display:'block'});
								var thisHe = tThis.height();
								tThis.css({height:'0px'});
								tThis.stop().animate({height:thisHe}, hei_anim);
								tThis.find('a').show(cl_anim);
								$('body').toggleMovie();
							}
							);
						} else {
							e.toggleMovie();
							container.find(elements).getMovie().toggleMovie();
							var HeRol = e.parent().parent().find(".role").height() + parseInt(e.parent().parent().find(".role").css("padding-top")) + parseInt(e.parent().parent().find(".role").css("padding-bottom"));
							var tThis = e.parent().parent().find('.userscribe');
							tThis.find('a').hide(cl_anim);
							tThis.stop().animate({height:'0px'}, hei_anim, function(){
								tThis.css({display:'none', height:'auto'});
								if (($('body').width() - $('#content').width())/2>110) { var baKr = (($('body').width() - $('#content').width())/2)+32; } else { var baKr = 105; }
								e.parents('li').animate({width:'100px',left:thiLiOff.left, top:thiLiOff.top}, li_anim, function(){
									e.parents('li').css({/*position:'relative', */zIndex:1/*, top:0, left:0*/, height:'180px'});
									/*e.parents('li').next().css({marginLeft:0});*/
								});
								$('.hidd').removeClass('hidd').animate({opacity:1},hid_anim);
								e.rotateAnimation(65);
								e.find('canvas').animate({opacity:'0'},op_anim[1]);
								if (!jQuery.browser.msie) {e.find('*').css({opacity:0})}
								e.parent().animate(
								{top:tops + "px", left:"-391px"}, 230, function(){
									$(this).parent().toggleMovie();
									$('body').toggleMovie();
									if (!$(this).parent().find(".role").hasMovie()) {
										var HeRol = $(this).parent().find(".role").height() + parseInt($(this).parent().find(".role").css("padding-top")) + parseInt($(this).parent().find(".role").css("padding-bottom"));
										$(this).parent().find(".role").css({display:'none'}).animate({marginTop: HeRol * -1}, 100);
										var tTop = parseInt(e.parent().css("top"));
										e.parent().stop().animate({top: '-400px'}, 100);
										return false;
									}
								}
								);
							});
						}
						$(this).parent().parent().parent().removeClass("goo")
					}
				}
		
		
		
		if (!jQuery.browser.msie) { for (i=1;i<=$(container).find(elements).size();i++) {
				var roo = $(toRotate[0]+i+toRotate[1]).rotate({maxAngle:0,minAngle:-90,
				bind:[{"click":function(event){ var ev = $(event.target).parent(); animRot(ev)}}]});
			}
		} else { for (i=1;i<=$(container).find(elements).size();i++) {
				var rot=$(toRotate[0]+i+toRotate[1]).rotate({maxAngle:0,minAngle:-90,
				bind:[{"click":function(event){ var ev = $(event.target); animRot(ev)}}]});
			}	
		}
	})
};