$(document).ready(function(){

	// function to set the width for switch area, according to width of children elements
	var setSwitchAreaWidth =function(){
		   var newSwitchAreaWidth = 0;
			 
			 $('#content div.switch-area ul').children().each(function() {
					newSwitchAreaWidth = newSwitchAreaWidth + $(this).width();
					// Add extra padding for MAC bug - firefox
					newSwitchAreaWidth += 5;
			  });
			 
			$('#content div.switch-area').css('width',newSwitchAreaWidth);
		}
		
	setSwitchAreaWidth();
	
	setTimeout(setSwitchAreaWidth,300); // to fix chrome extra width issue of switch area
	
	// function to set the width for pagination according to number of children elements
	$(function(){
		   var newWidth = 0;
			 
			 $('.pagination .wp-pagenavi').children().each(function() {
					newWidth = newWidth + 53;
			  });
			 
			 newWidth = newWidth - 12; // to counter -12 left margin of container
			 
			$('.pagination').css('width',newWidth);
	});
	
/****************************/
/** Code For Avia Sliders  **/
/****************************/

	//activate the lightbox
	//jQuery('a[href$=jpg], a[href$=png], a[href$=gif], a[href$=jpeg]').prettyPhoto({theme: "light_square"});

	// avia slider activation  for left-slider
	$('#left-slider').aviaSlider({
			blockSize: {height: 40, width:'full'},
			display: 'topleft',
			transition: 'fade',
			betweenBlockDelay:150,
			animationSpeed: 500,
			switchMovement: false,
			appendControlls:'#left-slider-controlls',
			autorotationSpeed:3
		});
	
	// avia slider activation  for middle-slider
	$('#middle-slider').aviaSlider({
			blockSize: {height: 40, width:'full'},
			display: 'topleft',
			transition: 'fade',
			betweenBlockDelay:200,
			animationSpeed: 500,
			switchMovement: false,
			appendControlls:'#middle-slider-controlls',
			autorotationSpeed:3
		});
	
	// avia slider activation  for right-slider
	$('#right-slider').aviaSlider({
			blockSize: {height: 40, width:'full'},
			display: 'topleft',
			transition: 'fade',
			betweenBlockDelay:250,
			animationSpeed: 500,
			switchMovement: false,
			appendControlls:'#right-slider-controlls',
			autorotationSpeed:3
		});
	
/****************************/
/** Code For Contact Panel **/
/****************************/
	
	// contact panel slide down code
	var openContactPanel = function(){
			$('#panel').slideDown('slow');
			$('.header-nav').slideUp('slow');
			$('.contact-panel-nav').slideDown('slow');
			
			$('#banner').stop().animate({ opacity: 0.3 });
			$('#content').stop().animate({ opacity: 0.3 });
			$('#footer').stop().animate({ opacity: 0.3 });
			
		}
	
	// contact panel slide up code
	var closeContactPanel = function(){
			$('#panel').slideUp('slow');
			$('.contact-panel-nav').slideUp('slow');
			$('.header-nav').slideDown('slow');

			$('#banner').stop().animate({ opacity: 1 });
			$('#content').stop().animate({ opacity: 1 });
			$('#footer').stop().animate({ opacity: 1 });

		}
		
	// contact panel open event
	$('ul.header-nav li:last-child').prev().click(function(){								
		openContactPanel();
		return false;
	});
	
	// contact panel close event
	$('#slide-close').click(function(){								
		closeContactPanel();
		return false;
	});
	
	// if clicked outside contact panel . panel will be closed
	 $('body').click(function() {
	 	closeContactPanel();
	 });
	
	// to stop propagation of above event when clicked in panel
	 $('#panel').click(function(event){
		 event.stopPropagation();
	 });
	 // to stop propagation of above event (attached with body) when clicked in .contact-panel-nav
	 $('.contact-panel-nav').click(function(event){
		 event.stopPropagation();
	 });
	 
/****************************/
/** Code For Sliding Menu **/
/****************************/
	// 
	$(function(){

		var $el, leftPos, newWidth,	
		$mainNav = $("#sliding-menu");
		
		var $slidingTab = $("#sliding-tab");
		
		var slideTabToCurrentMenuItem = function(){
				var slidingTabLeftPosition =  $(".current-menu-item span").position().left + Math.round($(".current-menu-item span").width()/2)- 26 ;
				$slidingTab.stop().animate({ left: slidingTabLeftPosition })
				.data("origLeft", slidingTabLeftPosition);
			}
		
		slideTabToCurrentMenuItem();
		
		$("#sliding-menu li").find("span").hover(function() {
			$el = $(this);
			leftPos = $el.position().left + Math.round($el.width()/2) - 26; 
			$slidingTab.stop().delay(200).animate({
				left: leftPos
			})
		}, function() {
			$slidingTab.stop().animate({
				left: $slidingTab.data("origLeft")
			});    
		});
		
		var setCurrentMenuTo = function(currentMenu){
				$("#sliding-menu li.current-menu-item").removeClass('current-menu-item');
				currentMenu.parent().addClass('current-menu-item');
			}
		
		$('#updates-switch').click(function(){
			setCurrentMenuTo($(this));
			slideTabToCurrentMenuItem();
			
			$('.home-sheets').fadeOut('slow',function(){
									$('.home-updates').fadeIn('slow');					
								});	
			return false;		
			
		});
		
		$('#projects-switch').click(function(){
			setCurrentMenuTo($(this));
			slideTabToCurrentMenuItem();
			
			$('.home-updates').fadeOut('slow',function(){
									$('.home-sheets').fadeIn('slow');								
								});
			return false;
			
		});
		
		// chrome sliding tab miss-placement fix
		setTimeout(function(){
							setCurrentMenuTo($(".current-menu-item span"));
							slideTabToCurrentMenuItem();
							},100);
		
	});
	
	// auto grow text area
	$('textarea[name=comment]').autoGrow();
	
	// for validation purposes : this function attached focus and blur event
	var addFocusAndBlur = function($input){
			
			$input.focus(function(){
				if (this.value == 'Please type here') {this.value = '';}
			});
			
			$input.blur(function(){
				if (this.value == '') {this.value = 'Please type here';}
			});
	}
	
	addFocusAndBlur($('#contact-name'));
	addFocusAndBlur($('#contact-email'));
	addFocusAndBlur($('#message'));
	addFocusAndBlur($('#author'));
	addFocusAndBlur($('#email'));
	addFocusAndBlur($('#comment'));
	
});

