$(document).ready(function()
{

	var count = $('ul.ovw_m').children().size();
	var pos = 0;
	var image = null;
	
	var artwork_id = 0;
	
	var cached = new Array(count);
	
	var info = new Array(5);
	
	$('#large').wrap("<a href='#' title='Click for more information'></a>");
	
	if(count > 1)
	{					
		// 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);
	}
	
	
	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;
	}
	
	// Fade Items
	function fade()
	{
		$('ul.ovw_m li[class!=selected]').css("opacity",0.2);
		info = $('ul.ovw_m li.selected a').attr("rel").split("|");
		
		$('h3.a_desc').html("&mdash; " + info[0]);
		$('#large').attr("alt",info[0]);
		
		artwork_id = info[1];
		
		myvariations(artwork_id);
		
		if(info[4] > 0)
		{
			$('li.a_add a').html("- Remove from my saved artworks");
		}
		else
		{
			$('li.a_add a').html("+ Add to my saved artworks");
		}
		
		$('li.a_profile a').attr("href", "/art_and_artists/artists/name/" + info[2] + "/");
		$('li.a_work a').attr("href", "/art_and_artists/artworks/name/" + info[2] + "/");
		$('h3.a_more a').attr("href", "/art_and_artists/artworks/viewer/" + info[1] + "/" + info[3] + "/");
		$('#large').parent().attr("href", "/art_and_artists/artworks/viewer/" + info[1] + "/" + info[3] + "/");
	}
	
	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","_t7")).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","_t7")).load(function(e)
				{
					$('#large').animate(
					{
			       		opacity: 1
			     	},
					200,
					"linear",
					function()
					{
						$(this).removeAttr("style");
						//$(this).parent().removeClass("loading");
						
						paging();
					});
				});
		
				cached[pos] = pos;
			}
		});
	}
	
	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);
		$('ul.ovw_m li a img.img' + pos).parent().parent().addClass('selected');
		
		fade();
		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...");
			
			$.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);'>- Remove from my saved artworks</a>");
						
						
						info = $('ul.ovw_m li.selected a').attr("rel").split("|");
						
						$('ul.ovw_m li.selected a').attr("rel",info[0] + "|" + info[1] + "|" +  info[2] + "|" + info[3] + "|" + artwork_id);
						
						myexhibition();
					}
					else
					{
						$('li.a_add a').parent().html("<a href='javascript:void(0);'>+ Add to my saved artworks</a>");
						$('ul.ovw_m li.selected a').attr("rel",info[0] + "|" + info[1] + "|" +  info[2] + "|" + info[3] + "0");
						myexhibition();
					}
				},
				error: function(msg)
				{
					var o = null;
					eval("o = " + msg + ";");
					$('li.a_add a').parent().html("Could not be added");
				}
			 });
		});
	}
	
	myexhibition();
	
	function myvariations(id)
	{
		$.ajax(
		{
			type: "POST",
			url: myAjaxPath,
		    data: '{"id":' + id + '}',
			timeout: 2000,
		    beforeSend: function(xhr) {
		        xhr.setRequestHeader("X-AjaxPro-Method", "get_variations");
		    },
			success: function(msg)
			{
				var o = null;
				eval("o = " + msg + ";");
				
				$("#v_PageBody_hook").children("ul.cfx").remove();
				$("#v_PageBody_hook").prepend(o.value);
				//$("#v_PageBody_hook").html(o.value);
			},
			error: function(msg)
			{

			}
		 });
	}
	
});