// wait until document is fully scriptable
$(document).ready(function(){

        $('input#age_input').focus();

        $('input').keydown(function(e){
            if (e.keyCode == 13) {
                $('#enter_input').click();
                return false;
            }
        });

        initScroll();
        initTabs();
        initLegal();

        //$('#present-screens-thumbs a').lightBox();
        $('div#present-concept-display a').lightBox();
        $('div#present-screen-display a').lightBox();
        $('ul#present-screens-thumbs li a').click(onScreenThumbClicked);
        $('ul#present-concept-thumbs li a').click(onConceptThumbClicked);
        $('ul#present-videos-thumbs li a').click(onVideosThumbClicked);
        $('ul#wallpapers-thumbs li a').click(onWallpaperThumbClicked);
});

function onScreenThumbClicked()
{
	var src = $(this).attr('href');
        //$('#present-screen-display').html('<img src="' + src + '" alt="screenshot">');
        $('#present-screen-display a.showing').removeAttr('class');
        $('#present-screen-display a' + src).attr('class', 'showing');
        $('ul#present-screens-thumbs li a.active').removeAttr('class');
        $(this).attr('class', 'active');
	return false;
}

function onConceptThumbClicked()
{
	var src = $(this).attr('href');
        //$('#present-concept-display').html('<img src="' + src + '" alt="screenshot">');
        $('#present-concept-display a.showing').removeAttr('class');
        $('#present-concept-display a' + src).attr('class', 'showing');
        $('ul#present-concept-thumbs li a.active').removeAttr('class');
        $(this).attr('class', 'active');
	return false;
}

function onVideosThumbClicked()
{
	var src = $(this).attr('href');

        var agent = navigator.userAgent;

        if (agent.indexOf("iPhone") > 0)
        {
            //document.location.replace("MyMovie.mp4");
            window.location = "/home/iphone/" + src; // iPhones play MP4 at full screen
        }

        $('ul#present-videos-thumbs li a.active').removeAttr('class');
        $(this).attr('class', 'active');

        if ($.hasFlashPlayer)
        {
            var d = new Date();

            $('#present-videos-display').flash({
                    id: 'movie-player',
                    swf: 'http://2010.volatilegames.com/swf/main.swf?ck='+d.getTime(),
                    width: 445,
                    height: 241,
                    params: {
                            bgcolor: '#000000',
                            wmode: 'transparent',
                            menu: 'false',
                            AllowScriptAccess: 'always',
                            AllowFullScreen: 'true',
                            flashvars: {
                                            movieURL: src
                                            /*  e: 0,
                                            b1u: 1 */
                            }
                    }
            });
        }
        else
        {
            $('#present-videos-display').html('<a href="http://get.adobe.com/flashplayer/" title="Get Flash Player"><img src="/gfx/present/no-flash.jpg" alt="No flash player installed"></a>');
        }

	return false;
}

function onWallpaperThumbClicked()
{
	var src = $(this).attr('href');
        var name = $(this).attr('id');
        $('#wallpapers-display').html('<img src="' + src + '" alt="screenshot">');
        $('ul#wallpapers-thumbs li a.active').removeAttr('class');
        $(this).attr('class', 'active');

        // UPDATE DIMENSION LINKS
        $('#wallpaper-dimensions li#ten24 a').attr('href', '/downloads/wallpapers/' + name + '_800x600.jpg');
        $('#wallpaper-dimensions li#twelve80 a').attr('href', '/downloads/wallpapers/' + name + '_1024x768.jpg');
        $('#wallpaper-dimensions li#forteen40 a').attr('href', '/downloads/wallpapers/' + name + '_1280x960.jpg');
        $('#wallpaper-dimensions li#sixteen100 a').attr('href', '/downloads/wallpapers/' + name + '_1600x1200.jpg');

	return false;
}

function initScroll()
{
	$('ul#present-screen-thumbs-control li a').click(onScrollClickedScreens);
        screen_curr_col = 1;
        $('#screens-thumb-prev').hide();

        $('ul#present-concept-thumbs-control li a').click(onScrollClickedConcept);
        concept_curr_col = 1;
        $('#concept-thumb-prev').hide();

        $('ul#present-videos-thumbs-control li a').click(onScrollClickedVideos);
        videos_curr_col = 1;
        $('#videos-thumb-prev').hide();
        $('#videos-thumb-next').hide();

        $('ul#wallpapers-thumbs-control li a').click(onScrollClickedWallpapers);
        wallpapers_curr_col = 1;
        $('#wallpapers-thumb-prev').hide();

        $('ul#past-present-nav li a').click(onScrollClicked);
        current_pane = 2;
}

