$(document).ready(function () {

	Cufon.now();
	Cufon.replace('h1');
	Cufon.replace('h4, h3');// Works without a selector engine
	Cufon.replace('#main_nav');
	Cufon.replace('.makeitbigger');
	Cufon.replace('ul#process_nav li');
	
	//get the Tumblr RSS feed
	
	//console.log(window.innerWidth);
	
	var newsFetch = setTimeout("getNews()", 2100);
	
	$('#expand_tab').click(function() {
		var targetTop = Math.floor($(this).offset().top);
		$('html,body').animate({scrollTop: targetTop}, 1000,'easeOutQuad', function() {
			$('.project_images').slideDown(750, 'easeInOutQuad');
		});
		
	});
	
	$('#work_close').click(function() {
		$('.project_images').slideUp(750, 'easeInOutQuad');
	});
	
	/*PROCESS SLIDER*/
	
	$('#process_nav li a').click(function(evt) {
		evt.preventDefault();
		$('#process_nav').children().removeClass('selected');
		var btn = $(this).attr('href');
		//console.log(btn);
		updateProcess(btn);
		$(this).parent().addClass('selected');
		Cufon.refresh();
	});
	
	if(is_ipad) {
		$('#process_slide_wrapper').hide();
	} else {
		$('#process_slider').draggable({ axis: "x",  scroll: false, snap: true, containment: [180,0,940,0], stop: customSnap});
	}
	
	$.YQL("select * from weather.forecast where location=32801",function(data){
            var w=data.query.results.channel;
            $('#temp').html(w.item.condition.temp+"&deg;");
            var hi = w.item.forecast[0].high;
            var low = w.item.forecast[0].low;
            $('#hilo').html("Hi: " + hi + "&deg;<br> Low: " + low + "&deg;");
            var imgurl = "http://l.yimg.com/a/i/us/nws/weather/gr/"+w.item.condition.code+"d.png";
            $('#weather_icon').html('<img src="'+ imgurl +'">');
            $('#weather_loc').html('Current Conditions: <p id="city">' + w.location.city + ', ' + w.location.region + '</p>');
            //console.log("weather fetched");
            timer = setInterval("updateWeather()", 300000);
       });
	
});

function getNews() {
	$.getJSON("http://redfishdigital.tumblr.com/api/read/json?callback=?&num=1", function(data) {
	var post = data.posts[0];
	//console.log(post);
	$('#headline').html(post['link-text']);
	var postDate = post.date.substring(0,16);
	//console.log(postDate);
	$('.newscontent').append('<span class="smalldate">' + postDate+ '</span><br><br>' + post['link-description'] + '<div class="linkto"><a href="http://redfishdigital.tumblr.com">Visit Blog</a></div>');
	}//end success
	);
}

function updateProcess(process) {
	switch(process) {
		case "#discovery":
		$('.detailslider').animate({left : '0px'}, 750, 'easeInOutQuad');
		$('#process_slider').animate({left : '45px'}, 750, 'easeInOutQuad');
		break;
		case "#strategy":
		$('.detailslider').animate({left : 940 * -1+'px'}, 750, 'easeInOutQuad');
		$('#process_slider').animate({left : '233px'}, 750, 'easeInOutQuad');
		break;
		case "#concept":
		$('.detailslider').animate({left : 940 * -2+'px'}, 750, 'easeInOutQuad');
		$('#process_slider').animate({left : '422px'}, 750, 'easeInOutQuad');
		break;
		case "#development":
		$('.detailslider').animate({left : 940 * -3+'px'}, 750, 
		'easeInOutQuad');
		$('#process_slider').animate({left : '610px'}, 750, 'easeInOutQuad');
		break;
		case "#keep-it-real":
		$('.detailslider').animate({left : 940 * -4+'px'}, 750, 'easeInOutQuad');
		$('#process_slider').animate({left : '804px'}, 750, 'easeInOutQuad');
		break;
	}
}

function customSnap(evt) {
	var slider = evt.target;
	var xpos = $(evt.target).position().left + 46;
	$('li').removeClass('selected');
	if(xpos < 186) {
		updateProcess('#discovery');
		$('li a[href*=discovery]').parent().addClass('selected');
	}
	
	if(xpos > 186 && xpos < 372) {
		updateProcess('#strategy');
		$('li a[href*=strategy]').parent().addClass('selected');
	}
	
	if(xpos >= 372 && xpos < 558) {
		updateProcess('#concept');
		$('li a[href*=concept]').parent().addClass('selected');
	}
	
	if(xpos >= 559 && xpos < 744) {
		updateProcess('#development');
		$('li a[href*=development]').parent().addClass('selected');
	}
	if(xpos >= 745) {
		updateProcess('#keep-it-real');
		$('li a[href*=keep]').parent().addClass('selected');
	}
	Cufon.refresh();
}

$.YQL = function(query, callback) {
    	var encodedQuery = encodeURIComponent(query.toLowerCase()),
       	 url = 'http://query.yahooapis.com/v1/public/yql?q='
            + encodedQuery + '&format=json&callback=?';
    	$.getJSON(url, callback);
};

function updateWeather() {
	$.YQL("select * from weather.forecast where location=32801",function(data){
            var w=data.query.results.channel;
            $('#temp').html(w.item.condition.temp+"&deg;");
            var hi = w.item.forecast[0].high;
            var low = w.item.forecast[0].low;
            $('#hilo').html("Hi: " + hi + "<br> Low: " + low);
            var imgurl = "http://l.yimg.com/a/i/us/nws/weather/gr/"+w.item.condition.code+"d.png";
            $('#weather_icon').html('<img src="'+ imgurl +'">');
             $('#weather_loc').html('Current Conditions: <p id="city">' + w.location.city + ', ' + w.location.region + '</p>');
       });
}
