$(function() {
	$('#menu a').css('backgroundImage', $('#menu a').css('backgroundImage'));
	$('#menu a').hover(
		function() {
			var menu = $(this).parent('li').attr("id");
			var menuID = menu.substr(4, 1);
			var menuAnim = $('#menu' + menuID + 'anim');
			var menuAnimDefault = $('#menu'+menuDefault+'anim');
			
			$(this).stop().fadeTo(300, 0);
			$(menuAnim).stop().animate({top:0}, 200, 'easeOutQuad');
			$(menuAnimDefault.stop()).animate({top:-100}, 300, 'easeInQuart');
		},function() {
			var menu = $(this).parent('li').attr("id");
			var menuID = menu.substr(4, 1);
			var menuAnim = $('#menu' + menuID + 'anim');
			var menuAnimDefault = $('#menu'+menuDefault+'anim');
			
			$(this).stop().fadeTo(400, 1);
			$(menuAnim).stop().animate({top:-100}, 300, 'easeInQuart');
			menuAnimDefault.stop().animate({top:0}, 200, 'easeOutQuart');
		}
	);
	
	$('a').click(function() {
  		this.blur();
	});
	
	var adminInterval;
	$('#copyright .copyright').hover( 
      function() {
          $(this).addClass('copyrightHover');
          adminInterval = setTimeout('openAdmin()',2000);
      }, function() {
          $(this).removeClass('copyrightHover');
          clearTimeout(adminInterval);
	    }
  	);
	
	$('#content a').not('a.edit').hover(
		function() {
			//alert($(this).css('backgroundImage'));
			$(this).stop()
				.css({
					backgroundImage: 'url("./style/page/list-link-select.gif")', 
					color:'#8CD3DF'})
				.animate({backgroundPosition: '4px 1px'});
		}, function() {
			$(this).stop()
				.css({
					backgroundImage: 'url("./style/page/list-link.gif")', 
					color: '#FFFFFF'})
				.animate({backgroundPosition: '0px 1px'});
		}
	);
	
});

function openAdmin() {
  $('#copyright .admin').fadeIn(1000);
}

function menuImgAnimation() {
//	var opacity = (Math.round(Math.random()*10)/50) + 0.7;
//	$('#menuAnim img').stop().fadeTo(200, opacity);
//	$x = setTimeout("menuImgAnimation()",200);
}

var textRotatorInterval;	
function textRotation(phase){
  var text = textForRotator;
	if(phase >= text.length) phase = 0;
	setCounterRotator(phase);
	
	$('#rotatorTitle').css({left:"400px",top:"100px", fontSize:0, opacity:'hide'}).text(text[phase][0]).stop().animate({opacity:'hide'},1)
		.animate({left:125, top:130,fontSize:18, opacity:'show'}, 1500, 'easeOutBack')
		.animate({opacity:1},2500)
		.animate({left:500,opacity:'hide'}, 3000, 'easeInQuint');
		
	$('#rotatorText').css({left:"350px",top:"110px", fontSize:0, opacity:'hide'}).text(text[phase][1]).stop().animate({opacity:'hide'},1).animate({opacity:1},500)
		.animate({left:125, top:155, opacity:'show', fontSize:13}, 1500, 'easeOutBack')
		.animate({opacity:1},2500)
		.animate({left:500,opacity:'hide'}, 2800, 'easeInQuint');
	
	$.cookie('textRotatorPhase', phase, { expires: 7 });
	phase++;
	clearTimeout(textRotatorInterval);
	textRotatorInterval = setTimeout('textRotation('+phase+')',7600);
}
function setCounterRotator(phase){
  $('#rotatorCounter .number').each(function() {
		var counter = $(this).parent('.wrap').attr("id");
		var counterID = counter.substring(7, counter.length);
    
    if(phase == counterID) {
      $(this).fadeIn(500);
    } else {
      $(this).fadeOut(1000);
    }
  }); 
}

var textForRotator = [
  ["Co umíme natočit","reklamní, vyukové a dokumentární filmy"],
  ["Co umíme natočit","videoklipy a televizní reklamy"],
  ["Co nedokážeme natočit","vyrobíme animačně ve 3D, 2D a kreslené animace"],
  ["navrhneme námět, napíšeme scénář","natočíme záběry, složíme hudbu"],
  ["namluvíme komentáře a celé sestříháme","předáme vám na CD, DVD a HVS kopiích"],
  ["včetně výroby jazykových mutací","namluvených rodilými speakery"],
  ["vytvoříme WWW a CD-Rom prezentace","s originálním designem"],
  ["obsahující katalogové databáze","vašich výrobků"],
  ["originální grafiku tiskovin","a propagačních materiálů"],
  ["design reklamních poutačů","stojanů, výstavních stánků"]
];


var textForRotator;
$.ajax({
	type: "POST",
	url:  "index.php",
	data: "ajax=getTextRotator",
	success: function(data){
		textForRotator = eval(data);
		startTextRotator();
	}
});


$(function() {
	menuImgAnimation();
});
function startTextRotator() {
	var phase = $.cookie('textRotatorPhase') == null ? 0:$.cookie('textRotatorPhase');
	textRotation(phase);
	$('#rotatorCounter .background').css('opacity',0);
	$('#rotatorCounter').css('visibility','visible').hover(
			function() {	   
			   $('#rotatorCounter .background').stop().fadeTo(500,1, 'easeInQuad');
			}, function() {
			   $('#rotatorCounter .background').stop().fadeTo(1000,0, 'easeInQuad');
			}
  )
	$('#rotatorCounter .background').click( function() {
      var counter = $(this).parent('.wrap').attr("id");
      var counterID = counter.substring(7, counter.length);
  		 
      $('#rotatorTitle').stop().stop().stop().stop().animate({opacity:'hide'}, 500, 'easeInQuad');		
      $('#rotatorText').stop().stop().stop().stop().animate({opacity:'hide'}, 500, 'easeInQuad');
      setCounterRotator(counterID);
      clearTimeout(textRotatorInterval);
      textRotatorInterval = setTimeout('textRotation('+counterID+')',600);
  });			   
}


(function($) {
	$.extend($.fx.step,{
	    backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $.curCSS(fx.elem,'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
			}
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
            fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];
            
           function toArray(strg){
               strg = strg.replace(/left|top/g,'0px');
               strg = strg.replace(/right|bottom/g,'100%');
               strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
               var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
               return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
           }
        }
	});
});

