/**
 * Namespaced object. Encapsulating private methods, exposing these
 * as public via an object proxy.
 *
 * @author 		Zone
 * @email		info@zonecontent.com
 * @url 	    http://www.zonecontent.com/
 * @copyright 	Copyright (c) 2009, zonecontent.com. All rights reserved.
 * @version		0.0.1
 */

$(document).ready(function(){

	window.titan = function($) {

	var heroTimer;
	var blogHeroTimer;

    function initialiseHero() {

        $('#hero').addClass('java');

        $('#hero ul li:not(:first)').each(function() {
            $(this).addClass('inactive');
        });

        $('#hero ul li:not(:first) a img').each(function() {
            $(this).css('left', '972px');
        });

        $('#hero ul li:not(:first) div').children('span.background').css({
            'display': 'none'
        });

        $('#hero ul li:first div span.background').css('height', $('#hero ul li:first div').outerHeight());
        $('#hero ul li:first div span.background').css('border', '0');

        $('#hero ul li div').bind('click', clickHandlerHero);

		heroTimer = setInterval('window.titan.scrollHero()', 7000);

    }

    function clickHandlerHero() {

        if ($(this).parent().hasClass('inactive')) {

            $('#hero ul li div').unbind('click', clickHandlerHero);

            $('#hero ul li').each(function() {
                $(this).addClass('inactive');
                $(this).children('div').css({
                    'padding': '0px 30px 0px 13px',
                    'width': '167px'
                });
                $(this).find('span.background').css({
                    'display': 'none'
                });
            });

            $(this).parent().removeClass('inactive', 500);
            $(this).css({
                'padding': '15px 60px 15px 20px',
                'width': '183px'
            });

            $(this).children('span.background').css({
                'display': 'block'
            });

            $(this).children('span').css({
                'width': '263px'
            });

            $(this).children('span.background').css('height', $(this).outerHeight());
            $(this).siblings('a').children('img').animate({
                left: '0'
            }, 1000, function() {
                $('#hero ul li.inactive a img').each(function() {
                    $(this).css('left', '972px');
                    $('#hero ul li div').bind('click', clickHandlerHero);
                });
            });

            if ($.browser.msie) {
                $('#hero ul li.inactive div h3').each(function() {
                    var h3pad = ($(this).parent().height() - $(this).height()) / 2;
                    $(this).css('margin-top', h3pad + 'px');
                });
            }

			clearInterval(heroTimer);
			heroTimer = setInterval('window.titan.scrollHero()', 7000);

        }

    }

    function scrollHero() {

        if ($('#hero ul li:not(.inactive)').next().length === 0) {

            $('#hero ul li.inactive:first').children('div').click();

        } else {

            $('#hero ul li:not(.inactive)').next().children('div').click();

        }

    }

    function initialiseScroller() {

        $('#main div.panel div.scroller').addClass('scroller-background');
        $('#main div.panel div.scroller ul').addClass('java');

        var count = 0;

        if ($('#main div.panel div.scroller').hasClass('scroller_wide')) {
            $('#main div.panel div.scroller ul li:nth-child(4n)').addClass('book-padded');
            count = 4;
        } else {
            $('#main div.panel div.scroller ul li:nth-child(3n)').addClass('book-padded');
            count = 3;
        }

        $('#main div.panel div.scroller ul').each(function() {
            var sets = Math.ceil($(this).children().length / count);
            for (var i = 1; i <= sets; i++) {
                if (i == 1) {
                    $(this).parent().parent().siblings('h3').append('<a class="active"></a>');
                } else {
                    $(this).parent().parent().siblings('h3').append('<a></a>');
                }
            }
            $(this).parent().parent().siblings('h3').children('a').wrapAll('<span class="indicators" />');
        });

        $('#main div.panel div.scroller ul').before('<span class="scroll-button left"></span>');
        $('#main div.panel div.scroller ul').after('<span class="scroll-button right active"></span>');

        $('#main div.panel h3 span.indicators a').bind('click', clickHandlerScrollerIndicators);
        $('#main div.panel div.scroller span.scroll-button').bind('click', clickHandlerScroller);

    }

    function clickHandlerScrollerIndicators() {

        if ($(this).hasClass('active') === false) {

            var count = 0;

            if ($(this).parent().parent().parent().find('div.scroller').hasClass('scroller_wide')) {
                count = 4;
            } else {
                count = 3;
            }

            $('#main div.panel h3 span.indicators a').unbind('click', clickHandlerScrollerIndicators);

            var index = $(this).index();
            var position = $(this).parent().parent().parent().find('li:eq(' + index * count + ')').position().left - 43;
            var panel = $(this).parent().parent().parent();

            $(this).parent().parent().parent().find('li').animate({
                left: '-=' + position + 'px'
            }, 1000, function() {
                $('#main div.panel h3 span.indicators a').bind('click', clickHandlerScrollerIndicators);
            });

            handleClassesScroller(panel, index);

        }

    }

    function clickHandlerScroller() {

        if ($(this).hasClass('active')) {

            var index = 0;

            if ($(this).hasClass('left')) {
                index = $(this).parent().parent().siblings('h3').children('span.indicators').children('a.active').index() - 1;
            } else {
                index = $(this).parent().parent().siblings('h3').children('span.indicators').children('a.active').index() + 1;
            }

            $(this).parent().parent().siblings('h3').children('span.indicators').children('a:eq(' + index + ')').click();

        }

    }

    function handleClassesScroller(panel, index) {

        $('h3 span.indicators a', panel).removeClass('active');
        $('h3 span.indicators a:eq(' + index + ')', panel).addClass('active');

        if ($('h3 span.indicators a.active', panel).prev().length === 0) {
            $('div.scroller span.left', panel).removeClass('active');
        } else {
            $('div.scroller span.left', panel).addClass('active');
        }

        if ($('h3 span.indicators a.active', panel).next().length === 0) {
            $('div.scroller span.right', panel).removeClass('active');
        } else {
            $('div.scroller span.right', panel).addClass('active');
        }

    }

    function initialiseBookshelf() {

        $('#bookshelf div.scroller').before('<a class="prevPage browse left"></a>');
        $('#bookshelf div.scroller').after('<a class="nextPage browse right"></a>');

		$('#bookshelf div.scroller').before('<span class="corner-left"></span>');
        $('#bookshelf div.scroller').before('<span class="corner-right"></span>');

        $('#bookshelf div.scroller ul li a.description span.content').before('<span class="top"></span>');
        $('#bookshelf div.scroller ul li a.description span.content').after('<span class="bottom"></span>');

        if ($('#bookshelf div.scroller ul').length == 1) {
            if ($('#bookshelf div.scroller ul li').length < 8) {
                while ($('#bookshelf div.scroller ul li').length < 8) {
                    $('#bookshelf div.scroller ul li').each(function() {
                        var li = $(this).clone();
                        $('#bookshelf div.scroller ul').append(li);
                    });
                }
            }
        }

        $('#bookshelf div.scroller ul li:last').prependTo('#bookshelf div.scroller ul');

        $('#bookshelf div.content a.browse').bind('click', clickHandlerBookshelf);

    }

    function clickHandlerBookshelf() {

        $('#bookshelf div.content a.browse').unbind('click', clickHandlerBookshelf);

        if ($(this).hasClass('left')) {

            $('#bookshelf div.scroller ul li:last').prependTo('#bookshelf div.scroller ul');
            $('#bookshelf div.scroller ul li:last').prependTo('#bookshelf div.scroller ul');
            $('#bookshelf div.scroller ul li:last').prependTo('#bookshelf div.scroller ul');

            $('#bookshelf div.scroller ul').animate({
                left: '-=837px'
            }, 0, function() {
            });

            $('#bookshelf div.scroller ul').animate({
                left: '+=837px'
            }, 1000, function() {
                $('#bookshelf div.content a.browse').bind('click', clickHandlerBookshelf);
            });

        } else {

            $('#bookshelf div.scroller ul').animate({
                left: '-=837px'
            }, 1000, function() {
                $('#bookshelf div.scroller ul li:first').appendTo('#bookshelf div.scroller ul');
                $('#bookshelf div.scroller ul li:first').appendTo('#bookshelf div.scroller ul');
                $('#bookshelf div.scroller ul li:first').appendTo('#bookshelf div.scroller ul');

                $('#bookshelf div.scroller ul ').animate({
                    left: '+=837px'
                }, 0, function() {
                    $('#bookshelf div.content a.browse').bind('click', clickHandlerBookshelf);
                });
            });

        }

    }

    function initialiseBlogHero() {

        $('#blog-hero ul li a.thumb:first').addClass('active');

        $('#blog-hero ul li a.thumb:not(.active)').siblings('div.content').each(function() {
            $(this).css('display', 'none');
        });

        $('#blog-hero ul li a.thumb').bind('click', clickHandlerBlogHero);

		blogHeroTimer = setInterval('window.titan.scrollBlogHero()', 5000);

    }

    function clickHandlerBlogHero() {

        $('#blog-hero ul li div.content').each(function() {
            $(this).css('display', 'none');
        });

        $(this).siblings('div.content').css('display', 'block');

        $('#blog-hero ul li a.thumb').each(function() {
            $(this).removeClass('active');
            $(this).css('background-position', 'left bottom');
        });

        $(this).addClass('active');

        $(this).css('background-position', 'left top');

		clearInterval(blogHeroTimer);
		blogHeroTimer = setInterval('window.titan.scrollBlogHero()', 5000);

        return false;

    }

	function scrollBlogHero() {

		var active = $('#blog-hero ul li a.active');

        if (active.hasClass('last')) {

            $('#blog-hero ul li a.first').click();

        } else {

            $('#blog-hero ul li a.active').parent().next().children('a').click();

        }

    }

    return {
        // Public variables / methods
        'init': function() {
            initialiseHero();
            initialiseScroller();
            initialiseBookshelf();
            initialiseBlogHero();
        },

        'scrollHero': scrollHero,
		'scrollBlogHero': scrollBlogHero
    };

} (jQuery);

	$('a.video_player').each(function(){

		$(this).empty();

		flowplayer(this,"/static/flowplayer/flowplayer.commercial-3.2.2.swf",{
		    key: '#$cea6a583caae2b713aa',
			clip: {
				autoPlay: false,
				scaling: 'orig',
		        onStart: function(clip) {
					//_gaq.push(['_trackEvent', 'Video', 'Play', clip.url]);
		        },

		        // track finish event for this clip
		        onFinish: function(clip) {
		            //_gaq.push(['_trackEvent', 'Video', 'Finish', clip.url]);
		        }
			},
			canvas: {
		    	backgroundColor: '#000000'
			}
		});
	});

	$('a.audio_player').each(function(){

		$(this).empty();

		flowplayer(this,"/static/flowplayer/flowplayer.commercial-3.2.2.swf",{
		    key: '#$cea6a583caae2b713aa',
			checkPolicyFile: true,
		    key: '#$cea6a583caae2b713aa',
		    plugins: {
		        audio: {
		            url: 'flowplayer.audio-3.2.0.swf'
		        },
		        controls: {
		            fullscreen: false,
		            height: 30,
					autoHide: false
		        }
		    },
			clip: {
				autoPlay: false,
		        onStart: function(clip) {
					_gaq.push(['_trackEvent', 'Audio', 'Play', clip.url]);
		        },

		        // track finish event for this clip
		        onFinish: function(clip) {
		            _gaq.push(['_trackEvent', 'Video', 'Finish', clip.url]);
		        }
			},
			canvas: {
		    	backgroundColor: '#000000'
		    }});

	});


	window.titan.init();

});
