try { document.execCommand('BackgroundImageCache', false, true); } catch(e) {}
var fb_opt = {'overlayShow': true, 'overlayOpacity': 0.70, 'zoomSpeedIn' : 300, 'zoomSpeedOut' : 400};
var imageId = 0;

$(function() {
	// image preload
	$.preloadImages(_appRoot + "/images/arrow-down.png", _appRoot + "/images/loader.gif");
	
	// fancybox init
	if ($.isFunction($.fn.fancybox)) {
		$("#gallery-image a").fancybox(fb_opt);
	}
	
	// link submit
	$("a.submit").click(function(){$(this).parents("form").submit(); return false;});
	
	// gallery
	$("#gallery-prev a, #gallery-next a").click(function() { loadImage($(this).attr('rel')); });
	if ($("#gallery-image").size() > 0) {
		loadImage(imageId);
	}
	$("#gallery img").rotateRight(7);
});


function loadImage(id) {
	$cnt = $("#gallery-image");
	$anchor = $("#gallery-image a").hide();
	$prev = $("#gallery-prev a").hide();
	$next = $("#gallery-next a").hide();
	$("#loader", $cnt).show();
	$.post(_appRoot+'/ajax/image.php', {method:'get',id:id} , function(data){ 
		$("#loader", $cnt).hide();
		$("p", $cnt).html(data.title);
		$("a", $cnt).attr('href', data.url).attr('title', data.title);
		$("a img", $cnt).attr('src', data.url_tn);
		$prev.attr('rel', data.prev).showhide(data.prev > 0);
		$next.attr('rel', data.next).showhide(data.next > 0);
		$anchor.show();
		
	}, "json");
}


jQuery.preloadImages = function() {
  for(var i = 0; i<arguments.length; i++) {
    jQuery("<img>").attr("src", arguments[i]);
  }
}
jQuery.fn.showhide = function(val) {  
	return this.each(function(i, n) {  
		val ? $(this).show() : $(this).hide();  
	});  
}  
