jQuery.fn.center = function () {
	var popup = this;
	if ($.browser.msie){
		popup.css('left','0px');
		popup.css('top','0px');
	}
	var hght = $(window).height() - popup.find('.lightbox-container').outerHeight() - 100;
		popup.find('.lightbox-container').css("margin-left", "0");
		popup.find('.lightbox-container').css("margin-top", "0");
		popup.css({ position: "fixed", visibility: "hidden", display: "block" });
		popup.find('.lightbox-container').css("left", ( $('body').width() - popup.find('.lightbox-container').outerWidth() ) / 2 + "px");
		popup.find('.lightbox-container').css("top", Math.max( ( $(window).height() - popup.find('.lightbox-container').outerHeight() - 30 ), 0 ) / 2 + "px");
		
		popup.css({ position: "", visibility: "", display: "" });
	
    return this;
}

$.snippet = function(template, data) { 
	var i;
   
   for (i in data) {
		template = template.split('%7B' + i + '%7D').join(data[i]);
   	template = template.split('{' + i + '}').join(data[i]);
   }

   return template;
}

$.widget('ui.miniLightboxF', {

	_init: function(){
		var obj = this;
		var parent = this.element;
		var infoId = this.element.attr('rel');
		var collection = this.element.attr('title');
		var circle_html = $('<a class="circle"></a>');

		$('#gallery-lightbox .thumbnails').append('<div class="list"></div>');

		obj.prevWidth = '100px';
		obj.prevHeight = '100px';
		obj.page_count = Math.ceil( $('.mini-lightbox-button-f[title="'+collection+'"]').length / 6 );

		for( var i=0;i<obj.page_count; i++ ){
			circle_html.clone().attr('rel', i).appendTo( $('#gallery-lightbox .pagination') );
		}

		$('.mini-lightbox-button-f[title="'+collection+'"]').clone().appendTo('#gallery-lightbox .thumbnails .list');

		$('#gallery-lightbox .thumbnails .list .mini-lightbox-button-f').click(function(){
			obj.change( $(this).attr('rel') );
		});

		$('#gallery-lightbox .thumbnails .list .mini-lightbox-button-f img').mouseenter(function(){
			$(this).stop(true,false).fadeTo('fast', 1);
		});
		$('#gallery-lightbox .thumbnails .list .mini-lightbox-button-f img').mouseleave(function(){
			$(this).stop(true,false).fadeTo('fast', 0.4);
		});
		
		$('#gallery-lightbox .image-title').html('');
		obj.change(infoId);

		$('#gallery-lightbox .pagination .circle:first').addClass('active');

		$('#gallery-lightbox .pagination .circle').click(function(){
			if( !$(this).hasClass('active') ){
				//$('#gallery-lightbox .pagination .circle.active').removeClass('active');
				//$(this).addClass('active');
				/*var margin = -618 * parseInt( $(this).attr('rel') );
				$('#gallery-lightbox .thumbnails .list').animate({
					marginLeft: margin+'px'
				}, 'fast', 'linear');*/

				var thumb_index = $(this).attr('rel')*6;
				$('#gallery-lightbox .thumbnails .list .mini-lightbox-button-f:eq('+thumb_index+')').click();
			}
		});

		$('#gallery-lightbox .lightbox-container .close, #gallery-lightbox .overlay').unbind().bind('click',function(){
			obj.hide();
		});
		
		$('#gallery-lightbox .lightbox-container').mouseenter(function(){
			$(this).children('.prev,.next').show();
		});
		$('#gallery-lightbox .lightbox-container').mouseleave(function(){
			$(this).children('.prev,.next').hide();
		});
		
	},
	
	change: function(infoId){
		var obj = this;
		var infoElement = $('.image-info'+infoId);
		var imgPath = infoElement.find('.image-path').html();
		//imgPath = imgPath.replace(' ','%20');
		var imgTitle = infoElement.find('.image-title').html();
		var imgDesc = infoElement.find('.image-description').html();
		
		var prev_index = $('#gallery-lightbox .thumbnails .list .mini-lightbox-button-f.clicked').index();

		$('#gallery-lightbox .thumbnails .list .clicked').removeClass('clicked');
		$('#gallery-lightbox .thumbnails .list .mini-lightbox-button-f[rel="'+infoId+'"]').addClass('clicked');
		var current_index = $('#gallery-lightbox .thumbnails .list .mini-lightbox-button-f[rel="'+infoId+'"]').index();

		var margin = -618 * Math.floor(current_index/6);
		$('#gallery-lightbox .thumbnails .list').animate({
			marginLeft: margin+'px'
		}, 'fast', 'linear');
		$('#gallery-lightbox .pagination .circle.active').removeClass('active');
		$('#gallery-lightbox .pagination .circle:eq('+Math.floor(current_index/6)+')').addClass('active');
		
		$('#gallery-lightbox .preloader').show();
		$('#gallery-lightbox .image-container').children('img').replaceWith('<img src="'+imgPath+'" />');
		
		//$('#gallery-lightbox .image-container').children('img').css({ width: obj.prevWidth, height: obj.prevHeight });

		//$('#gallery-lightbox').center();
		$('#gallery-lightbox').show();
		
		$('#gallery-lightbox .image-container').children('img').load(function(){
			var currentWidth = $(this).width();
			var currentHeight = $(this).height();
			
			//$(this).css({ width: 'auto', height: 'auto' });
			var maxWidth = Math.min( $(window).width()-200, $(this).width() );
			var maxHeight = Math.min( $(window).height()-200, $(this).height() );
			if( maxWidth < maxHeight ){
				//$(this).css({ width: maxWidth+'px' });
				if( maxHeight < $(this).height() ){
					var height = $(this).height();
					//$(this).css({ height: height+'px' });
					var maxWidth = parseInt( $(this).width() );
				}else{
					var maxHeight = parseInt( $(this).height() );
				}
			}else{
				//$(this).css({ height: maxHeight+'px' });
				if( maxWidth < $(this).width() ){
					var width = $(this).width();
					//$(this).css({ width: width+'px' });
					var maxHeight = parseInt( $(this).heigth() );
				}else{
					var maxWidth = parseInt( $(this).width() );
				}
			}
			
			//$('#gallery-lightbox .image-container').children('img').css({ width: obj.prevWidth, height: obj.prevHeight });
			
			var leftDiff = ( maxWidth - parseInt( obj.prevWidth ) ) / 2;
			var topDiff = ( maxHeight - parseInt( obj.prevHeight ) ) / 2;
			var left = parseInt( $('#gallery-lightbox .lightbox-container').css('left') ) - leftDiff;
			var top = parseInt( $('#gallery-lightbox .lightbox-container').css('top') ) - topDiff;
			
			obj.prevWidth = maxWidth;
			obj.prevHeight = maxHeight;
			
			/*$('#gallery-lightbox .lightbox-container').animate({
				left: left+'px',
				top: top+'px'
			}, 500);
			
			$(this).animate({
				width: maxWidth+'px',
				height: maxHeight+'px'
			},500,function(){
				$('#gallery-lightbox .image-title').html(imgTitle);
				$('#gallery-lightbox .preloader').hide();
			});*/
			
			$('#gallery-lightbox .image-title').html(imgTitle);
			if( $.browser.msie ){
				$('#gallery-lightbox .preloader').hide();
			}else{
				$('#gallery-lightbox .preloader').fadeOut();
			}
			
			$(this).unbind().bind('click',function(){
				if( $('#gallery-lightbox .thumbnails .list .clicked').next('.mini-lightbox-button-f').length > 0 ){
					obj.change( $('#gallery-lightbox .thumbnails .list .clicked').next('.mini-lightbox-button-f').attr('rel') );
				}else{
					obj.change( $('#gallery-lightbox .thumbnails .list .mini-lightbox-button-f:first').attr('rel') );
					$('#gallery-lightbox .thumbnails .list').animate({
						marginLeft: '0px'
					}, 'fast', 'linear');
					$('#gallery-lightbox .pagination .circle.active').removeClass('active');
					$('#gallery-lightbox .pagination .circle:first').addClass('active');
				}
			});
			$('#gallery-lightbox .next').unbind().bind('click',function(){
				if( $('#gallery-lightbox .thumbnails .list .clicked').next('.mini-lightbox-button-f').length > 0 ){
					obj.change( $('#gallery-lightbox .thumbnails .list .clicked').next('.mini-lightbox-button-f').attr('rel') );
				}else{
					obj.change( $('#gallery-lightbox .thumbnails .list .mini-lightbox-button-f:first').attr('rel') );
					$('#gallery-lightbox .thumbnails .list').animate({
						marginLeft: '0px'
					}, 'fast', 'linear');
					$('#gallery-lightbox .pagination .circle.active').removeClass('active');
					$('#gallery-lightbox .pagination .circle:first').addClass('active');
				}
			});
			$('#gallery-lightbox .prev').unbind().bind('click',function(){
				if( $('#gallery-lightbox .thumbnails .list .clicked').prev('.mini-lightbox-button-f').length > 0 ){
					obj.change( $('#gallery-lightbox .thumbnails .list .clicked').prev('.mini-lightbox-button-f').attr('rel') );
				}else{
					obj.change( $('#gallery-lightbox .thumbnails .list .mini-lightbox-button-f:last').attr('rel') );
					var margin = -618 * (obj.page_count-1);
					$('#gallery-lightbox .thumbnails .list').animate({
						marginLeft: margin+'px'
					}, 'fast', 'linear');
					$('#gallery-lightbox .pagination .circle.active').removeClass('active');
					$('#gallery-lightbox .pagination .circle:last').addClass('active');
				}
			});
			
		});
	},

	hide: function(){
		var obj = this;
		$('#gallery-lightbox .thumbnails .list').remove();
		$('#gallery-lightbox .pagination').empty();
		$('#gallery-lightbox').removeClass('centered');
		$('#gallery-lightbox').children('.close').hide();
		$('#gallery-lightbox .lightbox-container').animate({ opacity: '0' }, 500);
		$('#gallery-lightbox .overlay').animate({ opacity: '0' }, 500, function(){
			$('#gallery-lightbox').hide();
			//$('#gallery-lightbox .image-container img').css({ width: 'auto', height: 'auto' });
			//$('#gallery-lightbox .lightbox-container').css({ width: 'auto', height: 'auto' });
			$('#gallery-lightbox .lightbox-container .prev, #gallery-lightbox .lightbox-container .next').hide();
			
			$('#gallery-lightbox .overlay').css({ opacity: '0.5' });
			$('#gallery-lightbox .lightbox-container').css({ opacity: '1' });
			
			obj.destroy();
		});
	},

	destroy: function() {
       $.Widget.prototype.destroy.apply(this, arguments); // default destroy
        // now do other stuff particular to this widget
   }

});

