$(function(){
	//make the quick search ready
	initSearch();
	
	$("#tfb").click(function(){
		//handle the tell a friend link
		$html="<div id='telBox'><h2 id='tfTitle'>Tell A Friend Form</h2><div id='tfMsg'></div><form action='tellAFriendHandler.php' id='telForm'><div style='padding: 5px 0px;' class='clearfix'><label>Your Name:</label><input type='text' size='33' id='p_name' name='p_name'/></div><div style='padding: 5px 0px;' class='clearfix'><label>Friend(s) Email:</label><input type='text' size='33' id='email' name='email'/></div><div id='tellusers'><b>NOTE: </b>You can tell multiple friends by using a <b>comma</b> to separate their email addresses</div><div style='padding: 5px 0px;' class='clearfix'><label>Comment:</label><textarea wrap='soft' rows='4' cols='32' id='comment' name='comment'/></div><div style='padding-bottom: 30px;' class='clearfix'><input type='submit' value='Submit' id='tfBtn1'/><input type='reset' value='Reset' id='tfBtn2'/></div></form></div>";
							 
		$.facebox($html);
		
		return false;
	});
	
	$('input#tfBtn1').live('click', function(evt){//when the recommend song form is submitted
		$msgBox = $('div#tfMsg').empty();//clear the error message if any message is there
		
		$name = $('input#p_name').val();
		if(!$name.match(/.+/)){
			$msgBox.text("You need to specify the your name").slideDown('fast');
			return false;
		}
		
		if($name.length < 3){
			$msgBox.text("You need to specify a valid name").slideDown('fast');
			return false;
		}
		
		$email = $('input#email').val();
		
		if(!$email.match(/.+/)){
			$msgBox.text("You need to specify your friends' email address").slideDown('fast');
			return false;
		}
		
		//first check if multiple email addresses were specified
		$emails = $email.split(",");
		for(a=0; a < $emails.length; a++){
			//trim the email first b4 validating
			$em = $emails[a].replace( /^\s+/g,'').replace(/\s+$/g,'');
			
			if(!$em.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/)){
				$msgBox.text("There was an error in the email address specified: '"+$emails[a]+"'").slideDown('fast');
				return false;
			}
		}
		
		
		
		/*if(!$email.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/)){
			$msgBox.text("You need to specify a valid email address").slideDown('fast');
			return false;
		}*/
		
		$comment = $('textarea#comment').val();
		if(!$comment.match(/.+/)){
			$msgBox.text("You need to specify the your comment").slideDown('fast');
			return false;
		}
		
		if($comment.length < 5){
			$msgBox.text("Your comment is too short").slideDown('fast');
			return false;
		}
		
		//everything seems okay, so post the data and hide the overlay
		$form = $('form#telForm');
		$form.slideUp('fast', function(){
			$msgBox.after('<div class=\'nMsg\'>Your message has sucessfully been sent</div>').show();
			setTimeout(function(){
				$(document).trigger('close.facebox');
			}, 2000);
		});
		
		$.post($form.attr('action'), $form.serialize());
		
		
		return false;
	});
	
	$('select#alert-type').live('change', function(evt){
		$cb = $('div#commentBox');
		
		if($(this).val() == 'H')
			$cb.slideDown();
		else
			$cb.slideUp();
	 });
	
	//equalize the heights of the 3 mini panels
	var normalizePanelHeights = function(){
		$mini1 = $('#mini1');
		$mini2 = $('#mini2');
		$h = ($mini1.css('height') > $mini2.css('height')) ? $mini1.css('height') : $mini2.css('height');
		$mini1.css("height", $h);$mini2.css("height", $h);
		
		
		$mini3 = $('#mini3');
		$mini4 = $('#mini4');
		$h = ($mini3.css('height') > $mini4.css('height')) ? $mini3.css('height') : $mini4.css('height');
		$mini3.css("height", $h);$mini4.css("height", $h);
		
		
		$mini5 = $('#mini5');
		$mini6 = $('#mini6');
		$mini7 = $('#mini7');
		$h1 = ($mini5.css('height') > $mini6.css('height')) ? $mini5.css('height') : $mini6.css('height');
		$h = ($h1 > $mini7.css('height')) ? $h1 : $mini7.css('height');
		$mini5.css("height", $h);$mini6.css("height", $h);$mini7.css("height", $h);
	}
	
	//normalizePanelHeights();
	
	$adsParent = $('div.panelsHolder');
	
	$adsParent.hover(
		function(){
			$ad = $(this).find('div.tt-ad');
			$movie = $ad.find('object').hide();
			
			$ad.slideDown(200, function(){
				$movie.show();
			});
		}, 
		function(){
			$(this).find('div.tt-ad').slideUp(200, function(){
					$(this).find('object').hide();									
			});
	});
});

function initSearch(){
	$(function(){
		$searchBox = $("div[class='inputBox']").after("<div class='asb' id='asb'><a id='ass-link1' href='#'>Show Assisted Search</a></div>");
		$('#ass-link1').click(function(){
			$link = $(this);
			$searchBox.hide('fast', function(){
				$link.hide();
				$('#assistBox').show('fast');
			});
			
			return false;
		});
		
		$('#ass-link2').click(function(){
			$link = $(this);
			$('#assistBox').hide('fast', function(){
				$searchBox.show();
				$('#ass-link1').show();
			});
			
			return false;
		});
		
		$('#search-cat').change(function(){
			//remove artist combo if present
			$('#search-art').remove();
			$this = $(this);
			$this.attr('disabled', 'disabled');
			$loader = $("<div id='search-status''>Loading available artists...</div>").insertAfter($this).show('slow');
			$category = $this.val();
			$.get('categorize-artists.php', {'c': $category}, function(data){
				//alert(data);
				$this.removeAttr('disabled');
				$loader.hide('fast', function(){
					$(this).remove();
					$(data).insertAfter($this).show('slow').change(function(){
						document.location = 'search.php?pn=1&ct='+$category+'&searchText='+$('#search-art').val();
					});
					
				});
			});
		});
	});
}