var AG_Featured_Pages = 0;
var AG_Featured_Page = 1;
var AG_Featured_Image;
var AG_Featured_Image_Width = 576;
var AG_Featured_Image_Spacing = 15;
var AG_Featured_Excerpt;
var AG_Featured_Excerpt_Width = 495;

var AG_Pages, AG_Page_Width;
var AG_Page_Count = 0;
var AG_Page_Number = 1;

var AG_Order = "recentlyadded";

$j(function(){
	AG_Featured_Pages = $j("#ag_featured div.image div.container div").length;
	AG_Featured_Image = $j("#ag_featured div.image div.container");
	//AG_Featured_Image_Item_Width = AG_Featured_Image.width() + AG_Featured_Image_Spacing;
	AG_Featured_Image_Item_Width = AG_Featured_Image_Width + AG_Featured_Image_Spacing;
	AG_Featured_Image.width((AG_Featured_Image_Item_Width * AG_Featured_Pages));
	
	AG_Featured_Excerpt = $j("#ag_featured div.excerpt div.container");
	//AG_Featured_Excerpt_Item_Width = AG_Featured_Excerpt.width();
	AG_Featured_Excerpt_Item_Width = AG_Featured_Excerpt_Width;
	AG_Featured_Excerpt.width((AG_Featured_Excerpt_Item_Width * AG_Featured_Pages));
	
	AG_Pages_Prepare();
});

function AG_Featured_Scroll(iStep){
	AG_Featured_Page += iStep;
	if(AG_Featured_Page < 1){
		AG_Featured_Page = 1;
		return;
	}
	else if(AG_Featured_Page > AG_Featured_Pages){
		AG_Featured_Page = 1;
	}
	AG_Featured_Image.animate({left: ((AG_Featured_Image_Item_Width) * (AG_Featured_Page - 1) * -1)});
	AG_Featured_Excerpt.animate({left: ((AG_Featured_Excerpt_Item_Width) * (AG_Featured_Page - 1) * -1)});
}

function AG_Update(){
	$j("#ag_loading").css({
		left : Math.round(($j("#ag_content").width() / 2) - ($j("#ag_loading").width() / 2)) + "px",
		top : "20px"
	});
	$j("#ag_content").animate({opacity : 0.5}, "fast");
	$j("#ag_loading").fadeIn("fast");
	var iMediumID = $j("#ag_medium")[0].value;
	$j.post("index.php", {
		cmd : "load-gallery",
		medium : iMediumID,
		sortby : AG_Order
	}, function(sResponse){
		$j("#ag_content").html(sResponse);
		$j("#ag_content").animate({opacity : 1}, "fast");
		$j("#ag_loading").fadeOut("fast");
		AG_Pages_Prepare();
	});
}

function AG_SortBy(sOrder){
	if(AG_Order == sOrder) return;
	AG_Order = sOrder;
	if(sOrder == "recentlyadded"){
		$j("#ag_sortby_recentlyadded").addClass("selected");
		$j("#ag_sortby_mostpopular").removeClass("selected");
	}
	else if(sOrder == "mostpopular"){
		$j("#ag_sortby_mostpopular").addClass("selected");
		$j("#ag_sortby_recentlyadded").removeClass("selected");
	}
	AG_Update();
}

function AG_Pages_Prepare(){
	AG_Pages = $j("#ag_main div.container");
	AG_Page_Count = $j("#ag_main div.container div.page").length;
	AG_Page_Width = $j("#ag_main").width();
	AG_Pages.width(AG_Page_Width * AG_Page_Count);
	AG_Page_Number = 1;
}

function AG_Pages_Go2(iPage){
	if(iPage < 1 || iPage > AG_Page_Count) return;
	AG_Page_Number = iPage;
	AG_Pages.animate({left: ((AG_Page_Width * (AG_Page_Number - 1)) * -1)});
	$j("#ag_nav div").removeClass("selected");
	$j("#ag_nav div:nth-child(" + (iPage + 1) + ")").addClass("selected");
}

function AG_Pages_Previous(){
	AG_Pages_Go2(AG_Page_Number - 1);
}

function AG_Pages_Next(){
	AG_Pages_Go2(AG_Page_Number + 1);
}

function AG_Pages_Last(){
	AG_Pages_Go2(AG_Page_Count);
}