$(function() {

    // Initialize lightbox
    var show_video_dialog = $('<div id="video-dialog"></div>')
        .html('<b>youtube</b>')
        .dialog({
        autoOpen: false,
        width: 670,
        height: 495,
        modal: true,
        closeOnEscape: true,
        title: ''
    });

    $(".open-video").click(function() {
        show_video_dialog.html(' <h2>'+$(this).text()+'</h2>' +
        '<iframe class="youtube-player" type="text/html" width="640" height="385" src="http://www.youtube.com/embed/'+$(this).attr('id')+'" frameborder="0"> </iframe>');
        show_video_dialog.dialog('open');	
        return false;
    });

    $("#add-video").click(function() {
        console.log('adding video');
        $("#add-video-form").dialog({
            modal: true,
            title: 'Add Youtube Video',
            closeOnEscape: true,
            resizable: false,
            width: 340,
            draggable: false,
         });	
        return false;
    });
    
    // Delete video
    $(".delete-video").click(function() {
        var index = $(".delete-video").index(this);
        $('#add-video-form input.video[type="hidden"]:eq('+index+")").remove();
        $('#add-video-form form').submit();
        return false; 
    });
	
	// Overview, About, Fans tabs
	if ($("#tabs").length > 0) {
		var tabs = $("#tabs > div"),
			tabHeadings = $("#band-header div > ul");
			
		tabHeadings
			.delegate("li", "click", function(e) {
				var li = $(this),
					hash = li.children("a").attr("href");
	
				li.addClass("selected-band-header").siblings().removeClass("selected-band-header");
				tabs.hide().filter(hash).show();
						
				e.preventDefault();
			});
	}

	// Show/Hide details popovers on shows
	$("#details1-button").click(function() {
		$("#details1").toggle();
	});
	$("#details2-button").click(function() {
		$("#details2").toggle();
	});
	
	// Show/Hide login popover
	$("header ul li#login-button").click(function() {
		$("#login").toggle();
		return false;
	});
	
	// Show/Hide post link popover
	$("#post-link-button").click(function() {
		$("#post-link").toggle();
		return false;
	});
	
	// Activities page - Show/Hide edit delete buttons on hover
	$("#activities div").hover(function() {
		$(this).children(".as-update-delete").show();
	}, function(){
		$(this).children(".as-update-delete").hide();
	});
	
	// Set height of sidebar, #progress div
	if ($("#progress").length > 0) {
		(function() {
			var heightOfHeader = $("header").height(),
				sidebar = $("#progress").height();

			function loopHeightOfSidebar() {
				var heightOfScreen = window.innerHeight,
					heightOfContent = $(".grid_9").height() + 20;
					
				if (heightOfContent < heightOfScreen) {
					$("#progress").css("height", heightOfScreen - heightOfHeader);	
				} else {
					$("#progress").css("height", heightOfContent + 10);	
				}
			}
			setInterval(loopHeightOfSidebar, 2000);
			loopHeightOfSidebar();
		})();
	}
	
	
	// "Upcoming", "Past" shows switcher
	if ($("#shows").length > 0) {
		$("#upcoming-button").click(function() {
			$("#upcoming").show();
			$("#past").hide();
			$(this).addClass("selected-shows");
			$("#past-button").removeClass("selected-shows");
			
			return false;
		});
		$("#past-button").click(function() {
			$("#upcoming").hide();
			$("#past").show();
			$(this).addClass("selected-shows");
			$("#upcoming-button").removeClass("selected-shows");
			
			return false;
		});
	}
	
	//Show confirm when deleting
	$(".delete-show-confirm").click(function() {
		$(this).hide();
		$(this).siblings(".delete-confirm-container").show();
		return false;
	});
	$("a.delete-cancel-table").click(function() {
		window.location.reload();
	});
	$(".delete-confirm-button").click(function() {
		$(".delete-confirm").show();
		$(this).hide();
		return false;
	});
	$("a.delete-cancel").click(function() {
		$(".delete-confirm-button").show();
		$(".delete-confirm").hide();
		return false;
	});
	
	// Add support for the HTML5 placeholder attribute for unsupported browsers
	(function($) {
	    function Placeholder(input) {
	        this.input = input;
	        if (input.attr('type') == 'password') {
	            this.handlePassword();
	        }
	        $(input[0].form).submit(function() {
	            if (input.hasClass('placeholder') && input[0].value == input.attr('placeholder')) {
	                input[0].value = '';
	            }
	        });
	    }
	    Placeholder.prototype = {
	        show : function(loading) {
	            if (this.input[0].value === '' || (loading && this.valueIsPlaceholder())) {
	                if (this.isPassword) {
	                    try {
	                        this.input[0].setAttribute('type', 'text');
	                    } catch (e) {
	                        this.input.before(this.fakePassword.show()).hide();
	                    }
	                }
	                this.input.addClass('placeholder');
	                this.input[0].value = this.input.attr('placeholder');
	            }
	        },
	        hide : function() {
	            if (this.valueIsPlaceholder() && this.input.hasClass('placeholder')) {
	                this.input.removeClass('placeholder');
	                this.input[0].value = '';
	                if (this.isPassword) {
	                    try {
	                        this.input[0].setAttribute('type', 'password');
	                    } catch (e) { }
	                    this.input.show();
	                    this.input[0].focus();
	                }
	            }
	        },
	        valueIsPlaceholder : function() {
	            return this.input[0].value == this.input.attr('placeholder');
	        },
	        handlePassword: function() {
	            var input = this.input;
	            input.attr('realType', 'password');
	            this.isPassword = true;
	            if ($.browser.msie && input[0].outerHTML) {
	                var fakeHTML = input[0].outerHTML.replace(/type=(['"])?password\1/gi, 'type=$1text$1');
	                this.fakePassword = $(fakeHTML).val(input.attr('placeholder')).addClass('placeholder').focus(function() {
	                    input.trigger('focus');
	                    $(this).hide();
	                });
	            }
	        }
	    };
	    var NATIVE_SUPPORT = !!("placeholder" in document.createElement( "input" ));
	    $.fn.placeholder = function() {
	        return NATIVE_SUPPORT ? this : this.each(function() {
	            var input = $(this);
	            var placeholder = new Placeholder(input);
	            placeholder.show(true);
	            input.focus(function() {
	                placeholder.hide();
	            });
	            input.blur(function() {
	                placeholder.show(false);
	            });
	            if ($.browser.msie) {
	                $(window).load(function() {
	                    if(input.val()) {
	                        input.removeClass("placeholder");
	                    }
	                    placeholder.show(true);
	                });
	                input.focus(function() {
	                    if(this.value == "") {
	                        var range = this.createTextRange();
	                        range.collapse(true);
	                        range.moveStart('character', 0);
	                        range.select();
	                    }
	                });
	            }
	        });
	    }
	})(jQuery);
	$('input[placeholder], textarea[placeholder]').placeholder();
	
	
});

