var gpicSpeed = 300;
var gpicAnimation = false;
var gpicIndex;
var gpicArray;
var gpic;
var gpicLoading = true;
var gpicImagesPath = "/js/gpic/images/";


function gpic_option_remove() {
	$("#gpic_move_prev_IMG").remove();
	$("#gpic_move_next_IMG").remove();
	if ($("#gpic_title_DIV").size() > 0) {
		$("#gpic_title_DIV").slideUp(200, function(){
			$("#gpic_title_DIV").remove();
		});
	}
	return true;
}




function gpic_close(event) {
		var remove = gpic_option_remove(event);
		$("#gpic_IMG").fadeOut(gpicSpeed, function(){
			$("#gpic_IMG").remove();
			$("#gpic_DIV").fadeOut(gpicSpeed, function(){
				$("#gpic_DIV").remove();
				closeAlphaBody();
			});
		});
	return true;
}





function gpic_move() {
	if (($(gpicArray).size() > 1)) {
		if ($("#gpic_move_prev_IMG").size() == 0) {
			var imgMovePrev = $("<img id='gpic_move_prev_IMG' border='0' width='60' height='60' align='absmiddle' hspace='0' vspace='0' src='"+gpicImagesPath+"prev.png'>");
			imgMovePrev.css("position", "absolute");
			imgMovePrev.css("left", "10px");
			imgMovePrev.css("top", String(parseInt($("#gpic_DIV").height()/2)-15)+"px");
			imgMovePrev.css("cursor", "pointer");
			imgMovePrev.bind("click", function(){
				gpicIndex = gpicIndex-1;
				gpic_go();
			});
			$(imgMovePrev).appendTo($("#gpic_DIV"));
		}
		if ($("#gpic_move_next_IMG").size() == 0) {
			var imgMoveNext = $("<img id='gpic_move_next_IMG' border='0' width='60' height='60' align='absmiddle' hspace='0' vspace='0' src='"+gpicImagesPath+"next.png'>");
			imgMoveNext.css("position", "absolute");
			imgMoveNext.css("left", String($("#gpic_DIV").width()-50)+"px");
			imgMoveNext.css("top", String(parseInt($("#gpic_DIV").height()/2)-15)+"px");
			imgMoveNext.css("cursor", "pointer");
			imgMoveNext.bind("click", function(){
				gpicIndex = gpicIndex+1;
				gpic_go();
			});
			$(imgMoveNext).appendTo($("#gpic_DIV"));
		}
	}
}





function gpic_pisitioning_DIV(img, position) {
	if (position == "left") {
		position = Math.floor(($(window).width()/2)-(($(img).width()+22)/2));
	} else if (position == "top") {
		position = Math.floor(($(window).height()/2)-(($(img).height()+22+30+31)/2));
	}
	return position;
}





function gpic_create_DIV() {
	var div;
	var id = "gpic_DIV";
	if ($("#"+id).size() > 0) {
		div = $("#"+id);
	} else {
		var div = $("<div id='"+id+"'>");
		div.css("position", "fixed");
		div.css("top", parseInt($(window).height()/2));
		div.css("left", parseInt($(window).width()/2));
		div.css("background-color", "#ffffff");
		div.css("padding", "10px");
		div.css("border", "1px solid #000000");
		div.css("width", "200px");
		div.css("height", "200px");
		div.css("text-align", "center");
		div.css("z-index", "4");
		div.css("overflow", "visible");

		$(div).hover(
			function(event){
				if (($(event.target).attr("id").indexOf("gpic_") > -1) && (!gpicAnimation)) {
					gpic_move();
					gpic_title();
				}
			},
			function(event){
				gpic_option_remove();
			}
		);

/*




		$(div).bind("mousemove", function(event){
			if (($(event.target).attr("id").indexOf("gpic_") > -1) && (!gpicAnimation)) {
				gpic_move();
				//gpic_title();
			}
		});

		$(div).bind("mouseout", function(event){
			if ($(event.target).attr("id").indexOf("gpic_") == -1) {
				gpic_option_remove();
			}
		});

		$(div).mouseout(function(event){
			if ($(event.target).attr("id").indexOf("gpic_DIV") > -1) {
				gpic_option_remove();
			}
		});

*/
	}
	return div;
}





function gpic_create_IMG() {
	var id = "gpic_IMG";
	var img = $("<img id='"+id+"' border='0' align='absmiddle' vspace='0' hspace='0' src='"+$(gpic).attr("href")+"'>");
	$(img).css("z-index", "0");
	$(img).hide();
	return img;
}





