$(function() {

    var animation_parent = $('.intro_animation');
	// Header Banner Animation
    if( animation_parent.length ) {
        var frames   = animation_parent.find('.frame'),
            timeline = $('.intro_timeline img').width(0).height(8),
            chapters = $('.animation_chapters li');
        
        frames.find('.summary').hide();

        var currently_showing = 0,
            tick = function() {
                var w = timeline.width();

                if( w >= 940 ) {
                    if( ticker == null ) {
                        ticker = setTimeout(function() {
                            if( timeline.width() == 940 )
                                timeline.width(0)

                            ticker = null;
                        }, 3000);
                    }

                    return;
                }

                timeline.width( timeline.width()+1 );
                w++;

                var frame_index = Math.floor( w / (940 / 4) );
                frame_index = Math.min(frame_index, 3);

                if( currently_showing != frame_index ) {
                    $(chapters).eq( currently_showing ).removeClass('active');
                    $(chapters).eq( frame_index ).addClass('active');
                    
                    $(frames).eq( frame_index ).fadeIn();
                    $(frames).eq( currently_showing ).fadeOut()
                             .find('.summary').hide();

                    currently_showing = frame_index;
                } else {
                    var off = w - ((940 / 4) * currently_showing);
                    if( off == 40 )
                        $(frames).eq(currently_showing)
                                 .find('.summary').fadeIn(1000);
                }

            },
            ticker = null;

        setInterval(tick, 25);

        chapters.mouseenter(function() {
            var off = $(this).prevAll().length;

            timeline.width( off * (940 / 4) );
        });
    }


    var twitter_feed = $('.twitter_feed');
    if( twitter_feed.length ) {
        var url = 'http://twitter.com/statuses/user_timeline.json';
            url+= '?count=5&id=duncanwoodallum&callback=?';

        var tp = twitter_feed.children().eq(1);

        tp.css('margin', 0).css('font-size', '11px');
        var tweet_url = 'http://twitter.com/#!/duncanwoodallum/statuses/';
        $.getJSON(url, function(data) {
            var showing = -1,
                tick = function() {
                    showing++;
                    showing = showing % data.length;
                    
                    var t = data[showing],
                        a = $('<a>').text(t.text)
                                    .attr('href', tweet_url + t.id_str )
                                    .attr('target', '_blank');

                    tp.fadeOut(function() {
                        tp.html( a ).fadeIn();
                    });
                };
                tick();
                setInterval(tick, 7000);
        });
    }
	// Homepage scroller
    var client_scroller = $('#client_scroller').each(function() {

        var showing = 0,
            logos = $('#client_scroller img'),
			cur = 0,
			length,
			total;
			
		//	length = logos.width();
			//console.log(length);
        var tick = function() {
				// break;
				var length = ((logos.width() - 2)) * 165;
				total = logos.width();

				client_scroller.animate({
                    //scrollLeft: '+=181'
					left: '-=165'
                });
				var curLeft = client_scroller.css('left');
				$tmp = curLeft.split("px");
				curLeft = ($tmp[0] * -1);
				cur = curLeft;
				if((cur + 340) >= total) {
					ticker = setTimeout(startA,3600);
				}
				else
				{
					ticker = setTimeout(tick, 3600);
				}
                
            },
            ticker = setTimeout(tick, 3600);
		var startA = function() {
			cur = cur + 181;
			client_scroller.animate({
				left:'+=' + (total - 165)
			},4000,function() {
				ticker = setTimeout(tick, 3600);
			});
		}
        $(this).parent().prev().click(function() {
            window.clearTimeout(ticker);
			
            client_scroller.parent().animate({
                scrollLeft: '-=181'
            }, function() {
                ticker = setTimeout(tick, 3600);
            });
            return false;
        });
        $(this).parent().next().click(function() {
            window.clearTimeout(ticker);
            tick();
            return false;
        });
    });

    $('.news').each(function() {
        var $h2 = $(this).find('.scroll h3'),
            $p  = $(this).find('.scroll p'),
            $pages = $(this).find('.small_pagination a');
        
        var trim = $(this).is('.col_1');

        $pages.hide();
        $.getJSON('/rss.php?callback=?', function(data) {
            $pages.show();
            var currently_showing = -1,
                tick = function() {
                    currently_showing++;
                    currently_showing = currently_showing % data.length;

                    $pages.removeClass('active')
                          .eq(currently_showing).addClass('active');
                    
                    if( trim )
                        data[currently_showing].text = data[currently_showing].short_text;

                    $h2.html( data[currently_showing].title );
                    $p.html( data[currently_showing].text );

                    ticker = setTimeout(tick, 5000);
                },
                ticker = null;

            tick();

            $pages.click(function() {
                window.clearTimeout(ticker);
                currently_showing = $(this).prevAll().length - 1;
                tick();
                ticker = setTimeout(tick, 5000);
                return false;
            });
        });
    });

    // $('.content').each(function() {
    //     $(this).find('*').each(function() {
    // 
    //         if( $(this).is('img, br, input, textarea') )
    //             return;
    // 
    //         if( $(this).text() == '' )
    //             $(this).remove();
    //     });
    // });

	//SEARCH FUNCTIONALITY


});