function onScrollClicked()
{
        var sign, next_pos, dir;
	var v_jump = 1440;
	var curr_pos = parseInt($('#flowpanes .items').css('left').match(/[+-]?\d+/));
	var cols = 3; // PAST PRESENT FUTURE
	var min_col = 1;
	var max_col = 3;

        var tab_id = $(this).attr('id');

        // HIDE VIDEO TO FIX IE ISSUES
        var active_tab_id = $('#present-menu li a.active').attr('id');
        if (active_tab_id == 'tab_present-videos' && (tab_id == 'btn_past' || tab_id == 'btn_future'))
        {
            $('#present-videos-display').html('<img src="/gfx/present/video_instructions.jpg" alt="screenshot"/>');
        }
        // Alternative at end of function.

        if (tab_id == 'btn_past')
        {
            dir = 'left';
        }
        else if (tab_id == 'btn_future')
        {
            dir = 'right';
        }

        if (dir == 'right' && current_pane < max_col)
	{
                sign = '-';
		next_pos = curr_pos - v_jump;
		current_pane++;
	}
        if (dir == 'left' && current_pane > min_col)
	{
                sign = '+';
		next_pos = curr_pos - v_jump;
		current_pane--;
	}

        if (dir == 'right' && current_pane == 3)
        {
            $('#future-projects-bg').fadeIn(400);
            $('#future-projects-black').fadeIn(400);
            $('#present').animate({'opacity':0});
            $('#present-menu').animate({'opacity':0});
        }
        if (dir == 'left' && current_pane == 2)
        {
            $('#future-projects-bg').fadeOut(400);
            $('#future-projects-black').fadeOut(400);
            $('#present').animate({'opacity':1});
            $('#present-menu').animate({'opacity':1});
        }

        if (dir == 'right' && current_pane == 2)
        {
            $('#past-projects-res-dogs-white').fadeOut(400);
            $('div#past-projects-image').fadeOut(400);
            $('#present').animate({'opacity':1});
            $('#present-menu').animate({'opacity':1});
        }
        if (dir == 'left' && current_pane == 1)
        {
            $('#past-projects-res-dogs-white').fadeIn(400);
            $('div#past-projects-image').fadeIn(400);
            $('#present').animate({'opacity':0});
            $('#present-menu').animate({'opacity':0});
        }

	$('#flowpanes .items').animate({left: sign + '=' + v_jump + 'px'}, 400);

        check_open_legals(current_pane);
        update_nav_buttons(current_pane);
        update_nav_span(current_pane);
        update_footer_nav(current_pane);
        update_legal_buttons(current_pane);
}

function update_legal_buttons(current_pane)
{
        if (current_pane == 3)
	{
		$('ul#legal-menu').fadeOut(400);
                $('ul#past-legal-menu').fadeOut(400);
	}
	if (current_pane == 2)
	{
		$('ul#legal-menu').fadeIn(400);
                $('ul#past-legal-menu').fadeOut(400);
	}
        if (current_pane == 1)
	{
		$('ul#legal-menu').fadeOut(400);
                $('ul#past-legal-menu').fadeIn(400);
	}
}

function update_nav_buttons(current_pane)
{
        if (current_pane != 3)
	{
		$('ul#past-present-nav li a#btn_future').show();
	}
	if (current_pane != 1)
	{
		$('ul#past-present-nav li a#btn_past').show();
	}
        if (current_pane == 3)
	{
		$('ul#past-present-nav li a#btn_future').hide();
	}
	if (current_pane == 1)
	{
		$('ul#past-present-nav li a#btn_past').hide();
	}
}

function update_nav_span(current_pane)
{
        // UPDATE SPAN
        if (current_pane == 1)
        {
            $('ul#past-present-nav li a#btn_future span').removeAttr('class');
            $('ul#past-present-nav li a#btn_future span').attr('class', 'present-white');
        }
        else if (current_pane == 2)
        {
            $('ul#past-present-nav li a#btn_future span').removeAttr('class');
            $('ul#past-present-nav li a#btn_future span').attr('class', 'future');
            $('ul#past-present-nav li a#btn_past span').removeAttr('class');
            $('ul#past-present-nav li a#btn_past span').attr('class', 'past');
        }
        else if (current_pane == 3)
        {
            $('ul#past-present-nav li a#btn_past span').removeAttr('class');
            $('ul#past-present-nav li a#btn_past span').attr('class', 'present');
        }
}

