var teamIsOn = false;
var sliderIsOn = false;

$(document).ready(function() {

	$("#features div").mouseover(function() {
		$(this).addClass('hovered');
	});
	$("#features div").mouseout(function() {
		$(this).removeClass('hovered');
	});
	$(".text_input").focus(function() {
		if($(this).val() == $(this).attr("title")) $(this).val("");
	});
	$(".text_input").blur(function() {
		if($(this).val() == "") $(this).val($(this).attr("title"));
	});
	
	$("#team .item").click(function() {
		var th = $(this);
		if((th.css('z-index') == 90) && (teamIsOn == false)) {
			teamIsOn = true;
			th.css('z-index','99');
			$("#team .selected_item").each(
				function() {
					var th2 = $(this);
					th2.css('z-index','90');
					th2.find("h4").hide();
					th2.children(".photo").animate(
							{ width: "85px" }, 500, function() {
								th2.find("h1").fadeIn(100);
								th2.removeClass("selected_item");
							}
					);
				}
			);
			
			th.children(".photo").each(function() { 
				th.find("h1").fadeOut(300);
				$(this).animate(
						{ width: "290px" }, 
						500, 
						function() {
							teamIsOn = false;
							th.find("h4").fadeIn(500);
							th.addClass("selected_item");
						}
				);

			});
		}
	});

    $(document).ready(function() {
        var doneJ = false;var doneK = false;var doneR = false;var doneD = false;var doneA = false;
    	  $('.nI').hover(function() {
    		  var pid = $(this).parent().attr("id");
    		  if(pid == "nIj") done = doneJ;
    		  else if(pid == "nIk") done = doneK;
    		  else if(pid == "nIr") done = doneR;
    		  else if(pid == "nId") done = doneD;
    		  else if(pid == "nIa") done = doneA;
    		  if(!done) {
    			  if(pid == "nIj") doneJ = true;
            else if(pid == "nIk") doneK = true;
            else if(pid == "nIr") doneR = true;
            else if(pid == "nId") doneD = true;
            else if(pid == "nIa") doneA = true;
            	$(this).find("img").animate({width:0, marginLeft:80},200,"",function() {
            		  $(this).parent().hide();
            		  if($(this).parent().next().hasClass("nI")) {
            			  var next = $(this).parent().next();
            		  } else {
            			  var next = $(this).parent().prev();
            		  }
            		  
            		  img = next.find("img");
            		  img.css("width","0px").css("margin-left","100px");
            		  next.show();
            		  img.animate({width:160, marginLeft:0},200).animate({width:160},100,"",function() {
            			  if(pid == "nIj") doneJ = false;
	                  else if(pid == "nIk") doneK = false;
	                  else if(pid == "nIr") doneR = false;
	                  else if(pid == "nId") doneD = false;
	                  else if(pid == "nIa") doneA = false;        
	            		});
            	});
    		  }            	
    	  });
    });

	
  $('#contact_form').submit(function(){
    err = false;
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

    if ($("#i_imie").val() == "") {
	    $("#i_imie").addClass('error');
      err = true;
    } else {
      $("#i_imie").removeClass('error');
      var name = $("#i_imie").val();
    }

    if ($("#i_mail").val() == "") {
       $("#i_mail").addClass('error');
       err = true;
    } else {
    	$("#i_mail").removeClass('error');
    	if(!emailReg.test($("#i_mail").val())) {
    		$("#i_mail").addClass('error');
        err = true;
   	  } else {
   	  	$("#i_mail").removeClass('error');
      	var mail = $("#i_mail").val();
   		}
    }

    if ($("#i_pytanie").val() == "") {
       $("#i_pytanie").addClass('error');
       err = true;
    } else {
      $("#i_pytanie").removeClass('error');
      var issue = $("#i_pytanie").val();
    }
    if(!err) {
       $("#submit_link").addClass('hidden');
       $("#loading").removeClass('hidden');
       $.ajax({
         type: "POST",
         url: root_path+"/inc/contact_script.php",
         data: "name="+name+"&mail="+mail+"&issue="+issue,
         success: function(msg){
           if(msg === 0) {
            $("#loading").addClass('hidden');
            $("#loading").removeClass('hidden');
           } else {
            $("#loading").addClass('hidden');
            $(".message_sent").removeClass('hidden');
           }
         }
       });
    }
    return false;
  });

  $("#message_box").slideDown(1000);
  
  startSwitching();

});

function closeMessage() {
	$("#message_box").slideUp(1000);
}

function startSwitching() {
	$("#company_logo div:first").each(function() {
		switchLogo($(this));
	});
}


function showPortfolio() {
  $(".list li a").addClass('bold');
  setTimeout(function() {$(".list li a").removeClass('bold')}, 2500);
}


function switchLogo(obj) {
	obj.fadeOut(200, function() {
		obj.children().toggleClass("hidden");
		if(obj.next().html() == null) {
			$("#company_logo div:first").each(function() {
				setTimeout("startSwitching()",4000);
			});
		} else switchLogo(obj.next());
		obj.fadeIn(900);
	});
}

function addOverlay() {
	$("html").attr("id","fade-on");
	$("#player").flowplayer(root_path+"/movie/flowplayer-3.1.1.swf");
}
function removeOverlay() {
	$("#popup").remove();
	$("html").removeAttr("id");
	$.cookie('advative_intro', 'No more default intro for You :)', { expires: 1 });
}

function slideMe(direction) {
	if(!sliderIsOn) {
		sliderIsOn = true;
		if(direction == "left") {
			_moveBox("moveable_box",true,"400px",500);
			_moveBox("moveable_text",true,"475px",500);
		} else {
			_moveBox("moveable_box",false,"400px",500);
			_moveBox("moveable_text",false,"475px",500);
		}
	}
}

function _moveBox(name, direction, marg, speed) {
	$("#"+name+" div.visible").each(function() {
		var next_obj = $(this).next();
		if(direction) next_obj = $(this).prev();
		if(next_obj.html() == null) {
			if(direction) next_obj = $("#"+name+" div:last");
			else next_obj = $("#"+name+" div:first");
		}
		var margLeft = marg;
		if(direction) margLeft = "-"+marg;
		next_obj.addClass('visible').css("margin-left",margLeft);
		next_obj.animate({marginLeft: "0px"}, speed );
		margLeft = "-"+marg;
		if(direction) margLeft = marg;
		$(this).animate({ 
	        marginLeft: margLeft
	      }, speed , function() {
	    	  $(this).removeClass('visible');
	    	  sliderIsOn = false;
	      });
	});
}