$(function(){
  
  	//awpsome counter
  	function HasBeenScrolledTo(elem) {
	    var docViewTop = $(window).scrollTop();
	    var docViewBottom = docViewTop + $(window).height();
	    var elemTop = $(elem).offset().top;
	    return elemTop < docViewBottom;
	}
	
	if (!Modernizr.touch) {
		$(window).scroll(function() {
			if (HasBeenScrolledTo($('.percent'))) {
			    $('.percent').countTo({
		            from: 50,
		            to: 100,
		            speed: 2000,
		            refreshInterval: 20,
		            onComplete: function(value) {
		                //done
		            }
		        });
		        $(window).unbind('scroll');
			}
		});
	}
	
	var proj_tcat = [
		{href : 'img/projects/tcat-home.jpg', title : 'TCAT Bus Company: website design with WebTree CMS, trip planner integration. (www.tcatbus.com)'}
	],
	proj_fly = [
		{href : 'img/projects/fly-home.jpg', title : 'Ithaca / Tompkins County Regional Airport: website design with WebTree CMS, trip planner integration. (www.flyithaca.com)'}
	],
	proj_pp = [
		{href : 'img/projects/pp-annual1.jpg', title : 'Planned Parenthood of the Southern Finger Lakes: 2010 annual report.'},
		{href : 'img/projects/pp-annual3.jpg', title : 'Planned Parenthood of the Southern Finger Lakes: 2010 annual report.'},
		{href : 'img/projects/pp-annual2.jpg', title : 'Planned Parenthood of the Southern Finger Lakes: 2010 annual report.'},
		{href : 'img/projects/pp-annual4.jpg', title : 'Planned Parenthood of the Southern Finger Lakes: 2010 annual report.'},
		{href : 'img/projects/pp-stationary.jpg', title : 'Planned Parenthood of the Southern Finger Lakes: complete stationary set.'},
		{href : 'img/projects/pp-capteen.jpg', title : 'Planned Parenthood of the Southern Finger Lakes: campaign posters.'},
		{href : 'img/projects/pp-condomsense.jpg', title : 'Planned Parenthood of the Southern Finger Lakes: &quot;Condom Sense&quot; campaign booklet.'},
		{href : 'img/projects/pp-condomsense2.jpg', title : 'Planned Parenthood of the Southern Finger Lakes: &quot;Condom Sense&quot; campaign booklet.'},
		{href : 'img/projects/pp-annual09-1.jpg', title : 'Planned Parenthood of the Southern Finger Lakes: 2009 annual report.'},
		{href : 'img/projects/pp-annual09-2.jpg', title : 'Planned Parenthood of the Southern Finger Lakes: 2009 annual report.'},
		{href : 'img/projects/pp-annual09-3.jpg', title : 'Planned Parenthood of the Southern Finger Lakes: 2009 annual report.'},
		{href : 'img/projects/pp-annual09-4.jpg', title : 'Planned Parenthood of the Southern Finger Lakes: 2009 annual report.'}
	],
	proj_cu = [
		{href : 'img/projects/cu-astro.jpg', title : 'Cornell Department of Astronomy: website design with WebTree CMS.'},
		{href : 'img/projects/jm-home.jpg', title : 'Cornell Johnson Museum of Art: website design with WebTree CMS.'},
		{href : 'img/projects/cu-warren.jpg', title : 'Cornell University Warren Hall: website design with WebTree CMS.'},
		{href : 'img/projects/cu-stocking.jpg', title : 'Cornell University Stocking Hall: website design with WebTree CMS.'},
		{href : 'img/projects/cu-dairy.jpg', title : 'Cornell University Dairy: website design with WebTree CMS.'},
		{href : 'img/projects/cu-cuic.jpg', title : 'Cornell Univeristy Insect Collection: website design with WebTree CMS.'},
		{href : 'img/projects/cu-chorus.jpg', title : 'Cornell University Chorus: website design with WebTree CMS.'},
		{href : 'img/projects/cu-cmi.jpg', title : 'Cornell University Center For Molecular Interfacing: website design with WebTree CMS.'},
		{href : 'img/projects/cu-fts.jpg', title : 'Cornell Farm to School Program: website design with WebTree CMS..'}
	],
	proj_ic = [
		{href : 'img/projects/ic-brochure.jpg', title : 'Ithaca College School of Business: promotional brochure/booklet design.'}
	],
	proj_car = [
		{href : 'img/projects/car-sign.jpg', title : 'Ithaca Car Share: public signage.'},
		{href : 'img/projects/car-brochures1.jpg', title : 'Ithaca Car Share: brochure design.'},
		{href : 'img/projects/car-brochures2.jpg', title : 'Ithaca Car Share: brochure design.'}
	],
	proj_hangar = [
		{href : 'img/projects/hangar-home.jpg', title : 'Hangar Theatre: website design with WebTree CMS.'}
	],
	proj_ib2 = [
		{href : 'img/projects/ib-home.jpg', title : 'Ithaca Bakery: main company website design with WebTree CMS. (www.ithacabakery.com)'}, 
		{href : 'img/projects/ib-catering.jpg', title : 'Ithaca Bakery: catering website design with WebTree CMS. (www.ithacabakery.com)'},
		{href : 'img/projects/ib-store.jpg', title : 'Ithaca Bakery: online store design and integration. (www.ithacabakery.com)'}
	],
	proj_ib = [
		{href : 'img/projects/ib-home.jpg', title : 'Ithaca Bakery: main company website design with WebTree CMS. (www.ithacabakery.com)'}, 
		{href : 'img/projects/ib-catering.jpg', title : 'Ithaca Bakery: catering website design with WebTree CMS. (www.ithacabakery.com)'},
		{href : 'img/projects/ib-store.jpg', title : 'Ithaca Bakery: online store design and integration. (www.ithacabakery.com)'}
	],
	proj_gf = [
		{href : 'img/projects/gf-home.jpg', title : 'Corning Glass Festival: website via AWP Express. (www.glassfest.org)'}, 
		{href : 'img/projects/gf-inner.jpg', title : 'Corning Glass Festival: website via AWP Express. (www.glassfest.org)'}
	],
	proj_jm = [
		{href : 'img/projects/jm-home.jpg', title : 'Cornell University Johnson Museum of Art: website design with WebTree CMS. (museum.cornell.edu)'}, 
		{href : 'img/projects/jm-collections.jpg', title : 'Cornell University Johnson Museum of Art: website design with WebTree CMS. (museum.cornell.edu)'},
		{href : 'img/projects/jm-education.jpg', title : 'Cornell University Johnson Museum of Art: website design with WebTree CMS. (museum.cornell.edu)'},
		{href : 'img/projects/jm-kiosk.jpg', title : 'Cornell University Johnson Museum of Art: interactive kiosks for Asian Art floor. (museum.cornell.edu)'}
	],
	proj_gg = [
		{href : 'img/projects/gg-truck.jpg', title : 'Garden Gate Delivery: branding, e-commerce, promotional materials. (www.gardengatedelivery.com)'},
		{href : 'img/projects/gg-home.jpg', title : 'Garden Gate Delivery: branding, e-commerce, promotional materials. (www.gardengatedelivery.com)'}, 
		{href : 'img/projects/gg-cards.jpg', title : 'Garden Gate Delivery: branding, e-commerce, promotional materials. (www.gardengatedelivery.com)'},
		{href : 'img/projects/gg-maildart.jpg', title : 'Garden Gate Delivery: branding, e-commerce, promotional materials. (www.gardengatedelivery.com)'},
		{href : 'img/projects/gg-cards2.jpg', title : 'Garden Gate Delivery: branding, e-commerce, promotional materials. (www.gardengatedelivery.com)'}
	],
	proj_spider = [
		{href : 'img/projects/spider-display.jpg', title : 'Spider Camera Holster: product display at B&H Photo &amp; Video store. (www.spiderholster.com)'},
		{href : 'img/projects/spider-home.jpg', title : 'Spider Camera Holster: website design with WebTree CMS and e-commerce integration. (www.spiderholster.com)'}, 
		{href : 'img/projects/spider-magazine.jpg', title : 'Spider Camera Holster: branding, advertisements and promotional materials. (www.spiderholster.com)'},
		{href : 'img/projects/spider-display2.jpg', title : 'Spider Camera Holster: in-store countertop display. (www.spiderholster.com)'},
		{href : 'img/projects/spider-scsbox.jpg', title : 'Spider Camera Holster: product packaging. (www.spiderholster.com)'},
		{href : 'img/projects/spider-card.jpg', title : 'Spider Camera Holster: instruction card with QR codes. (www.spiderholster.com)'},
		{href : 'img/projects/spider-booklet.jpg', title : 'Spider Camera Holster: instruction booklet design. (www.spiderholster.com)'}
	],
	proj_ofh = [
		{href : 'img/projects/ofh-stickers.jpg', title : 'Out For Health: branding and promotional materials. (www.outforhealth.org)'},
		{href : 'img/projects/ofh-home.jpg', title : 'Out For Health: website design with CMS. (www.outforhealth.org)'}, 
		{href : 'img/projects/ofh-posters.jpg', title : 'Out For Health: promotional posters. (www.outforhealth.org)'},
		{href : 'img/projects/ofh-hat.jpg', title : 'Out For Health: branding and promotional materials. (www.outforhealth.org)'},
		{href : 'img/projects/ofh-markers.jpg', title : 'Out For Health: magic markers! (www.outforhealth.org)'}
	];
	
	
	
	if (Modernizr.touch){
			
			(function(window, PhotoSwipe){
			
			var instance;
			
			var options = {
					getImageSource: function(obj){
						return obj.href;
					},
					getImageCaption: function(obj){
						return obj.title;
					}
				};

			$('#project-tcat').click(function(){
				instance = Code.PhotoSwipe.attach(proj_tcat, options);
				instance.show(0);
				return false;
			});

			$('#project-fly').click(function(){
				instance = Code.PhotoSwipe.attach(proj_fly, options);
				instance.show(0);
				return false;
			});

			$('#project-pp').click(function(){
				instance = Code.PhotoSwipe.attach(proj_pp, options);
				instance.show(0);
				return false;
			});

			$('#project-cu').click(function(){
				instance = Code.PhotoSwipe.attach(proj_cu, options);
				instance.show(0);
				return false;
			});

			$('#project-ic').click(function(){
				instance = Code.PhotoSwipe.attach(proj_ic, options);
				instance.show(0);
				return false;
			});

			$('#project-car').click(function(){
				instance = Code.PhotoSwipe.attach(proj_car, options);
				instance.show(0);
				return false;
			});

			$('#project-hangar').click(function(){
				instance = Code.PhotoSwipe.attach(proj_hangar, options);
				instance.show(0);
				return false;
			});

			$('#project-ib2').click(function(){
				instance = Code.PhotoSwipe.attach(proj_ib2, options);
				instance.show(0);
				return false;
			});

			$('#project-ib').click(function(){
				instance = Code.PhotoSwipe.attach(proj_ib, options);
				instance.show(0);
				return false;
			});

			$('#project-gf').click(function(){
				instance = Code.PhotoSwipe.attach(proj_gf, options);
				instance.show(0);
				return false;
			});

			$('#project-jm').click(function(){
				instance = Code.PhotoSwipe.attach(proj_jm, options);
				instance.show(0);
				return false;
			});

			$('#project-gg').click(function(){
				instance = Code.PhotoSwipe.attach(proj_gg, options);
				instance.show(0);
				return false;
			});

			$('#project-spider').click(function(){
				instance = Code.PhotoSwipe.attach(proj_spider, options);
				instance.show(0);
				return false;
			});

			$('#project-ofh').click(function(){
				instance = Code.PhotoSwipe.attach(proj_ofh, options);
				instance.show(0);
				return false;
			});
			
		}(window, window.Code.PhotoSwipe));
	} else {
	   	//Fancybox galleries
		var fbOpt = {
			openEffect:	'elastic',
			openSpeed:	400,
			closeSpeed:	250,
			nextEffect:	'fade',
			prevEffect:	'fade',
			loop:		false,
			helpers:{
				title:	{ type:'inside' },
				buttons:{}
			}
		};

		$('#project-tcat').click(function(){
			$.fancybox([
				{href : 'img/projects/tcat-home.jpg', title : 'TCAT Bus Company: website design with WebTree CMS, trip planner integration. (www.tcatbus.com)'}
			], fbOpt);
			return false;
		});

		$('#project-fly').click(function(){
			$.fancybox([
				{href : 'img/projects/fly-home.jpg', title : 'Ithaca / Tompkins County Regional Airport: website design with WebTree CMS, trip planner integration. (www.flyithaca.com)'}
			], fbOpt);
			return false;
		});

		$('#project-pp').click(function(){
			$.fancybox(proj_pp, fbOpt);
			return false;
		});

		$('#project-cu').click(function(){
			$.fancybox(proj_cu, fbOpt);
			return false;
		});

		$('#project-ic').click(function(){
			$.fancybox(proj_ic, fbOpt);
			return false;
		});

		$('#project-car').click(function(){
			$.fancybox(proj_car, fbOpt);
			return false;
		});

		$('#project-hangar').click(function(){
			$.fancybox(proj_hangar, fbOpt);
			return false;
		});

		$('#project-ib2').click(function(){
			$.fancybox(proj_ib2, fbOpt);
			return false;
		});

		$('#project-ib').click(function(){
			$.fancybox(proj_ib, fbOpt);
			return false;
		});

		$('#project-gf').click(function(){
			$.fancybox(proj_gf, fbOpt);
			return false;
		});

		$('#project-jm').click(function(){
			$.fancybox(proj_jm, fbOpt);
			return false;
		});

		$('#project-gg').click(function(){
			$.fancybox(proj_gg, fbOpt);
			return false;
		});

		$('#project-spider').click(function(){
			$.fancybox(proj_spider, fbOpt);
			return false;
		});

		$('#project-ofh').click(function(){
			$.fancybox(proj_ofh, fbOpt);
			return false;
		});
	}
	
	
	//Reveal webcam text if image is loaded
	$('img.webcamimage').load(function() {
		$('.webcamtext, #live').fadeIn();
	});
  
    //Animate WSKG sash
  	$('#wskgsash')
  		.delay(1500)
  		.animate({top: 36}, 2000, 'easeOutBounce')
  		.delay(5000)
  		.animate({top: -200}, 500);

   	//Page scrolling
	$('.pagelink').click(function(e){
   	 	e.preventDefault();
   	 	var target = ($(this).attr('id')) ? '#'+$(this).attr('id').replace('link', '') : '#container';
   	 	var time = ($(this).attr('id')) ? 400 : 1000;
   	 	var to = $(target).offset().top - 20;
   	 	
   	 	console.log(target);
   	 	
   	 	$('html, body').animate({ scrollTop: to }, time);
   	});
   	
   	// Contact form submission
   	$('#contactform').submit(function(e) {
   	    e.preventDefault();
   	    
   	    var form = this;
        var notifications = $('.notifications', this).empty();
        
        // Submit the newsletter form with values from
        // the submitted contact form.
        var newsletterSubmit = function() {
            var action = $('#maillistform').attr('action') + '?callback=?';
            var data = $(form).serialize(); 

            $.getJSON(action, data, function(resp) {
                // Do we care if this succeeds?
            });
        };
        
        $(notifications).append('<li>Sending...</li>');
        
        $('input[name=fullname], input[name=email], input[name=phone]', form).each(function() {
            if ($(this).val() === $(this).attr('placeholder')) {
                $(this).val('');
            }
        });
        
        $.ajax({
            url: '/form.php',
            type: 'POST',
            cache: false,
            data: $(this).serialize(),
            success: function(resp) {
                if (resp.errors) {
                    $(notifications).empty();
                    for (var i = 0; i < resp.errors.length; i++) {
                        $(notifications).append('<li>' + resp.errors[i] + '</li>');
                    }
                } else {
                    if ($('input[name=newsletter]', form).attr('checked')) {
                        newsletterSubmit();
                    }
                    $('input[name=fullname], input[name=email], input[name=phone]', form).each(function(input) {
                        $(this).val($(this).attr('placeholder'));
                    });
                    $(notifications).empty().append('<li>Sent successfully!</li>');
                }
            },
            error: function() {
                $(notifications).append('<li>Error submitting form.</li>');
            }
        });
    });
   	 
   	if(!Modernizr.input.placeholder){
   	    $('input[placeholder]').each(function(i, el){
            $(el)
                .val( $(el).attr('placeholder') )
                .focus(function(){
                    if($(this).val() == $(this).attr('placeholder')) $(this).val('');    
                })
                .blur(function() {
                    if ($(this).val() === '') $(this).val($(this).attr('placeholder'));
                });
        });
   	} 
   	 
});
   
