(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]];
           }
        }
	});
})(jQuery);

function swapImage(theImage, theImageType)
{
	$(theImage).hover(
		function(){
			if($(this).attr("src").indexOf("_over") == -1) 
			{
				var newSrc = $(this).attr("src").replace("." + theImageType,"_over."+theImageType);
				$(this).attr("src",newSrc);
			}
		},
		function(){		
			if($(this).attr("src").indexOf("_over") != -1) 
			{
				var newSrc = $(this).attr("src").replace("_over."+theImageType,"."+theImageType);
				$(this).attr("src",newSrc);
			}
		}	
	);
}


function giveImage(){
	var imgArray = new Array("bubbles.png", "candy.png", "sandycove.png", "m_castle.png");
	var num = Math.ceil(Math.random() * imgArray.length);
	//console.log("img=" + imgArray[num] + " : " + num );
	return imgArray[num-1];
}

function openCurtain(theSource, theMethod)
{
	var args = openCurtain.arguments.length;
	
	$(".menu a").addClass("darker");
	
	$("#curtain").show();
	var s = theSource;
	
	if(theMethod == "load")
	{	
		
		$("#subcontent_stage").load(s, function(){
			if($('body').hasClass("hp")){
				$x = $("#subcontent_stage").html().replace('src="../', 'src="');
				
				if($ie6 || $ie7)
				{
					var loco = document.location + '';
					loco = loco.split('/');
					beforeLoc = '';
					for(i=0; i<loco.length-2;i++)
					{
						beforeLoc += loco[i] + "/";
					}
					afterLoc = '';
					for(i=0; i<loco.length-1;i++)
					{
						afterLoc += loco[i] + "/";
					}
					$x = $("#subcontent_stage").html().replace('src="'+ beforeLoc, 'src="' + afterLoc);
				}
				else
				{
					$x = $("#subcontent_stage").html().replace('src="../', 'src="');
				}
				$("#subcontent_stage").html($x);
			}
		});
	}
	else if(theMethod == "html")
	{
		$("#subcontent_stage").html(s.html());	
	}
	else if(theMethod == "html2")
	{
		$("#subcontent_stage").html(s);	
	}
	else
	{
		return false;
	}
	
	var top = 120 + $(window).scrollTop();;
	//var top = ((($(window).height() - $("#subcontent_stage").height() ) / 2)+$(window).scrollTop());
	var left = (($(window).width() - $("#subcontent_stage").width())  / 2);
	
	$("#subcontent_stage").css("top", top + "px"); 
	$("#subcontent_stage").css("left", left - 35+ "px"); 
	
	$(".close").css("top",top-22 + "px");
	$(".close").css("left",left + $("#subcontent_stage").width() + 5 + "px");
	
	
	swapImage(".close img", "png");
	
	$("#curtain").animate({ 
			width: $(window).width() + "px",
			opacity: 0.5,
			height: $(document).height() + "px",
			top: 0,
			left: 0 },
		  	500,
			function(){
			  $(".close").show();
			  $("#subcontent_stage").show();
			  $("#curtain").css("height",$(document).height() + "px");
			  $("#overlay").css("opacity", 0);
			});
	
}


