$(document).ready(function() {
	
	/* ------------------------------------------------- */
	/* HOMEPAGE SLIDER                                   */
	/* ------------------------------------------------- */
	
	// START SLIDER FUNCTIONS
	function slider_mouse_enter(e) {
		if ($(this).find('.fade-cnt:animated').length==0) {
			$(this).find('.fade-cnt').fadeIn(200);
		}
	}
	function slider_mouse_out(e) {
		if ($(this).find('.fade-cnt:animated').length==0) {
			$(this).find('.fade-cnt').fadeOut(150);
			$('#slides-nav').fadeOut(150);
		}	
	}
	function move_to_next() {
		if (mouse_in_slider) {
			return;
		}
		if (slides_idx==$('#slides > .slide').length - 1) {
			slides_idx = 0;
		} else {
			slides_idx++;
		}
		move_slider();
	}
	// END SLIDER FUNCTIONS
	
	// SHOW STUFF ON HOVER
	$('#new-slider').hover(slider_mouse_enter, slider_mouse_out);
	
	// START HOMEPAGE SLIDER
	var slides_idx = 0;
	var slides_interval = null;
	var mouse_in_slider = false;
	
	$('.slide-nav.prev').addClass('disabled');
	$('.slide-page:first').addClass('active');
	
	function move_slider() {
		var slider_width = $('#new-slider').width();
		var pos = slides_idx * slider_width * -1;
		$('#slides').animate({left: pos + "px"},{queue:false,duration:550,easing:'easeOutExpo'});
		$('.slide-nav').removeClass('disabled');
		
		if (slides_idx==0) {
			$('.slide-nav.prev').addClass('disabled');
		} else if (slides_idx==$('#slides > .slide').length - 1) {
			$('.slide-nav.next').addClass('disabled');
		}
		$('.slide-page').removeClass('active');
		$('.slide-page:eq(' + slides_idx + ')').addClass('active');
	}
	
	$('.slide-page').click(function() {
		slides_idx = $('.slide-page').index(this);
		move_slider();
		return false;
	});
	
	$('.slide-nav').click(function() {
		if ($(this).hasClass('disabled')) {
			return false;
		}
		if ($(this).hasClass('prev')) {
			slides_idx--;
		} else if($(this).hasClass('next')) {
			slides_idx++;
		} else {
			throw('This should not happen');
		}
		move_slider();
		return false;
	});
	// END HOMEPAGE SLIDER
	
	
	
	// START KEYBOARD CONTROL
	function keyNext() {
		if (slides_idx==$('#slides > .slide').length - 1) {
			// Do nothing
		} else {
			slides_idx++;
			move_slider();
		}
	}
	
	function keyPrev() {
		if (slides_idx == 0) {
			// Do nothing
		} else {
			slides_idx--;
			move_slider();
		}
	}
	
	$(document).keydown(function(e) {
		if(e.which == 39) { keyNext(); }
		if(e.which == 37) { keyPrev(); }
	});
	// END KEYBOARD CONTROL
	

	
	// START AUTO-ROTATE CODE
	if (autoRotate == 1){
		var timer;
	  	var donext = function (x){ move_to_next(); }
	  
	  	var dotimer = function (){
	    	if(timer != null) {
	      		clearInterval(timer);
	    	}
	  
	    	timer = setInterval(function() {
	      		donext();
	    	}, 4500); // Change the time in between rotations here (in milliseconds)   
	  	}
	  
	  	dotimer();
	  	
	  	$('#new-slider,#slides-list').hover(function(){
	  		clearInterval(timer);
	  	},function(){
	  		dotimer();
	  	});
  	}
  	// END AUTO-ROTATE CODE
  	
  	// Comment Box Expansion (like Facebook)
	$("#comment, .wpcf7 textarea").elastic();
	$("#comment, .wpcf7 textarea").blur(function(){
		if (this.value == '') {
			$(this).animate({"height": "23px"}, "fast");
		}
	});
	
	// Navigation Dropdowns
	$("ul.navigation-top-ul").superfish();
	$("ul.navigation-top-ul ul li:last-child a").addClass("no-border");
	$("ul.sidebar-subpages > ul > li:last-child a").addClass("no-border");
	$("li.widget > ul > li:last-child a").addClass("no-border");
	$('.category-switch-list > ul li:last-child').addClass("no-border");
	
	
	$('#category-switcher').hoverIntent(function(){
		$(this).addClass('point-down');					 
		$(this).find('.category-switch-list').slideDown("fast");										 
	}, function(){
		$(this).removeClass('point-down');		
		$(this).find('.category-switch-list').hide();
	});
	
	
	if (galleryType) {
		$('.view').removeClass('active');
		$('#'+galleryType).addClass('active');
	} else {
		$('#list').addClass('active');
	}
	
	// View Switcher
	$(".view-switcher > .view").click(function(){
		if (animationOn == false) {
			$('.view').removeClass('active');
			$(this).addClass('active');
		}
	});
	
	// Full View Hover Faders
	$(".view-block").hover(function(){
		$(this).find(".single-image-block-fader").fadeIn(300);
	}, function(){
		$(this).find(".single-image-block-fader").fadeOut(300);
	});
	
	
	/* ---------------------------------- */
	/* View Styles AJAX Loader            */
	
	// START IF GALLERYTYPE
	if (galleryType) {
		
		function getFromURL(name){
			name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
			var regexS = "[\\?&]"+name+"=([^&#]*)";
			var regex = new RegExp( regexS );
			var results = regex.exec( window.location.href );
			if( results == null ){
				return "";
			} else {
				return results[1];
			}
		}

		// Show Loading Graphic
		function showLoading(){
			loading.show();
		}
		//hide loading bar
		function hideLoading(){
			loading.fadeOut("fast",function(){
				new_content.fadeIn("normal");
				animationOn = false;
			});
		}

		var viewType = getFromURL('type');

		// Category Dropdown List
		var currentCatList=new Array();
		var countItOut = 0;
		$(".category-switch-list a").each(function(){ 
			currentCatList[countItOut] = [this.href];
			var currentHREF = this.href;
			if (viewType) {
				var newHREF = '?type='+viewType;
			} else {
				var newHREF = '?type=thumb';
			}
			$(this).attr("href",currentHREF +''+ newHREF);
			countItOut++;
		});
		
		// Breadcrumbs list
		var breadCatList=new Array();
		var countItOut = 0;
		$("#breadcrumbs a").each(function(){ 
			breadCatList[countItOut] = [this.href];
			if (countItOut > 0) {
				var currentHREF = this.href;
				if (viewType) {
					var newHREF = '?type='+viewType;
				} else {
					var newHREF = '?type=thumb';
				}
				$(this).attr("href",currentHREF +''+ newHREF);
			}
			countItOut++;
		});
		
		// References  
		var views = $(".view-switcher > .view-ajax");
		var loading = $("#loading");
		var new_content = $('#featured-area');
		var animationOn = true;
		
		showLoading();
		new_content.hide();
		new_content.load(templateURL+"/views/"+galleryType+".php", galleryData, hideLoading);
		
		//Manage click events
		views.click(function(){
							 
			galleryData = $(this).attr('rel');
							 
			if (animationOn == false && this.id != current_view) {
				
				animationOn = true;
							 
				//load selected section
				switch(this.id){
					case "list":
						if (current_view != "list") {
							showLoading();
							new_content.hide();
							new_content.load(templateURL+"/views/list.php", galleryData, hideLoading);
							current_view = "list";
							
							countItOut = 0;
							$(".category-switch-list a").each(function(){
								var currentHREF = currentCatList[countItOut];
								var newHREF = '?type=list';
								$(this).attr("href",currentHREF +''+ newHREF);
								countItOut++;
							});
							
							countItOut = 0;
							$("#breadcrumbs a").each(function(){
								if (countItOut > 0) {
									var currentHREF = breadCatList[countItOut];
									var newHREF = '?type=list';
									$(this).attr("href",currentHREF +''+ newHREF);
								}
								countItOut++;
							});
						}
						break;
					case "thumb":
						if (current_view != "thumb") {
							showLoading();
							new_content.hide();
							new_content.load(templateURL+"/views/thumb.php", galleryData, hideLoading);
							current_view = "thumb";
							
							countItOut = 0;
							$(".category-switch-list a").each(function(){
								var currentHREF = currentCatList[countItOut];
								var newHREF = '?type=thumb';
								$(this).attr("href",currentHREF +''+ newHREF);
								countItOut++;
							});
							
							countItOut = 0;
							$("#breadcrumbs a").each(function(){
								if (countItOut > 0) {
									var currentHREF = breadCatList[countItOut];
									var newHREF = '?type=thumb';
									$(this).attr("href",currentHREF +''+ newHREF);
								}
								countItOut++;
							});
						}
						break;
					default:
						//hide loading bar if there is no selected section
						hideLoading();
						break;
				}
			}
		});
	} // END IF GALLERYTYPE
 	
});