$(document).ready(function() {
	var portfolioSorter = jQuery('.screen_images'); // selects the portfolio container

    //$('.screen_images').image_preloader({delay:100});

	//on sites without portfolio activate basic image preloading

    //$('a.hovertip').tipsy({fade: true, title: 'title'});



    // == Portfolio Overlay == //
    my_lightbox("a[rel^='prettyPhoto'], a[rel^='lightbox']",true);
})

jQuery.fn.anchorAnimate = function(settings) {

 	settings = jQuery.extend({
		speed : 1100
	}, settings);

	return this.each(function(){
		var caller = this
		$(caller).click(function (event) {
			event.preventDefault()
			var locationHref = window.location.href
			var elementClick = $(caller).attr("href")

			var destination = $(elementClick).offset().top;
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
				window.location.hash = elementClick
			});
		  	return false;
		})
	})
}

function my_lightbox($elements, autolink){

	if(autolink)
	{
		jQuery('a[href$=jpg], a[href$=png], a[href$=gif], a[href$=jpeg], a[href$=.mov] , a[href$=.swf] , a[href*=vimeo.com] , a[href*=youtube.com]').contents("img").parent().each(function()
		{
			if(!jQuery(this).attr('rel') != undefined && !jQuery(this).attr('rel') != '' && !jQuery(this).hasClass('noLightbox'))
			{
				jQuery(this).attr('rel','prettyPhoto[auto_group]')
				jQuery(this).addClass('lightboxlink')
			}
		});
	}

	jQuery($elements).each(function()
	{
		var $image = jQuery(this).contents("img");
		$newclass = 'lightbox_video';

		if(jQuery(this).attr('href').match(/(jpg|gif|jpeg|png|tif)/)) $newclass = 'lightbox_image';

		if ($image.length > 0)
		{

			var $bg = jQuery("<span class='"+$newclass+" '></span>").appendTo(jQuery(this));

			jQuery(this).bind('mouseenter', function()
			{
				$height = $image.height();
				$width = $image.width();
				if(jQuery.browser.msie &&  jQuery.browser.version == 7){
				    $height = $height + 4;
				    $width = $width + 4;
				}
				$pos =  $image.position();
				$bg.css({height:$height, width:$width, top:$pos.top, left:$pos.left});
			});
		}
	});

	jQuery($elements).contents("img").hover(function()
	{
		jQuery(this).stop().animate({opacity:0.5},400);
	},
	function()
	{
		jQuery(this).stop().animate({opacity:1},400);
	});
}




(function($)
{
	$.fn.image_preloader = function(options)
	{
		var defaults =
		{
			repeatedCheck: 500,
			fadeInSpeed: 1000,
			delay:600,
			callback: ''
		};

		var options = $.extend(defaults, options);

		return this.each(function()
		{
			var imageContainer = jQuery(this),
				images = imageContainer.find('img').css({opacity:0, visibility:'hidden'}),
				imagesToLoad = images.length;

				imageContainer.operations =
				{
					preload: function()
					{
						var stopPreloading = true;

						images.each(function(i, event)
						{
							var image = $(this);

							if(event.complete == true)
							{
								if($.browser.opera) imagesToLoad --;
								imageContainer.operations.showImage(image);
							}
							else
							{
								if($.browser.opera) imagesToLoad --;
								image.bind('error load',{currentImage: image}, imageContainer.operations.showImage);
							}

						});

						return this;
					},

					showImage: function(image)
					{
						if(!$.browser.opera) imagesToLoad --;
						if(image.data.currentImage != undefined) { image = image.data.currentImage;}

						if (options.delay <= 0) image.css('visibility','visible').animate({opacity:1}, options.fadeInSpeed);

						if(imagesToLoad == 0)
						{
							if(options.delay > 0)
							{
								images.each(function(i, event)
								{
									var image = $(this);
									setTimeout(function()
									{
										image.css('visibility','visible').animate({opacity:1}, options.fadeInSpeed, function()
										{
											$(this).parent().removeClass('preloading');
										});
									},
									options.delay*(i+1));
								});

								if(options.callback != '')
								{
									setTimeout(options.callback, options.delay*images.length);
								}
							}
							else if(options.callback != '')
							{
								(options.callback)();
							}

						}

					}

				};

				imageContainer.operations.preload();
		});

	}
})(jQuery);

jQuery.easing['jswing'] = jQuery.easing['swing'];


