
$(document).ready(function(){
 	/*------------------- Galeries ------------------------------------------------------------------------*/
	//----------- Main Slideshow
	
	$('#slideshow').cycle({ 
		fx:     'fade', 
		timeout: 10000, 
		speed:   10, 
		speedIn: 2500,
		speedOut: 2500,
		next:   '#next',
        prev:   '#prev',
		before:  onBefore, 
		after:   onAfter
	 });
	
	function onBefore() { 
		
		$('#output').fadeOut(2000);		
	}
	
	function onAfter(curr,next,opts) { 
		if (this.alt != '') {
			$('#output').html("");
			$('#output').append('<p>' + this.alt + '</p>'); 
			$('#output').fadeIn(2000);
		}
		if (opts.slideCount==1) {
			$('#slideshow').cycle('stop');
			return false;
		}
	}
	
	$('#pause').click(function() { $('#slideshow').cycle('pause'); return false; });
    $('#play').click(function() { $('#slideshow').cycle('resume'); return false; });

	/*-----------------------------------------------------------------------------------------*/
	if ($('span.date').length) {
		$('span.date').prev().css('paddingBottom','0');	
	}
	/*-----------------------------------------------------------------------------------------*/
	$.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;
		}
	};
	
	//$('.acc_item').eq(0).show();
	$('.acc_item h1').click(function() { 
		var target = $(this).next("div.acc_content");	
		target.animate({ height: "toggle", opacity: "toggle" }, 700 );
		$('div.acc_content').not(target).slideUp();
	});

	/*-------------------------------------------------------------------------------------------*/
	//Newsletter form validation
	$("form#subscribeForm").submit(function() {
		if ( ( $("input#name").val() == "") || ($("input#name").val() == "Your name")) {
			//alert('Please type your name');
			$('span#valResult').html('Please type your name');
			return false;
		}
		if ( ( $("input#tkhilj-tkhilj").val() == "") || ($("input#tkhilj-tkhilj").val() == "Your email address")) {
			//alert('Please type your email address');
			$('span#valResult').html('Please type your email address');
			return false;
		} else {
			var rules  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if ($("input#tkhilj-tkhilj").val().match(rules)) {
				return true;
			} else {
				//alert('You povided wrong email address');
				$('span#valResult').html('Email address is not valid');
				return false;
			}
		}
    });

	/*-----------------------------------------------------------------------------------------*/
	//XML Feed read more link fix
	$('#market_news div').each(function() {
		var readon = $(this).find('a.readon');								
		$(this).find('p').append(' ').append(readon);
	});
	/*------------------------------------------------------------------------------------------*/
	// Add RSS Icon to News and Events heading
	$('#content h1:contains("Stembridge News"), #content h1:contains("Seminars")').append('<a href="feed.rss"><img src="assets/templates/default_tpl/images/feed.gif" /></a>');
	/*-------------------------------------------------------------------------------------------*/
	 $('input#newsletter').attr('checked', true);
	 /*----------------------------------------------------------*/
	 
	 var menuW = $('#mainNav').width();
	 var howMany = $('#mainNav ul li a').size();
	 var itemWidth =  Math.floor(menuW/howMany);
	 
	 var o = menuW-(itemWidth*howMany);
	 var lastItem = itemWidth+o;
	 
	 /*
	 console.log('Menu width:'+menuW);
	 console.log('How many items:'+howMany);
	 console.log('Item width:'+itemWidth);
	 console.log('Offset:'+o);
	 */
	 $('#mainNav ul li a').width(itemWidth);
	 $('#mainNav ul li a:last').css('width',lastItem);
							  
}); //Close document.ready

$(window).bind("load", function() {							
    //-----------  Preload images for 
	$('#loading').hide();
	$('#slideshowWrapper').fadeIn('slow');

});