function playVideoIndex(current, next)
{
	$(function(){
        $("#thumbs table tr td#img" + next).stop().animate({ opacity: 1 }, 500);
        $("#thumbs table tr td#img" + current).stop().animate({ opacity: .5 }, 500);
        
		$("#thumbs table tr td#img" + current).toggleClass('inactive');
		$("#thumbs table tr td#img" + next).toggleClass('inactive');
		$("#thumbs table tr td#img" + current).toggleClass('active');
		$("#thumbs table tr td#img" + next).toggleClass('active');
	});
}

$(document).ready(function() {
    var sliderLeft;
	var	scrubLeft;
    $('.container').hide();

    if (jQuery.browser.safari && document.readyState != "complete") {
        setTimeout(arguments.callee, 100);
        return;
    }


    var i = 1;
    for (var vid in vids) {
        $("#thumbs table tr td#img" + i).click(function() {
      //  	alert("Click!");
            var obj = swfobject.getObjectById("introVidPlayer");
            if (obj && typeof obj.sendToActionscript != "undefined") {
            	if($(this).hasClass('inactive')){  
              	  obj.sendToActionscript($(this).attr("rel"));
              	}
            
            }
        });
        i++;
    }
    pauseVideo = function() {
        var obj = swfobject.getObjectById("introVidPlayer");
        if (obj && typeof obj.sendPauseToActionscript != "undefined") {
            obj.sendPauseToActionscript();
        }
    }

//    $("body").css("overflow", "hidden");
    $('.container').show();
    $('.trackpad').show();

    $('.launch').click(function() {
        var end = $('.scrub-area').width() - $('#slider').width();
        var time = (end - parseInt($('#slider').css('left'))) / 2;
        $('#slider').animate({ 'left': end }, time);
        $('.row').each(function() {
            $(this).animate({ scrollLeft: $(this).children('table').width() }, time);
        });
    });

    $('.today').click(function() {
        var end = 0;
        var time = parseInt($('#slider').css('left')) / 2;
        $('#slider').animate({ 'left': end }, time);
        $('.row').each(function() {
            $(this).animate({ scrollLeft: 0 }, time);
        });
    });

    $('.handle').mouseover(function() {
        var width = parseInt($(this).css("width")) + 2;
        $(this).css({ "width": (width + "px") });
    });

    $('.handle').mouseout(function() {
        var width = parseInt($(this).css("width")) - 2;
        $(this).css({ "width": (width + "px") });
    });

    $('.handle').click(function() {
        if ($('.drawer').hasClass('init')) {
            $('.loader').remove();
            $('.rows-overlay').fadeTo(500, 0);

            setTimeout(function() {
                $('.drawer').animate({
                    "left": 0
                }, 650, "easeOutExpo", function() {
                    $(this).addClass('out');
                    $(this).removeClass('init');
                });

                $('.rows').animate({
                    "left": 752
                }, 650, "easeOutExpo", function() {
                });

                $('.rows-overlay').css({ 'opacity': 0, 'z-index': 10 });
                $('.rows-overlay').fadeTo(500, .75);
				$("#img1").trigger('click');

				var obj = swfobject.getObjectById("introVidPlayer");
            	if (obj && typeof obj.sendToActionscript != "undefined")
            	{
            		obj.sendToActionscript(1);
            	}

            }, 1250);
        }
        else if ($('.drawer').hasClass('out')) {
            $('.drawer').animate({
                "left": -754
            }, 650, "easeOutExpo", function() {
                $(this).toggleClass('in');
                $(this).toggleClass('out');
            });

            $('.rows').animate({
                "left": 0
            }, 650, "easeOutExpo");

            pauseVideo();

            $('.rows-overlay').fadeTo(500, 0, function() {
                $(this).css({ "z-index": -10 });
            });
        }
        else if ($('.drawer').hasClass('in')) {
            $('.drawer').animate({
                "left": 0
            }, 650, "easeOutExpo", function() {
                $(this).toggleClass('out');
                $(this).toggleClass('in');
            });

            $('.rows').animate({
                "left": 752
            }, 650, "easeOutExpo", function() {
            });

            $('.rows-overlay').css({ 'opacity': 0, 'z-index': 10 });
            $('.rows-overlay').fadeTo(500, .75);
			
			$("#img1").trigger('click');
		
	//		alert($("#thumbs table tr td.active").attr('id'));
        }

    });

    function resize() {
        var viewportHeight = window.innerHeight ? window.innerHeight : $(window).height();
        var viewportWidth = $(window).width();
				
        $(".scrub-area").width(viewportWidth - 160);
        $(".container").width(viewportWidth);
        $(".wrapper").width(viewportWidth);
      //  $(".container").height(viewportHeight);

		$('#slider').css({'left': 0 });
	
        $(".row").each(function() {
            $(this).width(viewportWidth - (parseInt($(this).css('padding-left')) - parseInt($(this).css('padding-right')))-16);
            $(this).scrollLeft(0);
        });

    }

	resize();

    $(window).resize(function() {
        resize();
    });


    $('.rows-overlay').live('click', function() {

        if ($('.drawer').hasClass('out')) {
            $('.handle').trigger('click');
        }

    });

    $('#thumbs table tr td ').hover(function() {
        if ($(this).hasClass('inactive')) {
            $(this).stop().animate({ opacity: 1 }, 500);
        }
    }, function() {
        if ($(this).hasClass('inactive')) {
            $(this).stop().animate({ opacity: .5 }, 500);
        }
    });

    $('.row table tr td.dyson').unbind('hover');

    $('.row table#tweets tr td.alt').hover(function() {
        $(this).children('div.border').stop(true, true).fadeIn(500);
    }, function() {
        $(this).children('div.border').stop(true, true).fadeOut(500);
    });


    $('.row table tr td').hover(function() {
    	$(this).children('div.containment').highlightFade({start:'#0C0C0C', end: '#626469', attr:'borderColor'});
        $(this).find('div.fade').stop(true, true).fadeIn();
    }, function() {
    	$(this).children('div.containment').highlightFade({start:'#626469', end: '#0C0C0C', attr:'borderColor'});

        $(this).find('div.fade').stop(true, true).fadeOut();
    });
    $('td.dyson').unbind('hover');

    $('#thumbs table tr td.inactive').live('click', function() {
        	$('#thumbs tr td.active').stop().animate({ opacity: .5 }, 500);
        	$('#thumbs tr td.active').toggleClass('inactive');
        	$('#thumbs tr td.active').toggleClass('active');
        	$(this).toggleClass('active');
        	$(this).toggleClass('inactive');
    });

    $(".thumbnails button.next").click(function() {
        var thumbs = $(this).siblings('#thumbs');
        $(thumbs).animate({ scrollLeft: $(thumbs).scrollLeft() + 100 }, 600);
    });

    $(".thumbnails button.prev").click(function() {
        var thumbs = $(this).siblings('#thumbs');
        $(thumbs).animate({ scrollLeft: $(thumbs).scrollLeft() - 100 }, 600);
    });

    $(".rows").mouseact({ "tracking": $('.trackpad'), "controller": $('#slider') });

    $('input#copy').live('click', function() {
        this.select();
    });

    if ($.url.attr("path").indexOf('item') >= 0) // $.url.segment(1)
    {
        $('.loader').remove();
        $('.rows-overlay').fadeTo(500, 0);
        $('.rows-overlay').css({ "z-index": -10 });
        
        
        var itemId = $.url.attr("path").replace("/item/",'').replace(".aspx",'');
        //tb_show("","boxes/image.php?height=557&width=841&modal=true&itemId="+itemId);
        tb_show('', '/includes/popupbox.aspx?height=557&width=841&modal=true&ItemId=' + itemId);
        
    }
    else if ($('#sorted').val() == '1') {
        $('.loader').remove();
        $('.rows-overlay').fadeTo(500, 0);
        $('.rows-overlay').css({ "z-index": -10 });
    }
    else {
        setTimeout(function() {
            $('.handle').trigger('click');
        }, 3000)
    }


    var options = { selectCallback: callbackFunction };
    $("select.select").niceSelect(options);

    function callbackFunction(vals, select) {
        $("#the_value").html(vals.value);
    }




});