function update_footer_nav(current_pane)
{
        // UPDATE FOOTBAR
        if (current_pane == 1)
        {
            $('ul#volatile-footbar-nav li a.active').removeAttr('class');
            $('ul#volatile-footbar-nav li#footbar-past a').attr('class', 'active');
        }
        else if (current_pane == 2)
        {
            $('ul#volatile-footbar-nav li a.active').removeAttr('class');
            $('ul#volatile-footbar-nav li#footbar-present a').attr('class', 'active');
        }
        else
        {
            $('ul#volatile-footbar-nav li a.active').removeAttr('class');
            $('ul#volatile-footbar-nav li#footbar-future a').attr('class', 'active');
        }
}

function onScrollClickedScreens()
{
        var sign, next_pos;
	var v_jump = 67 + 6 + 2;
	var curr_pos = parseInt($('#present-screens-thumbs').css('left').match(/[+-]?\d+/));
	var cols = Math.ceil($('#present-screens-thumbs li').length / 2);
	var min_col = 1;
	var max_col = cols - 3;
	var dir = $(this).attr('id').substr(9);

	if (dir == 'left' && screen_curr_col > 1)
	{
		sign = '+';
		next_pos = screen_curr_col + v_jump;
		screen_curr_col--;
	}
	else if (dir == 'right' && screen_curr_col < max_col)
	{
		sign = '-';
		next_pos = curr_pos - v_jump;
		screen_curr_col++;
	}

	if (screen_curr_col == 1)
	{
		$('#screens-thumb-prev').hide();
	}
	else
	{
		$('#screens-thumb-prev').show();
	}

	if (screen_curr_col == max_col)
	{
		$('#screens-thumb-next').hide();
	}
	else
	{
		$('#screens-thumb-next').show();
	}

	$('#present-screens-thumbs').animate({left: sign + '=' + v_jump + 'px'}, 222);

	return false;
}

function onScrollClickedConcept()
{
        var sign, next_pos;
	var v_jump = 67 + 6 + 2;
	var curr_pos = parseInt($('#present-concept-thumbs').css('left').match(/[+-]?\d+/));
	var cols = Math.ceil($('#present-concept-thumbs li').length / 2);
	var min_col = 1;
	var max_col = cols - 3;
	var dir = $(this).attr('id').substr(9);

	if (dir == 'left' && concept_curr_col > 1)
	{
		sign = '+';
		next_pos = concept_curr_col + v_jump;
		concept_curr_col--;
	}
	else if (dir == 'right' && concept_curr_col < max_col)
	{
		sign = '-';
		next_pos = curr_pos - v_jump;
		concept_curr_col++;
	}

	if (concept_curr_col == 1)
	{
		$('#concept-thumb-prev').hide();
	}
	else
	{
		$('#concept-thumb-prev').show();
	}

	if (concept_curr_col == max_col)
	{
		$('#concept-thumb-next').hide();
	}
	else
	{
		$('#concept-thumb-next').show();
	}

	$('#present-concept-thumbs').animate({left: sign + '=' + v_jump + 'px'}, 222);

	return false;
}

function onScrollClickedVideos()
{
        var sign, next_pos;
	var v_jump = 67 + 6 + 2;
	var curr_pos = parseInt($('#present-videos-thumbs').css('left').match(/[+-]?\d+/));
	var cols = Math.ceil($('#present-videos-thumbs li').length / 2);
	var min_col = 1;
	var max_col = cols - 3;
	var dir = $(this).attr('id').substr(8);

	if (dir == 'left' && videos_curr_col > 1)
	{
		sign = '+';
		next_pos = videos_curr_col + v_jump;
		videos_curr_col--;
	}
	else if (dir == 'right' && videos_curr_col < max_col)
	{
		sign = '-';
		next_pos = curr_pos - v_jump;
		videos_curr_col++;
	}

	if (videos_curr_col == 1)
	{
		$('#videos-thumb-prev').hide();
	}
	else
	{
		$('#videos-thumb-prev').show();
	}

	if (videos_curr_col == max_col)
	{
		$('#videos-thumb-next').hide();
	}
	else
	{
		$('#videos-thumb-next').show();
	}

	$('#present-videos-thumbs').animate({left: sign + '=' + v_jump + 'px'}, 222);

	return false;
}

