/* Author: 

*/
$(function() {

    var App = {
        init: function() {

            App.Home.init();
            App.Search.init();
            App.Forms.init();
            App.Youtube.init();
            App.Twitter.init();
            App.TV.init();

            $('div.overflow-list').overflowList();
        }
    }

    App.Search = {
        init: function() {

            var $searchBox = $('#search-box');
            $searchBox.hide();

            // Set placeholder of textbox so it can be picked up in App.Form.setFieldDefaults
            $searchBox.find('input.textbox').attr('placeholder', function() {
                return $(this).siblings('label').text();
            });

            $searchBox.before('<a href="" id="search-toggle"><span>Search</span></a>');
            $('a#search-toggle').click(function() {
                App.Search.toggle();
                return false;
            });

        },
        toggle: function() {
            log('clicked');
            var $searchBox = $('#search-box');
            if ($searchBox.is(':visible')) {
                $searchBox.hide();
            } else {
                $searchBox.show();
            }
        }
    };

    App.Forms = {
        init: function() {
            App.Forms.setFieldDefaults();
        },
        setFieldDefaults: function() {

            $('[placeholder]').focus(function() {
                var input = $(this);
                if (input.val() == input.attr('placeholder')) {
                    input.val('');
                    input.removeClass('placeholder');
                }
            }).blur(function() {
                var input = $(this);
                if (input.val() == '' || input.val() == input.attr('placeholder')) {
                    input.addClass('placeholder');
                    input.val(input.attr('placeholder'));
                }
            }).blur();

            $('[placeholder]').parents('form').submit(function() {
                $(this).find('[placeholder]').each(function() {
                    var input = $(this);
                    if (input.val() == input.attr('placeholder')) {
                        input.val('');
                    }
                })
            });
        }
    };

    App.Home = {
        init: function() {
            var hashPrefix = 'item-',
                    selClass = 'selected',
                    $items = $('nav#gallery-nav ul li');

            if ($items.length > 0 && $('body.index').length > 0) {

                $('a', $items).attr('href', function() {
                    return $(this).attr('href').replace('#', '#' + hashPrefix);
                }).click(function(e) {
                    $('div#header-gallery-list').data('playing', false);
                    $items.removeClass(selClass);
                    $(this).parent().addClass(selClass);
                });

                $('div#header-gallery-list').gallery({
                    'delayTime': 9000,
                    'hashPrefix': hashPrefix,
                    'hashChange': function(newHash) {
                        $items.removeClass(selClass);
                        $('a[href="' + newHash + '"]', $items).parent().addClass(selClass);
                    }
                });

                if (window.location.hash !== '') {
                    $('a[href="' + window.location.hash + '"]', $items).parent().addClass(selClass);
                } else {
                    $($items[0]).addClass(selClass);
                }

            }
        }
    };

    App.Youtube = {
        init: function() {
            if ($.jTube !== undefined) {
                var $youtube = $('div#hot-blogs');
                if ($youtube.length > 0) {
                    $.jTube({
                        user: 'SELcic',
                        userType: 'favorites',
                        limit: 3,
                        page: 1,
                        success: function(videos) {
                            var html = '';
                            for (var i = 0; i < videos.length; i++) {
                                html += '<div class="item"><a href="' + videos[i].link + '" target="_blank"><img src="' + videos[i].thumbnail + '" width="106" alt="' + videos[i].title + '" title="' + videos[i].title + '"></a><h4>' + videos[i].title + '</h4><div class="buttons"><a href="' + videos[i].link + '" target="_blank" title="' + videos[i].title + '" class="button button-watch">Watch the video</a></div></div>';
                            }
                            $youtube.append(html);
                        },
                        error: function(error) {
                            log(error);
                        }
                    });
                }
            }
        }
    }
    
    App.Twitter = {
        init: function() {
            var interval = 6000;
            var fadeTime = 300;
            if (window.twitterlib !== undefined) {
                var $seltweets = $('div.twitter-box.sel');
                if ($seltweets.length > 0) {
                    twitterlib.timeline('SocEntLdn', { limit: 5 }, function(tweets, options) {
                        if (tweets.length) {
                            function hideTweet() {
                                next = curr + 1 == $tweets.length ? 0 : curr + 1;
                                $tweets.hide();
                                $($tweets[next]).show();
                                curr = next;
                            }
                            var html = '';
                            for (var i = 0; i < tweets.length; i++) {
                                html += twitterlib.render(tweets[i]);
                            }
                            $('div.tweets', $seltweets).html(html);
                            var curr = 1,
                                $tweets = $('div.twitter-box.sel div div.hentry');
                            $tweets.hide();
                            $($tweets[1]).show();
                            var selInt = setInterval(hideTweet, interval);

                            $seltweets.hover(function() {
                                clearInterval(selInt);
                                $('div.tweets', $seltweets).addClass('hover');
                            }, function() {
                                selInt = setInterval(hideTweet, interval);
                                $('div.tweets', $seltweets).removeClass('hover');
                            });
                            
                        } else {
                            $seltweets.hide();
                        }
                    });
                }

                var $selsearch = $('div.twitter-box.socent');
                if ($selsearch.length > 0) {
                    twitterlib.search('#Socent', { limit: 5 }, function(tweets, options) {
                        if (tweets.length) {
                            var html = '';
                            function hideTweet() {
                                next = curr + 1 == $tweets.length ? 0 : curr + 1;
                                $tweets.hide();
                                $($tweets[next]).show();
                                curr = next;
                            }
                            for (var i = 0; i < tweets.length; i++) {
                                html += '<div>' + twitterlib.render(tweets[i]) + '</div>';
                            }
                            $('div.tweets', $selsearch).html(html);
                            var curr = 1,
                                $tweets = $('div.twitter-box.socent div div.hentry');
                            $tweets.hide();
                            $($tweets[1]).show();
                            var socInt = setInterval(hideTweet, interval);
                            
                            $selsearch.hover(function() {
                                clearInterval(socInt);
                                $('div.tweets', $selsearch).addClass('hover');
                            }, function() {
                                socInt = setInterval(hideTweet, interval);
                                $('div.tweets', $selsearch).removeClass('hover');
                            });
                            
                        } else {
                            $selsearch.parent().hide();
                        }
                    });
                }


            }
        }
    }

    App.TV = {
        init: function() {

            var $items = $('div#home-orange-tv div.tv-images img');
            var imageIndex = 0;

            function animateToPhoto(next, $collection, callback) {

                var $next = $($collection[next]);

                $next.hide();

                $collection.each(function(i) {

                    var $this = $(this);

                    $this.css('z-index', +($this.css('z-index')) - 1);

                });

                $next.css('z-index', $collection.length).show(function() {

                    callback.call(this, next);

                });
            }

            if ($items.length > 1) {

                $items.filter(':first').imagesLoaded(function(e) {

                    setTimeout(function() {

                        animateToPhoto(imageIndex, $items, function doNext(next) {

                            imageIndex = next + 1;

                            if (imageIndex > $items.length - 1) {

                                imageIndex = 0;

                            }

                            setTimeout(function() {

                                animateToPhoto(imageIndex, $items, doNext);

                            }, 100);

                        });

                    }, 100);

                });

            }
        }
    }

    App.init();

});