$(document).ready(function(){
	
	$("#explaindiv").css("opacity",0);
	$("#banner").css("background-image", "url(img/banners/"+ giveImage() +")");
	
	/*if ($("#twitterballoon").length){					   
		//$twitter = "inc/twitter.php?id=95468637";
		//$("#twitterballoon p").load($twitter);	
		//$("#twitterballoon").append("<div class=\"close\">x</div>");
		
		$.ajax({
		  url: "inc/twitter.php",
		  data:  { id : '95468637' },
		  success: function(data){
			console.log("data");
			console.log(data);
		  },
		  dataType: 'html'
		});

	}*/
	
	$('#twitterballoon').animate({opacity:0},1);
	
	
	$("#twitterballoon p").tweet({
            username: "jvdesign",
            join_text: "auto",
            avatar_size: 0,
            count: 1,
            auto_join_text_default: "we said,",
            auto_join_text_ed: "we",
            auto_join_text_ing: "we were",
            auto_join_text_reply: "we replied to",
            auto_join_text_url: "we were checking out",
            loading_text: "loading tweets..."
        });
	
	
	swapImage("#stardiv img", "png");

	$("#stardiv img").click(function(){
								document.location = "#contact";	
								$("#name").focus();
								return false;
							});

	$("#categories li").hover(
			function(){
				//load the text
				$("#explaindiv").html("");
				
				$theText = '';
				$theCat = $(this).attr("id");
				switch ($theCat){
					case 'cat_web':
						$theText = "inc/web.php";
						break;
					case 'cat_print':
						$theText = "inc/print.php";
						break;
					case 'cat_presentations':
						$theText = "inc/presentations.php";
						break;
					case 'cat_marketing':
						$theText = "inc/online_marketing.php";
						break;
					default:
						break;
				}
				$("#explaindiv").load($theText, function(){
													$("#explaindiv").css("z-index",60);	 
													$("#explaindiv").stop().animate({
																						opacity: .9
																					}, 750);
												});
				
			},
			
			function(){
				//empty text
				$("#explaindiv").stop().animate({
													opacity: .0
												}, 750, function(){
															$("#explaindiv").html("");
															$("#explaindiv").css("z-index",0);	
														});
				
			}
		);
		
	$("#twitterbird").click(
			function(){
				$('#twitterballoon').animate({opacity:1}, 750);	
			}
		);
	
	$(".closeTwitter").live("click",function(){
			$(this).parent().animate({opacity:0},500);		
	});

	
	$('.project_img, .footerimage').live("mouseover",function(){
								$html = $(this).siblings('.overlay').html();				
								
								$("#overlay").html($html);
								
								$("#overlay").css("top",$(this).offset().top+"px");
								$("#overlay").css("left",$(this).offset().left+"px");
								
								$("#overlay").stop().fadeIn("slow", function(){
																			 $("#overlay").css("opacity", .9)
																			 });
									
	});
	
	$('#overlay').mouseleave(function(){
								$("#overlay").html("");
								
								$("#overlay").stop().fadeOut("slow", function(){
																				$("#overlay").css("top",-300 +"px");
																				$("#overlay").css("left",-300+"px");
																			 });
								
	});

	$(".currentpage").append("&nbsp;<img src='../img/bgs/arrow.gif' alt=''>");

	$("#rotator li").not(':first').css("opacity",0);
	
	var loc = '' + document.location;
	
	
	$("#overlay a").live("click",function(){
		
		$x = $(this).attr("href");
		
		if($x.indexOf('http') >= 0)
		{
			$v = $x.split('.php');
			$x = $v[1];
		}
										  
		$thePage = "pf/" + $x.replace("#","") + ".php";
		
		openCurtain($thePage, 'load');	
	});
	
	$("#curtain, .close").click(function(){
		
		$(".close").hide();
		$("#subcontent_stage").hide();
		$("#subcontent_stage").css("top", "0px"); 
		$("#subcontent_stage").css("left", "0px"); 
		
		
		
		$("#curtain").animate({ 
								width: "0",
								height: "0"
							  }, 500);
		
		return false;
	});
	
		
	$("#contactform").validate({
		rules: {
			name: "required",
			email: {
				required: true,
				email: true
			},
			comment: "required"
		},
		messages: {
			name: "Please specify your name",
			email: {
				required: "We need your email address to contact you",
				email: "Your email address must be in the format of name@domain.com"
			},
			comment: "Please let us know what you want to talk to me about"
		},
		submitHandler: function(form) {
			var dataString = 'name='+ $("#name").val() + '&email=' + $("#email").val() + '&comment=' + $("#comment").val() + '&testvraag=' + $("#testvraag").val();
			$.ajax({
				 type: "POST",
				 url: "process.php",
				 data: dataString,
				 success: function(data) 
						 {
							$rv_title = "";
							$rv_text = ""; 
							
							if(data == 1)
							{
								$rv_title = "Got your request";
								$rv_text = "I will be in touch soon";
							}
							else
							{
								$rv_title = "Oops, somethings went wrong there";
								$rv_text = "Sorry, your request has not been submitted, try calling me instead on +353 86 389 6205";
							}
							 $('.formPart').html("<div id='message'></div>");
							 $('#message').html("<h3>" + $rv_title + "</h3>")
							 .append("<p>" + $rv_text +" </p>")
						  }
				});
				return false;
		   }
	});

	
});
