(function($){

$(document).ready(function() {
	
	// Scroll to Top
	$('a#scrolltop').click(function(){
		$('html,body').animate({scrollTop: 0}, 800);
		return false;
	});
	
	// Navigation menu
	$('#main_menu > li:gt(0) > a').each(function(index,button){
		$(button).parent('li').children('ul').css({'left': ($(button).parent('li').width()/2)+'px'});
		$(button).click(function(){
			return false;
		});
		$(button).hover(function(){
			$(button).parent('li').children('ul').toggle();
			$(button).parent('li').children('ul').hover(function(){
				$(this).show();
				$(this).parent('li').addClass('current_page_item');
			},function(){
				$(this).hide();
				$(this).parent('li').removeClass('current_page_item');
			});
		},function(){
			$(button).parent('li').children('ul').toggle();
		});
	});		
	
	// We only want these styles applied when javascript is enabled
	$('div.content').css('display', 'block');

	// Initially set opacity on thumbs and add
	// additional styling for hover effect on thumbs
	var onMouseOutOpacity = 0.67;
	if($('#thumbs').length != 0) {
		$('#thumbs ul.thumbs li').opacityrollover({
			mouseOutOpacity:   onMouseOutOpacity,
			mouseOverOpacity:  1.0,
			fadeSpeed:         'fast',
			exemptionSelector: '.selected'
		});
	}
	
	// Initialize Advanced Galleriffic Gallery
	if($('#thumbs').length != 0) {
		var gallery = $('#thumbs').galleriffic({
			delay:                     2500,
			numThumbs:                 15,
			preloadAhead:              10,
			enableTopPager:            true,
			enableBottomPager:         true,
			maxPagesToShow:            7,
			imageContainerSel:         '#slideshow',
			controlsContainerSel:      '#controls',
			captionContainerSel:       '#caption',
			loadingContainerSel:       '#loading',
			renderSSControls:          true,
			renderNavControls:         true,
			playLinkText:              'Play Slideshow',
			pauseLinkText:             'Pause Slideshow',
			prevLinkText:              '&lsaquo; Previous Photo',
			nextLinkText:              'Next Photo &rsaquo;',
			nextPageLinkText:          'Next &rsaquo;',
			prevPageLinkText:          '&lsaquo; Prev',
			enableHistory:             false,
			autoStart:                 false,
			syncTransitions:           true,
			defaultTransitionDuration: 900,
			onSlideChange:             function(prevIndex, nextIndex) {
				// 'this' refers to the gallery, which is an extension of $('#thumbs')
				this.find('ul.thumbs').children()
					.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
					.eq(nextIndex).fadeTo('fast', 1.0);
			},
			onPageTransitionOut:       function(callback) {
				this.fadeTo('fast', 0.0, callback);
			},
			onPageTransitionIn:        function() {
				this.fadeTo('fast', 1.0);
			}
		});
	}
	
	// Contact form AJAX
	if($('#contactform').length != 0) {
		$("#contactform").submit(function(){
			return false;
		});
		$("#btn_submit").click(function(){
			SubmitAjax();
		});
		
		var SubmitAjax = function(){
			var author = $("#author").attr("value");
			var email = $("#email").attr("value");
			var message = $("#message").attr("value");
			$("#log").html("");
			$("#log").addClass("loading");
			jQuery.ajax({
				type: "POST",
				url: "http://www.daniilsimkin.com/1/wp-content/themes/daniil2010/sendmail.php",
				dataType: "html",
				data: "author=" + author + "&email=" + email  + "&message=" + message,
				success: function(response){
					$("#log").html(response);
					$("#log").removeClass("loading");
				},
				error: function(){
					$("#log").html("<span class=\"error\">An error occured while connecting to the server.</span>");
					$("#log").removeClass("loading");
				}
			});
		}
	}
});


$(window).load(function() {

	/**********************************************************
	 * FeedBurner counters
	 **********************************************************/
	
	function get_feedburner_count(uri, callback) {
		var request_url = theme_path + "/feedburner.php";
		$.get(request_url, {uri:uri}, callback);
	};

	get_feedburner_count('DaniilSimkin', function(result){
		// print the counter and show it
		$(".feedburner-counter").text(result + ' ').css('display','inline');
	});
	
});


})(jQuery);