function gpic_title() {
	var id = "gpic_title_DIV";
	if (($("#"+id).size() == 0) ) {
		var picTitle = $(gpic).attr("title");
		var picGallery = $(gpic).attr("rel");
		if (picGallery.indexOf("[") > -1) {
			picGallery = picGallery.substring(picGallery.indexOf("[")+1, picGallery.length-1);
		} else {
			picGallery = "";
		}
		if (picGallery != "") {
			picGallery = " in "+picGallery;
		}
		var title = $("<div id='"+id+"'>");
		//title.css("float", "left");
		title.css("position", "absolute");
		title.css("left", "-1px");
		title.css("top", String($("#gpic_IMG").height()+20)+"px");
		title.css("border-left", "1px solid #000000");
		title.css("border-right", "1px solid #000000");
		title.css("border-bottom", "1px solid #000000");
		title.css("width", String($("#gpic_DIV").width())+"px");
		title.css("height", "29px");
		title.css("line-height", "12px");
		title.css("font-weight", "normal");
		title.css("font-family", "verdana");
		title.css("font-size", "10px");
		title.css("text-align", "left");
		title.css("padding", "0px 10px 0px 10px");
		title.css("background-color", "#ffffff");
		$(title).hide();
		$(title).appendTo($("#gpic_DIV"));
		$(title).slideDown(200, function(){
			$(title).html("<strong id='gpic_title_STRONG'>"+picTitle+"</strong><br /><span id='gpic_title_SPAN'>&nbsp;&nbsp;Foto "+String(gpicIndex+1)+" di "+$(gpicArray).size().toString()+picGallery+"</span><a id='gpic_close_A' href='javascript:void(0);' onClick='$(this).parent().slideUp(200, gpic_close()); $(this).remove();' style='position:absolute; top:-2px; left:"+String($("#gpic_DIV").width()-20)+"px; float:right; width:28px; height:28px: text-align:center; font-weight:bold; font-size:16px;'><img id='gpic_close_IMG' width='20' height='20' hspace='4' vspace='4' align='absmiddle' border='0' src='"+gpicImagesPath+"close.gif'></a>");
		});
	}
}

function gPic_resizeContainer(div, img) {
	gpicAnimation = true;
	var imgW = $(img).width();
	var imgH = $(img).height();
	var imgMaxW = $(window).width()-200;
	var imgMaxH = $(window).height()-200;
	var imgMinW = 200;
	var imgMinH = 200;
	if (imgH < imgMinH) {
		imgW = Math.ceil((imgW/imgH)*imgMinH);
		imgH = imgMinH;
	} else if (imgH > imgMaxH) {
		imgW = Math.ceil((imgW/imgH)*imgMaxH);
		imgH = imgMaxH;
	} else if (imgW < imgMinW) {
		imgH = Math.floor((imgH/imgW)*imgMinW);
		imgW = imgMinW;
	} else if (imgW > imgMaxW) {
		imgH = Math.floor((imgH/imgW)*imgMaxW);
		imgW = imgMaxW;
	}
	$(img).attr("width", imgW);
	$(img).attr("height", imgH);
	$(div).animate({width:imgW, left:gpic_pisitioning_DIV(img, "left")},gpicSpeed, function(){});
	$(div).animate({height:imgH, top:gpic_pisitioning_DIV(img, "top")},gpicSpeed, function(){
		$(img).fadeIn(gpicSpeed, function(){
			gpicAnimation = false;
		});
	});
}


function gpic_loading_scroll(step) {
	step++;
	$("#gpic_loading_DIV").css("background", "url("+gpicImagesPath+"loading.png) no-repeat 0px "+ -parseInt(50*step) +"px");
	if (step >= 7) {
		step = 0;
	}
	if (gpicLoading) {
		setTimeout("gpic_loading_scroll("+step+")", 25);
	}
}

function gpic_loading() {
	var id = "gpic_loading_DIV";
	if (($("#"+id).size() == 0) && (!gpicAnimation)) {
		var gpicLoading = $("<div id='"+id+"'>");
		gpicLoading.css("position", "absolute");
		gpicLoading.css("width", "50px");
		gpicLoading.css("height", "50px");
		gpicLoading.css("margin", "auto");
		gpicLoading.css("padding", "0px");
		gpicLoading.css("background", "url(images/loading.png) no-repeat 0px 0px");
		gpicLoading.css("z-index", "1");
	} else {
		gpicLoading = $("#"+id);
	}
	gpicLoading.css("top", parseInt(($("#gpic_DIV").height()/2)-15)+"px");
	gpicLoading.css("left", parseInt(($("#gpic_DIV").width()/2)-15)+"px");
	$(gpicLoading).show();
	gpic_loading_scroll(0);
	return gpicLoading;
}


function gpic_go() {
	gpicAnimation = true;
	gpic_option_remove();

	if (gpicIndex >= gpicArray.size()) {
		gpicIndex = 0;
	} else if (gpicIndex < 0) {
		gpicIndex = gpicArray.size()-1;
	}


	gpic = $(gpicArray).get(gpicIndex);
	var mainAppend = $(document.body);
	var url = $(gpic).attr("href");

	if ($("#gpic_IMG").size() > 0) {
		//if (url == $("#gpic_IMG").attr("src")) {return false;}
		$("#gpic_IMG").fadeOut(gpicSpeed, function(){
			$("#gpic_IMG").remove();
		});
	}

	var img = $(gpic_create_IMG());
	var div = $(gpic_create_DIV());
	$(img).appendTo(div);
	$(div).appendTo(mainAppend);
	var loading = $(gpic_loading());
	$(loading).appendTo(div);
	$(img).bind("load", function(){
		$("#gpic_loading_DIV").hide();
		gpicLoading = false;
		gPic_resizeContainer(div, img);
	});
}



function gpic_initialize() {
	gpicArray = $("a[rel^='gpic']");
	$(gpicArray).click(function(){
		createAlphaBody(null, null);
		gpicArray = $("a[rel='"+$(this).attr("rel")+"']");
		gpicIndex = $(gpicArray).index($(this));
		gpic_go();
		return false;
	});

	$(document).click(function(event){
		if ($(event.target).attr("id")) {
			if ($(event.target).attr("id").indexOf("gpic_") == -1) {
				gpic_close();
			}
		}
	});
}