$(document).ready(function(){
	$('a.skypejslink').each(function(){
		$(this).attr('href', $(this).attr('rel') );
	});
	
	$('.view-more-images-link a').click( function() {
		$('.full-gallery-first').trigger('click');
	});
	
	/*$('.header .navigation li').mouseenter(function(){
		$(this).addClass('over');
		if( $(this).children('div').length > 0 ){
			var block_class = $(this).children('a').attr('rel');
			var me = $(this);

			setTimeout(function(){
				if( me.hasClass('over') ){
					$('.'+block_class).stop(true,true).slideDown('fast', 'linear');
				}
			}, 150);
		}
	});
	$('.header .navigation li').mouseleave(function(){
		$(this).removeClass('over');
		if( $(this).children('div').length > 0 ){
			var block_class = $(this).children('a').attr('rel');
			//$('.'+block_class).stop(true,true).slideUp('fast', 'linear');
			$('.'+block_class).hide();
		}
	});*/

	$('.mini-lightbox-button-f').click(function(){
		$(this).miniLightboxF();
		return false;
	});

	$('.gallery-img .zoom, .gallery-info a').click(function(){
		$(this).closest('.gallery-block').find('.mini-lightbox-button-f:first').trigger('click');
	});

	$('.area-block').slice(4).hide();
	$('.more-results').click(function(){
		var blocks_remaining = ( $('.area-block:hidden').length ) * (-1);
		var blocks_to_ignore = blocks_remaining + 4;

		if( blocks_remaining/-1 > 4){
			$('.area-block:hidden').slice(blocks_remaining, blocks_to_ignore).fadeIn('slow');
		}else{
			$(this).hide();
			$('.area-block:hidden').fadeIn('slow');
		}

		return false;
	});


	/* ROTATOR */
	var images = $('.container .slide img');

	for( var i=1; i <= images.length; i++ ){
		$('.container .slide .controls').append('<a href="javascript:;">'+i+'</a>');
	}

	var buttons = $('.container .slide .controls a');
	var counter = 0;


	$(images).hide();
	$(images[0]).show();
	$(buttons[0]).addClass('on');
	
	var refreshIntervalId = setInterval(function() {
		$(images[counter]).fadeOut('slow');
		
		counter++;
		if (counter == images.length) {
			counter = 0;
		}
		$(images[counter]).fadeIn(1000);
		
		$('.container .slide .controls a.on').removeClass('on');
		$(buttons[counter]).addClass('on');
	}, 5000);
	
	$('.container .slide .controls a').click(function(){
		clearInterval(refreshIntervalId);
		var num = $(".container .slide .controls a").index(this);
		var images = $('.container .slide img');
		$(images).fadeOut('slow');
		$(images[num]).fadeIn();
		
		$('.container .slide .controls a.on').removeClass('on');
		$(this).addClass('on');
		return false;
	});

	$('.image-controls .full-gallery-button').click(function(){
		$(this).closest('.image-container').find('.mini-lightbox-button-f:first').trigger('click');

		return false;
	});

	$('.in_left .menu li').each(function(){
		var active_elements = $(this).find('a.active').length;
		
		if(active_elements === 0){
			$(this).removeClass('expanded').addClass('contracted');
		}
	});

	$('.our-destination').click(function(){
		document.location = $(this).find('.find-more').attr('href');
	});

});
