/* Javascript Document © 2009 Andrei Gonzales. All Rights Reserved */
/* Version 1.0 */
/* Type: Javascript General Code Sheet for Techniks Group Corporation */

// jQuery Codes
jQuery(document).ready(function(){	

	/* Wait() Custom Function */
	$.fn.wait = function(time, type) {
        time = time || 2000;
        type = type || "fx";
        return this.queue(type, function() {
            var self = this;
            setTimeout(function() {
                $(self).dequeue();
            }, time);
        });
    };

	$("#ajax-preloader").hide();	
	$('noscript').hide();	
	
	// IE6 and IE7 fix for form submit buttons
	if(document.all){
		$("input[type=submit]").attr("value"," ");
	};
	
	if($('li.page-item-5').hasClass('current_page_item')){
		$('div#arrow-silhouette-bg').addClass('arrow-gone');
	}
	
	if ($('.page-item-37').hasClass('current_page_parent')) { // Checks to see if children are active, if not, hide
		return false;
	}
	else {
		$('.page-item-37').children("ul").slideUp(1);
	};
	
	// Replacement for Accordion - less glitches
	$('li.page-item-37 a').mouseover(function(){
		if($(this).parent("li.page-item-37").hasClass('selected')){
			$(this).siblings("ul").slideUp(300).end();
			$(this).parent("li.page-item-37").removeClass('selected');
		} else {
			$(this).parent("li.page-item-37").addClass('selected');
			$(this).siblings("ul").slideDown(300);
		};
	});
	
	$('#mc-right-content-nap').addClass('css-sifr-on');
	$('#mcr-chistory-container').addClass('css-sifr-on');
	$('#mcr-foa-banner').addClass('css-sifr-on');
	$('#mcr-sos-banner').addClass('css-sifr-on');
	$('#contactus-form').addClass('css-sifr-on');
	$('#mcc-container').addClass('css-sifr-on');
	

	/* Transportation */
	$('#pp-transportation-btn-main').click(function(){
		$(this).removeAttr("href");
		$(this).parent("li").siblings().removeClass("p-current");
		$(this).parent("li").addClass("p-current");
		$('#ajax-main-container').load("http://techniksgroup.com/project-portfolio/transportation/transportation-01/ div#pp-transportation");
		
		$("#ajax-preloader").ajaxStart(function(){
			$(this).show();
		}).ajaxStop(function() {
			$(this).hide();
		});
		
	});
	
	/* Public Works */
	$('#pp-public-works-btn-main').click(function(){
		$(this).removeAttr("href");
		$(this).parent("li").siblings().removeClass("p-current");
		$(this).parent("li").addClass("p-current");
		$('#ajax-main-container').load("http://techniksgroup.com/project-portfolio/public-works/public-works-01/ div#pp-public-works");
		
		$("#ajax-preloader").ajaxStart(function(){
			$(this).show();
		}).ajaxStop(function() {
			$(this).hide();
		});
		
	});
	
	/* Architecture and Planning */
	$('#pp-architecture-btn-main').click(function(){
		$(this).removeAttr("href");
		$(this).parent("li").siblings().removeClass("p-current");
		$(this).parent("li").addClass("p-current");
		$('#ajax-main-container').load("http://techniksgroup.com/project-portfolio/architecture-and-planning/architecture-and-planning-01/ div#pp-architecture");
		
		$("#ajax-preloader").ajaxStart(function(){
			$(this).show();
		}).ajaxStop(function() {
			$(this).hide();
		});
		
	});
	
	/* liveQuery Rebind and SlideScroll */
	$('div.photo-slider-hide').livequery(function(){
		/* Photo Slider */
		var $panels = $('.slider:visible .scrollContainer > div');
		var $container = $('.slider:visible .scrollContainer');
		
		// If false, we'll float all the panels left and fix the width 
		// of the container
		var horizontal = true;
		
		// Float the panels left if we're going horizontal
		if (horizontal) {
		  $panels.css({
		    'float' : 'left',
		    'position' : 'relative' // IE fix to ensure overflow is hidden
		  });
		  
		  // Calculate a new width for the container (so it holds all panels)
		  $container.css('width', $panels[0].offsetWidth * $panels.length);
		}
		
		// Collect the scroll object, at the same time apply the hidden overflow
		// to remove the default scrollbars that will appear
		var $scroll = $('.slider .scroll').css('overflow', 'hidden');
	
		// Handle nav selection
		function selectNav() {
		  $(this)
				.parents('ul:first')
					.find('a')
						.removeClass('slider-selected')
					.end()
				.end()
			.addClass('slider-selected');
		}
		
		$('.slider .sliderNavigation').find('a').click(selectNav);
		
		// Go find the navigation link that has this target and select the nav
		function trigger(data) {
		  var el = $('.slider .sliderNavigation').find('a[href$="' + data.id + '"]').get(0);
		  selectNav.call(el);
		}
		
		if (window.location.hash) {
		  trigger({ id : window.location.hash.substr(1) });
		} else {
		  $('ul.sliderNavigation a:first').click();
		}
		
		// Offset is used to move to *exactly* the right place, since I'm using
		// padding on my example, I need to subtract the amount of padding to
		// the offset
		// Try removing this to get a good idea of the effect
		var offset = parseInt((horizontal ? 
		  $container.css('paddingTop') : 
		  $container.css('paddingLeft')) 
		  || 0) * -1;
		
		var scrollOptions = {
		  target: $scroll, // The element that has the overflow
		  
		  // Can be a selector which will be relative to the target
		  items: $panels,
		  
		  navigation: '.sliderNavigation a',
		  
		  // Allow the scroll effect to run both directions
		  axis: 'x',
		  
		  onAfter: trigger, // Our final callback
		  
		  offset: offset,
		  
		  // Duration of the sliding effect
		  duration: 500,
		  
		  // Easing - can be used with the easing plugin:
		  // http://gsgd.co.uk/sandbox/jquery/easing/
		  easing: 'swing'
		};
		
		// Apply serialScroll to the slider - we chose this plugin because it 
		// supports the indexed next and previous scroll along with hooking
		// in to our navigation
		$('.slider').serialScroll(scrollOptions);
		
		// Now apply localScroll to hook any other arbitrary links to trigger
		// the effect
		$.localScroll(scrollOptions);
		
		// Finally, if the URL has a hash, move the slider in to position,
		// setting the duration to 1 because I don't want it to scroll in the
		// very first page load
		// We don't always need this, but it ensures the positioning is
		// absolutely spot on when the pages loads
		scrollOptions.duration = 1;
		//$.localScroll.hash(scrollOptions);
				
	});
	
	$('div.photo-slider-hide').livequery(function(){
		/* Image Show/Hide */
		$('.photo-slider-hide').hide();
		
		/* Image Camera Icon Functions */	
		$('.show-image-slider').click(function(){						
								
			if(document.all){
				var $resetView = $open.find('.sliderNavigation li a:first');	
				$resetView.click();
			};	
			
			var $others = $(this).parent('p').parent('div').parent('div').prevAll('div').find('.photo-slider-hide:visible');
			$others.slideUp();
			
			var $more = $(this).parent('p').parent('div').parent('div').nextAll('div').find('.photo-slider-hide:visible');			
			$more.slideUp();
			
			var $open = $(this).parent('p').parent('.pp-main-table-left').siblings('.photo-slider-hide');										
			$open.slideDown();					
			
			return false;
		});
	});
		
	// Validation
	// Fix this -> needs to be manual to be foolproof. Remove validate JS if needed.
	$('#contactus-form-id').validate({		
		highlight: function (element, redbar) {
				$(element).parent("div").addClass("errbar");
			},
			unhighlight: function (element, redbar) {
				$(element).parent("div").removeClass("errbar");
			},			
			errorPlacement: function(error) {
			error.appendTo($("div#errorbox"));
			},		
			submitHandler: function(form) {
				$('div#mcc-container').fadeIn(1000).wait();
				$('div#mcc-container').wait().wait().wait().addClass('fireaway');
				if ($('div#mcc-container').hasClass('fireaway')) {
					$('div#mcc-container').removeClass('fireaway');
					$(this).ajaxSubmit()
				}
				else {
					null;
				};	
			}
		});
	
	$(this).click(function(){
		$('#errorbox').hide();
		$('#mcc-container').hide();
	})

});