// jQuery.preloadImages = function(){
// 	for(var i = 0; i<arguments.length; i++) {
// 		jQuery("<img>").attr("src", arguments[i]);
// 	}
// } 

$(document).ready(function() {

	//nav carousel
	var carousel = $('#nav-carousel');
    
	//$.preloadImages(carousel.find("img"));

	// main functions
	jQuery.showItem = function(param){
		//carousel.find('li').css("width","", "left","", "right","");
	    $(param).addClass("show").animate({ width: "530px" }, { duration: 500, easing: "easeInOutExpo" });
		//$(param).append('<img class="loading" src="assets/images/ajax-loader.gif" alt="" />');
		$(param).find('img').fadeIn({ duration: 100 });   
		$(param).find('div').fadeIn({ duration: 100 });
		//$(param).find('.button').fadeIn('fast');
	}

	jQuery.hideItem = function(param) {
		carousel.find('li').css("width","", "left","", "right",""); 
		$(param).removeClass("hover").removeClass("show").css("width","", "left","", "right","");
		//$(param).find('img.loading').replaceWith();     
		$(param).find('img').fadeOut('fast');
		$(param).find('div').fadeOut('fast');
		//$(param).find('.button').fadeOut('fast');
	}

	jQuery.hoverItem = function(param) {
		$(param).addClass("hover");
	}
	
	jQuery.compressCarousel = function(){
		carousel.css("height", "125px");
		carousel.hover(
			function(){
				carousel.animate({ height: "372px" }, { queue: false, duration: 600, easing: "easeInOutBack" });
			},
			function(){ 
			}                                            
		); 
		// $('#content-carousel').hover(
		// 	function(){
		// 	},
		// 	function(){ 
		// 	 	carousel.animate({ height: "125px" }, { queue: false, duration: 1000, easing: "easeInOutBack" });
		// 	}                                            
		// ); 
	} 
	
	jQuery.insertSlide = function(){
		var slide = $('.slide a');

		slide.each(function(){
			$(this).click(function(){
				slide.removeClass("current");
				$(this).addClass("current");
			});
		});
		
		$("#content-slide").addClass("enhanced").jCarouselLite({
			btnNext: "#next",
			btnPrev: "#prev",
		    visible: 1,
			speed: 500,
			btnGo:
				[".slide .1", ".slide .2",
			    ".slide .3", ".slide .4",
			    ".slide .5", ".slide .6",
			    ".slide .7", ".slide .8",
			    ".slide .9", ".slide .10",
			    ".slide .11", ".slide .12"]
		});

		$('#prev').click(function(){
			$('#content-slide li').fadeOut('fast').fadeIn();
		});
	
		$('#next').click(function(){
			$('#content-slide li').fadeOut('fast').fadeIn();
		});
	}
	
	jQuery.insertIntro = function(){
		//$('#content-carousel').append('<div id="content-intro"><h2>See the invisible Do the impossible</h2><h2>Explore each gallery with its 53 facts of Africa.</h2></div>').hover(function(){
		$('#content-carousel').append('<img class="intro" src="assets/images/facts-of-africa.jpg" alt="Facts of africa" />').mouseover(function(){
			$('img.intro').fadeOut({ duration: 2000 });
		}); 
	}
	
	// nav carousel
	
	//lazy load - http://www.appelsiini.net/projects/lazyload
	carousel.find('img').lazyload({
		placeholder: "assets/images/ajax-loader.gif",
		effect: "fadeIn",
		container: carousel
	});
	
	carousel.find('img').hide();
	carousel.find('div').hide();
	//carousel.find('.button').hide();  
	
	var count = 1;
    
	carousel.find('li').each(function() {
	    
		var item = $(this);
		var prev = count-1;
		var next = count+1;

		//item.find('.button').hide();  
		
		//add prev button
		// if(count != 1) {
		// 	item.append('<a id="prev" class="button" href="#nav-' + prev + '" title="View the previous fact">&nbsp;</a>'); 
		// 	item.find("#prev").toggle(
		// 		function(){
		// 			$.showItem($(this).attr("href"));
		// 	   	},
		// 		function(){
		// 			$.showItem($(this).attr("href"));
		// 	   	}  
		// 	);
		// }     

		//add next button
		// if(count != 53) { 
		// 	item.append('<a id="next" class="button" href="#nav-' + next + '" title="View the next fact">&nbsp;</a>');
		//     item.find("#next").toggle(
		// 		function(){
		// 			$.showItem($(this).attr("href"));
		// 		},
		// 		function(){
		// 			$.showItem($(this).attr("href"));
		// 		}
		// 	);
		// }  
		
		item.hover(
			function(){
				$.hoverItem(item);
			},	
			function(){
				$.hideItem(item); 
			}
		);

		item.click(function(){  
			$.showItem(item);    
		});
		count++
	});
	
	//carousel buttons
	$('#content-carousel').append('<a href="#" class="left" title="Slide gallery left">&nbsp;</a>');      
	$(".left").click(function(e){
		e.preventDefault();
		carousel.animate({ left: "0", queue: false, duration: 600 }).addClass("carousel-right");        
	});
	
	$('#content-carousel').append('<a href="#" class="right" title="Slide gallery right">&nbsp;</a>');          
	$(".right").click(function(e){
		e.preventDefault();    
		carousel.animate({ left: "-376px", queue: false, duration: 600 }).addClass("carousel-right");  	 
	});  
	
	//nav-carousel   
	// var theWindow = $(window);
	// var currentWidth = theWindow.width();
	// var minWidth = 1024;
	// 
	// theWindow.resize(function() { // on window resize  
	// 	var resizedWidth = theWindow.width();
	// 	if (resizedWidth < minWidth) { // if less
	//         	$.myPlugin.showButtons();
	// 		$.myPlugin.compressCarousel();
	// 	} else { //if more
	//         	$.myPlugin.showButtons();
	// 		$.myPlugin.compressCarousel();
	// 	}
	// });
	// 
	// if (currentWidth < minWidth) { // if less
	// 	$.myPlugin.showButtons();
	// 	$.myPlugin.compressCarousel();         
	// } else { // if more
	//     	$.myPlugin.showButtons();
	// 	$.myPlugin.compressCarousel();     
	// } 
	
	//nav-facts
	var facts = $('#nav-facts');
	var div = facts.find('div').hide();      
	var btn =  facts.find('h4');
	
	btn.wrapInner('<a href="#"></a>').click(function(e){
		e.preventDefault();
		if(div.is(':visible')){
			btn.removeClass("selected");  
				div.slideUp({ duration: 200 });
	        } else {
			btn.addClass("selected");   
	           	//div.slideDown({ duration: 200 });
	        }
		}
	);
	
	div.hover(
		function(){
		},
		function(){
			btn.removeClass("selected");
			div.slideUp({ duration: 200 });
		}		
	);
	
	var title = $("#content-carousel h3 span");
	
	facts.find('li').each(function(){
		var id = $(this).attr("id");
		var link = $(this).find('a');
		var name = $(this).find('a').text();
		
		link.click(function(e){
			e.preventDefault();  
			facts.find('a').removeClass("current");
			$(this).addClass('current');
			title.html(name); 
			//carousel.load("assets/includes/" + id + ".php");
			$.ajax({
				url: "assets/includes/" + id + ".php",
				cache: false,
				success: function(html){
					carousel.html(html);
				}
			}); 
		});
	}); 
	
	//nav-filter
	var filter = $('#nav-filter');
	
	filter.find('div').hide();
	filter.find('li').each(function(){
		$(this).hover(
			function(){
				$(this).find('div').show();
				$(this).find('a').each(function(){
				   	$(this).click(function(){
						filter.find('a').removeClass("current");
						$(this).addClass("current");
						return false;
				   	}); 
				});
			},
			function(){
				$(this).find('div').hide();
			}
		);
	});
	
	//content-menu
	var menu = $('.content-menu');
	
	menu.addClass('enhanced');
	menu.find('.content').hide();
	menu.find('li').each(function(){

		var head = $(this).find('.head');
		var content = $(this).find('.content');
		$(this).hover(
			function(){
				head.addClass('current');
				content.stop().slideDown({ duration: 500, easing: "easeOutQuint" });
			},
			function(){
				head.removeClass('current');
				content.slideUp({ duration: 500, easing: "easeOutQuint" });
			} 
		);
	});
	
	//content-list
	var list = $('.content-list');
	
	list.addClass('enhanced');
	list.find('dd').hide();
	list.find('dt').wrapInner('<a href="#"></a>').click(function(e){
		e.preventDefault();
		
	 	var head = $(this);
		var content = $(this).next();
	
		if (content.is(':visible')){
			head.removeClass("open");   
			content.slideUp().animate({ duration: 600, easing: "easeOutQuad" });
	    } else {
			head.addClass("open");   
	       	content.slideDown({ duration: 600, easing: "easeOutQuad" });
	    }
	});    
	
	//switch
	var see = $('.switch');
	
	see.find('img').hide();
	see.each(function(){
		$(this).click(function(){
			$(this).find('img').fadeIn();      			
			$(this).find('a').hide();
		});
	});	

	//members
	var members = $('#members');
	
	members.find('dd').hide();
	members.find('dd.open').show();
	members.find('dt').wrapInner('<a href="#"></a>').click(function(e){
		e.preventDefault(); 
		
		var head = $(this);
		var content = $(this).next();
	
		members.find('dt').removeClass('open');
		members.find('dd').removeClass('open').hide();   
		head.addClass("open");
		content.addClass("open").fadeIn({ duration: 800 });
	   	return false;
	});
	
	//smooth page scrolling
    // $('a[href*=#]').click(function() {
    // 	if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
    //     	&& location.hostname == this.hostname) {
    //     	var $target = $(this.hash);
    //         $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
    //         if ($target.length) {
    //             var targetOffset = $target.offset().top;
    //             $('html,body').animate({scrollTop: targetOffset}, 1000);
    //             return false;
    //         }
    //     }
    // });
});