function onScrollClickedWallpapers()
{
        var sign, next_pos;
	var v_jump = 67 + 6 + 2;
	var curr_pos = parseInt($('#wallpapers-thumbs').css('left').match(/[+-]?\d+/));
	var cols = Math.ceil($('#wallpapers-thumbs li').length);
	var min_col = 1;
	var max_col = cols - 3;
	var dir = $(this).attr('id').substr(12);

	if (dir == 'left' && wallpapers_curr_col > 1)
	{
		sign = '+';
		next_pos = wallpapers_curr_col + v_jump;
		wallpapers_curr_col--;
	}
	else if (dir == 'right' && wallpapers_curr_col < max_col)
	{
		sign = '-';
		next_pos = curr_pos - v_jump;
		wallpapers_curr_col++;
	}

	if (wallpapers_curr_col == 1)
	{
		$('#wallpapers-thumb-prev').hide();
	}
	else
	{
		$('#wallpapers-thumb-prev').show();
	}

	if (wallpapers_curr_col == max_col)
	{
		$('#wallpapers-thumb-next').hide();
	}
	else
	{
		$('#wallpapers-thumb-next').show();
	}

	$('#wallpapers-thumbs').animate({left: sign + '=' + v_jump + 'px'}, 222);

	return false;
}

function initTabs()
{
	$('#present-menu li a, #present-logo a').click(onTabClicked);
        $('#present-downloads-menu li a').click(onDownloadTabClicked);
        $('div.close_panel').click(onCloseClicked);
        $('ul#volatile-footbar-nav li a').click(onNavClicked);

        $('div#present-screens a#tab_present-videos').click(onVideosClick);
        $('div#present-videos a#tab_present-screens').click(onScreensClick);
}

function onScreensClick()
{
    $('#present-videos-display').html('<img src="/gfx/present/video_instructions.jpg" alt="screenshot"/>');
    $('#present-screens').fadeIn(400);
    $('#present-videos').fadeOut(400);
}
function onVideosClick()
{
    $('#present-screens').fadeOut(400);
    $('#present-videos').fadeIn(400);
}

function onTabClicked()
{
        $('#present-videos-display').html('<img src="/gfx/present/video_instructions.jpg" alt="screenshot"/>');
	var active_tab_id = $('#present-menu li a.active').attr('id');
	var tab_id = $(this).attr('id');

        if(active_tab_id != undefined)
        {
            var active_box_id = active_tab_id.replace('tab_', '');
        }
        var box_id = tab_id.replace('tab_', '');

        if (tab_id != 'tab_official-site')
        {
            if (tab_id == 'tab_present-home' || tab_id == 'tab_present-buy')
            {
                $('#present-black').fadeOut(400);
            }
            else
            {
                $('#present-black').fadeIn(400);
            }

            $('#'+active_tab_id).removeAttr('class');
            $('#'+tab_id).attr('class', 'active');

            if(active_tab_id != undefined && active_box_id != 'present-screens')
            {
                $('#'+active_box_id).fadeOut(400);
            }
            else
            {
                $('#present-screens').fadeOut(400);
                $('#present-videos').fadeOut(400);
            }

            $('#'+box_id).fadeIn(400);
        }
        else
        {
            window.open ("http://www.deadtorights.com","Dead to Rights");
        }

	return false;
}

function onDownloadTabClicked()
{
	var active_tab_id = $('#present-downloads-menu li a.active').attr('id');
	var tab_id = $(this).attr('id');

        if(active_tab_id != undefined)
        {
            var active_box_id = active_tab_id.replace('tab_', '');
        }
        var box_id = tab_id.replace('tab_', '');

	$('#'+active_tab_id).removeAttr('class');
	$('#'+tab_id).attr('class', 'active');

        if(active_tab_id != undefined)
        {
            //$('#' + active_box_id).fadeOut(400);
            $('#' + active_box_id).css('display', 'none');
        }

        $('#'+box_id).fadeIn(400);

	return false;
}

function onCloseClicked()
{
	var active_tab_id = $('#present-menu li a.active').attr('id');

        $('#present-black').fadeOut(400);

        if(active_tab_id != undefined)
        {
            var active_box_id = active_tab_id.replace('tab_', '');
        }

	$('#'+active_tab_id).removeAttr('class');

        if(active_tab_id != undefined && active_tab_id != 'tab_present-screens')
        {
            $('#'+active_box_id).fadeOut(400);
        }
        else
        {
            $('#present-screens').fadeOut(400);
            $('#present-videos').fadeOut(400);
        }

	return false;
}

