$(document).ready(function() {
	menu();
	getNews();
	youtube();
	picture();
	contact();
	email();
	download();

	$(window).resize(function() {
		$("html, body, #wrapper, #container").css("height", "100%");
		$("#container").css("minHeight", "100%");
	});

	$(".thumb").bind("mouseover", function() {
		if (!$(this).hasClass("selected"))
			$(this).css("borderColor", "orange");
	}).bind("mouseout", function() {
		if (!$(this).hasClass("selected"))
			$(this).css("borderColor", "black");
	});
	
	if ($("#portfolio").length) {
		$("#portfolio").show();
		$('#portfolio').innerfade({
			speed: 'slow',
			timeout: 2000,
			type: 'sequence',
			containerheight: '306px'
		});
	}

});

function email() {
	var email = [98,101,97,116,98,117,109,122,64,101,110,116,114,111,112,121,114,101,99,111,114,100,115,46,99,111,109];
	var emailInfo = "";
	for (var i = 0; i < email.length; i++)
		emailInfo += String.fromCharCode(email[i]);

	if ($(".emailInfo").length) {
		$(".emailInfo").attr("href", "mailto:" + emailInfo);
	}
}

function menu() {
	$("#menu").children("ul").children("li").hover(function() {
			$(this).addClass("menuHover");
			if (!$(this).hasClass("menuSelect")) {
				$(this).children("a").css("background-image", "url(img/menuTextHover.png)");
				$(this).children("ul").children("li").children("a").css("background-image", "none");
			}
			$(this).siblings().not(".menuHover, .menuSelect").children("a").css("background-image", "url(img/menuText.png)");
			$(this).siblings().not(".menuHover, .menuSelect").children("ul").children("li").children("a").css("background-image", "none");
			$(this).siblings().children("ul").hide();
			$(this).children("ul").fadeIn("normal")
		}, function() {
			$(this).removeClass("menuHover");
			//if (!$(this).hasClass("menuSelect")) {
			//	$(this).children("a").css("background-image", "url(img/menuText.png)");
			//	$(this).children("ul").children("li").children("a").css("background-image", "none");
			//}
			setTimeout('menuHide(1)', 1000);
		}
	);
	
	$("#menu").children("ul").children("li").children("ul").children("li").hover(function() {
			$(this).siblings().children("ul").hide();
			$(this).children("ul").fadeIn("normal");
		}, function() {
			setTimeout('menuHide(2)', 500);
		}
	);
	
	$("#menu").children("ul").children("li").children("a").mouseover(function() {
		$(this).parent("li").children("ul").children("li").children("ul").hide();
	});
}

function menuHide(level) {
	if (level == 1) {
		$("#menu").children("ul").children("li").not(".menuHover").children("ul").fadeOut("fast", function() {
			$("#menu").children("ul").children("li").not(".menuHover, .menuSelect").children("a").css("background-image", "url(img/menuText.png)");
			$("#menu").children("ul").children("li").not(".menuHover, .menuSelect").children("ul").children("li").children("a").css("background-image", "none");
		});
	} else if (level == 2) {
		$("#menu").children("ul").children("li").not(".menuHover").children("ul").children("li").children("ul").fadeOut("fast", function() {
			$("#menu").children("ul").children("li").not(".menuHover, .menuSelect").children("a").css("background-image", "url(img/menuText.png)");
			$("#menu").children("ul").children("li").not(".menuHover, .menuSelect").children("ul").children("li").children("a").css("background-image", "none");
		});
	}
}

function getNews(newsDate) {
	if ($("#news").length) {
		var success = false;
		var currentDate = new Date();
		var month = Right("00" + String(currentDate.getMonth() + 1), 2);
		var year = String(currentDate.getFullYear());
		
		if (newsDate == undefined) newsDate = year + month;
		
		$.ajax({
			type: "POST",
			url: "ajax/news.php",
			data: "newsDate="+newsDate,
			dataType: "html",
			success: function(e) {
				$(".newsLink").remove();
				$("#news").append(e);
				success = true;
			},
			error: function(a, b, c) {
				alert("There was an error processing the request.\n\nPlease try again.");
			},
			complete: function() {
				if (success) {
					$(".newsMonth:last").hide().fadeIn("normal");
					$(".newsLink a").unbind().bind("click", function() {
						getNews($(this).attr("rel"));
					});
				}
			}
		});
	} else if ($(".newsLink").length) {
		$(".newsLink a").unbind().bind("click", function() {
			window.location = "/page/news";
		});
	}
}

