var currentCast		= '';
var currentCreative	= '';
var currentStar		= 5;

$(document).ready(function() {
	
	//header
	$("#navigation li").hover(	
		function() { $(this).removeClass("inactive").addClass("active"); },
		function() { $(this).removeClass("active").addClass("inactive"); }
	);
	
	$("#newsNav li").hover(	
		function() { $(this).removeClass("inactive").addClass("active"); },
		function() { $(this).removeClass("active").addClass("inactive"); }
	);

	$(".newsLink li").hover(	
		function() { $(this).removeClass("inactive").addClass("active"); },
		function() { $(this).removeClass("active").addClass("inactive"); }
	);

	/*
	$(".castMember").hover(	
		function() { $(this).children("div").animate({bottom:0},150) },
		function() { $(this).children("div").animate({bottom:-164},150) }
	);
	
	$(".creativeMember").hover(	
		function() { $(this).children("div").animate({bottom:0},150) },
		function() { $(this).children("div").animate({bottom:-164},150) }
	);
	*/
	
	$("#creativeButton").hover(	
		function() { $(this).attr("src",'/img/creative_button_hover.gif') },
		function() { $(this).attr("src",'/img/creative_button.gif') }
	);
	
	$("#castButton").hover(	
		function() { $(this).attr("src",'/img/cast_button_hover.gif') },
		function() { $(this).attr("src",'/img/cast_button.gif') }
	);
	
	$("#groupSalesButton").hover(	
		function() { $(this).attr("src",'/img/group_sales_hover.gif') },
		function() { $(this).attr("src",'/img/group_sales_button.gif') }
	);
	
	$("#buyTicketsButton").hover(	
		function() { $(this).attr("src",'/img/buy_tickets_button_hover.gif') },
		function() { $(this).attr("src",'/img/buy_tickets_button.gif') }
	);
	
	$("#reviewSubmit").hover(	
		function() { $(this).attr("src",'/img/review_submit_hover.gif') },
		function() { $(this).attr("src",'/img/review_submit.gif') }
	);
	
	$("#shareFriendsButton").hover(	
		function() { $(this).attr("src",'/img/share_with_friends_button_hover.gif') },
		function() { $(this).attr("src",'/img/share_with_friends_button.gif') }
	);
	
	$("#noThanks").hover(	
		function() { $(this).attr("src",'/img/no_thanks_hover.gif') },
		function() { $(this).attr("src",'/img/no_thanks.gif') }
	);
	
	$("#getTheMusic").hover(	
		function() {$("#slideUp").animate({bottom:0},150)},
		function() {$("#slideUp").animate({bottom:-94},150)}
	);
	$("#noThanks").click(
		function(){
		$("#sendFriend").fadeTo(500, 0);
		$("#dither").fadeTo(500, 0);
		setTimeout('$("#sendFriend").css({ display:"none" })',500);
		setTimeout('$("#dither").css({ display:"none" })',500);
		
	});
	
	$("#shareFriendsButton").click(
		function(){
			var yourName	= $("#yourName").val();
			var yourEmail	= $("#yourEmail").val();
			var emails		= $("#emails").val();
			var review		= $("#review").val();
			var emailArr	= new Array();
			var valid		= 1;
			emailArr		= emails.split(",");
			
			for(var i = 0; i < emailArr.length; i++) {
				if(!is_valid_email(emailArr[i].replace(/^\s/, "").replace(/\s$/, ""))) {
					valid = 0;
				}
			}
			
			if(valid == 1 && yourName != "" && yourEmail != "") {
				for (var i = 0; i < emailArr.length; i++) {
					$.ajax({
						type: "POST",
						url: "/sendFriend.php",
						data: "name=" + yourName + "&email=" + yourEmail + "&sendTo=" + emailArr[i] + "&review=" + review,
						success: function(msg) {
					 		if(msg != 'yes') {
								alert('Review could not be submitted at this time, please try again later.');
							} else {
								$("#sendFriend").fadeTo(500, 0);
								$("#dither").fadeTo(500, 0);
								setTimeout('$("#sendFriend").css({ display:"none" })',500);
								setTimeout('$("#dither").css({ display:"none" })',500);
							}
						}
				 	});
				}
			}
		}
	);
	
	
	$("#reviewSubmit").click(
		function(){
			var reviewName = $("#reviewName").val();
			var reviewEmail = $("#reviewEmail").val();
			var reviewTitle = $("#reviewTitle").val();
			var review = $("#review").val();
			var reviewRating = $("#rating").val();
			if (is_valid_email(reviewEmail)&&reviewName!=''&&reviewTitle!=''&&review!='')
			{
				$.ajax({
				   type: "POST",
				   url: "/postReview.php",
				   data: "name="+reviewName+"&email="+reviewEmail+"&title="+reviewTitle+"&review="+review+"&reviewRating="+reviewRating,
				   success: function(msg){
				 		if (msg!='yes')
							alert('Review could not be submitted at this time, please try again later');
						else
						{
							$("#reviewPop").css({ display:'none' });
							$("#sendFriend").css({ display:'block' });
								$("#sendFriend").fadeTo(500, 1.0);
						}
				   }
			 	});
			}
			else
				alert('Please enter a name, valid email address, review title, and review')
			
		}
	);
	
	
	/*
	$(".castOverlay").click(
		function(e){
			var id=$(this).parent().find(".castOverlay").attr("id");
			changeCast(this.id);
			e.stopPropagation();
		}
	);
	$(".creativeOverlay").click(
		function(){
			changeCreative(this.id);
		}
	);
	*/
	
	//Star count for audience form
	$("#onestar").hover(	
		function() { setStar(1); },
		function() { setStar(currentStar); }
	);
	$("#twostar").hover(	
		function() { setStar(2); },
		function() { setStar(currentStar); }
	);
	$("#threestar").hover(	
		function() { setStar(3); },
		function() { setStar(currentStar); }
	);
	$("#fourstar").hover(	
		function() { setStar(4); },
		function() { setStar(currentStar); }
	);
	$("#fivestar").hover(	
		function() { setStar(5); },
		function() { setStar(currentStar); }
	);
	$("#onestar").click(	
		function() { setRating(1); }
	);
	$("#twostar").click(	
		function() { setRating(2); }
	);
	$("#threestar").click(	
		function() { setRating(3); }
	);
	$("#fourstar").click(	
		function() { setRating(4); }
	);
	$("#fivestar").click(	
		function() { setRating(5); }
	);
});

