/* Author: Brandon Showers : Clear Pixel Web Design */
/**
 * @author Alexander Farkas
 * v. 1.21
 */
(function($) {
	if(!document.defaultView || !document.defaultView.getComputedStyle){ // IE6-IE8
		var oldCurCSS = jQuery.curCSS;
		jQuery.curCSS = function(elem, name, force){
			if(name === 'background-position'){
				name = 'backgroundPosition';
			}
			if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
				return oldCurCSS.apply(this, arguments);
			}
			var style = elem.style;
			if ( !force && style && style[ name ] ){
				return style[ name ];
			}
			return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
		};
	}
	
	var oldAnim = $.fn.animate;
	$.fn.animate = function(prop){
		if('background-position' in prop){
			prop.backgroundPosition = prop['background-position'];
			delete prop['background-position'];
		}
		if('backgroundPosition' in prop){
			prop.backgroundPosition = '('+ prop.backgroundPosition;
		}
		return oldAnim.apply(this, arguments);
	};
	
	function toArray(strg){
		strg = strg.replace(/left|top/g,'0px');
		strg = strg.replace(/right|bottom/g,'100%');
		strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
		var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
		return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
	}
	
	$.fx.step. backgroundPosition = function(fx) {
		if (!fx.bgPosReady) {
			var start = $.curCSS(fx.elem,'backgroundPosition');
			
			if(!start){//FF2 no inline-style fallback
				start = '0px 0px';
			}
			
			start = toArray(start);
			
			fx.start = [start[0],start[2]];
			
			var end = toArray(fx.options.curAnim.backgroundPosition);
			fx.end = [end[0],end[2]];
			
			fx.unit = [end[1],end[3]];
			fx.bgPosReady = true;
		}
		//return;
		var nowPosX = [];
		nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
		nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
		fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

	};
})(jQuery);



// create our transition as a plugin
$.fn.crossfade = function () {
	return this.each(function () { 

	// cache the copy of jQuery(this) - the start image
	var $$ = $(this);
	// get the target from the backgroundImage + regexp
	var target = $$.css('backgroundImage').replace(/^url|[()]/g, '');
	// nice long chain: wrap img element in span
	$$.wrap('<span style="position: relative; display:block;"></span>')
	// change selector to parent - i.e. newly created span
	.parent()
	// prepend a new image inside the span
	.prepend('<img src=' + target + '>')
	// change the selector to the newly created image
	.find(':first-child');
	// position the original image
	$$.css({
		// this.offsetTop aligns the image correctly inside the span
//		'top' : $(this).position().top,		//unnecessary for this application
		'position' : 'absolute', 
		'left' : '0px'
	});

	// note: the above CSS change requires different handling for Opera and Safari,
	// see the full plugin for this.
	
	// similar effect as single image technique, except using .animate 
	// which will handle the fading up from the right opacity for us
	$$.hover(function () {
		$$.stop().animate({
			opacity: 0
		}, 300);
	}, function () {
		$$.stop().animate({
			opacity: 1
		}, 300);
		});
	});
};




// Not only when the DOM is ready, but when the images have finished loading,
// important, but subtle difference to $(document).ready();
$(function() {
	$(window).bind('load', function () {
		// run the cross fade plugin against selector
		$('img.fade').crossfade();
	});

	$(window).bind('unload', function () {
		$('img.fade').css('opacity','1');
	});
});

$(document).ready(function(){
	
	
	if ($('.accordion').length > 0) {
		$('.accordion').accordion();
		
		var page_url = document.URL;
		var url_hash = page_url.split("#")[1];

		if (url_hash != undefined) {
			switch (url_hash) {
				case "healthcare":
					$('.accordion').accordion("activate",1);
				case "commercial":
					$('.accordion').accordion("activate",2);
			}
		}
	}
	
	
	
	
	$('.industry-desc .col a').hover(function () {
		$(this).stop().animate({
			paddingLeft : '5px',
			color : '#000'
		}, 300);
	}, function () {
		$(this).stop().animate({
			paddingLeft : '0',
			color : '#666'
		}, 200);
	});




	$('#navigation a:not(.active)').hoverIntent(function () {
		$(this).stop().animate({
			backgroundPosition: '(0 21px)'
		}, 300);
	}, function () {
		$(this).stop().animate({
			backgroundPosition: '(0 27px)'
		}, 300);
	});
	$('.home-gallery-slider .body .inner a').hoverIntent(function () {
		$(this).stop().animate({
			backgroundPosition: '(0 16px)'
		}, 300);
	}, function () {
		$(this).stop().animate({
			backgroundPosition: '(0 20px)'
		}, 200);
	});
	
	
	
	
	
	if ($('.services-gallery').length > 0) {
		Galleria.loadTheme('js/libs/galleria/themes/atg/galleria.atg.js');
		$('.services-gallery #gallery').css('display','');
		$('.services-gallery #gallery').galleria({

/*
			debug:"true"
			data_config: function(img) {
				return {
					title: $(img).attr('alt'),
					description: $(img).attr('alt')
				}
			},
			extend: function(options) {
				this.bindTooltip($('.services-gallery #gallery .galleria-thumbnails-container .galleria-image'),this.title);
			}
*/
		});
	}

});
