$(function(){
	
    $('.tag-list.add-comma').each(function(){
        $(this).find('a:not(:last)').append(',&nbsp;');
    })
    
    $('.add-last-class>:last-child').addClass('last');
    
    $('.rf').click(function(event){
        event.preventDefault();
    });
    
    $('.text, .textarea').focus(function(){
        $(this).addClass('focus');
    }).blur(function(){
        $(this).removeClass('focus');
    });
    
	$('.rf').click(function(e){
		e.preventDefault();
	});
	
	$('.submit-form').click(function(){
		$(this).parents('form').submit();
	});
	
    
    // adds padding to boxes with javascript, saves html clutter
    $('.box:not(.no-inner-buffer)').wrapInner('<div class="inner-buffer"/>');
    $('.inner-buffer').append('<div class="clear"/>');
	

	
	// replaces all embed placeholders with video embed
	$('.embed.placeholder').each(function(){
		$tempEmbed = $(this);
		$.ajax({
			url: TEMPLATE_URL + '/proxy.php?url='+$tempEmbed.attr('src'), 
			dataType: 'json', 
			beforeSend: function(){
				$tempEmbed.before('<div class="loading-icon"/>');
			},
			success: function(blipResponse){
				var json = blipResponse;
				json = json[0] || json;
				json = json.Post || json;

				// changing the source dynamically does not work in IE
				// so forced to clone the embed, change source, then reinsert it into DOM
				$embed = $tempEmbed.clone();
				$tempEmbed.remove();
				$embed.attr('src', json.embedUrl);
				$('.video-player').html($embed);
				$embed.removeClass('placeholder');
			}
		});
	});	
	
	// replaces all thumbnail placeholders with real thumbnail
	$('.thumbnail.placeholder').each(function(){
		$thumbnail = $(this);
		
		var size = 'small';
		if(! $thumbnail.hasClass('small')){
			size = 'large';
		}

		$.ajax({
			url: TEMPLATE_URL + '/proxy.php?url='+$thumbnail.attr('videourl'), 
			dataType: 'json',
			context: $thumbnail,
			success: function(blipResponse){
				var json = blipResponse[0] || blipResponse;
				json = json.Post || json;
				if(size == 'small'){					
					if (json.thumbnail120Url != null) {
						$(this.context).attr('src', json.thumbnail120Url);
					} else {
						$(this.context).hide();
					}
				}else{
					$(this.context).attr('src', json.thumbnailUrl);
				}
				$(this.context).removeClass('placeholder');
			}
		});
	});

	
	// subscribe textbox
	if ($('#signup-mailing-list input[name=email]').size()) {
		$('#signup-mailing-list input[name=email]')
			.val($('#signup-mailing-list input[name=email]').attr('default'))
			.focus(function(){
				if($(this).val() == $(this).attr('default'))
					$(this).val('');
			})
			.blur(function(){
				if(! $(this).val())
					$(this).val('Enter email address...');
			})
			.keydown(function(event){
				if(event.keyCode == 13)
					event.preventDefault();
			});
	} 
	
	/* NAV DROPDOWN MENU */
	$('#nav-links li:has(.sub-menu)').hover(
		function(){
			$(this).find('.sub-menu').show();
		},
		function(){
			$(this).find('.sub-menu').hide();
		}	
	);
	

    // SLIDE VIDEO PANEL
    // populate slide-panel-nav
    $('.slide-panel')
        // for each slide panel
        .each(function(){
            $slidePanel = $(this);
            $slidePanelNav = $(this).siblings('.slide-panel-nav');
            
            // for each child of each slidepanel
            $slidePanel.find('li').each(function(){
                // build nav button
                $navButton = $('<li/>');
                $navButton.append('<a href="#" class="button rf">Video</a>');
                // add a button to the slide navigation that is a sibling of the current slide panel
                $slidePanelNav.find('li:first').after($navButton);
            });
            $slidePanel.find('li:not(:first)').hide();
            $slidePanelNav.find('li:eq(1) a').addClass('selected');
            
        });        
                

    // handles forward/back button clicks
    $('.slide-panel-nav li.scroll-left, .slide-panel-nav li.scroll-right').click(function(){
        var $slidePanelNav = $(this).parent();
        var $slidePanel = $slidePanelNav.siblings('.slide-panel');
        
        // cancel existing rotation
        cancelRotation($slidePanel);
        
        var index = 0;
        
        if($(this).hasClass('scroll-left')){
            index = getPrevIndex($slidePanel);
        }else{
            index = getNextIndex($slidePanel);
        }        
        
        updateSlidePanel($slidePanel, index);
    });

    // handles random slide access
    $('.slide-panel-nav a.button').click(function(e){
        e.preventDefault();
        var $slidePanelNav = $(this).parents('.slide-panel-nav');
        var $slidePanel = $slidePanelNav.siblings('.slide-panel');
        // get index of clicked 
        var index = $slidePanelNav.find('a.button').index(this);
        
        cancelRotation($slidePanel);
        updateSlidePanel($slidePanel, index);
    });
    
    // auto rotate slideshow
    $('.slide-panel').each(function(i){
        var e = $(this)
        var intervalID = setInterval(function(){
            updateSlidePanel(e, getNextIndex(e));
        }, 3500 + (i * 200));  // basically just staggers the interval slightly
        // used for cancelling animation later
        e.data('intervalID', intervalID);
    });
    
/*
	if($('.box.twitter').size()){
		// load latest tweets
		$.ajax({
			url: TEMPLATE_URL + '/getTwitterFeed.php', 
			beforeSend: function(){
				$('.box.twitter .scroll-pane').prepend('<div class="loading-icon"/>');
			},
			success: function(html){				
				$('.box.twitter .loading-icon').remove();
				$('.box.twitter .scroll-pane').prepend(html);//.jScrollPane();
				$('#twitter-flock a').attr('target', '_new');			
			}
		});
	}
*/

	

});
 
function cancelRotation($slidePanel){
    clearInterval($slidePanel.data('intervalID'));
}

function getCurrentIndex($slidePanel){
    var $slidePanelNav = $slidePanel.siblings('.slide-panel-nav');
    var index = $slidePanelNav.find('li a.button').index($slidePanelNav.find('a.selected'));
    return index;
}

function getNextIndex($slidePanel){
    var index = getCurrentIndex($slidePanel);
    index++;
    // handles right wrapping
    if(index > $slidePanel.find('li').size()-1){
        index = 0;
    }
    return index;
}
function getPrevIndex($slidePanel){
    var index = getCurrentIndex($slidePanel);
    index--;
    if(index < 0){
        index = $slidePanel.find('li').size()-1;
    }
    return index;
}

function updateSlidePanel($slidePanel, selectedIndex){
    // hide current panel
    $slidePanel.find('li:visible').hide();
    // show selected
    $slidePanel.find('li:eq(' + selectedIndex + ')').show();
    
    $slidePanel.siblings('.slide-panel-nav').find('li a.selected').removeClass('selected');
    $slidePanel.siblings('.slide-panel-nav').find('li a:eq(' + (selectedIndex+1) + ')').addClass('selected');
}
