// default

//
$.postJSON = function (url, data, callback) {
	$.post(url, data, callback, 'json');
};

//
$(function(){
	
	// event tracker for outgoing links/file downloads
	$('a').each(function(){
		var path;
		
		if(path = $(this).attr('href')){
			var outgoing 	= /^(http|https)/i;
			var ext 		= /[^/].(?:doc|eps|jpg|jpeg|png|svg|xls|ppt|pdf|xls|zip|txt|vsd|vxd|js|css|rar|exe|wma|mov|avi|wmv|mp3|mp4)$/i;
			
			if (path.match(outgoing) && !path.match(document.domain) ) {
				$(this).click(function(){
					if(_gaq){ _gaq.push(['_trackEvent', 'Link', 'Outgoing', $(this).attr('href')]); }
				});
			} else if(path.match(ext)){
				$(this).click(function(){
					if(_gaq){ _gaq.push(['_trackEvent', 'File', 'Download', $(this).attr('href')]); }
				});	
			}
		}
	});
	
	// forms
	$('#form').ajaxForm({
        beforeSubmit:  	function(formData, form, options){
							$('input.warning,textarea.warning,checkbox.warning,select.warning').removeClass('warning');
							$('.validate').slideUp();
							return true;
						}, 
        success:       	function(data, status, xhr, form){
							if(data.success == true){
								$('.validate>p').html(data.thank_you);
								$('.validate').slideDown();
								$('html, body').animate({ scrollTop: 100 }, 1000);
								form.slideUp();
							} else {
								$('.validate>p').html('');
								
								for (var i in data.error){
									$('.validate>p').append(data.error[i] + '<br />');
								}
								
								for (var i in data.error){
									$('[name=' + data.name[i] + ']').addClass('warning');
								}
								
								$('html, body').animate({ scrollTop: 100 }, 1000, function(){$('.validate').slideDown();});
							}
						},
		type:			'post',
		dataType:		'json'
	});
	
	// newsletter
	$('#newsletter>form').ajaxForm({
		dataType		:	'json',
		beforeSubmit	:	function(){
								$('input.warning,textarea.warning,checkbox.warning,select.warning').removeClass('warning');
							},  
        success			:	function(response, status){
								if(response.success == true){
									$('#newsletter>form').fadeOut();
									$('#newsletter-msg>p').html(response.msg).parent('div').fadeIn();
								} else {
									$('#newsletter-email').addClass('warning');
									$('#newsletter-msg>p').html(response.msg).parent('div').fadeIn();
								}
							}
	});
	
	// input clear
	$('input').focus(function(){
		if($(this).val() == $(this).attr('title')){ $(this).val(''); }
	}).blur(function(){
		if($(this).val() == ''){ $(this).val($(this).attr('title')); }
	});
	
	// map
	$('.map').each(function(){
		if (GBrowserIsCompatible()) {
			
			var lat_lng = $(this).data('options');
			
        	map = new GMap2(this);
			map.addControl(new GSmallMapControl());

        	map.setCenter(new GLatLng(lat_lng.lat, lat_lng.lng), 15 /*,G_PHYSICAL_MAP*/);
 			
			marker = new GMarker(new GLatLng(lat_lng.lat, lat_lng.lng));
			map.addOverlay(marker);
			  
			GEvent.addListener(marker, "click", function() {
				window.open(lat_lng.url);
			});
			
		}	
	});
	
	//
	$('ul.select>a.button').click(function(){
		if($.browser.msie && $.browser.version < 8){
			var contentOffset = $('#content').offset();
			var buttonOffset = $(this).offset();
			$(this).parent('ul').find('ul').css({position:'absolute',top: (buttonOffset.top - contentOffset.top + 29) + 'px', right: '-1px'});
		}
		$('.hover').not($(this).parent('ul')).removeClass('hover');
		$(this).parent('ul').toggleClass('hover');
		return false;	
	});
	
	$('ul.select>li>ul>li>a,body').click(function(){
		$('.hover').removeClass('hover');
		return true;
	});
	
	// view all
	$('.view-all>a').click(function(){
		$(this).parent('li').slideUp();
		$(this).parent('li').siblings('.hide').slideDown();
		return false;
	});
	
	// more
	$('.moreLink,.moreBtn').click(function(){
		$(this).siblings('.moreTxt').show('slow', function(){ $(this).css({'display':'inline'})});
		$(this).siblings('.moreBtn').hide();
	});
	
	//windowshade
	$('.windowshade').each(function(){
		$(this).find('.string').bind('click', function(){
			$(this).parent('p').siblings('.shade').slideToggle();
			return false;
		});
	});
	
	//
	$('.hide').hide();
	$('.show').show();
	
});
