Ext.ns('Gals');


Gals.initialize = function(){
	

	
	Ext.get("photoNext").on('click', function() {
		updatePhoto("forward");		
	});
		
	Ext.get("photoCheck").on('click', function() {
		updatePhoto("forward");		
	});
	
	Ext.get("photoPrev").on('click', function() {
		updatePhoto("backward");		
	});
	
	function updatePhoto(direction) {
		
		var aID = Ext.fly("albumIDhit").dom.attributes.value.nodeValue;
		var cMax = Ext.fly("counterhit").dom.attributes.value.nodeValue;
		
		var counter = Number(Ext.fly("photoViewer").dom.attributes.alt.nodeValue);
		
		if(direction == "forward") {
			var next = counter + 1;
			if (next > cMax) {
				next = 1;
			} 
		} else {
			var next = counter - 1;
			if (next == 0) {
				next = cMax;
			} 
		}
		
		var galsite = Ext.fly("galsite").dom.attributes.value.nodeValue;

		
		var pID = Ext.fly("p"+next).dom.attributes.src.nodeValue;
		var cID = Ext.fly("p"+next).dom.attributes.alt.nodeValue;	
		pID = pID.replace("/data-"+galsite+"/gallery_pics/"+aID+"/", "");
		pID = pID.replace(".jpg", "");
		pID = pID.replace("http://"+galsite+".barnet.tv", "");
		
		//alert(aID+" "+cMax+" counter:"+counter+" photo:"+pID+" cID:"+cID+" Next:"+next);
		
		Ext.fly('addthisbox').replaceWith({
			tag: 'span', 
			html: '<!-- AddThis Button BEGIN --><a class="addthis_button" href="http://addthis.com/bookmark.php?v=250&amp;username=barnet" addthis:url="http://'+galsite+'.barnet.tv/data-'+galsite+'/gallery_pics/'+aID+'/'+pID+'.jpg"><img src="http://s7.addthis.com/static/btn/sm-share-en.gif" width="83" height="16" alt="Bookmark and Share" style="border:0"/></a><script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=barnet"></script><!-- AddThis Button END -->',
			id: 'addthisbox'
			});
		
		
		Ext.Ajax.request({
				url: '/gallery?photo_view='+pID
				,params: {
					'photo_check': '1'
				}

			});
		
			if(Ext.fly('linkDel')) {
				Ext.fly('linkDel').replaceWith({
					tag: 'a',
					href: '/gallery?album='+aID+'&delete_photo='+pID,
					html: 'Delete Photo',
					id: 'linkDel'
				});
			}
			
		
			Ext.fly('linkTag').replaceWith({
				tag: 'a',
				href: '/gallery?album='+aID+'&tag_photo='+pID,
				html: 'Bookmark',
				id: 'linkTag'
			});
		
		
		Ext.fly('photoViewer').replaceWith({
				tag: 'img',
				src: '/data-'+galsite+'/gallery_pics/'+aID+'/'+pID+'.jpg',
				id: 'photoViewer',
				alt: cID
			});
			
		
		
	};
	
	
	
	
	Ext.select('.hitPhoto', true).each(function(element) {
		
		element = Ext.get(element.dom);
		
		element.on('click', function() {
			
			var aID = Ext.fly("albumIDhit").dom.attributes.value.nodeValue;
			
			var pID = this.dom.attributes.src.nodeValue;
			var cID = this.dom.attributes.alt.nodeValue;
			
			var galsite = Ext.fly("galsite").dom.attributes.value.nodeValue;

			pID = pID.replace("/data-"+galsite+"/gallery_pics/"+aID+"/", "");
			pID = pID.replace(".jpg", "");
			pID = pID.replace("http://"+galsite+".barnet.tv", "");
			
			
			Ext.Ajax.request({
				url: '/gallery?photo_view='+pID
				,params: {
					'photo_check': '1'
				}

			});
		
			if(Ext.fly('linkDel')) {
				Ext.fly('linkDel').replaceWith({
					tag: 'a',
					href: '/gallery?album='+aID+'&delete_photo='+pID,
					html: 'Delete Photo',
					id: 'linkDel'
				});
			}
		
			Ext.fly('linkTag').replaceWith({
				tag: 'a',
				href: '/gallery?album='+aID+'&tag_photo='+pID,
				html: 'Bookmark',
				id: 'linkTag'
			});
			
			
			Ext.fly('photoViewer').replaceWith({
				tag: 'img',
				src: '/data-'+galsite+'/gallery_pics/'+aID+'/'+pID+'.jpg',
				id: 'photoViewer',
				alt: cID
			});
			
			
		});
	});



};
Ext.onReady(Gals.initialize);
