var $j = jQuery.noConflict();
$j(document).ready(function(){
	$j('body').focus();						
 	$j('#loginForm input').blur();
	/*------------------- Galeries ------------------------------------------------------------------------*/
	//----------- Main Slideshow
	$j('#slideshow').cycle({ 
		fx:     'fade', 
		speed:   2000, 
		timeout: 4000, 
		next:   '#next',
        prev:   '#prev',
		before:  onBefore, 
		after:   onAfter,
		cleartype:  1
	 });
	
	function onBefore() { 
		$j('#output').animate({  
			height: "0"
		}, 500 );
		$j('#output').html("");
	}
	
	function onAfter(curr,next,opts) { 
		if (this.alt != '') {
			$j('#output').animate({ 
				height: "36px"
			}, 800 );
			//console.log(opts.slideCount);
			$j('#output').append('<p>' + this.alt + '</p>'); 

		}
		if (opts.slideCount==1) {
			$j('#slideshow').cycle('stop');
			return false;
		}
	}
	
	$j('#pause').click(function() { $j('#slideshow').cycle('pause'); return false; });
    $j('#play').click(function() { $j('#slideshow').cycle('resume'); return false; });
	

	/*-----------------------------------------------------------------------------------------*/
	
	$j('#trigger').animPanel({
		target: '#memberLogin',
		topStart: '0px',
		topEnd: '-85px',
		textOpen: 'Close',
		textClose: 'Member Log-In'
	});
	

	/*------------------------------------------------------------------------------------------------------*/
	//Search form validation
	$j("form#ajaxSearch_form").submit(function() {
		if ( ($j("input#ajaxSearch_input").val() == "") || ($j("input#ajaxSearch_input").val() == "Search here...")){
			//alert('Please type your name');
			alert('At least 3 characters are required for your search term');
			return false;
		}
    });

	/*-------------------------------------------------------------------------------------------------------*/
	//Main Menu
	$j("#mainNav ul li a").hoverIntent(function() { 
		$j(this).parent().find("ul").animate({ height: "toggle", opacity: "toggle" }, 450, "easeOutExpo");
		$j(this).parent().hover(function() {}, function(){ $j(this).parent().find("ul").slideUp('slow'); });		 
	}, function() {});
	$j('#mainNav ul li ul').hover(function() {
		$j(this).parents("li").find('a').addClass('selected');
	},
	function() {
		$j(this).parents("li").find('a').removeClass('selected');
	});

	
	var selector = $j('#mainNav ul li.last ul');
	var myLastItemWidth = selector.prev('a').outerWidth(true);
	var myLastUlWidth = selector.outerWidth(true);
	//my_alert(myLastUlWidth);
	if ($j.browser.msie) {
		var margin = myLastUlWidth - myLastItemWidth;
	} else {
		var margin = myLastUlWidth - myLastItemWidth - 1;
	}
	selector.css('left','-'+margin+'px');

	//Layout
	if (!$j('#subMenu').length ) {
		$j("#content").css({
			'background-image' : 'url(assets/templates/default_tpl/images/content_wide_bg.png)',
			'background-position' : 'top right',
			'width'	: '710px'
		}); 
	}
	$j('#content span.date:last').not('#content div.list_item span.date:last').css('padding-bottom','10px');
	
	$j('span#see_list').click(function() {	
		$j.cookie('list_view',true);
		var myUrl = '?ditto_tpl=benefits_list_only_tpl&ditto_paginate=0&ditto_display=all';
		location.search = myUrl;
		
	});
	$j('span#see_details').click(function() {
		$j.cookie('list_view', null);	
		location.href = '/about-iod/benefits-discounts';
	});

	if ($j.cookie('list_view')=='true')	{
		$j('body').find('span#see_list').addClass('active');
		$j('body').find('span#see_details').removeClass('active');
	} else {
		$j('body').find('span#see_details').addClass('active');	
		$j('body').find('span#see_list').removeClass('active');
	}
	
	
	/*---- Apply to the directory listing/search -*/
	var status = $j("input[name='extended_listing']:checked").val();
	if (status == 'Yes') {
		$j('#extendedListing').show();
	}
	
	$j("input[name='extended_listing']").click( function() {
	  var data = $j("input[name='extended_listing']:checked").val();
	  if (data == 'Yes') {
	 	$j('#extendedListing').slideDown();

	  } else {
		$j('#extendedListing').slideUp();
		$j('#company_profile').val('');
		$j('#company_logo').val('');
	  }

	});

	$j('.directory_list_item').each(function() {
		target = $j(this).find('.directory_item_logo').length;
		if (!target) {
			$j(this).addClass('lessDetails');
		}
	});

	$j('#members_search_form select').each(function()
	{
		var title = $j(this).attr('title');
		if(title != false)
		{
			$j(this).val(title);
		}
	});
	
	/*----- Pagination ---------------------------*/
	( !$j('a.ditto_page').length ) ? $j('span.ditto_currentpage').css('display','none') : '';
	if ($j('#company_profile').length ) {
	 $j('#company_profile').jqEasyCounter({
			'maxChars': 1000,
			'msgFontSize': '10px',
			'msgFontColor': '#9e0707',
			'msgFontFamily': 'Arial',
			'msgTextAlign': 'right',
			'msgAppendMethod': 'insertAfter'				
		});
	}

}); //Close document.ready

$j(window).bind("load", function() {								
	//Slideshows
	$j('.loading').each(function () {
		$j(this).hide();
	});
	$j('#banner').fadeIn('slow');
	$j('#slideshowWrapper').fadeIn('slow');
	
	
});

/*------- Equal hights ---------------------------------------------------------*/
function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}
/*------- Toggle animation (open - close) --------------------------------------*/
(function($) {
	$.fn.animPanel = function(options) {
		var opts = $.extend({}, $.fn.animPanel.defaults, options);
		return this.each(function() {
			$this = $(this);
			var o = $.meta ? $.extend({}, opts, $this.data()) : opts;				
			$this.toggle(function() {
				$(o.target).animate({ top: o.topStart }, 300, "easeOutBounce", function() { 
					$(o.target).find('#trigger').html(o.textOpen);
				});						   
			},function() {
				$(o.target).animate({ top: o.topEnd }, 300, "easeOutBounce", function() { 
					$('#topPanelOne ul li ul:visible').slideUp();	
					$(o.target).find('#trigger').html(o.textClose);
				});						   	
			})	
			
		});
	};
})(jQuery);
/*------ Easing animations -----------------------------------------------------*/
(function($) {
	$.easing.custom = function (x, t, b, c, d) { 
		var s = 1.70158;  
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b; 
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b; 
	}
	$.easing.easeOutExpo = function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	};
	$.easing.easeOutBounce = function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	};
})(jQuery);
/*----- Custom alert popup -----------------------------------------------------*/
function my_alert(alert_content) {
	$j('body').append('<div id="alert">'+alert_content+'<br /> <span class="close">Close</span></div>');
	$j("#alert").overlay({ 
		top: 200, 
		expose: { 
			color: '#fff', 
			loadSpeed: 500, 
			opacity: 0.6 
		}, 
		closeOnClick: true,
		api: true 
	}).load();
}