// global javascript functions for the entire theme

jQuery(document).ready(function($) {
	
	var msie6 = ( $.browser.msie && $.browser.version == 6 ) ? true : false ;
	// --------------------------------------------------------
	// JS specific CSS target
	//
	
	$('body').removeClass('no-js').addClass('js');
	
	// --------------------------------------------------------
	// Remove title from navigation links, and others
	//
	
	$('.menu a, #page_list a, .services_links_widget a').removeAttr('title');
	
	// --------------------------------------------------------
	// autoresize contact form
	//

  $('.contact_form_widget textarea').autoResize({
    // On resize:
    onResize : function() {
        $(this).css({opacity:0.8});
    },
    // After resize:
    animateCallback : function() {
        $(this).css({opacity:1});
    },
    // Quite slow animation:
    animateDuration : 300,
    // More extra space:
    extraSpace : 0,
    limit: 80
	});
	
	// --------------------------------------------------------
	// Auto Fill functionality for footer contact form
	//
	
	var autofillop = {
		prePopulate: '',
		defaultTextColor: '#999',
		activeTextColor: '#555'
	};
	
	$('#s').autofill(
	$.extend( autofillop, {
		value: 'Search for'
	}));	
	
	// client login form.
	$('.client_login_widget input[name=username]').autofill(
	$.extend( autofillop, {
		value: 'E-mail'
	}));
	
	if(!msie6)
	$('.client_login_widget input[name=password]').autofill(
	$.extend( autofillop, {
		value: 'Password'
	}));
	
	
	// project management login form.
	$('.client_login_pm_widget input[name=login[email]], #mc_mv_EMAIL').autofill(
	$.extend( autofillop, {
		value: 'E-mail'
	}));
	
	if(!msie6)
	$('.client_login_pm_widget input[name=login[password]]').autofill(
	$.extend( autofillop, {
		value: 'Password'
	}));
						
	var contactopt = $.extend( autofillop, {
		defaultTextColor: '#0070e6',
		activeTextColor: '#888'
	} );
	
	// contact form
	$('.contact_form_widget input[name=cfw_name]').autofill(
	$.extend( contactopt, {
		value: 'your name'
	} ));
	$('.contact_form_widget textarea[name=cfw_about]').autofill(
	$.extend( contactopt, {
		value: 'type your inquiry here'
	} ));
	$('.contact_form_widget input[name=cfw_contact_email]').autofill(
	$.extend( contactopt, {
		value: 'email'
	} ));
	$('.contact_form_widget input[name=cfw_contact_phone]').autofill(
	$.extend( contactopt, {
		value: 'phone'
	} ));
	
	//$('.contact_form_widget input[name=cfw_name],.contact_form_widget textarea[name=cfw_about],.contact_form_widget input[name=cfw_contact]').autofill( contactopt );
	
	// --------------------------------------------------------
	// .poper functionality
	//
	
	$('.has-poper').hover(function(){
		/*
if( $.browser.msie )
			$('div.poper', this ).show();
		else
*/
			$('div.poper', this ).stop(true, true).slideDown(300);
			
	},function(){
		/*
if( $.browser.msie )
			$('div.poper', this ).hide();
		else
*/
			$('div.poper', this ).stop(true, true).slideUp(300);
			
	});
	
	// --------------------------------------------------------
	// Comment form slider
	//
	
	$('#reply-title').toggle(function(){
		$(this).toggleClass('down');
		$('#commentform').slideDown( 500 );
	},function(){
		$(this).toggleClass('down');
		$('#commentform').slideUp( 500 );
	});
	
	// --------------------------------------------------------
	// Image resizer for content incase of large images.
	//
	
	if( false ){
	
		var entry_width = $('.entry-content').width();
		
		$('.entry-content img').each( function( i ){
		
			var width = $(this).width();
			
			//console.log(width);
				
			// if its wider than entry shrink it down
			// and if it already isn't inside a link wrap it in a link
			if( width > entry_width ){
				
				if( $(this).parent().is('a') ){
					//console.log('inside an a tag');
				}else{
					var link = $(this).attr('src');
					$(this).wrap('<a href="'+link+'" ></a>');
				}		
				
				// also scale height.
				$(this).width( entry_width );
				
			}
			
		});
	}
	
	// --------------------------------------------------------
	// Services tipsy
	//
		
	//$('.service_tax').tipsy({ gravity: 'se', fade: true, delayOut: 300}); // nw | n | ne | w | e | sw | s | se
	
	// --------------------------------------------------------
	// Work thumbnails on Front page.
	//
	
	(function(){
		
		var msie6 = ( $.browser.msie && $.browser.version == 6 ) ? true : false ;			
		var msie6_hide = 'div.excerpt, h2';
		
		// hide all other items
		
		var animation_speed = 400;
		
		var $fpw = $('#front_page_works');
		var $fpw_children = $fpw.children();
		
		// works holder styles
		$fpw.css({
			'postition':'relative',
			'overflow':'hidden',
			'height':$fpw_children.eq(0).height()
		});
		
		$fpw_children.each(function(){
			//console.log(this);
			$.data( this, 'height', $(this).height() );
			$(this).css( 'position', 'absolute');
		});
		
		//$fpw.children(':not(:eq(0))').hide();
		var cur_index = 0;
		
		$('#work_thumbs.front > div').click( function(e){
			e.preventDefault()
			//var link = $('this').attr('href');
			
			//$(this).siblings('div').removeClass('selected');
			//$(this).addClass('selected');
				
			thumb_index = $(this).index();
			if( cur_index != thumb_index ){
								
				var main_work = $fpw_children.eq( thumb_index ).get(0);
				//console.log(main_work);
				
				move_height = $.data( main_work, 'height' );
				//console.log( move_height );
				
				// animate whole container
				if( move_height !== null ){
				
					if(msie6) // hide text on IE6
						$( msie6_hide, $fpw ).hide();
					
					$fpw.animate({ height: move_height }, animation_speed , function(){
						if(msie6)// show text on IE6 when done animating
							$( msie6_hide, this ).show();
						
					});
				}
				
				$fpw_children.fadeOut( animation_speed ).eq( thumb_index ).fadeIn( animation_speed );
				
				cur_index = thumb_index;
				
			}
			
		}).eq(0).click();
		
	})();
		
	// --------------------------------------------------------
	// Client List fade
	//
	
	(function() {
		var client_fade_speed = 333;
		
		$('#ourclients td:has(a)').hover(function(){
			$('div.popover', this ).stop(true, false).fadeTo(client_fade_speed, 1);
			$('img', this ).stop(true, false).fadeTo(client_fade_speed, 0.1);
		},function(){
			$('div.popover', this ).stop(true, false).fadeTo(client_fade_speed, 0);
			$('img', this ).stop(true, false).fadeTo(client_fade_speed, 1);
		});
	})();
	
	// --------------------------------------------------------
	// image lightbox
	//
	
	// images on single work page.
	$('.image-slider a.zoom').fancybox({
		'cyclic': true,
		'showNavArrows' : true,
		'titleShow'			: true,
		'transitionIn'	: 'fade',
		'transitionOut'	: 'fade',
		'titlePosition'	: 'inside',
		'hideOnContentClick' : true,
		'showNavArrows' : true,
		'onStart'				: function(selectedArray, selectedIndex, selectedOpts){
			
			//console.log(selectedArray);
			var $this = selectedArray.eq(selectedIndex);
			
			var title = '';
			
			// check for image caption
			title = $this.parent().parent().find('h3').text();
			
			if( title ){
				selectedOpts.title = title;
			}
			
		}
	});
	
	// Gallery
	$('.entry-content .gallery a').fancybox({ //'.entry-content a:has(img.attachment-thumbnail)'
		'cyclic': true,
		'showNavArrows' : true,
		'titleShow'			: true,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'titlePosition'	: 'inside',
		'hideOnContentClick' : true
	});
	
	// don't perform fancybox on pages with #ourclients displaying.

	if( !$('#ourclients').length ){
		// All other images in blog
		$('.entry-content a:has(img:not(.attachment-thumbnail))').fancybox({			'titleShow'			: true,
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'titlePosition'	: 'inside',
			'hideOnContentClick' : true,
			'onStart'				: function(selectedArray, selectedIndex, selectedOpts){
				
				var $this = $(selectedArray);
				
				var title = '';
				
				// check for image caption
				title = $this.siblings('.wp-caption-text').text();
				if( title ){
					selectedOpts.title = title;
				}else{
					// update with image title.
					selectedOpts.title = $this.children('img').attr('title');
				}
			}
		});
	}
	
	
	// --------------------------------------------------------
	// Boxes hover effects, removed entire box background color change.
	//
	
	// Check for boxes on page
	if( $('#boxes').length ){
	
		var new_border_color = '#0070e6';
		
		var cur_color = $('#boxes .box a').css('background-color');
		
		//var border_color = $('#boxes .box a').css('border-color');
		
		var border_color = '#eaebeb';
		
		var box_anim_speed = 300;

		$('#boxes .box a').hover( function(){
			// Hover						
			$(this).stop().animate( { backgroundColor : 'white', borderColor: new_border_color} , box_anim_speed );			
		},function(){	
			// Un-Hover
			$(this).stop().animate( { backgroundColor : cur_color, borderColor: border_color } , box_anim_speed );
		});
		
	}
	
	// --------------------------------------------------------
	// Works slider
	//
	
	(function() {
		var slideAnchorsList = jQuery('ul.slider-controls>li>a');
		
		// iphone menu hover 
		$slider_menu = jQuery('div.image-slider>div.loading-wrapper>ul>li').mouseover(function() {
			
			//if(!jQuery(this).data('firedEvent')) {
				//jQuery(this).data('firedEvent',true);
				
				if( $.browser.msie && $.browser.version <= 7 ){
					jQuery('div.slider-menu',this).show();
				}else{
					jQuery('div.slider-menu',this).stop().fadeTo( 333, 1);
				}
			//}
		}).mouseout(function() {
			//jQuery(this).data('firedEvent',false);
			
			if( $.browser.msie && $.browser.version <= 7 ){
				jQuery('div.slider-menu',this).hide();
			}else{
				jQuery('div.slider-menu',this).stop().fadeTo( 333, 0 );
			}
		})
		//console.log($slider_menu.eq(0));
		$slider_menu.eq(0).mouseover(); //.delay( 4000 ).mouseout() // doesn't work
		// hover over the first one and then hide it again to make the user aware of its precense.	
		
		jQuery(slideAnchorsList).click(function(e) {
			e.preventDefault();
			var id = jQuery(this).parent().attr('id').replace(/\D/g,'');
			var hash = this.href.substring(this.href.indexOf('#')+1);
			jQuery('div.image-slider>div.loading-wrapper>ul>li').animate({
				opacity: 0
			},800,function() {
				jQuery(this).hide();
				var targetedElm = jQuery('#slide-' + hash);
				if(targetedElm.css('display') == 'none') {
					jQuery('#slide-' + hash).show().animate({
						opacity: 1
					},800);
				}
			});
		});
		
		jQuery(slideAnchorsList[0]).click();
	})();
	
	(function() {
		var imageCount = 3;
		var currentOffset = (imageCount + 1) * -1;
		var slideList = jQuery('ul.slider-controls>li');
		var lockActions = false;
		// If not enough  images to warrent prev/next hide them
		var $slider_controls = jQuery('.slider_cont div.controls ');
		if(slideList.length <= imageCount) {
			jQuery('a' , $slider_controls).addClass('disabled');
		}/*
else if( slideList.length > imageCount ){
			jQuery('a.prev', $slider_controls).addClass('disabled');
		}
*/
		
		// Prev Button
		jQuery('div.slider_cont a.prev').click(function(e) {
			e.preventDefault()
			if(!lockActions) {
				lockActions = true;
				var newOffset = currentOffset - imageCount;
				if(newOffset >= -1) {
					currentOffset = newOffset;
				}
				
				var showSlidesList = [];
				jQuery(slideList).each(function() {
					var idNum = jQuery(this).attr('id').replace(/\D/g,'');
					if(idNum > currentOffset && idNum <= currentOffset + imageCount) {
						showSlidesList.push(this);
					} else {
						jQuery(this).animate({
							opacity: 0
						},500,function() {
							jQuery(this).hide();
						});
					}
				});
				setTimeout(function() {
					for(var showSlidesListKey in showSlidesList) {
						jQuery(showSlidesList[showSlidesListKey]).show().animate({
							opacity: 1
						},500);
					}
					
					//jQuery('a' , $slider_controls).addClass('disabled');
					
					/*
if( showSlidesList.length <= imageCount ){
						//jQuery('a.next' , $slider_controls).removeClass('disabled');
					}
*/
					
					lockActions = false;
				},600);
			}
		});
		// Next Button
		jQuery('div.slider_cont a.next').click(function(e) {
			e.preventDefault();
			if(!lockActions) {
				lockActions = true;
				var newOffset = currentOffset + imageCount;
				if(newOffset <= slideList.length - imageCount + 1) {
					currentOffset = newOffset;
				}
				var showSlidesList = [];
				jQuery(slideList).each(function() {
					var idNum = jQuery(this).attr('id').replace(/\D/g,'');
					if(idNum > currentOffset && idNum <= currentOffset + imageCount) {
						showSlidesList.push(this);
					} else {
						jQuery(this).animate({
							opacity: 0
						},500,function() {
							jQuery(this).hide();
						});
					}
				});
				setTimeout(function() {
					for(var showSlidesListKey in showSlidesList) {
						jQuery(showSlidesList[showSlidesListKey]).show().animate({
							opacity: 1
						},500);
					}
					
					/*
jQuery('a' , $slider_controls).addClass('disabled');
					//console.log();
					if( showSlidesList.length < imageCount ){
						jQuery('a.prev' , $slider_controls).removeClass('disabled');
						jQuery('a.next' , $slider_controls).addClass('disabled');
					}else if( showSlidesList.eq(0) === slideList.eq(0) && slideList.length > imageCount ){
						//console.log('more images!');
						jQuery('a.prev' , $slider_controls).addClass('disabled');
						jQuery('a.next' , $slider_controls).removeClass('disabled');
					}
*/
					
					lockActions = false;
				},600);
			}
		}).click();
	})();
});
