// <![CDATA[
    
    var imagepath = new Array();
    var imagename = new Array();
    var imageid = new Array();
    var imagedbid = new Array();
    var imagewidth = new Array();
    var imagedescription = new Array();
    var currentpic = 1;
    var imagemoving = false;
    var currentourmedia = 0;
    var tmp_play = '';
  
	function loadMedia(media_uid, band_uid, width, height, topmargin, gallery_id)
	{
		if(imagedbid[currentpic] == media_uid)
		{ 
			// Only show if selected
			var picdiv = $('picturewrapper_' + media_uid);
			tmp_play = picdiv.innerHTML;
			
			ajax = new Ajax.Updater(
				 'picturewrapper_' + media_uid,
				 '/Volumes/mars_002/Webdata/global/bands/media/getmedia.php',
				 {
					method: 'get',
					parameters: 'media_uid=' + media_uid + '&band_uid='+ band_uid + '&pwidth='+ width + '&pheight='+ height + '&ptop='+ topmargin + '&gallery_id=' + gallery_id,
					asynchronous: true,
					evalScripts: true
				 }
			);
		}
	}
  
	function nextpics(maxwidth) 
	{
		var smallpictureframe = $('smallpictureframe');

		if(strippx(smallpictureframe.style.left) > maxwidth)
		{
			Effect.MoveBy('smallpictureframe', 0, -250);	
		}	
	}
		
	function prevpics() 
	{
		var smallpictureframe = $('smallpictureframe');
		
		if(strippx(smallpictureframe.style.left) < 0)
		{
			Effect.MoveBy('smallpictureframe', 0, 250);	
		}
	}


  
	function picjump(pic, band_uid) 
	{
  		if(currentpic != pic)
    	{ 
    		// lock for media-play
    		if(tmp_play != '')
			{
				var picdiv = $('picturewrapper_' + imagedbid[currentpic]);
				picdiv.innerHTML = tmp_play;
				tmp_play = '';
			}
  
			var moveme = false;
			if(!imagemoving)
			{
				var movement = 0;
				
				if(pic > currentpic)
				{
					if(pic < imagepath.length)
					{
						imagemoving = true;
						for(var i = currentpic; i < pic; i++)
						{
							var widthNextPic = imagewidth[i + 1];
							var widthCurrentPic = imagewidth[i];
							movement = movement - widthCurrentPic/2 - widthNextPic/2 - 20;
						}
						moveme = true;
					}
				}
				else if(pic < currentpic)
				{
					if(pic > 0)
					{	
						imagemoving = true;
						for(var i = currentpic; i > pic; i--)
						{
							var widthPrevPic = imagewidth[i - 1];
							var widthCurrentPic = imagewidth[i];
							movement = movement + widthCurrentPic/2 + widthPrevPic/2 + 20;
						}
						moveme = true;
					}
				}
				
				if(moveme)
				{
					Effect.MoveBy('pictureframe', 0, movement,
					{
						afterFinish: function(e)
						{
							currentpic = pic;					
							var name = $('imagetitle');
							name.innerHTML = urldecode(imagename[currentpic]);
							var caption = $('imagecaption');
							caption.innerHTML = urldecode(imagedescription[currentpic]);
							showRatings(imageid[currentpic], band_uid);
							imagemoving = false;
						}
					});	
				}
			}
		}
	}
	
	function urlencode(string)
	{
		var safechars = "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "-_.!~*'()";
		var hex = "0123456789ABCDEF";
	
		var encoded = "";
		
		for (var i = 0; i < string.length; i++ ) 
		{
			var ch = string.charAt(i);
			if(ch == " ") 
			{
				encoded += "+";
			} 
			else if(safechars.indexOf(ch) != -1) 
			{
				encoded += ch;
			} 
			else 
			{
				var charCode = ch.charCodeAt(0);
				if(charCode > 255) 
				{
					encoded += "+";
				} 
				else 
				{
					encoded += "%";
					encoded += hex.charAt((charCode >> 4) & 0xF);
					encoded += hex.charAt(charCode & 0xF);
				}
			}
		}
	
		return encoded;
	}
	
	
	function urldecode(string)
	{
		var hexchars = "0123456789ABCDEFabcdef"; 
		var encoded = string;
		var plaintext = "";
		
		var i = 0;
		while (i < encoded.length) 
		{
			var ch = encoded.charAt(i);
			if (ch == "+") 
			{
				plaintext += " ";
				i++;
			} 
			else if (ch == "%") 
			{
				if (i < (encoded.length - 2) && hexchars.indexOf(encoded.charAt(i+1)) != -1 && hexchars.indexOf(encoded.charAt(i+2)) != -1 ) 
				{
					plaintext += unescape( encoded.substr(i,3) );
					i += 3;
				} 
				else 
				{
					plaintext += "%[ERROR]";
					i++;
				}
			} 
			else 
			{
				plaintext += ch;
				i++;
			}
		}

		return plaintext;
	}
	
	
	function showRatings(media_id, band_uid) 
    {	
    	ajax = new Ajax.Updater(
			 'imagereview',
			 '/Volumes/mars_002/Webdata/global/bands/media/mediareview.php',
			 {
				method: 'get',
				parameters: 'media_id=' + media_id + '&band_uid=' + band_uid,
				asynchronous: true,
				evalScripts: true
			 }
		);
	}
	
	function saveReview(form) 
    {	
    	ajax = new Ajax.Updater(
			 'imagereview',
			 '/Volumes/mars_002/Webdata/global/bands/media/mediareviewsave.php',
			 {
				method: 'post',
				parameters: Form.serialize($(form)),
				asynchronous: true,
				evalScripts: true
			 }
		);
	}
	
	function strippx (value) 
	{
		if(value == '') 
		{ 
			return 0;
		}
		return parseFloat(value.substring(0, value.length - 2));
	}
	
	function createSortableGalleries(element_id, form_id)
	{
		var gallerytiles = $(element_id);
		Sortable.create(gallerytiles,
		{
			tag: 'div',
			overlap: 'horizontal',
			constraint: false,
			onUpdate: function()
			{
				var elementList = Form.getElements(form_id);
				var check = Sortable.serialize(gallerytiles);
				var chunks = check.split('&' + element_id + '[]=');
				chunks[0] = chunks[0].replace(element_id +'[]=', '');
				for(var i = 0; i < chunks.length; i++)
				{
					elementList[i].value = chunks[i];
				}
			}
		});
 	}
	
	function createSortableImages(element_id, form_id)
	{
		var imagetiles = $(element_id);
		Sortable.create(imagetiles,
		{
			tag: 'div',
			overlap: 'horizontal',
			constraint: false,
			onUpdate: function()
			{
				var elementList = Form.getElements(form_id);
				var check = Sortable.serialize(imagetiles);
				var chunks = check.split('&' + element_id + '[]=');
				chunks[0] = chunks[0].replace(element_id + '[]=', '');
				
				for(var i = 0; i < chunks.length; i++)
				{	
					elementList[i].value = chunks[i];
				}
			}
		});
 	}


 	function AvoidJumpto(loading_text)
 	{
    	// Show loading notification
    	var loadingdiv = $('loadingdiv');
    	loadingdiv.innerHTML = loading_text;
    	Appear(0, 'loadingdiv');
    	$('jumpto').value = '-1';
	} 


 	function LoadGalleryDiv(band_uid, gallery_id, loading_text)
 	{
    	// Show loading notification
    	var loadingdiv = $('loadingdiv');
    	loadingdiv.innerHTML = loading_text;
    	Appear(0, 'loadingdiv');
		ajax = new Ajax.Updater(
			'uploaddiv',
			'/Volumes/mars_002/Webdata/global/bands/media/gallery_edit.php',
			{
				method: 'get',
				parameters: 'band_uid=' + band_uid + '&gallery_id=' + gallery_id,
				asynchronous: true,
				evalScripts: true,
				onSuccess: function(t) 
				{
					Disappear(0, 'loadingdiv');
			 		Appear(0.5, 'uploaddiv');
				}
			}
		);
	}
 
 	function LoadUploadsDiv(band_uid, gallery_id, key, filename, gallery_order, loading_text)
 	{
    	// Show loading notification
    	var loadingdiv = $('loadingdiv');
    	loadingdiv.innerHTML = loading_text;
    	Appear(0, 'loadingdiv');
		ajax = new Ajax.Updater(
			'uploaddiv',
			'/Volumes/mars_002/Webdata/global/bands/media/image_edit.php',
			{
				method: 'get',
				parameters: 'band_uid=' + band_uid + '&gallery_id=' + gallery_id + '&key=' + key + '&filename=' + filename + '&gallery_order=' + gallery_order,
				asynchronous: true,
				evalScripts: true,
				onSuccess: function(t) 
				{
					Disappear(0, 'loadingdiv');
			 		Appear(0.5, 'uploaddiv');
				}
			}
		);
	}
		
	
 	function LoadUploadsDiv_java(band_uid, gallery_id, key, filename, gallery_order, loading_text)
 	{
    	// Show loading notification
    	var loadingdiv = $('loadingdiv');
    	loadingdiv.innerHTML = loading_text;
    	Appear(0, 'loadingdiv');
		ajax = new Ajax.Updater(
			'uploaddiv',
			'/Volumes/mars_002/Webdata/global/bands/jupload/file_upload.php',
			{
				method: 'get',
				parameters: 'band_uid=' + band_uid + '&gallery_id=' + gallery_id + '&key=' + key + '&filename=' + filename + '&gallery_order=' + gallery_order,
				asynchronous: true,
				evalScripts: true,
				onSuccess: function(t) 
				{
					Disappear(0, 'loadingdiv');
			 		Appear(0.5, 'uploaddiv');
				}
			}
		);
	}
	
 	function CloseUploadsIFrame()
 	{
 		var upload_div = $('uploaddiv');
 		upload_div.innerHTML = '';
 		Disappear(0.5, 'uploaddiv');
 	}
 
  
 	function popitup(url, name, height, width) 
	{
		width += 30;
		if(width < 420) { width = 420; }
		height += 245;
	
		if(!newwindow.closed && newwindow.location) 
		{
			newwindow.location.href = url;
		}
		else 
		{
			var openerWidth = getOpenerSize();
			var positionX = openerWidth/2 - width/2;

			var options = 'resizable=yes, scrollbars=no,toolbar=no, status=no, menubar=no, location=no, height=' + height + ', width=' + width + ', top=300, left=' + positionX + ', screenY=300, screenX=' + positionX;

			newwindow = window.open(url, '', options);
			
			if(!newwindow.opener)
			{
				newwindow.opener = self;
			}
		}
		if(window.focus) 
		{
			newwindow.focus();
		}
		return false;
	}
	
	function getOpenerSize() 
	{
		var myWidth = 0;
		var myHeight = 0;
		
		if(typeof(window.innerWidth ) == 'number') 
		{
			//Non-IE
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;	
		} 
		else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) 
		{
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		} 
		else if(document.body && (document.body.clientWidth || document.body.clientHeight)) 
		{
			//IE 4 compatible
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		}
		return myWidth;
	}
	
	function MediaShowVoteStars(value, media_uid)  
	{
		var savefield = $('rating_' + media_uid);
		savefield.value = value;
		
		element = $('startrack_' + media_uid);
			
		var finalwidth = 200 / 5 * value;
		element.style.width = finalwidth + 'px';
	}
	
	function CreateMediaSlider(media_uid)
	{
		var sliderVote = new Control.Slider('voteselect_' + media_uid, 'votetrack_' + media_uid, {range: $R(1, 5), values: [1, 2, 3, 4, 5]});
		sliderVote.options.onSlide = function(value)
		{
			MediaShowVoteStars(value, media_uid);
		}
		sliderVote.options.onChange = function(value)
		{
			MediaShowVoteStars(value, media_uid);
		}          
	}
	
	function ShowItemReviews(media_id, band_uid, edit)
	{
		var elementList = document.getElementsByClassName('mediareview');
		for (var i = 0; i < elementList.length; i++)
		{
			Disappear(0.1, elementList[i]);
		}
		
		if(media_id == currentourmedia)
		{
			currentourmedia = 0;
		}
		else
		{
			ajax = new Ajax.Request(
				 '/Volumes/mars_002/Webdata/global/bands/media/mediaitemreview.php',
				 {
					method: 'get',
					parameters: 'media_id=' + media_id + '&band_uid=' + band_uid + '&edit=' + edit+ '&only_own=0',
					asynchronous: true,
					evalScripts: true,
					onSuccess: function(t)
					{
						var div = $('reviews_' + media_id);
						div.innerHTML = t.responseText;
						Appear(0.2, 'reviews_' + media_id);
					}
				 }
			);
			currentourmedia = media_id;
		}
	}

	function ShowOurItemReviews(media_id, band_uid, edit)
	{
		var elementList = document.getElementsByClassName('mediareview');
		for (var i = 0; i < elementList.length; i++)
		{
			Disappear(0.1, elementList[i]);
		}
		
		if(media_id == currentourmedia)
		{
			currentourmedia = 0;
		}
		else
		{
			ajax = new Ajax.Request(
				 '/Volumes/mars_002/Webdata/global/bands/media/mediaitemreview.php',
				 {
					method: 'get',
					parameters: 'media_id=' + media_id + '&band_uid=' + band_uid + '&edit=' + edit + '&only_own=1',
					asynchronous: true,
					evalScripts: true,
					onSuccess: function(t)
					{
						var div = $('reviews_' + media_id);
						div.innerHTML = t.responseText;
						Appear(0.2, 'reviews_' + media_id);
					}
				 }
			);
			currentourmedia = media_id;
		}
	}

	
	function SpikeReview(review_id, media_id, band_uid)
	{
		ajax = new Ajax.Request(
			 '/Volumes/mars_002/Webdata/global/bands/media/spikereview.php',
			 {
				method: 'post',
				parameters: 'review_id=' + review_id,
				asynchronous: true,
				evalScripts: true,
				onSuccess: function(t)
				{
					currentourmedia = 0;
					ShowItemReviews(media_id, band_uid, 1);
				}
			 }
		);
	}
	
	function swapMediaSearchItem(media_uid, band_uid, band_name, media_name, media_description, tag_list, item_location, media_type, bottom_margin, left_margin, width, height, band_gallery_id, band_gallery_position)
	{
		ajax = new Ajax.Updater(
			 'pic_frame',
			 '/Volumes/mars_002/Webdata/global/bands/swap_media_search.php',
			 {
				method: 'get',
				parameters: 'div=1&media_uid=' + media_uid + '&band_uid=' + band_uid + '&item_location=' + item_location + '&media_type=' + media_type + '&bottom_margin=' + bottom_margin + '&left_margin=' + left_margin + '&height=' + height + '&width=' + width + '&band_gallery_id=' + band_gallery_id,
				asynchronous: true,
				evalScripts: true
			 }
		);
		
		ajax1 = new Ajax.Updater(
			 'current_item_info',
			 '/Volumes/mars_002/Webdata/global/bands/swap_media_search.php',
			 {
				method: 'get',
				parameters: 'div=2&media_uid=' + media_uid + '&band_uid=' + band_uid + '&band_name=' + band_name + '&media_name=' + media_name + '&media_description=' + media_description + '&tag_list=' + tag_list + '&band_gallery_position=' + band_gallery_position,
				asynchronous: true,
				evalScripts: true
			 }
		);
		
		ajax2 = new Ajax.Updater(
			 'rating_box',
			 '/Volumes/mars_002/Webdata/global/bands/swap_media_search.php',
			 {
				method: 'get',
				parameters: 'div=3&media_uid=' + media_uid + '&band_uid=' + band_uid,
				asynchronous: true,
				evalScripts: true
			 }
		);
	}
	
	function MediaSearchLoadMedia(media_uid, band_uid, width, height, topmargin, gallery_id)
	{
		var picdiv = $('pic_frame_internal');
		var left_margin = (400 - width) / 2; 
		//picdiv.style.padding-left = left_margin + 'px';
		
		ajax = new Ajax.Updater(
			 'pic_frame_internal',
			 '/Volumes/mars_002/Webdata/global/bands/media/getmedia.php',
			 {
				method: 'get',
				parameters: 'media_uid=' + media_uid + '&band_uid='+ band_uid + '&pwidth='+ width + '&pheight='+ height + '&ptop=0&gallery_id=' + gallery_id,
				asynchronous: true,
				evalScripts: true
			 }
		);
	}
	
 // ]]>