function contact() {
	if ($("#contact").length) {
		$("input, textarea").not(".button").bind("focus", function() {
			$(this).css("backgroundColor", "#ffff88");
		}).bind("blur", function() {
			$(this).css("backgroundColor", "white");
		});
	
		$("#reset").bind("click", function() {
			if (confirm("Reset the form?")) {
				$("input, textarea").not(".button").val("");
				$("#submit").removeAttr("disabled").css("color", "#000");
			}
		});
		
		$("#submit").bind("click", function() {
			if ($(this).attr("disabled") != "disabled") {
				var name = jQuery.trim($("#name").val());
				var phone = jQuery.trim($("#phone").val());
				var email = jQuery.trim($("#email").val());
				var message = jQuery.trim($("#message").val());
				var reEmail = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		
				if (name.length == 0) {
					alert("Please enter your name.");
					$("#name").focus();
				} else if (email.length == 0) {
					alert("Please enter your email address.");
					$("#email").focus();
				} else if (message.length == 0) {
					alert("Please enter a message.");
					$("#message").focus();
				} else {
					if (!email.match(reEmail)) {
						alert("Please enter a valid email address.");
						$("#email").focus();
					} else {
						$("#submit").attr("disabled", "disabled").css("color", "#aaa");
						sendMail();
					}
				}
			}
		});
	}
}

function sendMail() {
	var name = jQuery.trim($("#name").val());
	var email = jQuery.trim($("#email").val());
	var message = jQuery.trim($("#message").val());
	var success = false;
	var returnText;
	
	$.ajax({
		type: "POST",
		url: "ajax/mail.php",
		data: "name="+name+"&email="+email+"&message="+message,
		dataType: "text",
		success: function(e) {
			returnText = e;
			success = true;
		},
		error: function(a, b, c) {
				alert("There was an error processing the request.\n\nPlease try again.");
		},
		complete: function() {
			if (success) {
				if (returnText.indexOf("sent") != -1) {
					alert("Message sent! Thank you for your inquiry.");
					window.location = ".";
				} else {
					alert("Message delivery failed...Please try again.");
					$("#submit").removeAttr("disabled").css("color", "#000");
				}
			} else {
				alert("Message delivery failed...Please try again.");
				$("#submit").removeAttr("disabled").css("color", "#000");
			}
		}
	});
}

function youtube() {
	if ($(".youtube").length) {
		$(".youtube").bind("click", function() {
			if (!$(this).hasClass("selected")) {
				$(".youtube").removeClass("selected").css("borderColor", "black");
				$(this).addClass("selected").css("borderColor", "red");
				getYoutube($(this).attr("src").replace("http://i4.ytimg.com/vi/", "").replace("/default.jpg", ""),  $(this).attr("title"));									 
			}
		}).eq(0).click();
	}
}

function picture() {
	if ($("#photo").length) {
		getPicture("BeatBumz0.jpg", "The Beat Bumz");

		$(".pic").bind("click", function() {
			if (!$(this).hasClass("selected")) {
				$(".pic").removeClass("selected").css("borderColor", "black");
				$(this).addClass("selected").css("borderColor", "red");
				getPicture($(this).attr("alt"), $(this).attr("title"));
			}
		});
	}
}

function getYoutube(video, description) {
	if ($(".youtube").length) {
		var success = false;
		if (description == undefined) description = "";
		
		$(".video").empty().css("background-image", "url(img/ajax-loader.gif)");
	
		$.ajax({
			type: "POST",
			url: "ajax/youtube.php",
			data: "video="+video+"&description="+description,
			dataType: "html",
			success: function(e) {
				$(".video").append(e);
				success = true;
			},
			error: function(a, b, c) {
				alert("There was an error processing the request.\n\nPlease try again.");
			},
			complete: function() {
				if (success) {
					$(".video object, .video span").hide();
					$(".video object, .video span").fadeIn("slow");
				}
				$(".video").css("background-image", "none");
			}
		});
	}
}

function getPicture(picture, description) {
	if ($("#photo").length) {
		var success = false;
		if (description == undefined) description = "";
		
		$("#photo").empty().css("background-image", "url(img/ajax-loader.gif)");
	
		$.ajax({
			type: "POST",
			url: "ajax/picture.php",
			data: "picture="+picture+"&description="+description,
			dataType: "html",
			success: function(e) {
				$("#photo").append(e);
				success = true;
			},
			error: function(a, b, c) {
				alert("There was an error processing the request.\n\nPlease try again.");
			},
			complete: function() {
				if (success) {
					$("#photo img, #photo span").hide();
					$("#photo img, #photo span").fadeIn("slow");
				}
				$("#photo").css("background-image", "none");
			}
		});
	}
}

