//Sky gallery v1.01
var gallery = {

	write:function(){

		$('.gallery-enable').each(function(i){

			gallery.parent = $(this);

			//setup options
			var xmlpath = '';
			var decache = '?v=2';
			var showratings = 'no';
			var ratingsxml = '';
			var timer = 6000;

			gallery.interval = timer;

			if($(this).attr('options')){

				var options = $(this).attr('options');

				options = options.split("&");

				$(options).each(function(){

					var opts = this.split('=');
					if(opts[0]=='xmlpath'){xmlpath=opts[1]}
					if(opts[0]=='decache'){decache=opts[1]}
					if(opts[0]=='showratings'){showratings=opts[1]}
					if(opts[0]=='ratingsxml'){ratingsxml=opts[1]}
					if(opts[0]=='timer'){gallery.interval=parseInt(opts[1])*1000}

				});
			}


			//write the html in//
			gallery.parent.html('<div class="base-gallery-mainnav backone"><em> </em></div><div href="#" class="base-gallery-mainnav forwardone"><em> </em></div><div class="base-gallery-thumbs"><div href="#" class="back"><em></em></div><span><ul></ul></span><div href="#" class="forward"><em></em></div></div><div class="base-gallery-pic"></div><div class="base-gallery-panel"><div class="base-gallery-panel-inner"></div><div class="base-gallery-panel-outer"><h2></h2><p></p></div><div class="base-gallery-control"> <div class="base-gallery-timer"><span></span></div><span class="base-gallery-timercount"></span><div class="base-gallery-count"></div><div class="base-gallery-viewall"></div></div></div><div class=base-gallery-overview>arse</div><div class="base-gallery-panel-toggle">HIDE CAPTIONS</div>');

			//get XML
			$.ajax({
				type: "GET",
				async: true,
				url: xmlpath,
				success: function(xml){

					//get gallery length
					gallery.items = $(xml).find('ARTICLE');
					gallery.forlength = Math.floor(gallery.items.length/10);
					gallery.galltitle = $(xml).find("root").attr('galTitle');


					//write in initial data
					gallery.parent.find('.base-gallery-pic').html('<div class="ajax-loader"><img src="/images/ajax-loader.gif" style=display:none;></div><span></span><img src="'+$(xml).find('ARTICLE:first').attr('pic')+'" alt="'+$(xml).find('ARTICLE:first').find('HLHEAD').text()+'" />');
					gallery.parent.find('.base-gallery-panel-outer h2').html($(xml).find('ARTICLE:first').find('HLHEAD').text())
					gallery.parent.find('.base-gallery-panel-outer p').html($(xml).find('ARTICLE:first').find('LLSNIPPET').text());

					//add ratings//
					if(showratings!='no'){
						gallery.parent.find('.base-gallery-panel-outer').append(gallery.ratings({id:$(xml).find('ARTICLE:first').attr('picID'),value:'0',message:'Rate this',enable:'rating-enable'}));

						gallery.parent.find('.base-gallery-panel-outer').append(gallery.ratings({value:$(xml).find('ARTICLE:first').attr('rating'),message:'Av. rating'}))

						base.rating({ratingsxml:ratingsxml,picID:$(xml).find('ARTICLE:first').attr('picID')});
					}

					gallery.parent.find('.base-gallery-count').html('1 of '+gallery.items.length)


					//write in thumbnails
					gallery.items.each(function(i){

						gallery.parent.find('.base-gallery-thumbs ul').append('<li><a href="#"><img src="'+$(this).attr('picThumb')+'" headline="'+$(this).find('HLHEAD').text()+'" alt="'+$(this).find('LLSNIPPET').text()+'" bigpic="'+$(this).attr('pic')+'" rating="'+$(this).attr('rating')+'" voteid="'+$(this).attr('picID')+'" /></a></li>');

					})

					//highlight first thumbnail
					gallery.parent.find('.base-gallery-thumbs li:first').addClass('on');

					//bind actions to thumbnails navigation
					gallery.parent.find('.base-gallery-thumbs .back em,.base-gallery-thumbs .forward em').click(function(){

							gallery.parent.find('.base-gallery-thumbs .back em,.base-gallery-thumbs .forward em').hide();

							//check if we're going back or forward and do all the fiddly stuff
							var currentx = parseInt(gallery.parent.find('.base-gallery-thumbs ul').css('marginLeft'));


							if($(this).parent().hasClass('back')==true){currentx = currentx+760;}
							else{currentx = currentx-760;}

							gallery.thumbnailnav({ulx:currentx});

							return false;

					})


					//bind actions to thumbnails
					gallery.parent.find('.base-gallery-thumbs li').click(function(){

						gallery.parent.find('.base-gallery-pic .ajax-loader img').show();

						gallery.parent.find('.base-gallery-thumbs li').removeClass('on');
						gallery.parent.find('.base-gallery-overview li').removeClass('on');

						$(this).addClass('on');


						var index = gallery.parent.find('.base-gallery-thumbs li').index($(this));
						var setulx = Math.floor((index)/10)*-760;

						gallery.parent.find('.base-gallery-overview li:eq('+index+')').addClass('on');

						gallery.thumbnailnav({ulx:setulx});

						//get full size image path from thumbnail
						var impath = $(this).find('img').attr('bigpic');
						var title = $(this).find('img').attr('alt');
						var headline = $(this).find('img').attr('headline');
						var ratings = parseInt($(this).find('img').attr('rating'));
						var voteid = $(this).find('img').attr('voteid');

						//load new image into main container//
						gallery.parent.find('.base-gallery-pic span').image(impath,function(){

							//write in text
							gallery.parent.find('.base-gallery-panel-outer h2').html(headline);
							gallery.parent.find('.base-gallery-panel-outer p').html(title);

							//do ratings

							if(showratings!='no'){
								gallery.parent.find('.base-rating').remove();
								gallery.parent.find('.base-gallery-panel-outer').append(gallery.ratings({id:voteid,value:'0',message:'Rate this',enable:'rating-enable'}));

								gallery.parent.find('.base-gallery-panel-outer').append(gallery.ratings({value:ratings,message:'Av. rating'}))
								base.rating({ratingsxml:ratingsxml,picID:voteid});
							}


							gallery.parent.find('.base-gallery-pic span').animate({opacity:'show'},{duration:600,complete:function(){

								gallery.parent.find('.base-gallery-pic').html("<div class='ajax-loader'><img src='/images/ajax-loader.gif'></div><img src='"+impath+"' /><span></span>");

								gallery.parent.find('.base-gallery-pic .ajax-loader img').hide();
								gallery.parent.find('.base-gallery-mainnav em').show();


								gallery.parent.find('.base-gallery-count').html((index+1)+' of '+gallery.items.length);
								window.location.hash="photo="+index;

								//call tracking
								gallery.tracking();

							}});


						})


						return false;

					})


					//bind actions to main nav
					gallery.parent.find('.base-gallery-mainnav em').click(function(click,x){

						if(x!='x'){gallery.timer.stop()}

						$(this).hide();

						var index = gallery.parent.find('.base-gallery-thumbs li').index(gallery.parent.find('.base-gallery-thumbs li.on'))

						if($(this).parent().hasClass('forwardone')==true){index=index+1;}
						else{index=index-1}

						if(index<0){index=gallery.items.length-1;}
						if(index>=gallery.items.length){index=0}

						gallery.parent.find('.base-gallery-thumbs li:eq('+index+')').trigger('click');


						return false;

					})


					//bind actions to toggle panel button
					gallery.parent.find('.base-gallery-panel-toggle').toggle(function(){

						gallery.parent.find('.base-gallery-panel').animate({'marginTop':0},{queue:false})
						gallery.parent.find('.base-gallery-control').animate({'marginTop':-65},{queue:false})
						$(this).html("SHOW CAPTIONS");



					},function(){

						gallery.parent.find('.base-gallery-panel').animate({'marginTop':gallery.parent.find('.base-gallery-panel').height()*-1},{queue:false})
						gallery.parent.find('.base-gallery-control').animate({'marginTop':0},{queue:false})
						$(this).html("HIDE CAPTIONS");


					})

					//bind actions to thumbnail overview panel
					gallery.parent.find('.base-gallery-viewall').tooltip({
						track: true,
						delay: 0,
						showURL: false,
						fade: 250,
						bodyHandler: function() {
							return "View all thumbnails";
						}

					});

					gallery.parent.find('.base-gallery-viewall').click(function(){

						var t = gallery.parent.find('.base-gallery-thumbs ul').clone();

						gallery.parent.find('.base-gallery-overview').not('.full').append("<h3>"+gallery.galltitle+" ("+gallery.items.length+" pictures)</h3>");
						gallery.parent.find('.base-gallery-overview').not('.full').append(t).addClass('full');

						gallery.parent.find('.base-gallery-overview').animate({top:0},500);

						gallery.parent.find('.base-gallery-overview').click(function(){

							gallery.parent.find('.base-gallery-overview').animate({top:-660});
							gallery.parent.find('.base-gallery-overview li').removeClass('on');
						})


						gallery.parent.find('.base-gallery-overview li').click(function(){

							$(this).removeClass('on');

							var i = gallery.parent.find('.base-gallery-overview li').index($(this));

							gallery.parent.find('.base-gallery-thumbs li:eq('+i+')').trigger('click');
							gallery.parent.find('.base-gallery-overview').animate({top:-660});

							return false;

						})


					})

					//bind actions to timer button

					//tooltip
					gallery.parent.find('.base-gallery-timer').tooltip({
						track: true,
						delay: 0,
						showURL: false,
						fade: 250,
						bodyHandler: function() {
							return "Start slideshow";
						}

					});

					gallery.parent.find('.base-gallery-timer').click(function(){

						gallery.timer.set({el:$(this)})

					})


					//deeplinking
					if(window.location.hash.match('photo')){

						var photoNum = document.location.hash;
						photoNum = photoNum.split('photo=')[1];
						photoNum = parseInt(photoNum);

						gallery.parent.find('.base-gallery-thumbs li:eq('+photoNum+')').trigger('click');

					}

				}

			})



		})

	},

	thumbnailnav:function(p){

		/*controls the posiotion of thumbnail bar and whether to show the back and forward arrows*/
		gallery.parent.find('.base-gallery-thumbs ul').animate({marginLeft:p.ulx},{duration:500,queue:false,complete:function(){

			var currentx = parseInt(gallery.parent.find('.base-gallery-thumbs ul').css('margin-left'));

			if(currentx < 0 ){gallery.parent.find('.base-gallery-thumbs .back em').show();}
			if(currentx == 0 ){gallery.parent.find('.base-gallery-thumbs .back em').hide();}

			if(currentx<((gallery.forlength*-760)+760)){gallery.parent.find('.base-gallery-thumbs .forward em').hide()}
			if(currentx>(gallery.forlength*-760)){gallery.parent.find('.base-gallery-thumbs .forward em').show()}

		}});


	},

	timer:{

		set:function(p){

			if(p.el.hasClass('timer-on')==false){

				p.el.addClass('timer-on');
				gallery.timer.interval();
				gallery.parent.find('.base-gallery-timer').tooltip({
						track: true,
						delay: 0,
						showURL: false,
						fade: 250,
						bodyHandler: function() {
							return "Stop slideshow";
						}

				});

			}

			else{
				gallery.timer.stop();
				gallery.parent.find('.base-gallery-timer').tooltip({
						track: true,
						delay: 0,
						showURL: false,
						fade: 250,
						bodyHandler: function() {
							return "Start slideshow";
						}

				});
			}

		},


	interval:function(){

		var interval = gallery.interval/4;
		gallery.parent.find('.base-gallery-timercount').animate({lineHeight:0},interval,function(){

				$(this).attr('id','timer-on1');
				$(this).animate({lineHeight:0},interval,function(){

					$(this).attr('id','timer-on2');
					$(this).animate({lineHeight:0},interval,function(){

						$(this).attr('id','timer-on3');
						$(this).animate({lineHeight:0},interval,function(){

							$(this).attr('id','timer-on4');
							$(this).animate({lineHeight:0},interval,function(){

								$(this).attr('id','');
								gallery.parent.find('.base-gallery-mainnav:eq(1) em').trigger('click','x');
								gallery.timer.interval();

							})

						})

					})

				})

			})
		},

		stop:function(){
			gallery.parent.find('.base-gallery-timercount').stop().attr('id','');
			gallery.parent.find('.base-gallery-timer').removeClass('timer-on');



		}

	},

	ratings:function(p){

		var html =  "<div class='base-rating "+p.enable+"' id='"+p.id+"'><div class='base-rating-disable'></div><h5>"+p.message+"</h5><ul><li class='current-rating' style='width:"+(parseInt(p.value)*20)+"%;zoom:1;clear:both;position:relative'></li><li><a href='#' title='1 star out of 5' class='one-star'>1</a></li><li><a href='#' title='2 stars out of 5' class='two-stars'>2</a></li><li><a href='#' title='3 stars out of 5' class='three-stars'>3</a></li><li><a href='#' title='4 stars out of 5' class='four-stars'>4</a></li><li><a href='#' title='5 stars out of 5' class='five-stars'>5</a></li></ul></div>"

		return html;


	},

	tracking:function(p){

		if(window.refreshAds){
			refreshAds();
		}


	}
}
gallery.write();
