jQuery(document).ready(function(){
	//allow $ to be used within this set of functions
	var $ = jQuery.noConflict();
	
	
	
		
/* ========= equal heights ========== */

	function equalHeight(group) {
		var tallest = 0;
		group.each(function() {
			var thisHeight = $(this).height();
			if(thisHeight > tallest) 
			{
				tallest = thisHeight;
			}
		});
		group.height(tallest);
	}
	
	//make sure user business summary boxes are same size
	equalHeight($("#user-summary .box .content"));
	
	
	/* == user admin section == */
	if($('.box-row .box').length > 0)
	{
		$('.box-row .box:last-child').addClass('last-box');
	}
	
	//add onclick for business type check box container
	if($('.meta-checkbox').length > 0)
	{
		$('.meta-checkbox input:checked').parent().stop().addClass('checkbox-highlighted');
		
		$('.meta-checkbox').click(function(){
			//assign next input element as object to var box							  
			var box = $(this).toggleClass('checkbox-highlighted').find('input');
			//check is already checked
			if ($(box).is(':checked'))
			{
				//if so uncheck
				$(box).attr('checked', false);
			}
			else
			{
				//else check
				$(box).attr('checked', 'checked')	
			}
		});
	}
	
	
	//image and branding, allow click event on image wrapper 
	
		//check is necessary
	if ($('.meta-img').length)
	{
		//when image clicked
		$('.meta-img').click(function(){
			//assign next input element as object to var box							  
			var box = $(this).toggleClass('selected').find('input');
			//check is already checked
			if ($(box).is(':checked'))
			{
				//if so uncheck
				$(box).attr('checked', false);
			}
			else
			{
				//else check
				$(box).attr('checked', 'checked')	
			}
		});
	}	
	

	/* === for the event fallback calendar === */
	
	if ($('table.calendar').length > 0)
	{
		$('tr.calendar-row td:first-child').addClass('border-left');
		$('table.calendar tbody tr.calendar-row:last td.calendar-day').addClass('border-bottom');
	}

	/* =================================== *\
	Place holder input text 
	Add place holder link text over the search fields
	\* =================================== */
	
	$('input.placeholder').each(function(index) {
		var placeHolderText = $(this).val();
		if(placeHolderText !== '')
		{
			$(this).next('span.placeholder-text').hide();
		}
	});
	
	//for mouse clicks
	$('span.placeholder-text').live('focus click', function() {
									
		$(this).hide().parent('li').find('input.placeholder').stop().focus();
	})
	
	//for keyboard focus
	$('input.placeholder').focus(function(){
		$(this).next('span.placeholder-text').hide();
	});
	
	$('input.placeholder').blur(function(){
		var text = $(this).val();
		if (text == '')
		{
			$(this).next('span.placeholder-text').fadeIn(500);
		}
	});
	
	
	
	/* ===================================*/

	//remove last form li margin
	$('#form form ul.search-list li:last-child').css('margin-right', 0);
	$('#user-account .meta-img-wrapper:last-child').css('margin-right', 0);
	
	//automatically show business items with class show and add active class to trigger
	$('.business.show').find('.business-hidden').stop().show().parents('.business').find('a.view-more').toggleClass('active');
	
	//$('#user-account .meta-wrap').addClass('round');


	/* ==== make image wrapper the size of the image =====*/
	
	if($('img#page_image').length > 0)
	{
		var ih = $('img#page_image').height();
		if (ih > 0)
		{
			$('#page_image_wrapper').height(ih);
		}
	}
	
	
//=============================== ===========================================//
/*==== registration page ==== */

	if($('#register-options').length > 0)
	{
		$('#register-options .option:last-child').css('border-right', 'none');
	
		// keep track of the tallest column
		var tallest = 0;
		// loop through columns and find the tallest
		$('#register-options .option ul.points').each(function(){
			if ($(this).height() > tallest) tallest = $(this).height();
		});
		
		var special = $('#register-options .special ul.points').height();
			if(special > tallest) tallest = special;
		
		// loop through columns and adjust height as necessary
		$('#register-options .option ul.points').each(function(){
			// check if current column needs to be adjusted
			if ( $(this).height() < tallest ) $(this).height(tallest);
		});
		
		$('#register-options .special ul.points').height(tallest);	
		
		$('#register-options .option:first-child').addClass('round_left');
		$('#register-options .option:last-child').addClass('round_right');
	
		//as the special as a border, remove the border set for the others from the preceeding div
		//$('#register-options .special').prev('#register-options .option').css('border-right', 'none');
		$('#register-options .special').removeClass('round_right').addClass('round');
	
	}
	
//=============================== ===========================================//

	$('.business-meta a.view-gmap').click(function(){
									
		var parent = $(this).parents('.business').stop().find('.gmap-wrapper');		
		var href = $(this).attr('href');
		var frame = '<iframe class="gmap-iframe" width="100%" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'+href+'"></iframe>';

		var iframe = $(parent).find('.gmap-iframe').stop().length;
		if (iframe == 1)
		{
			$(parent).find('.gmap').remove();
			$(this).css('border', '1px solid #d8d8d8');
		}
		else
		{
			$(parent).prepend('<div class="gmap">' + frame + '</div>');
			$(this).css('border', '1px solid #51A32D');
			$(parent).show();
		}
		return false;
	});
	
	
//=============================== ===========================================//
/*==== quick info slider for business listings====*/
	
	if($('.business').length > 0)
	{
		$('.show').addClass('active');	
	}
	
	$('div.business-content, a.view-more').click(function(){
									
		var parent = $(this).parents('.business').stop();		
		var hidden = $(parent).find('.business-hidden').stop();
		var existing = $.cookie('chi-show');
		var id = $(parent).attr("id").split('-')[1];
		var newValue;

		quickViewControl(parent, hidden, existing, id, newValue);
		return false;
	});
	
	//the functional control of the expander
	function quickViewControl(parent, hidden, existing, id, newValue){
		if($(hidden).is(':hidden'))
		{
			$(hidden).slideToggle(500);
			$(parent).toggleClass('active');
			 
			 if (existing != null) 
			 {
				newValue = existing + ',' + id;
			 }
			 else 
			 {
				 newValue = id;
			 }
			 //write cookie of existing and new id
			 $.cookie('chi-show', newValue);
		}
		else
		{
			$(hidden).slideToggle(500);
			$(parent).toggleClass('active');	
			
			if(existing != null)
			{
				//make sure to remove trailing comma
				newValue = existing.replace(new RegExp(id, 'g'), '');
				$.cookie('chi-show', newValue);
			}
		}	
	}
	
	
	
/* ==== remove search listings and hide those already hidden ===*/

	//check if the cookie exists, if so give option to clear it. 
	if ($.cookie('chi-removed') !== null)
	{
		$('#business-removed').show();
	}
	
	//show the trigger. if js is off, its redundant so its hidden
	$('a.business-check-trigger').show();
	
	//when trigger clicked, record item id, add to cookie and remove
	$('a.business-check-trigger').click(function(){
		//get parent div
		var parent = $(this).parents('.business').stop();
		
		//record id number of parent
		 var id = $(parent).attr("id").split('-')[1];
		 var existing = $.cookie('chi-removed');
		 var newValue;
		 
		 if (existing != null) {
			newValue = existing + ',' + id;
		 }
		 else {
			 newValue = id;
		 }
		 //write cookie of existing and new id
		 $.cookie('chi-removed', newValue);
		 
		//fade out and slideup then delete from page
		$(parent).slideUp(500, function(){
			$(parent).remove();		
			$('#list_content .business').removeClass('odd').removeClass('even');
			$('#list_content .business:odd').addClass('odd');
			$('#list_content .business:even').addClass('even');
		});
		
		$('#business-removed:hidden').fadeIn(1000);
		return false; 
	});

//=== handles remove the items which are already in the cookie ===//
	function removeExisting(){
		//
		var cookie = $.cookie('chi-removed');
		if (cookie != null)
		{
			var cookieArray = cookie.split(',');
			
			$('.business').each(function(){
				var id = $(this).attr("id").split('-')[1];
	
				$(cookieArray).each(function(i, v){
					if (id == v)
					{
						$('#business-'+id).remove();
					}
				});
			});
		}
	}
	removeExisting();

//=== handles clearing the cookie and refreshing the page ===//
	$('a.clear-business-cookie').click(function(){
			$.cookie('chi-removed', null);
			window.location.reload(true);
			return false;
	});	
	


//=============================== ===========================================//
//=============================== client form slide===============================//

 
	$('a.logged-in').click(function()
	{
		var ua = $.browser;
		if (ua.version.slice(0,3) != '7.0') 
		{

			$('form#loginform').toggle();
			$(this).toggleClass('active');
			
			//if the form is visible move focus to the first input text element
			if($('form#loginform').is(':visible'))
			{
				$('form#loginform input[type=text]:eq(0)').focus();
			}		
			return false;
		}
	});
	


//=== handles remove the items which are already in the cookie ===//
	function showSelected(){
		//
		var cookie = $.cookie('chi-letter-show');
		if (cookie != null)
		{
			var cookieArray = cookie.split(',');
			
			$('.business-letter').each(function(){
				var id = $(this).attr("id").split('-')[1];

				$(cookieArray).each(function(i, v){
					if (id == v)
					{
						$('#letter-'+id).find('.business-list-wrapper').stop().show().parents('.business-letter').stop().find('a.business-type-trigger').addClass('active');

					}
				});
			});
		}
	}
	showSelected();



/* ========================================================================*/
//event specific listings
$(this).find('a.event-trigger').hide();
$('.user-event').hover(function(){
	$(this).find('a.event-trigger').show();
}, function(){
	$(this).find('a.event-trigger').hide();
});

/* === events actions on business fall back page ==== */

//hide content
$('.user-event-hidden').hide();
//add onclick event
$('.user-event-meta ul li a.view-date, a.event-view-more, .user-event-meta ul li a.view-phone').click(function(){
	$(this).parents('.user-event').stop().find('.user-event-hidden').stop().slideToggle(500); 
	return false; 
});

$('.user-event-meta a.view-gmap').click(function(){
								
	var parent = $(this).parents('.user-event').stop().find('.gmap-wrapper');		
	var href = $(this).attr('href');
	var frame = '<iframe class="gmap-iframe" width="100%" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'+href+'"></iframe>';

	var iframe = $(parent).find('.gmap-iframe').stop().length;
	if (iframe == 1)
	{
		$(parent).find('.gmap').remove();
		$(this).css('border', '1px solid #d8d8d8');
	}
	else
	{
		$(parent).prepend('<div class="gmap">' + frame + '</div>');
		$(this).css('border', '1px solid #51A32D');
		$(parent).show();
	}
	return false;
});



});
