if (typeof(pge) === "undefined") {
	var pge = {
		html: {
			ex: "\
<div class='gallery-ex'>\
	<div class='gallery-title'></div>\
	<div class='gallery-image'>\
		<a class='gallery-image-prev' title='previous image'>&nbsp;</a>\
		<img src='#'/>\
		<a class='gallery-image-next' title='next image'>&nbsp;</a>\
	</div>\
	<div class='gallery-link'>\
		<a>&nbsp;</a>\
	</div>\
	<div class='gallery-nav clearfix'>\
		<a class='gallery-nav-prev'>&nbsp;</a>\
		<div class='gallery-nav-wrap'></div>\
		<a class='gallery-nav-next'>&nbsp;</a>\
	</div>\
</div>\
",
			dx: "\
<div class='gallery-dx'>\
	<div class='gallery-title'></div>\
	<div class='gallery-image'>\
		<a class='gallery-image-prev' title='previous image'>&nbsp;</a>\
		<img src='#'/>\
		<a class='gallery-image-next' title='next image'>&nbsp;</a>\
	</div>\
	<div class='gallery-link'>\
		<a>&nbsp;</a>\
	</div>\
	<div class='gallery-nav clearfix'>\
		<a class='gallery-nav-prev'>&nbsp;</a>\
		<div class='gallery-nav-wrap'></div>\
		<a class='gallery-nav-next'>&nbsp;</a>\
	</div>\
</div>\
	"
		},
		
		log: function(){
			try { console.log(arguments[0]); } catch(e) {}
		},
		
		cur:0,
		store:{},
		reload:false,
		config:{
			ex:{
				max_w:334, max_h:250, th_w:60, th_h:43, suffix:'tx1', count:4
			},
			dx:{
				max_w:637, max_h:477, th_w:80, th_h:60, suffix:"tx2", count:6
			}
		},
		adc:1,
		adr:3,
		cache:{}
	};
};

