// default

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

//
$(function(){

	// css navigation dropdown/flyout replacement for ie 6
	if($.browser.msie && $.browser.version < 7){
		$('#nav ul>li').hover(/*over*/function(){ $(this).children('ul:first').css({display:'block'})},/*out*/function(){ $(this).children('ul:first').css({display:'none'})});
		$('li>a').hover(/*over*/function(){$(this).parent('li').addClass('hover')},/*out*/function(){$(this).parent('li').removeClass('hover')});
	}
	
	// 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')]); }
				});	
			}
		}
	});
	
	// 
	$('input').focus(function(){
		if($(this).val() == $(this).attr('title')){ $(this).val(''); }
	}).blur(function(){
		if($(this).val() == ''){ $(this).val($(this).attr('title')); }
	});
	
	// 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: 200 }, 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: 200 }, 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();
								}
							}
	});
	
	// billboard
	$('#billboard').cycle({ 
		fx:				'fade', 
		speed:			1000,
		timeout:		6500,
		pause:			1,
		pauseOnPagerHover: 0,
		pager: '.buttons',
		pagerAnchorBuilder: function(idx, slide) { 
                return '<li><a href="#">&nbsp;</a></li>';
        },
		updateActivePagerLink: function(container, slide) {
			$(container+'>li>a').removeClass('sel');
			$(container+'>li:eq('+slide+')>a').addClass('sel');
			
			var num_elements = $('.photo').size() - 1;
			var marker_position = ((num_elements - slide) * 19) + 20
			$('.marker').animate({right: marker_position + 'px'});
		},
		cleartype:		1,
		cleartypeNoBg:	1
	 });
	
	// 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;
		});
	});
	
	// gallery
	$("#gallery>ul>li>a,a.image").fancybox({
		'titlePosition'		: 'over',
		'overlayColor'		: '#000000',
		'overlayOpacity' 	: .5
	});
	
	//
	$('.hide').hide();
	$('.show').show();
	
});
