var slideshowSpeed = 10000;

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}


$(document).ready(function() {

	var interval;
	
	var activeContainer = 1;	
	var currentImg = 0;
	var animating = false;
	var navigate = function(direction) {
		if(animating) {
			return;
		}
		
		if(direction == "next") {
			currentImg++;
			if(currentImg == photos.length + 1) {
				currentImg = 1;
			}
		} else {
			currentImg--;
			if(currentImg == 0) {
				currentImg = photos.length;
			}
		}
		
		var currentContainer = activeContainer;
		if(activeContainer == 1) {
			activeContainer = 2;
		} else {
			activeContainer = 1;
		}
		
		showImage(photos[currentImg - 1], currentContainer, activeContainer, photos[currentImg]);
			
		
	};
	
	$('.albums').mouseover(function(){$(this).toggleClass('albums_bg')});
	$('.albums').mouseout(function(){$(this).toggleClass('albums_bg')});

	
	var currentZindex = -1;
	var showImage = function(photoObject, currentContainer, activeContainer, photo2load) {
		animating = true;
		
		currentZindex--;
		if(photoObject) {
			$("#headerimg" + activeContainer).css({
				"background-image" : "url(" + photoObject.image + ")",
				"display" : "block",
				"z-index" : currentZindex
			});
		} else {
			alert('Please upload header images.')
		}
		
		if(photo2load)
			$.preloadImages(photo2load.image);
			
		$("#headerimg" + currentContainer).fadeOut(function() {
			setTimeout(function() {
				animating = false;
			}, 500);
		});
	};
	
	
	navigate("next");
	
	// Start playing the animation
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
	
	
});

var spell_message1 = 'Selected text: '
var spell_message2 = 'Are you sure you want to send notification?'

function get_selection() {
		var userSelection;
		if (window.getSelection) 
			userSelection = window.getSelection();
		else if (document.selection) { 
			userSelection = document.selection.createRange();
			if (userSelection.text)
				userSelection = userSelection.text;
		}
		var r=confirm(spell_message1 + '"' + userSelection + '"' + '\r\n\r\n' + spell_message2);
		if (r==true) {
			location.href.substr(1.4)
		  $.post("/ajax/spell.php", { page: location.href, spell_text: String(userSelection)});
		}
		else
		  return false;
}

$(function(){
	$(window).keypress(function(event) {
	  if ((event.which == 13 && event.ctrlKey)){
		  get_selection()
		  event.preventDefault();
	  }
	});

	$('a[href][rel*=external]').each(function(i){this.target = "_blank";});

	$(".slide_menu").click(function() {
		if(!$(this).next().is(":visible")) {
			s_m = "selected-menu"
			
			$(".level2").slideUp("fast")
			$(".level2").parent().removeClass(s_m)
			$(".selected-menu").removeClass(s_m)
			$(this).next().slideToggle("slow")
			$(this).parent().toggleClass(s_m)
		} else {
			$(this).next().slideUp("slow")
			$(this).parent().toggleClass(s_m)
		}
	})
	
	$(".selected-submenu").parent().parent().addClass('selected-menu');
	$(".selected-submenu").parent().show();
	
	$("#alt_block_title").click(function() {
		$(this).toggleClass("alt_block_bg1");
		$(this).toggleClass("alt_block_bg2");
		$("#alt_block_text").slideToggle();
	})
});

/* Acoordion on pages */
$(document).ready(function() {
$('div.accordionButton').click(function() {
if($(this).next().is(':visible')) {
$(this).next().slideUp('normal');
} else {	
$(this).next().slideDown('normal');
$('html,body').animate({scrollTop: $(this).offset().top}, 500);
}
});
$("div.accordionContent").hide();
});
/* Acoordion on pages */

/* Easy Tooltip */ 
(function($) {

	$.fn.easyTooltip = function(options){
	  
		// default configuration properties
		var defaults = {	
			xOffset: 10,		
			yOffset: 25,
			tooltipId: "easyTooltip",
			clickRemove: false,
			content: "",
			useElement: ""
		}; 
			
		var options = $.extend(defaults, options);  
		var content;
				
		this.each(function() {  				
			var title = $(this).attr("title");				
			$(this).hover(function(e){											 							   
				content = (options.content != "") ? options.content : title;
				content = (options.useElement != "") ? $("#" + options.useElement).html() : content;
				$(this).attr("title","");									  				
				if (content != "" && content != undefined){			
					$("body").append("<div id='"+ options.tooltipId +"'>"+ content +"</div>");		
					$("#" + options.tooltipId)
						.css("position","absolute")
						.css("top",(e.pageY - options.yOffset) + "px")
						.css("left",(e.pageX + options.xOffset) + "px")						
						.css("display","none")
						.fadeIn("fast")
				}
			},
			function(){	
				$("#" + options.tooltipId).remove();
				$(this).attr("title",title);
			});	
			$(this).mousemove(function(e){
				$("#" + options.tooltipId)
					.css("top",(e.pageY - options.yOffset) + "px")
					.css("left",(e.pageX + options.xOffset) + "px")					
			});	
			if(options.clickRemove){
				$(this).mousedown(function(e){
					$("#" + options.tooltipId).remove();
					$(this).attr("title",title);
				});				
			}
		});
	  
	};

})(jQuery);
/* Easy Tooltip */
