projectbox_open =  false;
proyect_orient = "right";
$(document).ready(function(e){
	//Input Labels
	$(".label").focus(function(e){
		if($(this).val() == $(this).attr('title')){
			$(this).val("");
		};
	});
	$("input.label").blur(function(e){
		if($.trim($(this).val()) == ""){
			$(this).val($(this).attr("title"));
		}
	});
	//Quick Contact Open/Close
	$("#quick-contact h1").click(function(e){
		if($("#quick-contact").hasClass("open")){
			$("#quick-contact").removeClass("open");
		}else{
			$("#quick-contact").addClass("open");
		}
	});
	//Clouds Banner
	swfobject.embedSWF("/swf/background.swf", "cloud_banner", "100%", "388px", "9",false,false,{wmode:'transparent',scale:'noscale'});
	//Banner Functions
	$("#content .banner.menu .map a").click(function(e){
		e.preventDefault();
		var index = $(this).index();
		var current_offset = parseInt($("#content .banner.menu .overlay").css('left'));
		var new_offset = index * 225 - 681;
		var new_reel_offset = index * -462;
		$("#content .banner.menu .overlay").animate({left: new_offset+"px"},400,'swing');
		$("#content .banner .preview .reel").animate({left: new_reel_offset+"px"},400,'swing');
		/*
		$("#content .banner .preview img.on").fadeOut("normal","swing",function(){
			$(this).removeClass("on");
			$("#content .banner .preview img").eq(index).fadeIn("normal","swing").addClass("on");
		});
		*/
	});
	//Image Replace
	$('.img-replace').each(function(){
		string = $(this).text();
		filename = string.toLowerCase().replace(/ /g, '-').replace(/([^0-9a-z-])/g,'');
		$(this).html('<img src="/img/' + filename + '.png" alt="' + string + '" />');
	});
	// Porftafolio Tooltip
	$("#content .banner.menu .map a").hover(function(e){
		e.preventDefault();
		var index = $(this).index();
		var title = $(this).attr("title");
		var offset = index * 225 - 9;
		$("#content .banner .popup h2").html(title);
		$("#content .banner .popup").css("left",offset+"px");
		$("#content .banner .popup").show();
	},function(e){
		$("#content .banner .popup").hide();
	});
	//Video Functions
	var video_shown = true;
	$("#content .videoarea").slideDown(800,'swing');
	$("#content .videotab").click(function(e){
		e.preventDefault();
		if(video_shown){
			text = $(this).children(".open").html();
			$("#content .videoarea").slideUp(800,'swing');
			$("#content .main").animate({top: "-90px"},400,'swing');
			$("#content .videotab a,#content .videotab .shadow").html(text);
			video_shown = false;
		}else{
			text = $(this).children(".close").html();
			$("#content .videoarea").slideDown(800,'swing');
			$("#content .main").animate({top: "-54px"},400,'swing');
			$("#content .videotab a,#content .videotab .shadow").html(text);
			video_shown = true;
		}
	});
	
	//Service Tabs in HOme
	$("#content .main .service").hover(function(){
		var index = $(this).index();		
		$("#content .main .shadow").eq(index).fadeIn("normal");		
		$("#content .main .rollover.on").fadeOut(100,function(e){
			$("#content .main .rollover").eq(index).fadeIn(100).addClass('on');
		}).removeClass('on');
	},function(){
		var index = $(this).index();
		$("#content .main .shadow").eq(index).fadeOut("normal");
	});

	//Porfolio Functions
	hide_proyect = true;
	static_proyect_box = false;
	$("#content .portfolio a").mousemove(function(e){
		e.preventDefault();
		index = $(this).index();
		proyect = portfolio[index];		
		box = $("#content .proyect");
		pos = position_box(e);
		projectbox_open = true;
		img = box.children("img");
		if(img.attr("src") != "/img/portfolio/"+proyect.image && img.attr('src') != proyect.overlay){
			img.hide();
			$("#portfolio-img-loader").addClass('loading');
			if(!$("#content .portfolio").hasClass("video")){
				img.attr("src","/img/portfolio/"+proyect.image);
				box.children("h2").html(proyect.proyect);
				box.children("p").children(".client").html(proyect.client);
				box.children("p").children(".technology").html(proyect.technology);
				box.children(".text").html(proyect.text);
			}else{
				img.attr("src",proyect.overlay);
			}			
			img.load(function(e){
				if(!static_proyect_box){
					$("#portfolio-img-loader").removeClass('loading');
					img.show();
				}
			});
		}
		box.show();
		e.stopPropagation();
	});
	$("#content .proyect").mousemove(function(e){
		if(!static_proyect_box){
			$(this).show();
			projectbox_open =  true;
			position_box(e);
			e.stopPropagation();
		}
	});
	$("#content .proyect").hover(function(){},function(e){
		if(!static_proyect_box){
			$(this).hide();
			projectbox_open = false;
		}
	});
	$("#content .portfolio a").hover(function(e){
		if(!static_proyect_box){
			$(this).show();
			position_box(e);
			projectbox_open =  true;
			e.stopPropagation();
		}
	},function(e){
		if(hide_proyect){
			$("#content .proyect").hide();
			static_proyect_box = false;
			projectbox_open = false;
		}
	});
	$("#content .portfolio.video a").click(function(e){
		e.preventDefault();		
		index = $(this).index();
		hide_proyect = false;
		jwplayer().load(portfolio);
		$("#overlay").show();
		$("#content .proyect").show();
		$("#video_shell").show();
		$("#content .proyect img").hide();
		$("#portfolio-img-loader").removeClass('loading');
		static_proyect_box = true;
		$("#content .proyect img").animate({opacity:0},800,function(){
			jwplayer().playlistItem(index);
			$("#content .proyect img").css('opacity','1');
			//alert(index);
		});
	})

	$("#overlay").click(function(e){
		jwplayer().stop();
		$("#overlay").hide();		
		$("#video_shell").hide();	
		$("#content .proyect img").show();
		hide_proyect = true;
		$("#content .proyect").hide();
	});
	// Rotating Balls
	$("#content .balls .ball").click(function(e){
		e.preventDefault();
		index = $(this).index()-1;
		$("#content .balls .ball.on").removeClass("on");
		$(this).addClass("on");
		$("#content .main .information .block.on").fadeOut("fast",function(){
			$(this).removeClass("on");
			$("#content .main .information .block").eq(index).fadeIn("fast").addClass('on');
		});
	});

	hashchangeActions();
});

