var menu = {
	'news': '/',
	'discography': '/discography',
	'live': '/live',
	'biography': '/bio',
	'photos': '/photos',
	'contact': '/contact'
}

var ROOT_NAV_IMAGE_PATH = "/images/nav_menu/";

var getValue = function(key) {
	return menu[key];
}

function initNavigationMenu(currentPage) {
	changeNavMenuImage($("#"+currentPage), currentPage + "_active.png");
	
	$(".menu .menuItem").hover(function() {
		hoverMenuItem(currentPage, this, $(this).attr("id") + "_hover.png", "0.6");
		
    }, function() {
    	hoverMenuItem(currentPage, this, $(this).attr("id") + ".png", "1");
    });
	
	$(".menu .menuItem").click(function() {
		$(location).attr('href',getValue($(this).attr("id")));
	});
}

function clearText(textField) {
	if(textField.value==textField.defaultValue) {
		textField.value='';
	}
}

function resetText(textField) {
	if(textField.value=='') {
		textField.value = textField.defaultValue;
	}
}

function changeNavMenuImage(menuItem, imageName) {
	var image = ROOT_NAV_IMAGE_PATH + imageName;
    $("#"+$(menuItem).attr("id")).css("background-image", "url('" + image + "')");
}

function hoverMenuItem(currentPage, menuItem, imageName, opacity) {
	if(currentPage != $(menuItem).attr("id")) {
		$("#"+$(menuItem).attr("id")).animate({"opacity": opacity}, "fast");
	}
}

function initMailingListDialog() {
	$("#mailingListDialog").dialog({
        autoOpen: false,
        draggable: false,
        modal: true,
        resizable: false,
        closeOnEscape: true,
        closeText: 'hide'});
	
	$("#mailingListDialog").dialog('option', 'width', 250);
	$("#mailingListDialog").dialog('option', 'height', 180);
	
	$('#mailingList').bind('click', function(e) {
		 $('a.ui-dialog-titlebar-close' ).hide();
		 $("#mailingListDialog form").show();
		 $("#mailingListDialog #close").hide();
		 $("#mailingListDialog #message").html('');
		 $("#mailingListDialog #name").val('');
		 $("#mailingListDialog #email").val('');
		 $("#mailingListDialog").dialog('open');
	});
	
	$('#mailingListDialog #cancel').bind('click', function(e) {
		 $("#mailingListDialog").dialog('close');
	});
	
	$('#mailingListDialog #close').bind('click', function(e) {
		 $("#mailingListDialog").dialog('close');
	});
	
	$('#mailingListDialog #submit').bind('click', function(data) {
		
		submitVisitorsEmail();
    });
	
	$('#mailingListDialog').keypress(function(e) {
		if($('#mailingListDialog').dialog( 'isOpen' ) && e.which == 13) {
			submitVisitorsEmail();
		}
	});
}

function submitVisitorsEmail() {
	name = $("#mailingListDialog #name").val();
	email = $("#mailingListDialog #email").val();
	$("#mailingListDialog #message").html('Sending your info...');
	$.get("/form/joinmailinglist.php?name="+name + "&email="+email, function(data){
		if(data.indexOf("Error") < 0) {
			$("#mailingListDialog form").hide();
			$("#mailingListDialog #close").show();
		}
		$("#mailingListDialog #message").html(data);
	});
}

function initSongLyricsDialog() {
	$("#songLyricsDialog").dialog({
        autoOpen: false,
        draggable: false,
        modal: true,
        resizable: false,
        closeOnEscape: true,
        closeText: 'hide',
        shadow: false});
	
	$("#songLyricsDialog").dialog('option', 'width', 500);
	$("#songLyricsDialog").dialog('option', 'height', 300);
	$('#songLyricsDialog').dialog('option', 'show', 'fast', 'Scale');
	
	$('.song .lyrics').bind('click', function(e) {
		$("#songLyricsDialog .lyrics").html('');
		 $('.ui-dialog-titlebar').hide();   
		 $("#songLyricsDialog").dialog("open");
		 $("#songLyricsDialog .title").html(e.target.id);
		 
		 $.get("/lyrics/" + e.target.id + ".txt", function(data) {
			 $("#songLyricsDialog .lyrics").html(data);
		 });
		 
	});
	
	$('#songLyricsDialog .close').bind('click', function(e) {
		$("#songLyricsDialog").dialog("close");
	});
}

function initViewComments() {
	
	$('.moreComments').bind('click', function(e) {
		
		$('#' + e.target.id).hide();
		resetForm(e.target.id);
		toggleCommentForm(e.target.id);
	});
	
	$('.postComment .button').bind('click', function(e) {
		
		var value = e.target.id;
		var match = /blog-(\d+)\.post-(\d+)/.exec(value);		
		var postId = match[2];
		var message = "";
		
		action = value.substr(0,6);
		
		if(action == "submit") {
			sendPostComment(postId);
		} else {
			toggleCommentForm(postId);
			$('#' + postId).show();
			resetForm(postId);
		}
	});
}

function sendPostComment(postId) {

	name = $("#name_" + postId).val();
	comments = $("#yourComments_" + postId).val();
	postURL = $("#posturl_"+postId).val();
	
	if(name=='' || comments=='') {
		$(".postComment #message_" + postId).html("Enter your name and message");
	} else {
		
		url = "/form/submitNewsComment.php";
		comments = escape(comments);
		data = { postid: postId, name: name, message: comments, posturl: postURL };
		
		$.post(url, data, function(data) {
			if(data == 0) {
				$(".postComment #message_" + postId).html("An error occurred while submitting your comment.");
			} else {
				resetForm(postId);
				toggleCommentForm(postId);
				$('#' + postId).show();
				$(".comments #success_" + postId).html("Your comment has been submitted.");
			}
		 });
	}
}

function resetForm(postId) {
	$("#name_" + postId).val("");
	$("#yourComments_" + postId).val("");
	$(".postComment #message_" + postId).html("");
	$(".comments #success_" + postId).html("");
	
}

function toggleCommentForm(postId) {

	$('#remainingComments_' + postId).animate({
	    left: '+=50',
	    height: 'toggle'
	}, 1000);
}

function initViewShowAddress() {
	
	$('.moreInfo').bind('click', function(e) {
		var id = e.target.id;
		id = id.substr(5);
		$('#address_show_' + id).animate({
		    left: '+=50',
		    height: 'toggle'
		}, 500);
	});
}

function loadTuneCoreWidget() {
	$.get("/includes/tunecoreWidget.php", function(data) {
		 $("#tuneCoreContainer").html(data);
	 });
}

function initVideoLinks() {
	
	$(".video").bind("click", function(e) {
		id = e.target.id;
		playVideo(id, true);
	});
}

function playVideo(id, autoplay) {
	$.get("/includes/youtubeVideo.php", function(data) {
		
		youtubeURL = "http://www.youtube.com/v/" + id + "?fs=1&amp;hl=en_US";
		if(autoplay) {
			youtubeURL += "&autoplay=1";
		}
		
		videoObject = data.replace(new RegExp('#', 'g'), youtubeURL);
		 $(".youTubeContainer").html(videoObject);
		 $(".marker").removeClass("activeVideo");
		 $(".marker").addClass("video");
		 $("#"+id).removeClass("video");
		 $("#"+id).addClass("activeVideo");
	 });
}