function download() {
	var name = "The Beat Bumz Website";
	var email = "beatbumz@entropyrecords.com";
	var message = "Someone clicked the download button.";
	
	if ($("#momDownload").length) {
		$("#momDownload").click(function() {
			if ($("#momDownload").hasClass("button")) {
				$("#momDownload").removeClass("button");
				$("#momDownload").addClass("buttonDown");
				
				$.ajax({
					type: "POST",
					url: "ajax/mail.php",
					data: "name="+name+"&email="+email+"&subject=Mother's Day Downloaded&message="+message,
					dataType: "text",
					success: function(e) {
						//returnText = e;
					},
					error: function(a, b, c) {
						alert("There was an error getting the file.\n\nPlease try the link to the left.");
					},
					complete: function() {
						window.location = "/mp3/J-ricz Feat. The Beat Bumz - Mother's Day.mp3";
						$("#momDownload").removeClass("buttonDown");
						$("#momDownload").addClass("button");
					}
				});
			}
		});
	}
	
	if ($("#girlsDownload").length) {
		$("#girlsDownload").click(function() {
			if ($("#girlsDownload").hasClass("button")) {
				$("#girlsDownload").removeClass("button");
				$("#girlsDownload").addClass("buttonDown");
				
				$.ajax({
					type: "POST",
					url: "ajax/mail.php",
					data: "name="+name+"&email="+email+"&subject=Go-Go Girls Downloaded&message="+message,
					dataType: "text",
					success: function(e) {
						//returnText = e;
					},
					error: function(a, b, c) {
						alert("There was an error getting the file.\n\nPlease try the link to the left.");
					},
					complete: function() {
						window.location = "/mp3/Go-Go Girls [Explicit].mp3";
						$("#girlsDownload").removeClass("buttonDown");
						$("#girlsDownload").addClass("button");
					}
				});
			}
		});
	}

	if ($("#girlsInstrumentalDownload").length) {
		$("#girlsInstrumentalDownload").click(function() {
			if ($("#girlsInstrumentalDownload").hasClass("button")) {
				$("#girlsInstrumentalDownload").removeClass("button");
				$("#girlsInstrumentalDownload").addClass("buttonDown");
				
				$.ajax({
					type: "POST",
					url: "ajax/mail.php",
					data: "name="+name+"&email="+email+"&subject=Go-Go Girls Instrumental Downloaded&message="+message,
					dataType: "text",
					success: function(e) {
						//returnText = e;
					},
					error: function(a, b, c) {
						alert("There was an error getting the file.\n\nPlease try the link to the left.");
					},
					complete: function() {
						window.location = "/mp3/Go-Go Girls [Instrumental].mp3";
						$("#girlsInstrumentalDownload").removeClass("buttonDown");
						$("#girlsInstrumentalDownload").addClass("button");
					}
				});
			}
		});
	}

	if ($("#girlsAcappellaDownload").length) {
		$("#girlsAcappellaDownload").click(function() {
			if ($("#girlsAcappellaDownload").hasClass("button")) {
				$("#girlsAcappellaDownload").removeClass("button");
				$("#girlsAcappellaDownload").addClass("buttonDown");
				
				$.ajax({
					type: "POST",
					url: "ajax/mail.php",
					data: "name="+name+"&email="+email+"&subject=Go-Go Girls Acappella Downloaded&message="+message,
					dataType: "text",
					success: function(e) {
						//returnText = e;
					},
					error: function(a, b, c) {
						alert("There was an error getting the file.\n\nPlease try the link to the left.");
					},
					complete: function() {
						window.location = "/mp3/Go-Go Girls [Acappella].mp3";
						$("#girlsAcappellaDownload").removeClass("buttonDown");
						$("#girlsAcappellaDownload").addClass("button");
					}
				});
			}
		});
	}
	
	if ($("#horseDownload").length) {
		$("#horseDownload").click(function() {
			if ($("#horseDownload").hasClass("button")) {
				$("#horseDownload").removeClass("button");
				$("#horseDownload").addClass("buttonDown");
				
				$.ajax({
					type: "POST",
					url: "ajax/mail.php",
					data: "name="+name+"&email="+email+"&subject=I'm On A Horse Downloaded&message="+message,
					dataType: "text",
					success: function(e) {
						//returnText = e;
					},
					error: function(a, b, c) {
						alert("There was an error getting the file.\n\nPlease try the link to the left.");
					},
					complete: function() {
						window.location = "/mp3/I'm On A Horse.mp3";
						$("#horseDownload").removeClass("buttonDown");
						$("#horseDownload").addClass("button");
					}
				});
			}
		});
	}
}