jQuery(document).ready(function($) 
{  
   
   $('a[rel*=facebox]').facebox();
   
	// Query clearing
	var initialValue = $('.query').val();
	$('.query').focus(function()
	{
		if($(this).val() == initialValue)
		{
			$(this).val("");
		}
	});
	$('.query').blur(function()
	{
		if($(this).val() == "")
		{
			$(this).val(initialValue);
		}
	});
	// Location clearing
	var initialValue2 = $('.location').val();
	$('.location').focus(function()
	{
		if($(this).val() == initialValue2)
		{
			$(this).val("");
		}
	});
	$('.location').blur(function()
	{
		if($(this).val() == "")
		{
			$(this).val(initialValue2);
		}
	});
	

	$('form.search-form').submit(function(e)
	{	
		query_input = $('form#top_search :input[name="query"]').val().replace(/\s+/g, '');
//		if(query_input == '' || query_input == "I'mlookingfor?")
//		{
//			alert('Please enter a search term first!');
//			return false;
//		}
		
		location_input = $('form#top_search :input[name="location"]').val().replace(/\s+/g, '');
		if(location_input == '' || location_input == ',')
		{
			alert('Please enter a location to search in!');
			return false;
		}
		
		loadingResultsMessage();
		return true;	
	});
	
	//$('.nav-item a, .link-list a').click(function(event) {
	//	loadingResultsMessage();
	//	window.location = event.target.href;
	//});
	
	registerAbuseLinks();	
	registerLiking();
	registerBadging();
	
});