function onNavClicked()
{
        var tab_id = $(this).attr('id').substr(3);
        var sign;
        var v_jump = 1440;
        var curr_pos = parseInt($('#flowpanes .items').css('left').match(/[+-]?\d+/));
        var next_pos;

        // HIDE VIDEO TO FIX IE ISSUES
        var active_tab_id = $('#present-menu li a.active').attr('id');
        if (active_tab_id == 'tab_present-videos' && (tab_id == 'past' || tab_id == 'future'))
        {
            $('#present-videos-display').html('<img src="/gfx/present/video_instructions.jpg" alt="screenshot"/>');
        }
        // Alternative at end of function.

        if (tab_id == 'past')
        {
            if (current_pane == 2)
            {
                sign = "+";
                next_pos = curr_pos - v_jump;
                $('#present').animate({'opacity':0});
                $('#present-menu').animate({'opacity':0});
            }
            else if (current_pane == 3)
            {
                sign = "+";
                v_jump = v_jump * 2;
                next_pos = curr_pos - v_jump;
                $('#future-projects-bg').fadeOut(400);
                $('#future-projects-black').fadeOut(400);
            }
            current_pane = 1;
        }
        else if (tab_id == 'present')
        {
            if (current_pane == 1)
            {
                sign = "-";
                next_pos = curr_pos - v_jump;
                $('#past-projects-res-dogs-white').fadeOut(400);
                $('div#past-projects-image').fadeOut(400);
                $('#present').animate({'opacity':1});
                $('#present-menu').animate({'opacity':1});
            }
            else if (current_pane == 3)
            {
                sign = "+";
                next_pos = curr_pos - v_jump;
                $('#future-projects-bg').fadeOut(400);
                $('#future-projects-black').fadeOut(400);
                $('#present').animate({'opacity':1});
                $('#present-menu').animate({'opacity':1});
            }
            current_pane = 2;
        }
        else if (tab_id == 'future')
        {
            if (current_pane == 1)
            {
                sign = "-";
                v_jump = v_jump * 2;
                next_pos = curr_pos - v_jump;
                $('#past-projects-res-dogs-white').fadeOut(400);
                $('div#past-projects-image').fadeOut(400);
            }
            else if (current_pane == 2)
            {
                sign = "-";
                next_pos = curr_pos - v_jump;
                $('#present').animate({'opacity':0});
                $('#present-menu').animate({'opacity':0});
            }
            current_pane = 3;
        }

        if (current_pane == 1)
        {
            $('#past-projects-res-dogs-white').fadeIn(400);
            $('div#past-projects-image').fadeIn(400);
        }

        if (current_pane == 3)
        {
            $('#future-projects-bg').fadeIn(400);
            $('#future-projects-black').fadeIn(400);
        }

        $('#flowpanes .items').animate({left: sign + '=' + v_jump + 'px'}, 400);

        check_open_legals(current_pane);
        update_nav_buttons(current_pane);
        update_nav_span(current_pane);
        update_footer_nav(current_pane);
        update_legal_buttons(current_pane);
}

function initLegal()
{
	$('div#present ul#legal-menu li a').click(onLegalButtonClicked);
        present_legal_on = 0;
        $('div#past-projects ul#past-legal-menu li a').click(onLegalButtonClicked);
        past_legal_on = 0;
        $('div#res-dogs-legal').click(onLegalAreaClicked);
        $('div#volatile-legal').click(onLegalAreaClicked);
}

function onLegalButtonClicked()
{
        var tab_id = $(this).attr('id').substr(6);

        if (tab_id == 'past' && past_legal_on == 0)
        {
            $('#res-dogs-legal').animate({top: '-' + '=' + 200 + 'px'}, 600);
            past_legal_on = 1;
        }
        else if (tab_id == 'present' && present_legal_on == 0)
        {
            $('#volatile-legal').animate({top: '-' + '=' + 225 + 'px'}, 600);
            present_legal_on = 1;
        }
}

function onLegalAreaClicked()
{
        var tab_id = $(this).attr('id');

        if (tab_id == 'res-dogs-legal' && past_legal_on == 1)
        {
            $('#res-dogs-legal').animate({top: '+' + '=' + 200 + 'px'}, 600);
            past_legal_on = 0;
        }
        else if (tab_id == 'volatile-legal' && past_legal_on == 0)
        {
            $('#volatile-legal').animate({top: '+' + '=' + 225 + 'px'}, 600);
            present_legal_on = 0;
        }
}

function check_open_legals(current_pane)
{
        if (present_legal_on == 1 && current_pane != 2)
        {
            $('#volatile-legal').animate({top: '+' + '=' + 250 + 'px'}, 600);
            present_legal_on = 0;
        }
        if (past_legal_on == 1  && current_pane != 1)
        {
            $('#res-dogs-legal').animate({top: '+' + '=' + 220 + 'px'}, 600);
            past_legal_on = 0;
        }
}
