var currentSelectedButton = null;
var currentAnimatedMenu = null;
var treeFadeInterval = 800;
//var aniInterval = null;

$(document).ready(function() {

if (jQuery.browser.msie) {
  var ieVer = parseInt(jQuery.browser.version);
  if (ieVer <= 6) {
//	  $('#page').html();
  }
}

  $("#slogan").click(function() {
    window.location="/";
  });


  if ($("#editPanel").length > 0) {

  var edit = new nicEditor({buttonList : ['save','fontSize','fontFamily','fontFormat','bold','italic','strikethrough','left','center','right','justify','ol','ul','subscript','superscript','removeformat','hr','image','upload','forecolor','link','unlink','xhtml','forecolor','bgcolor'],
                            xhtml : true, onSave : function(content, id, instance) {
    //alert('save button clicked for element '+id+' = '+content);

    $(edit.nicInstances).each( function() {
      $.post("/save", {
        Property:this.e.id,
        caption:$('#caption').attr('value'),
        keywords:$('#keywords').attr('value'),
        description:$('#description').attr('value'),
        Content:this.content
      },function(data){
        //alert("Data Loaded: " + data);
        alert("Страница сохранена");
        });
    });
  }});

  edit.setPanel('editPanel');
  $(".editable").each( function() {
    edit.addInstance($(this).attr('id'));
  });

  }

  soundManager.url = '/soundmanager2.swf';
  soundManager.debugMode = false;
  soundManager.consoleOnly = false;
  soundManager.useConsole = false;
  soundManager.onload = function() {
    soundManager.createSound('01','/01.mp3');
    soundManager.createSound('02','/click.mp3');
    soundManager.createSound('03','/03.mp3');
  }

  $("#team-table .photo").click(function() {
    soundManager.play('02');
    window.location="/" + $(this).attr("alt");
  });
  
  $("#team-table .photo").mousemove(function() {
    try { soundManager.play('03'); } catch(err) {}
    $(this).attr("src","/images/photo-" + $(this).attr("alt")+ "-color.jpg");
  });

  $("#team-table .photo").mouseout(function() {
    $(this).attr("src","/images/photo-" + $(this).attr("alt")+ "-bw.jpg");
  });

  $("#team-table .lnk").mousemove(function() {
    soundManager.play('03');
    var img= $("#"+$(this).attr("alt"));
    $(img).attr("src","/images/photo-" + $(img).attr("alt")+ "-color.jpg");
  });
  
  $("#team-table .lnk").mouseout(function() {
    var img= $("#"+$(this).attr("alt"));
    $(img).attr("src","/images/photo-" + $(img).attr("alt")+ "-bw.jpg");
  });
    
  $("#slide-outter div").hide();

  $("#logo").click(function() { window.location="/"; });
  $("#b-team").click(function() { window.location="/team"; });
  $("#b-contacts").click(function() { window.location="/contacts"; });
  $("#top").click(function() { window.location="/outsource"; });
  
  $(".button").click(function() {
    try { soundManager.play('02'); } catch(err) {}
	$(this).addClass("selected");
    currentSelectedButton = this;
    if (this.id=="top") { ShowSubMenu(this, 'top'); } else { ShowSubMenu(this, 'right'); }
    $(this).css("background", "url(images/button.png) no-repeat 2px 2px");
    ResetButtons();
  });

  $(".button").mouseenter(function() {
	try { soundManager.play('01'); } catch(err) {}
    if (this != currentSelectedButton) {
      $(this).css("background", "url(images/button.png) no-repeat 0px 0px");
    }
  });

  $(".button").mouseleave(function() {
    if (this != currentSelectedButton) {
      $(this).css("background", "url(images/button.png) no-repeat 1px 1px");
    }
  });


  $("#slide-outter div:first-child").animate({"opacity":"show"}, treeFadeInterval, function(){	    
	$(this).parent().css("background", $(this).css("background-image"));
	$(this).hide().next().animate({"opacity":"show"}, treeFadeInterval, arguments.callee);		
  });

});

function ResetButtons() {
  $(".button").each(function() {
    if (this != currentSelectedButton) {
      $(this).css("background", "url(images/button.png) no-repeat 1px 1px");
      $(this).removeClass("selected");
    }
  });
}

function ShowSubMenu(rootMenu, direction) {

  $("#text .t").hide();
  HideAllSubMenu();

  var newStyle = null;
  var lineStyle = null;
  var lineControl;

  if (direction == 'right') {
    newStyle = { "opacity": "1", "width": "160px"};
    lineControl = $(".MenuHeadLine", rootMenu);
	lineStyle = { "width": "200px", "opacity": "1" };
  } 
  else if (direction == 'top') {
	newStyle = {"opacity": "1", "width": "450px"};
    lineControl = $(".MenuLeftLine", rootMenu);
	lineStyle = { marginTop: "-=113", height: "113px", opacity: "1"};
//	lineControl.css("margin-top", "113px").css("opacity","0").height(0);
  }
  
  lineControl.animate(lineStyle, 500, function(){
	$(".SubMenuItem:eq(0)", rootMenu).animate(newStyle, 700, function(){
		$(this).next().animate(newStyle, 700, arguments.callee);
	});
  });
  
	
}

function HideAllSubMenu() {
  $(".MenuHeadLine").stop().css("opacity", "0").width(1).height(1);
  $(".MenuLeftLine").stop().css("margin-top", "113px").css("opacity", "0").width(1).height(0);
  $(".SubMenuItem").stop().css("opacity", "0").width(0);
  //not("#top .SubMenuItem")
}