function analyticTrack(uri) {
	pageTracker._trackPageview(uri);
}

function setStar(starNum)
{
	starNum = starNum * 43;
	$("#fillLayer").css({width:starNum});
}
function setRating(starNum)
{
	currentStar=starNum;
	$("#rating").val(starNum);
}

function popitup(url,w,h) {
	newwindow=window.open(url,'name','height='+h+',width='+w+',scrollbars=yes,toolbar=no');
}

function is_valid_email (email) {
   	return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email);
}

//-------************--------------------
//NEWS FUNCTIONS
//-------************--------------------

function showNews(){
	$("#newsNav .current").removeClass("current");
	$("#nNews-tab").addClass("current");
	$("#newsSection").css({ display:'block' });
	$("#criticsSection").css({ display:'none' });
	$("#audienceSection").css({ display:'none' });
	$("#ta_badge").hide();
	analyticTrack('/news');
}
function showCritics(){
	$("#newsNav .current").removeClass("current");
	$("#critics-tab").addClass("current");
	$("#newsSection").css({ display:'none' });
	$("#criticsSection").css({ display:'block' });
	$("#audienceSection").css({ display:'none' });
	$("#ta_badge").hide();
	analyticTrack('/critics');
}
function showAudience(){
	$("#newsNav .current").removeClass("current");
	$("#audience-tab").addClass("current");
	$("#newsSection").css({ display:'none' });
	$("#criticsSection").css({ display:'none' });
	$("#audienceSection").css({ display:'block' });
	$("#ta_badge").show();
	$("#bgColor").width(0);
	$("#bgColor").animate({width:300},2000);
	analyticTrack('/audience');
}
function closeReview(){
	$("#reviewPop").css({ display:'none' });
	$("#dither").css({ display:'none' });
}
function showReview(){
     $("#dither").css({ display: "block", height: $(document).height() }).fadeTo(1, 0.7);
	$("#reviewPop").css({display:'block'});
}
//-------************--------------------
//CAST FUNCTIONS
//-------************--------------------
function prevCast(){
	changeCast(cast[currentCast]["prev"]);
}

function nextCast(){
	changeCast(cast[currentCast]["next"]);
}

function changeCast(lastname){
	$("#castName").html(cast[lastname]["actor"].replace(/_/,'-'));
	$("#castRole").html(cast[lastname]["role"]);
	$("#castBio").html(cast[lastname]["bio"]);
	$("#castDesc").html(cast[lastname]["desc"]);
	$("#castImage").attr("src","http://situationmarketing.com/content/cast_images/"+cast[lastname]["img"]);
	$("#castImage").attr("alt",cast[lastname]["actor"].replace(/_/,'-'));
	currentCast = lastname;
	$("#"+lastname).animate({height:164},150);
	
}

function prevCreative(){
	changeCreative(creative[currentCreative]["prev"]);
}

function nextCreative(){
	changeCreative(creative[currentCreative]["next"]);
}

function changeCreative(lastname){
	$("#creativeName").html(creative[lastname]["actor"].replace(/_/,'-'));
	$("#creativeRole").html(creative[lastname]["role"]);
	$("#creativeBio").html(creative[lastname]["bio"]);
	$("#creativeImage").attr("src","http://situationmarketing.com/content/cast_images/"+creative[lastname]["img"]);
	$("#creativeImage").attr("alt",creative[lastname]["actor"].replace(/_/,'-'));
	currentCreative = lastname;
	$("#"+lastname).animate({height:164},150);
}

function showCreative(){
	//$("#cast").animate({height:0},360);
	//$("#creative").animate({height:370},360);
	$("#castContainer").animate({top:-390},360);
}

function showCast(){
	//$("#creative").animate({height:0},360);
	//$("#cast").animate({height:370},360);
	$("#castContainer").animate({top:0},360);
}
//-------************--------------------
//NEWSLETTER FUNCTIONS
//-------************--------------------
function postNewsletter() {
	var html = $("#newsletter").html();
	
	e	= $("#newsletter .email").val();
	if(!e || !e.match(/^[\w\.+-]+@[\w\.+-]+\.[\w\.+-]+$/)) {
		alert("You must enter a valid email address.");
		return;
	}

	$("#newsletter").fadeTo(300, 0.0, function() {
		$(this).html('<div id="newsletterText">Please wait...</div>');
		$(this).fadeTo(300, 1.0);
	});
	
	$.ajax({
		type: "POST",
		url: "/addNewsletter.php",
		data: {
			account_id: "306",
			email: e
		},
		success: function(response) {
			//if(/error/.test(response)) { alert(response); }
			$("#newsletter").fadeTo(300, 0.0, function() {
				$(this).html('<div id="newsletterText">You have been added to the newsletter</div>');
				$(this).fadeTo(300, 1.0);
			});
		},
		error: function(r, t, e) {
			$("#newsletter").fadeTo(300, 0.0, function() {
				$(this).html(html);
				$(this).fadeTo(300, 1.0);
			});
		}
	});
}