$(document).ready(function()
{
	var count = $('ul.ovw_m').children().size();
	var pos = 0;
	
	var artwork_id = 0;
	
	var cached = new Array(count);

	if(count > 0)
	{
		// Change Large Image
		$('ul.ovw_m li a').attr("href","javascript:void(0);").click(function(e)
		{
			if($(this).parent().is('[class!=selected]'))
			{
				$('ul.ovw_m li').removeAttr('class').css("opacity",1.0);
				$(this).parent().addClass('selected');
			
				fade();
				paging();
			
				large();
			}
		});
	
		fade();
		paging();
	
		$('ul.paging li a').attr("href","javascript:void(0);");
		
		$('ul.paging li.next ').bind("click",page_next);
		$('ul.paging li.prev ').bind("click",page_prev);
	}
	
	$('#large').addClass("img_maximise").attr("title","Click to show larger version").bind("click",function()
	{
		//alert($(this).attr("alt"));
		
		//($(document).height() - 40)
		
	
		
		//$('form').hide().after("<div class='imagewrap' style='height:100%;'><div class='floating' style='height:100%;'><div style='height:100%;'><div id='flashviewer' style='height:100%;width:100%;'><img src='" + $(this).attr("src").replace("_t6","") + "' title='Click to hide larger version' style='cursor:pointer;margin:0 auto;' class='overimage img_minimize'/></div></div></div></div>");
		$('form').hide();
		$('form').after("<div id='imagewrap' class='imagewrap' style='background:transparent;height:100%;width:100%' id='wrapme'><div id='flashviewer' style='height:100%;width:100%;'><img src='" + $(this).attr("src").replace("_t6","") + "' title='Click to hide larger version' style='display:block;cursor:pointer;margin:0 auto;width:900px; padding:20px 0' class='overimage img_minimize'/></div><div style='cursor:pointer;margin:0 auto;height:" + ($(document).height() - 40) +"px;width:100%;position:absolute:top:0px;left:0px;' class='overimage img_minimize'>&nbsp;</div></div>");
		
		$('html,body').addClass("force_height");
		
		var so = new SWFObject ("/app_flash/loader.swf?image=" + $(this).attr("src").replace("_t6",""), "jsdisplay", "100%", "100%", "8");
		so.addVariable("jsfunction", "closewindow");
		so.addVariable("wmode", "transparent");
		so.write("imagewrap");
		
		$('div.imagewrap').unbind().bind("click",function()
		{
			$(this).remove();
			$('html,body').removeClass("force_height");
			$('form').removeAttr("style");
		});
	});
	
	
	// Fade Items
	function fade()
	{
		$('ul.ovw_m li[class!=selected]').css("opacity",0.2);
	}
	
	function large()
	{
		$('li.next').hide();
		$('li.prev').hide();
		
		
		$('#large').animate(
		{
       		opacity: 0
     	},
		100,
		"linear",
		function(e)
		{
			if(arr_contains(pos, cached))
			{
				$('#large').attr("src",$('ul.ovw_m li.selected a img').attr("src").replace("_t5","_t6")).animate(
				{
		       		opacity: 1
		     	},
				200,
				"linear",
				function()
				{
					$(this).removeAttr("style");
					
					paging();
				});
			}
			else
			{
				$('#large').attr("src",$('ul.ovw_m li.selected a img').attr("src").replace("_t5","_t6")).load(function(e)
				{
					$('#large').animate(
					{
			       		opacity: 1
			     	},
					200,
					"linear",
					function()
					{
						$(this).removeAttr("style");
						//$(this).parent().removeClass("loading");
						
						paging();
					});
				});
		
				cached[pos] = pos;
			}
		});
	}
	
	function arr_contains(search_term, arr)
	{
		var i = arr.length;
		if (i > 0)
		{
			do
			{
				if (arr[i] === search_term)
				{
				   return true;
				}
			}
			while (i--);
		}
		return false;
	}
	
	function paging()
	{
		pos = $('ul.ovw_m li.selected a img').attr("class").replace("img","");
		
		// Update Paging
		$('li.numb').html(pos + " / " + count);
		
		if(pos < count) $('li.next').removeAttr("style");
		else $('li.next').hide();
		
		if(pos > 1) $('li.prev').removeAttr("style");
		else $('li.prev').hide();
	}
	
	function content()
	{
		$('ul.ovw_m li').removeAttr('class').css("opacity",1.0);
		$('ul.ovw_m li a img.img' + pos).parent().parent().addClass('selected');
		
		fade();
		paging();
		large();
	}
	
	function page_next()
	{
		pos++;
		content();
	}
	
	function page_prev()
	{
		pos--;
		content();
	}
	
	function myexhibition()
	{
		$('li.a_add a').unbind().attr("href","javascript:void(0);").bind("click",function(e)
		{
			$('li.a_add a').html("updating...");
			
			artwork_id = $(this).attr("rel");
			
			$.ajax(
			{
				type: "POST",
				url: myAjaxPath,
			    data: '{"ArtworkID":"' + artwork_id + '"}',
				timeout: 2000,
			    beforeSend: function(xhr) {
			        xhr.setRequestHeader("X-AjaxPro-Method", "AddToLightbox");
			    },
				success: function(msg)
				{
					var o = null;
					eval("o = " + msg + ";");
					
					if(o.value == "1")
					{
						$('li.a_add a').parent().html("<a href='javascript:void(0);' rel='" + artwork_id + "'>- remove from my saved artworks</a>");
						myexhibition();
					}
					else
					{
						$('li.a_add a').parent().html("<a href='javascript:void(0);' rel='" + artwork_id + "'>+ add to my saved artworks</a>");
						myexhibition();
					}
				},
				error: function(msg)
				{
					var o = null;
					eval("o = " + msg + ";");
					$('li.a_add a').parent().html("could not be added");
				}
			 });
		});
	}
	
	myexhibition();
	
});

	
function closewindow() {
	$('#imagewrap').remove();
	$('form').show();
}