jQuery(function($) {

	haco.boxPosition();

	$(window).resize(function(){
		haco.boxPosition();
	});

    $("#logo, #copyright, #contactText").pngfix();

	$("a", "#box_menu_blur").focus(function(){
		$(this).blur();
	});
	$("a", "#box_logo").focus(function(){
		this.blur();
	});
	$("#innerLesson").fadeTo("fast", 0, function(){
		$(this).removeClass("none").fadeTo(1200, 0.6);
		$(".table", this).fadeTo(1200, 1);
	});
	$("#conceptBox").fadeTo(1, 0, function(){
		$(this).removeClass("none").fadeTo(1200, 1);
		$("#conceptText", this).pngfix();
	});

	$ref = document.referrer;
	$hst = location.hostname;

	//top
	if (($("#top").size() > 0)&&($ref.search($hst) < 0)) {
		haco.Start();
	}else{
		$("#slide").css({'display':'none'});
	}

	//profile
	if($("#profile").size() > 0) haco.Profile();

});

haco = new Object();
haco.Start = function(){
	var Gallery = new Image();
	var ram = Math.floor(Math.random()*15);
	
	//Set the photo URL
	Gallery.src="/images/gallery/g" + ram + ".jpg" + '?ram=' + Math.floor(Math.random()*10000000);
	Gallery.onload = function(){
		$("#slide").append('<div id="galleryLogo" class="hidden"><img src="/images/logo_gallery.png" width="318" height="56" alt="Haco Works" id="galleryLogoImg" /></div><img src="/images/dot_black.gif" width="1" height="74" alt="" class="dotX" /><img src="/images/dot_black.gif" width="74" height="1" alt="" class="dotY" /><img src="' + Gallery.src + '" alt="" class="bg hidden" />');
		$("#galleryLogo").fadeTo(1, 0);
		$("img:first", "#galleryLogo").pngfix();
		$(".bg:first", "#slide").fadeTo(2000, 0, function(){
			$(this).parent().css({'background-image':'none'});
			$(this).removeClass("hidden").fadeTo(1600, 1, function(){
				$(".dotX", "#slide").fadeTo(1, 0.3);
				$(".dotY", "#slide").fadeTo(1, 0.3);
				$(".dotX", "#slide").animate({
					width:"950px"
				}, 1600);
				$(".dotY", "#slide").animate({
					height:"700px"
				}, 1600, function(){
					$("#galleryLogo").removeClass("hidden").fadeTo(1600, 0.6, haco.End);
				});
			});
		});
	}
}

//Open the Box
haco.End = function(){
	$("#slide").css({'cursor':'pointer'}).click(function(){
		$(".dotX, #galleryLogo", "#slide").animate({
			left:"950px"
		}, 600, function(){
			$(this).parent().fadeTo(1600, 0, function(){
				$(this).css({'display':'none'});
			})
		});
		$(".dotY", "#slide").animate({
			top:"700px"
		}, 1200);
	});
}

//profile
haco.Profile = function(){
	$a = $('#aboutCARTONNAGE a');
	//$($a[0]).html('slideshow start');
	$($a[0]).addClass('Visible');
	$a.lightBox({
		fixedNavigation:true,
		overlayBgColor:'#4a064c',
		imageLoading:'/images/lightbox/lightbox-ico-loading.gif',
		imageBtnClose:'/images/lightbox/lightbox-btn-close.gif',
		imageBtnPrev:'/images/lightbox/lightbox-btn-prev.gif',
		imageBtnNext:'/images/lightbox/lightbox-btn-next.gif'
	});

	$b = $('#aboutHacoworks a');
	//$($b[0]).html('slideshow start');
	$($b[0]).addClass('Visible');
	$b.lightBox({
		fixedNavigation:true,
		overlayBgColor:'#93b25b',
		imageLoading:'/images/lightbox/lightbox-ico-loading.gif',
		imageBtnClose:'/images/lightbox/lightbox-btn-close.gif',
		imageBtnPrev:'/images/lightbox/lightbox-btn-prev.gif',
		imageBtnNext:'/images/lightbox/lightbox-btn-next.gif'
	});
}

haco.GetPagesSize = function(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = [w,h];
	return arrayPageSize;

}
haco.boxPosition = function(){
	$wh = haco.GetPagesSize();
	$marginLeft = Math.floor(($wh[0] - $(body).width()) / 2);

	if ($wh[1] > $("#body").height()){
		$marginTop = Math.floor((($wh[1] - $(body).height()) / 2) - ($("#foot").height() / 2));
		$("body").height("100%");
	}else{
		$marginTop = 0;
		$("body").height($("#body").height()+80);
	}
	$marginLeftShadow = $marginLeft + 15;
	$marginTopShadow = $marginTop + 15;

	$marginTopFoot = $marginTop + $("#shadow").height() + 26;

	$("#body").css({left:$marginLeft + "px", top:$marginTop + "px"});
	$("#shadow").css({left:$marginLeftShadow + "px", top:$marginTopShadow + "px"});
	$("#foot").css({left:0, top:$marginTopFoot + "px"});
}

