// Function to give images of different sizes a standard width and center those Images vertically
// Requires the desired width of the image in pixels

$.fn.centerImgs = function(width_in_px){
	
	var imgArr = new Array();
	var tallest = 0;
	var i = 0;
	
	// adjusts the size
	$(this).find('img').each(function(){
		
		var img_height = $(this).height();
		var img_width=$(this).width();
		var set_width = width_in_px;
		var ratio = set_width / img_width;
		var set_height = img_height*ratio;
		
		imgArr.push(set_height);
		if (set_height > tallest){
			tallest = set_height;
		};
		
		$(this).addClass('img' + i);
		i++;
		
		$(this).attr({
			width: set_width,
			height: set_height
		});
	});
	
	
	// adjusts padding to center vertically
	for (var j = 0; j<imgArr.length; j++) { 
		var padding = (tallest - imgArr[j])/2;
		$('.img'+j).css('padding-top', padding);
	};
};

//function to clear the form fields on focus for the contact page
$.fn.clearFormField = function() {
	return this.focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
};

$(document).ready(function(){
						   
// ****************** for navigation *************************

var pathname = window.location.pathname;

if (pathname.indexOf('index') != -1) {
	$('a#navhome').css({
		'background-position':'0 -64px',
		'border-bottom':'3px solid #FE731B'
	})
}

if (pathname.indexOf('tech') != -1) {
	$('a#navtech').addClass('nav_active')
}

if (pathname.indexOf('solution') != -1) {
	$('a#navsolutions').addClass('nav_active')
}

if (pathname.indexOf('about') != -1) {
	$('a#navabout').addClass('nav_active')
}

if (pathname.indexOf('contact') != -1) {
	$('a#navcontact').css({
		'background-position':'0 -64px',
		'border-bottom':'3px solid #FE731B'
	})
}

// ****************** for picture gallery ****************** ****************** currently unused ***********************
	
	//hides all captions on load
	$('.caption').each(function(){
		$(this).addClass('hide_caption');
	});//end each
	
	
	// returns the number of images being used for the slideshow
	function countGalleryImg(div){	
		var i = 0;
		$(div).each(function(){
			i++ ;
			});// end each
		return i
	}; //end countGalleryImg

	// creates a new unique class for each of the captions, ex. first caption is caption0, next one is caption1
	function addCaptionNum(div, img_count){ 
		i=0
		while (i<img_count){
			var currentClass = $(div).eq(i).attr('class');
			var newClass = currentClass + i;
			$(div).find('caption').eq(i).removeClass('caption');
			$(div).eq(i).addClass(newClass);
			i++;
		};//end while
	};//end addCaptionNum

	// toggles the visibility of the captions
	function showCaption(){
		$(div).addClass('hide_caption');
		$(div).eq(i).removeClass('hide_caption');
		i++;
		if (i == img_count){
			i=0;
		};
	};//end showCaption

	// global variables to be passed into showCaption
	// shiver... shrug... hack... I know this is ugly and I will fix it when I have the time
	var img_count = countGalleryImg('#gallery img');
	var i=0;
	var div = '#highlight .caption';
	
// ************************* Calls the cycle plugin and starts the slideshow *********************************

	$('#gallery').cycle({
		fx: 'scrollLeft',
		timeout: 10000,
		pager:'#controls',
		before: showCaption
	});


// ************************* Resizes, Centers, and Applies Cycle to Images on Home Page **********************

	//Vertically centers and adjusts the size of the client logos on the homepage
	$('#client_logos').centerImgs(200);
	
	
	// Applies the cycle plugin so the images rotate
	$('#client_logos').cycle({
		fx:'scrollLeft',
		timeout:4000
	});
	
	//Vertically centers and adjusts the size of the event images on the homepage
	$('#event_imgs').centerImgs(250);
	
	//Vertically centers and adjusts the size of the award images on the homepage
	$('#award_imgs').centerImgs(250);
	
	
// ****************** footer rollover ******************
	$('#site_map div').each(function(){
		var activeDiv = $(this).attr('id');
		$('#'+activeDiv).hover(function(){
			$(this).addClass('site_map_rollover')
		}, function(){
			$(this).removeClass('site_map_rollover')
		});// end hover
	});// end each


// ****************** Forms ******************
	
	//clears the form fields on focus
	$('.clear').clearFormField();   


	var map = new GMap2(document.getElementById("map_canvas"));
	var nomadconnection = new GLatLng(37.490448,126.99648);
	map.setCenter(nomadconnection, 17);

	// places the marker
	map.addOverlay(new GMarker(nomadconnection));
	
	GEvent.addListener(map, "click", function() {
		window.location="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=Bangbae+,+848-17+Seocho-gu,+Seoul,+South+Korea&sll=37.490232,126.997171&sspn=0.001296,0.003093&ie=UTF8&hq=&hnear=South+Korea+Seoul+Seocho-gu+Bangbae-dong+848-17&z=16"});
});//end ready