if (typeof(window.pge_init) === "undefined") {
	function pge_init(obj, size, path)
	{
		if(typeof(window.$) !== "undefined"){
			$(document).ready(function(){
				pge_load(obj, size, path);
			});
		};
	};
	
	function pge_view(obj, eid)
	{
		var id = obj;
		var obj = $("div#gallery-"+ id);
		var cfg = pge.config["dx"];
		
		pge.store[id] = {
			obj: obj,
			size: "dx",
			data: eval("gallery_"+ id)
		};
		
		if (pge.store[id].data.count == 1) {
			$(obj).find("div.gallery-nav").remove();
			$(obj).find("div.gallery-image a").remove();
		} else {
			$.each(pge.store[id].data.entries, function(x, e){
				if (eid == e.id) {
					pge.cur = x;
				};
			});
		};
		
		pge.reload = true;
		pge_nav_init(id);
	};
	
	function pge_load(obj, size, path)
	{
		var id = obj;
		var obj = $("div#gallery-"+ id);
		
		$.get(path, function(o, s){
		
			o = eval("("+ o.substr(o.indexOf(" {"), o.length) +")");
			
			if (typeof(o) === "object" && s === "success" && o.count > 0) {
				
				pge.store[id] = {
					obj: obj,
					size: size,
					data: o
				};
				
				var gal = $(pge.html[size]);
				
				$(gal)
					.children("div.gallery-title")
						.text(o.name)
					.end()
					.children("div.gallery-link")
						.children("a")
							.attr("href", o.url)
						.end()
					.end()
				.end();
				
				obj.append(gal);
				pge_nav_init(id);
				pge_swap(id, 0, true);
				
			} else {
				// not found or empty
				obj.remove();
			};
		});
	};
	
	function pge_swap(id, e, skad)
	{
		pge.cur = e;
		var ent = pge.store[id];
		var cfg = pge.config[ent.size];
		
		$("a#gallery_"+ id +"_thumb_"+ e)
			.parent()
				.children("a")
					.removeClass("on")
				.end()
			.end()
			.addClass("on")
		.end();
		
		if (typeof(skad) === "undefined" && window.ads) {
			// requires /php/adframe.php
			ads.refreshAll();
		}
	};
	
	function pge_image_move(id, m)
	{
		var ent = pge.store[id];
		if (m < 0) {
			m = (pge.cur === 0 ? ent.data.count - 1 : pge.cur - 1);
		} else {
			m = (pge.cur >= ent.data.count - 1 ? 0 : pge.cur + 1);
		};
		
		if (pge.reload) {
			window.location = ent.data.entries[m].link + "#gallery";
		} else {
			pge_swap(id, m);
		};
	};
	
	function pge_image_over(a)
	{
		return false;
		var is_next = /\-next/.test($(a).attr("class"));
		$(a)
			.css("background", "url(/images/slideshow/slideshow_dx_img_"+ (is_next ? "next" : "prev") +".gif) no-repeat")
			.css("background-position", (is_next ? "255px" : "7px") + " 210px")
		.end();
	}
	
	function pge_image_out(a)
	{
		return false;
		$(a)
			.css("background", "none")
		.end();
	}
	
	
	/*
		Build scrolling nav
	*/
	function pge_nav_init(id)
	{
		var ent = pge.store[id];
		var obj = ent.obj;
		var cfg = pge.config[ent.size];
		
		var s = (pge.cur == 0 ? ent.data.count - 1 : pge.cur - 1);
		var u = [];
		for(var e=0; e<cfg.count; e++)
		{		
			var t = ((s + e) >= ent.data.count ? (s - ent.data.count + e) : (s + e));
			if (ent.data.entries[t] && $.inArray(t, u) === -1) {
				u.push(t);
				$(obj)
					.find("div.gallery-nav > div.gallery-nav-wrap")
						.append( pge_nav_thumb(id, t) )
					.end()
				.end();
			};
		};
		
		$(obj)
			.find("a.gallery-image-prev")
				.bind("click", function(){ pge_image_move(id, -1); })
			.end()
			.find("a.gallery-image-next")
				.bind("click", function(){ pge_image_move(id, +1); })
			.end()
		.end();
		
		if (ent.data.count <= cfg.count) {
			$(obj)
				.find("div.gallery-nav > div.gallery-nav-wrap")
					.addClass("nav-less")
				.end()
				.find("div.gallery-nav > a")
					.remove()
				.end()
			.end();
		} else {
			$(obj)
				.find("a.gallery-nav-prev")
					.bind("click", function(){ pge_nav_move.call(this, id, +1); })
				.end()
				.find("a.gallery-nav-next")
					.bind("click", function(){ pge_nav_move.call(this, id, -1); })
				.end()
			.end();		
		};
	};
	
	function pge_nav_thumb(id, e)
	{
		var ent = pge.store[id];
		var cfg = pge.config[ent.size];
		
		// store image object
		var img_obj;
		if (typeof(pge.cache[e]) == "undefined") {
			var fsrc = ent.data.entries[e].tx2;
			
			img_obj = new Image();
			img_obj.name = fsrc.split("/").pop();
			img_obj.onload = function(){
				$("#thumb_"+e).attr("width", this.width);
			};
			img_obj.src = fsrc;
			
			pge.cache[e] = img_obj;
			
		} else {
			img_obj = pge.cache[e];
		};
		
		var img = $("<img/>")
			.attr("id", "thumb_" + e)
			.attr("src", img_obj.src)
			.attr("width", img_obj.width) // cfg.th_w
			.attr("height", cfg.th_h);
			
		var a = $("<a></a>")
			.attr("id", "gallery_"+ id +"_thumb_"+ e)
			.append(img);
			
		if (pge.reload) {
			$(a).attr("href", ent.data.entries[e].link + "#gallery").end();
		} else {
			$(a).bind("click", function(){ pge_swap(id, e); }).end();
		};
		
		return a;
	};

	function pge_nav_move(id, m)
	{
		var ent = pge.store[id];
		var arr = [];
		$("a[id^='gallery_"+ id +"_']").each(function(){
			arr.push(parseInt($(this).attr("id").split("_").pop()));
		});
		
		// find prev/next thumb
		var k = (m > 0 ? arr.pop() : arr.shift());
		if (m > 0) {
			// left
			var n = (k >= ent.data.count - 1 ? 0 : k + 1);
		} else {
			// right
			var n = (k === 0 ? ent.data.count - 1 : k - 1);
		};
		
		$("a#gallery_"+ id +"_thumb_"+ k)
			.remove()
		.end();
		
		// prepend/append
		if (m > 0) {
			// left
			$(pge.store[id].obj)
				.find("div.gallery-nav > div.gallery-nav-wrap")
					.prepend( pge_nav_thumb(id, n) )
				.end()
			.end();
		} else {
			// right
			$(pge.store[id].obj)
				.find("div.gallery-nav > div.gallery-nav-wrap")
					.append( pge_nav_thumb(id, n) )
				.end()
			.end();
		};
		
		// ad rotate
		try {
			if ((pge.adc % pge.adr) == 0) {
				ads = $(".ads").adRefresh();
			};
			pge.adc++;
		} catch (e) {};
	};
};