﻿//Fade Function
(function(jQuery) {
	jQuery.fn.customFadeIn = function(speed, callback) {
		jQuery(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				jQuery(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	jQuery.fn.customFadeOut = function(speed, callback) {
		jQuery(this).fadeOut(speed, function() {
			if(jQuery.browser.msie)
				jQuery(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
})(jQuery);

jQuery(document).ready(function(){	
	//Modal Window
	jQuery('a[name="modal"]').click(function(e) {
		var id = jQuery(this).attr('href');
		var maskHeight = jQuery(document).height();
		var maskWidth = jQuery(window).width();
		jQuery('#popup_bg').css({'width':maskWidth,'height':maskHeight});	
		jQuery('#popup_bg').customFadeIn(1000, function() {});
		var winH = jQuery(window).height();
		var winW = jQuery(window).width();
		jQuery(id).css("top", jQuery(window).scrollTop()+20 + "px");
		jQuery(id).css('left', winW/2-jQuery(id).width()/2);
		jQuery(id).customFadeIn(1000, function() {
			jQuery('a.close_popup').show();								   
		});
		return false;
	});
	jQuery('a.close_popup, #popup_bg').click(function(e) {										   
		jQuery('#popup_bg, .window').customFadeOut('fast', function() {});
		jQuery('a.close_popup').hide();	
		return false;
	});	
	jQuery(document).keypress(function(e){
		if(e.keyCode==27){
			jQuery('#popup_bg, .window').customFadeOut('fast', function() {});
			jQuery('a.close_popup').hide();	
		}
	});
	
	//Smooth Scroll
	jQuery('a[rel="scroll"]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
			&& location.hostname == this.hostname) {
			var jQuerytarget = jQuery(this.hash);
			jQuerytarget = jQuerytarget.length && jQuerytarget
			|| jQuery('[name=' + this.hash.slice(1) +']');
			if (jQuerytarget.length) {
				var targetOffset = jQuerytarget.offset().top;
				jQuery('html,body')
				.animate({scrollTop: targetOffset}, 1000);
				return false;
			}
		}
	});
	
	//External Link
	jQuery('a[rel="external"]').click(function(){
        window.open( jQuery(this).attr('href') );
        return false;
    });
	
	//Contact Map	
	jQuery('a.location-map').click(function() { 
        loadMap();
        return false; 
    });
	
});
