/*---[ Details ]---------------------------------------
Global Javascript Scripts
Author: Lee Powell
Contact: lee.powell@soup.co.uk
-------------------------------------------------------*/

(function($){

/* jQuery Pre-Document Load
-------------------------------------------------------*/
// Add class to html element for styling hook
$('html').addClass('js-active');
 
$(function(){
/* Start jQuery Document Load
-------------------------------------------------------*/

_ie = $.browser.msie;

// COLORBOX - http://colorpowered.com/colorbox/
/* ----------------------------------------------------*/
$('#portfolio a').colorbox({
	href: function(){
		return $(this).attr('href') + ' #project';
	},
	rel: 'portfolio',
	transition: ( !_ie ) ? 'fade' : 'none',
	overlayClose: false
});

/* When a portfolio item is opened, scroll window back to the top to avoid any inconsistent positioning */
$().bind('cbox_open', function(){
	$(window).scrollTop(0);
});

if( _ie && $.browser.version  <= 6 ){
	
	/* Hover functionality to next and prev buttons for overlay */
	var prevNext = $('#cboxPrevious, #cboxNext');
	
	prevNext.live('mouseover', function(){
		$(this).addClass( $(this).attr('id') + '-hover' );
	});
	
	prevNext.live('mouseout', function(){
		$(this).removeClass( $(this).attr('id') + '-hover' );
	});
	
	/* Apply dynamic png fix, and fix background overlay height */
	$().bind('cbox_complete', function(){
		$('h2.project-title').supersleight({shim: '/images/ie/spacer.gif'});
    	$('#cboxOverlay').height( $(window).height() ).css( 'top', $(window).scrollTop() );
	});
}

// CONTACT PAGE
/* ----------------------------------------------------*/
if( $('#content').hasClass('contact') ){

	/* Add fade to image */
	$('#header').append('<div class="fade"></div>');
	
	/* Add focus / blue event handlers to form input elements */
	$('form input, form textarea').each(
		function(){
			
			var titleVal = $(this).attr('title').toUpperCase();			
			
			$(this)
			
			/* Remove the title attribute to stop IE using it as a tooltip */
			.attr('title', '')
			
			/* Use the title of the element we got earlier to set it's default value */
			.val(titleVal)
			
			/* Add the default value to the data object to get later */
			.data('defaultValues', { title: titleVal })
			
			/* If user is focusing on element with default text, remove it to allow them to enter data on focus */
			.focus(
				function(){
					if( $(this).val() == $(this).data('defaultValues').title ){
						$(this).val('');
					}
				}
			)
			
			/* If user has losst focus on the element without entering content, replace with default text */
			.blur(
				function(){
					if( $(this).val() == '' ){
						$(this).val($(this).data('defaultValues').title);
					}
				}
			);
		}
	);
}

// Emulate target="_blank" by using rel="external" attribute for anchors
$('a[rel="external"]').bind(
	'click keypress', 
	
	function(){ 
		window.open( $(this).attr('href') ); 
	}
);
	
	

/* End jQuery Document Load
-------------------------------------------------------*/
});

})(jQuery);


jQuery.fn.supersleight = function(settings) {
	settings = jQuery.extend({
		imgs: true,
		backgrounds: true,
		shim: 'x.gif',
		apply_positioning: true
	}, settings);
	
	return this.each(function(){
		if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7 && parseInt(jQuery.browser.version, 10) > 4) {
			jQuery(this).find('*').andSelf().each(function(i,obj) {
				var self = jQuery(obj);
				// background pngs
				if (settings.backgrounds && self.css('background-image').match(/\.png/i) !== null) {
					var bg = self.css('background-image');
					var src = bg.substring(5,bg.length-2);
					var mode = (self.css('background-repeat') == 'no-repeat' ? 'crop' : 'scale');
					var styles = {
						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')",
						'background-image': 'url('+settings.shim+')'
					};
					self.css(styles);
				};
				// image elements
				if (settings.imgs && self.is('img[src$=png]')){
					var styles = {
						'width': self.width() + 'px',
						'height': self.height() + 'px',
						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + self.attr('src') + "', sizingMethod='scale')"
					};
					self.css(styles).attr('src', settings.shim);
				};
				// apply position to 'active' elements
				if (settings.apply_positioning && self.is('a, input') && (self.css('position') === '' || self.css('position') == 'static')){
					self.css('position', 'relative');
				};
			});
		};
	});
};