document.write('<style type="text/css">#content{display:none;}</style>');
jQuery(document).ready(function() {
	$=jQuery;	

	//cleanup non-js elements
	$('#content').hide();
	
	$(document).wpGallerySlideshow({ajaxLoaderImg:portfolio.ajax_loader_img});

	//fade in the main content
	$('body.home #content').fadeIn(8000);
	$('#content').fadeIn(2000);
		
	/* Overlayed contact form */	
	var $contactForm=$('#contact-form').hide();
	$contactForm.before('<div id="tint"></div>');
			
	$('a#contact-link,li.page-item-79 a, #close-link').click(function (){		
		//Close the form if opened...
		if($contactForm.data('opened')===true){
			$contactForm.fadeOut(1000,function(){	
				$('#tint').hide();
				$contactForm.data('opened',false);
			});
			return false;
		}
		
		//...otherwise tint the background and show the form
		$('#tint').css({'width':'100%','height':'100%','opacity':'0.7'}).show(function(){
			$contactForm.fadeIn(1000,function(){
				$contactForm.data('opened',true);
				//hide any previous response from wpcf7
				$('div.wpcf7-response-output').hide();
			});	
		});			
		return false;
	});
	/*END contact form*/

	/* Floating tool tips */ 
    $('.menu-parent-id-4 a,#portfolio-add-page,#generator-link a').mouseover(function(e) {        
		//Grab the title attribute's value and assign it to a variable  
   		var tip = $(this).attr('title');      
        tipWidth = 120;
        //Remove the title attribute's to avoid the native tooltip from the browser  
        $(this).attr('title','').find('img').attr('title','');  
          
        //Append the tooltip template and its value  
        $('body').append('<div id="tooltip"><div class="tipBody">' + tip + '</div></div>');       
          
        //Set the X and Y axis of the tooltip  
        
        $('#tooltip').css('left', e.pageX - (tipWidth/2) );
        $('#tooltip').css('top', e.pageY + 12 );
        if ($('#tooltip').offset().left<3) offsetX = 0;           
    }).mousemove(function(e) {  
        //Keep changing the X and Y axis for the tooltip, thus, the tooltip move along with the mouse  
        $('#tooltip').css('top', e.pageY +12);  
        $('#tooltip').css('left', e.pageX - (tipWidth/2)); 
        if ($('#tooltip').offset().left<3) $('#tooltip').css('left', e.pageX - 0); ;  
    }).mouseout(function() {  
        //Put back the title attribute's value  
        $(this).attr('title',$('.tipBody').html());  
      
        //Remove the appended tooltip template  
        $('div#tooltip').remove();   
    });  
    /*END Tooltips*/
});

(function($){
	$.fn.wpGallerySlideshow = function(options) {
		counter = 0;	
		options = $.extend({
			maxWidth: 640,
			maxHeight: 440,
			galleryElement: '.gallery',
			slideshowElement: 'slideshow',
			currentImageElement: '#current-image',
			ajaxLoaderImg: 'loader.gif',
			fadeTime: 2000,
			holdTime: 10000
		}, options);
			
		//add the load area for the gallery	
		$gallery=$(options.galleryElement).hide();
		$gallery.hide().after('<div id="slideshow"><img id="current-image" src="'+options.ajaxLoaderImg+'" /></div>');
		
		//Get current image element
		$currentImage=$(options.currentImageElement);

		
		function nextImage(){
			$currentImage.fadeOut(options.fadeTime, function(){
				
				//hide the existing image
				$currentImage.hide();
				
				//get new image src
				var src = $items.eq(counter).attr('href');
				
				// Image preload process from jLightbox
				var objImagePreloader = new Image();
				objImagePreloader.onload = function() {
					$currentImage.attr('src',src);
					var imageWidth = objImagePreloader.width;
					var imageHeight = objImagePreloader.height;
					var wScale = imageWidth / options.maxWidth;
					var hScale = imageHeight / options.maxHeight;
					var ratio = imageWidth/imageHeight;
					
					//clear onLoad, IE behaves irratically with animated gifs otherwise
					objImagePreloader.onload=function(){};
					
					//reset the image size to automatic
					$currentImage.width('auto').height('auto');
						
					if (wScale>hScale){
							$currentImage.width(imageWidth*(1/wScale));
							$currentImage.height($currentImage.width()/ratio);
					}			
					else if (hScale>wScale){
						$currentImage.height(imageHeight*(1/hScale));
						$currentImage.width($currentImage.height()/(1/ratio));
					}
				
				};
				
				objImagePreloader.src = src;
				
				//fade in the image
				$currentImage.fadeIn(options.fadeTime);
				
				//next image
				if(1<$items.length){
					counter=(counter+1)%$items.length;
					timer=setTimeout(nextImage,options.holdTime);
				}
			});		
		};

		//preload the images and show the first on once thats done
		$items=$gallery.find('.gallery-icon a').preload({
			onFinish: function(){nextImage();}
		});
		
	}
})(jQuery);

