
var g_img_path = "fourth_image/";
var g_movie_path = "../movies/";

var g_title_string = ""; //title changed...

var shadow = new ShadowObject();

uaDecide();

jQuery(function($){ NewAddress().Exec() });

function addFLVPlayer( id, fname, image, width, height ){
	$("#movie_frame").css("z-index","110").hide();
	$(id).bind("click",function(){ showFLVPlayer(id,fname,image,width,height) });
}

function showFLVPlayer( id, fname, image, width, height ){
	var version = "9.0.115";
	var s = new FLVPlayer(version,"../"+g_movie_path+fname,g_movie_path+image,"../",width,height);
	s.Write("movie_inside");

	addFilter("#movie_frame","#ffffff",function(){
		fadeOutFilter();
		s.FadeOut();
		shadow.Out("fast");
		document.title = g_title_string;
		pauseMovie();
	});
	shadow.Set(232,77+21,510,300,"104").In("normal",s.Write);

	var mf = $("#movie_frame");
	var ofsx = parseInt(mf.css("padding-left"));
	var ofsy = parseInt(mf.css("padding-top")) + 13;
	mf.width(width).height(height).css({
		marginLeft: -(width/2+ofsx)+"px",
		marginTop: -(height/2+ofsy)+"px"
	}).fadeIn("fast");
	
	document.title = g_title_string;	
}

function showForm(){
	$("#form").fadeIn("fast",addFormDialog);
	shadow.Set(244,88+16,488,268,"104").In("normal");
	window.location.href = "#form";
	addFilter("#form","#ffffff",false);
}

function addForm(){
$("#form").css("display","none").css("visibility","visible");
$("#pixeljunk_1_2_2").bind("click",function(){
	if( $("#form").css("display") == "none" ){
		showForm();
	}else{
		eraseFormDialog();
		$("#form").fadeOut("normal");
		typed();
		addFilter("#form","#ffffff",false);
	}
});
$("#clear_button").bind("click",eraseFormDg);
$("#submit_button").one("click",sendForm);
$("#form_dialog").bind("click",fadeOutFormDialog);
$(".close_button").click(eraseFormDg);

var typed = function(){
	$("#result").css({visibility:"hidden",opacity:"0"});
	$("#clear_button").css({visibility:"visible",opacity:"1"});
};
$("#title").keydown(typed);
$("#from").keydown(typed);
$("#message").keydown(function(){
	typed();
	var max = 10000;
	if( $(this).attr("value").length > max )
		$(this).attr("value",$(this).attr("value").substr(0,max)).focus();
});
}

function eraseForm(){
$("#form").fadeOut("normal",function(){
	$("input,textarea").each(function(index,dom){
		$(dom).val("");
	});
	$("#result").css({visibility:"hidden",opacity:"0"});
});
fadeOutFilter();
}

function eraseFormDg(){
	eraseForm();
	eraseFormDialog();
	shadow.Out("fast");
}

function sendForm(){
$("#clear_button").fadeTo("normal",0.0,function(){$(this).css("visibility","hidden")});
$("#submit_button").attr("src",g_img_path+"sending_mail.gif");
$.post("contact.php",$("#contact_form").serialize(),function(t){
	if(t=="YES"){ $("#form").fadeOut(); eraseForm(); }
	else $("#result").fadeTo("fast",1.0).css("visibility","visible");
	$("#clear_button").stop().css("visibility","visible").fadeTo("normal",1.0);
	$("#submit_button").attr("src",g_img_path+"button.gif").one("click",sendForm);
});
}

function addFormDialog(){
	$("#form_dialog").css("visibility","visible").show();
}
function eraseFormDialog(){
	$("#form_dialog").css("visibility","visible").hide();
	shadow.Out();
	window.location.href = "#";
}
function fadeOutFormDialog(){
	$("#form_dialog").fadeOut();
	shadow.Out();
	window.location.href = "#";
}

function addFilter(obj,color,click){
	if( click === true ){
		click = function(){
			fadeOutFilter();
			$(obj).css("z-index","0");
			pauseMovie();
		};
	}
	$("#filter")
		.width(960).height(476)
		.css({ backgroundColor:color, opacity:"0", zIndex:"100", display:"block", cursor:"auto" })
		.fadeTo("slow","0.5");
	if(!!click){
		$("#filter").one("click",click).css("cursor","pointer");
	}
}

function fadeOutFilter(speed){
	$("#filter").fadeOut(speed||"slow");
}

function pauseMovie(){
	var mi = document.getElementById("movie_inside");
	if( !!!mi.sendToActionScript && navigator.appName.indexOf("Microsoft") != -1 )mi = window["movie_inside"];
	if( !!mi.sendToActionScript )mi.sendToActionScript("resetAll");
}

function controlMovie(){
	
	var _Write = (function(){
		var name = "cssjavs/flash/pixeljunk_jop.swf";
		var flashvars = {};
		var params = {
			movie: name,
			wmode: "transparent",
			allowscriptaccess: "always",
			quality: "high",
			scale: "noscale"
		};
		var attributes = {
			movie: name,
			wmode: "transparent",
			allowscriptaccess: "always",
			quality: "high",
			scale: "noscale"
		};
		return function(){swfobject.embedSWF(name,"pixeljunk",960,476,"9.0",
			"cssjavs/flash/expressInstall.swf",flashvars, params, attributes )}
	})();

	var tag = $("#pixeljunk");
	var bs = jQuery.browser;
	
	if( $.cookie("opmovie")=="true"
		|| bs["safari"] || bs["playstation"] || bs["os"]=="mac"
	){
		$.cookie("opmovie","true",{expires:7});
		tag.remove();
	}else _Write();
}