function hashchangeActions(){
	if(window.location.hash == "#involver"){
		playvideo(3);
	}
	if(window.location.hash == "#zincro"){
		playvideo(7);
	}
}

function playvideo(index){
	var center = $(window).width()/2;
	center = center.toFixed();
	center = center - 383;
	// $("#content .proyect").css("position","fixed");
	$("#content .proyect").css("left",center + "px");
	$("#content .proyect").css("top","550px");
	jwplayer("video_container").setup({
		flashplayer: "/jwplayer/player.swf",
		skin: "/jwplayer/glow.zip",
		playlist: portfolio,
		height: 383,
		width: 680
	});
	jwplayer().load(portfolio);
	$("#overlay").show();
	$("#content .proyect").show();
	$("#video_shell").show();
	$("#content .proyect img").hide();
	static_proyect_box = true;
	
	$("#content .proyect img").animate({opacity:0},800,function(){
		jwplayer().playlistItem(index);
		$("#content .proyect img").css('opacity','1');
	});
}


function position_box(e){
	//Determine Position
	container_offset = $("#content").offset();
	window_width = $(window).width();
	window_height = $(window).height();
	scroll_top = $(window).scrollTop();
	x = e.pageX - container_offset.left + 15;
	y = e.pageY - container_offset.top - 200;	
	bottom = scroll_top + window_height - 5;
	var box_bottom = y + box.height() + 50;
	y = box_bottom > bottom ? bottom - box.height() - 50 : y;
	if(!projectbox_open){
		proyect_orient = x > window_width / 2 + 15 ? "left" : "right";
		//alert(proyect_orient);
	}
	x = proyect_orient == "left" ? x - box.width() - 30 : x;
	box.css("top",y+"px");
	box.css("left",x+"px")	
	return {x:x,y:y};
}