// Puts the cursor in the first input field when there's no value there
$(function()
{
	if($('.query').val() == "I'm looking for...")
	{
		$('.query').focus();
	}	
});

	function loadingResultsMessage()
	{
		$.facebox.settings.overlay = true;
		$.facebox('<style type="text/css">div#facebox div.footer{display: none;}</style><p style="display: block; text-align: center; color: #999;">Please wait while we load your results. <img src="/includes/images/ajax_bar.gif" /></p>');
		$('div#facebox div.footer').css('display', 'none');
		$('.filters ul.options input').attr('disabled', 'disabled');
	}
	
	function registerAbuseLinks()
	{	
		/* Reporting/Reviewing onclick */
		jQuery('a[rel="report"], a[rel="review"], a[rel="modal"]').unbind('click');
		jQuery('a[rel="report"], a[rel="review"], a[rel="modal"]').click(function(e){
			e.preventDefault();
	
	       	$.facebox.settings.overlay = true;
	
	        // Get the link's url for the ajax, but default it.
	        link = $(this).attr('href');
	
	        $.ajax({
	        	type: "GET",
	        	url: link,
	        	dataType: "html",
	        	success: function(data) {
	        		$.facebox(data);
	        	}
	        });
	        
	        jQuery.this_flag = $(this);
		});	
	}
	
	function registerLiking()
	{
		/* LIKING & DISLIKING CLICK CLICK CLICK */
		$('.thumbs-up,.thumbs-down').click(
			function()
			{
			
				var item_id = '';
				var type_name = '';
				var likeVal = 0;
		
				//stop further clicks and adjust style for display
				thisClass = $(this).attr('class');
				var classes = $(this).attr('class').split(' ');
				
				if (thisClass.indexOf('thumbs-up')>-1)
				{
					likeVal = 1;
				}
				else
				{
					likeVal = -1;
				}
				
				//change classes
				if (likeVal==1)
				{
					$(this).attr('class', 'thumbs-up-voted-up');
					$(this).next().next().attr('class', 'thumbs-down-voted-up');
				}
				else
				{
					$(this).attr('class', 'thumbs-down-voted-down');
					$(this).prev().prev().attr('class', 'thumbs-up-voted-down');
				}
		
				//disables all clicks on like classed elements (class includes identifying data
				$(this).unbind('click');
				if (likeVal==1)
				{
					$(this).next().next().unbind('click');
				}
				else
				{
					$(this).prev().prev().unbind('click');
				}
		
				//update values
				var count = $(this).next().text()-0;
				count++;
			
				$(this).next().text(count+'');
				
				//get attributes of item being liked
				for(i=0;i<classes.length;i++)
				{
					if (classes[i].indexOf('item_id=')>-1)
					{
						//set item_id
						item_id = classes[i];
					}
					
					if (classes[i].indexOf('type_name=')>-1)
					{
						//set type_name
						type_name = classes[i];
					}
				}
		
				//do ajax call (fire and forget
		        $.ajax({
		        		type: "GET"
		        	,	url: "../modal/castopinionuponanitem"
					,	data: "ajax=1&" + item_id + "&" + type_name + "&like=" + likeVal
		        });
			}
		);

	}
	
	function registerBadging()
	{
		/* WE DON'T NEED NO STEENKEEN BADGES Rollover */
	
		$('.badges').hover
		(
			function()
			{
				$(this).children('a').children('.desc').show();
			},
			function()
			{
				$(this).children('a').children('.desc').hide();
			}
		);
	}

	function addReviewToPage(review){	
		commentTemplate = '<div class="comment-last"><div class="comment"><div class="comment-l"><a href="[--USERLINK--]" class="profile-pic-b" style="background-image:url([--AVATAR--]);"></a><div class="clear"></div><div class="thumbs"><a href="javascript:void(0);" class="thumbs-up item_id=[--REVIEWID--] type_name=Review"></a><span class="count">0</span><a href="javascript:void(0);" class="thumbs-down item_id=[--REVIEWID--] type_name=Review"></a><span class="count">0</span><div class="clear"></div></div></div><div class="comment-r"><a href="[--USERLINK--]" class="name">[--USERNAME--]</a><span class="badges"><span class="points">[--AMPS--]</span><a class="badge-[--LEVEL--]" href="javascript:void(0)"><p class="desc">[--LEVELDESC--]</p></a><div class="clear"></div></span><div class="rating"><span class="stars-[--STARS--]"> </span><div class="clear"></div></div><div class="clear"></div><p class="date">[--REVIEWDATE--]</p><div class="clear"></div><p class="comment-text">[--REVIEWTEXT--]</p><a href="[--ABUSELINK--]" class="flag " rel="report">Report</a><div class="clear"></div></div><div class="clear"></div></div></div></div>';
	
		//build comment
		commentTemplate = commentTemplate.replace('[--USERLINK--]', review.user_profile_link);
		commentTemplate = commentTemplate.replace('[--AVATAR--]', review.photo);
		commentTemplate = commentTemplate.replace('[--REVIEWID--]', review.id);
		commentTemplate = commentTemplate.replace('[--REVIEWID--]', review.id);
		commentTemplate = commentTemplate.replace('[--USERLINK--]', review.user_profile_link);
		commentTemplate = commentTemplate.replace('[--USERNAME--]', jQuery.trim(review.first + ' ' + review.last));
		commentTemplate = commentTemplate.replace('[--AMPS--]', review.total_points);
		commentTemplate = commentTemplate.replace('[--LEVEL--]', review.amp_level);
		commentTemplate = commentTemplate.replace('[--LEVELDESC--]', review.amp_desc);
		commentTemplate = commentTemplate.replace('[--STARS--]', review.rating);
		commentTemplate = commentTemplate.replace('[--REVIEWDATE--]', review.formatted_create_datetime);
		commentTemplate = commentTemplate.replace('[--REVIEWTEXT--]', review.details);
		commentTemplate = commentTemplate.replace('[--ABUSELINK--]', review.abuse_link);

		//if first wrap in last div then replace BeTheFirstToReviewDiv
		//else insert before current first		

		if ($('#BeTheFirstToReviewDIV').length>0)
		{
			//replace be the first if it exists
			$('#BeTheFirstToReviewDIV').replaceWith(commentTemplate);
		}
		else
		{
			$('.comment:first').before(commentTemplate);
		}		
		
		registerAbuseLinks();	
		registerLiking();
		registerBadging();
	}