function uaDecide(){
	var ua = navigator.userAgent;
	jQuery.browser["playstation"] = ua.indexOf("PLAYSTATION")!=-1;
	jQuery.browser["os"]="";
	if(ua.indexOf("Mac")!=-1){
		jQuery.browser["os"]="mac";
	}else if(ua.indexOf("Windows")!=-1){
		jQuery.browser["os"]="win";
	}
}

(function(){ //ToolTip

ToolTip.Init();

var tooltip_array = {

	"#mailmagazine":	{ip:[0,50],p:[726,271]},
	"#tshirt_click":	{ip:[98,50],p:[604,400]},
	"#threeinone_click":{ip:[196,50],p:[702,387]},
	"#buy":				{ip:[294,50],p:[770,402]},
	"#pixeljunk_1_2_2":	{ip:[392,50],p:[824,380]},
	"#home":			{ip:[490,50],p:[539,382]},
	"#forum":			{ip:[490,100],p:[761,65]},

	"#pixeljunk_1_1":{ip:[0,0],p:[98,396]},
	"#pixeljunk_1_2,#pixeljunk_1_2a":{ip:[98,0],p:[237,372]},
	"#pixeljunk_1_3_1":{ip:[196,0],p:[340,371]},
	"#pixeljunk_1_4_0":{ip:[294,0],p:[475,385]},
	"#pixeljunk_1_3a":{ip:[392,0],p:[378,334]},
	"#pixeljunk_1_2p":{ip:[490,0],p:[313,275]},
	"#pixeljunk_pjmd_demo":{ip:[392,100],p:[169,258]},
	"#pixeljunk_1_1a":{ip:[588,0],p:[61,393]},
	"#pixeljunk_1_4a":{ip:[686,0],p:[451,382]},

	"#balloon":{ip:[0,100],p:[673,90]},
	"#butterfly":{ip:[98,100],p:[220,158]},
	"#sun":{ip:[196,100],p:[646,9]},
	"#helicopter":{ip:[294,100],p:[452,23]}
};

for( var key in tooltip_array ){
	(function(key,p,ip){
		$(key).hover(
			function(e){
				ToolTip.Stop().setImgPos(ip[0],ip[1]).setSize(98,50).Stop().setPos(p[0],p[1]).fadeTo(150,0.9) },
			function(e){
				ToolTip.fadeTo(150,"0",function(){ ToolTip.Init() }) }
		)
	})(key,tooltip_array[key]["p"],tooltip_array[key]["ip"]);
}

$("#tooltip").mousemove(function(){
	$(this).css("opacity",0.9).stop();
}).mouseout(function(){
	ToolTip.fadeTo(150,"0",function(){ ToolTip.Init() })
})

})();

function NewAddress(){
	this._url = document.URL.split("#");
	
//	g_title_string = document.title;
	addFLVPlayer("#balloon", "PixelJunk_1_1_movie.flv", "PixelJunk_1_1_movie.jpg", 480, 270);
	addFLVPlayer("#butterfly", "PixelJunk_1_2_movie.flv", "PixelJunk_1_2_movie.jpg", 480, 270);
	addFLVPlayer("#sun", "PixelJunk_1_3_movie.mp4", "PixelJunk_1_3_movie.jpg", 480, 270);
	addFLVPlayer("#helicopter", "PJ_Shooter_Trailer.mp4", "PixelJunk_Shooter_TGS.jpg", 480, 270);
	addForm();
	controlMovie();
	addNotice($("#logo_click"),$("#notice_window"));
	addNoticeNonLink($("#threeinone_click"),$("#threeinone_window"));
	addNoticeLink($("#tshirt_click"),$("#tshirt_window"),{x:182,y:72,w:608,h:345});

	this.Select = {
		"3in1": function(){
			showNotice($("#threeinone_click"),$("#threeinone_window"));
			return this;
		},
		"notice": function(){
			showNotice($("#logo_click"),$("#notice_window"));
			return this;
		},
		"tshirt": function(){
			showNotice($("#tshirt_click"),$("#tshirt_window"),{x:182,y:72,w:608,h:345});
			return this;
		},
		"form": function(){
			showForm();
			return this;
		},
		"shooter": function(){
			showFLVPlayer("#helicopter", "PJ_Shooter_Trailer.mp4", "PixelJunk_Shooter_TGS.jpg", 480, 270);
			return this;
		},
		"eden": function(){
			showFLVPlayer("#sun", "PixelJunk_1_3_movie.mp4", "PixelJunk_1_3_movie.jpg", 480, 270);
			return this;
		},
		"monsters": function(){
			showFLVPlayer("#butterfly", "PixelJunk_1_2_movie.flv", "PixelJunk_1_2_movie.jpg", 480, 270);
			return this;
		},
		"racers": function(){
			showFLVPlayer("#balloon", "PixelJunk_1_1_movie.flv", "PixelJunk_1_1_movie.jpg", 480, 270);
			return this;
		}
	};
	
	this.Exec = this.Select[this._url[1]] || function(){ return this; };
	return this;
}

