$(document).ready(function(){

    // global vars
    var is_animating = false;

    // pretty checkboxes
    $("#terms").fadeOut(0);
    $("#terms").before('<a href="#" data-target="terms" class="text_replace pretty_checkbox">Agree to Terms</a>');
    
    $(".pretty_checkbox, .terms_label").click(
            
            function ()
            {
                    
                    ($(".pretty_checkbox").hasClass("pretty_checked")) ? $(".pretty_checkbox").removeClass("pretty_checked") : $(".pretty_checkbox").addClass("pretty_checked");
                    $("#" + $(".pretty_checkbox").attr("data-target")).trigger("click");
                    return false;
                    
            }
            
    )

    // contest
    
    $("#photo").wrap('<div class="pretty_browse" />');			
	
    // main features slider
    
    var main_feature_index = 1;

    $("#main_feature_previous").animate({ opacity: 0.25 });
    
    $("#main_feature_next").click
    (
        
        function()
        {
            
            if(main_feature_index < $("#main_features").children().length && !is_animating)
            {

                is_animating = true;
                $("#main_features").animate({ left: '-=978px' }, 500, function () { is_animating = false } );
                main_feature_index++;
                check_main_feature_arrows();
                
            }
            return false;
            
        }
        
    );

    $("#main_feature_previous").click
    (
        
        function()
        {
            
            if(main_feature_index > 1 && !is_animating)
            {

                is_animating = true;
                $("#main_features").animate({ left: '+=978px' }, 500, function () { is_animating = false } );
                main_feature_index--;
                check_main_feature_arrows();
                
            }
            return false;
            
        }
        
    );

    function check_main_feature_arrows()
    {
        
        if(main_feature_index <= 1)
        {
            
            $("#main_feature_previous").animate({ opacity: 0.25 });
            
        }
        else
        {
            
            $("#main_feature_previous").animate({ opacity: 1 });
            
        }
        
        if(main_feature_index >= $("#main_features").children().length)
        {

            $("#main_feature_next").animate({ opacity: 0.25 });
            
        }
        else
        {
            
            $("#main_feature_next").animate({ opacity: 1 });
            
        }
        
    }

    // product preview slider

    var product_preview_index = 0;
    var product_x = new Array();
        product_x[0] = 0;
    var max_x = -870;
    
    $("#product_slider li").each
    (
        
        function(i)
        {
            
            max_x = product_x[i+1] = product_x[i] + Number($(this).css("width").replace("px", "")) + 20;
            
        }
        
    );
    
    $("#product_preview_previous").animate({ opacity: 0.25 })
    $(".preview_copy > *").fadeOut(0);
    $(".preview_copy").addClass("preview_hidden");
    $("#product_slider li:nth-child(1) > .preview_copy > *").fadeIn(0);
    $("#product_slider li:nth-child(1) > .preview_copy").removeClass("preview_hidden");
    $(".product_slider_wrapper").after('<hr class="preview_rule">');

    // $("#product_previews").before('<div id="debug" style="background:#f00; color:#fff; font-weight:bold; width:978px; height:100px; margin:0 auto;">' + max_x + '</div>');
	
    $("#product_preview_next").click
    (
        
        function()
        {
            

            if(product_preview_index < $("#product_slider").children().length - 1 && !is_animating)
            {
                                                
                is_animating = true;
                product_preview_index++;
                if (product_x[product_preview_index] < max_x - 870)
                {
                
                    $("#product_slider").animate({ left: '-' + product_x[product_preview_index] + 'px'}, 500, function () { is_animating = false } );
                    
                }
                else
                {

                    $("#product_slider").animate({ left: '-' + (max_x - 870) + 'px'}, 500, function () { is_animating = false } );
                    
                }
                
                // $("#debug").html('product_x[product_preview_index]: ' + product_x[product_preview_index] + '<br>');
                check_product_preview_arrows();

                $(".preview_copy > *").fadeOut(250);
                $(".preview_copy").addClass("preview_hidden");
                $("#product_slider li:nth-child(" + (product_preview_index + 1) + ") > .preview_copy > *").fadeIn(250);
                $("#product_slider li:nth-child(" + (product_preview_index + 1) + ") > .preview_copy").removeClass("preview_hidden");
	                
            }
            return false;
            
        }
        
    )

    $("#product_preview_previous").click
    (
        
        function()
        {
            
            if(product_preview_index > 0 && !is_animating)
            {

                is_animating = true;
                product_preview_index--;
                if (product_x[product_preview_index] < max_x - 870)
                {
                    
                    $("#product_slider").animate({ left: '-' + product_x[product_preview_index] + 'px'}, 500, function () { is_animating = false } );
                    
                }
                else
                {

                    $("#product_slider").animate({ left: '-' + (max_x - 870) + 'px'}, 500, function () { is_animating = false } );
                    
                }
                check_product_preview_arrows();

                $(".preview_copy > *").fadeOut(250);
                $(".preview_copy").addClass("preview_hidden");
                $("#product_slider li:nth-child(" + (product_preview_index + 1) + ") > .preview_copy > *").fadeIn(250);
                $("#product_slider li:nth-child(" + (product_preview_index + 1) + ") > .preview_copy").removeClass("preview_hidden");
				                
            }
            return false;
            
        }
        
    )

    function check_product_preview_arrows()
    {
        
        if(product_preview_index < 1)
        {
            
            $("#product_preview_previous").animate({ opacity: 0.25 });
            
        }
        else
        {
            
            $("#product_preview_previous").animate({ opacity: 1 });
            
        }
        
        if(product_preview_index >= $("#product_slider").children().length - 1)
        {

            $("#product_preview_next").animate({ opacity: 0.25 });
            
        }
        else
        {
            
            $("#product_preview_next").animate({ opacity: 1 });
            
        }
        
    } 
	
	// request form display

	if(window.location.hash)
	{
	
//            $(".sample_copy").fadeOut(0);
	
	}
	else
	{
	
//            $("#request_sample").fadeOut(0);
            $("#request_button").click
            (
            
                function()
                {
                        
                    $(".sample_copy").fadeOut(500, function() { $("#request_sample").fadeIn(500) });

                }
                    
            );
		
	}

    // videos
    
    var video_index = 1;
    var videoID = $("#ytapiplayer").attr('data-starting_video');
    var params = { allowScriptAccess: "always" }
    var atts = { id: "myytplayer" }
    swfobject.embedSWF("http://www.youtube.com/v/" + videoID + "?enablejsapi=1&playerapiid=ytplayer", "ytapiplayer", "602", "400", "8", null, null, params, atts);
    
    $(".video_link").click
    (
            
            function()
            {
                    
                    ytplayer.loadVideoById($(this).attr("data-video_id"), 0);
                    $("#video_title").html($(this).attr("data-description"));
		    $(".video_box").removeClass("thumb_active");
		    $("#video_thumb_" + $(this).attr("data-video_id")).addClass("thumb_active");
		    video_index = $("#video_thumb_" + $(this).attr("data-video_id")).attr("data-video_index");
                    return false;
                    
            }
            
    )

    $(".video_thumb_previous").animate({ opacity: 0.25 });

    $(".video_thumb_nav").click
    (
    
            function()
            {
                            
                    if ($(this).hasClass("video_thumb_previous")) 
                    {
                                                        
                            if (video_index > 1 && !is_animating)
                            {

                                    is_animating = true;
                                    video_index--;
                                    
                                    $(".thumb").removeClass("thumb_active");
                                    $(".thumb:nth-child(" + video_index + ")").addClass("thumb_active");

                                    $(".thumb_strip").animate({left: '-' + ((video_index - 3) * 224) + 'px'}, 150, function() { is_animating = false } );
                                            
                                    check_video_arrows();
                                    
                            }
                            
                    }
                    else
                    {
                            
                            if(video_index < $(".thumb_strip > ul").children().length && !is_animating)
                            {
                                    
                                    is_animating = true;
                                    video_index++;

                                    $(".thumb").removeClass("thumb_active");
                                    $(".thumb:nth-child(" + video_index + ")").addClass("thumb_active");

                                    if(video_index > $(".thumb_strip > ul").children().length - 1)
                                    {
                                        
                                        is_animating = false;
                                        
                                    }
                                    else
                                    {

                                        $(".thumb_strip").animate({left: '-' + ((video_index - 3) * 224) + 'px'}, 150, function() { is_animating = false; } );
                                            
                                    }
                                    check_video_arrows();
                            
                            }
                            
                    }

                    return false;
                                                                            
            }
                    
    );

    $("#video_nav_previous").click
    (
        
        function()
        {

            if (video_index > 1 && !is_animating)
            {

                is_animating = true;
                video_index--;
                
                $(".thumb").removeClass("thumb_active");
                $(".thumb:nth-child(" + video_index + ")").addClass("thumb_active");

                if($('html').hasClass('ie6') || $('html').hasClass('ie7') || $('html').hasClass('ie8'))
                {
                        
                    $(".thumb_strip").css("left", '-' + ((video_index - 3) * 224) + 'px');
                    is_animating = false;
                        
                }
                else
                {

                    $(".thumb_strip").animate({left: '-' + ((video_index - 3) * 224) + 'px'}, 150, function() { is_animating = false } );
                        
                }
                    
            }

            ytplayer.loadVideoById($(".thumb:nth-child(" + video_index + ") > a").attr("data-video_id"), 0);
            $("#video_title").html($(".thumb:nth-child(" + video_index + ") > a").attr("data-description"));

            return false;
            
        }
        
    );

    $("#video_nav_next").click
    (
        
        function()
        {

            if(video_index < $(".thumb_strip > ul").children().length && !is_animating)
            {
                    
                is_animating = true;
                video_index++;

                $(".thumb").removeClass("thumb_active");
                $(".thumb:nth-child(" + video_index + ")").addClass("thumb_active");

                if($('html').hasClass('ie6') || $('html').hasClass('ie7') || $('html').hasClass('ie8'))
                {
                        
                    $(".thumb_strip").css("left", '-' + ((video_index - 3) * 224) + 'px');
                    is_animating = false;
                        
                }
                else
                {

                    $(".thumb_strip").animate({left: '-' + ((video_index - 3) * 224) + 'px'}, 150, function() { is_animating = false } );
                        
                }
                
            }

            ytplayer.loadVideoById($(".thumb:nth-child(" + video_index + ") > a").attr("data-video_id"), 0);
            $("#video_title").html($(".thumb:nth-child(" + video_index + ") > a").attr("data-description"));
          
            return false;
        
        }
        
    )
	    
    function check_video_arrows()
    {
        
        if(video_index <= 1)
        {
            
            $(".video_thumb_previous").animate({ opacity: 0.25 });
            
        }
        else
        {
            
            $(".video_thumb_previous").animate({ opacity: 1 });
            
        }
        
        if(video_index == $(".thumb_strip > ul").children().length)
        {

            $(".video_thumb_next").animate({ opacity: 0.25 });
            
        }
        else
        {
            
            $(".video_thumb_next").animate({ opacity: 1 });
            
        }
        
    }
            
})

function onYouTubePlayerReady(playerId)
{
    
    ytplayer = document.getElementById("myytplayer");
    
}
