/**
 * Global functions and controls
 * Copyright (c) 2011 KILO - admin(at)kilohq(dot)com | http:kilohq.com
 * v1.0 - 01/2011
 * @project Pizza Luce
 * @author Martin Olson
 *
 */

$(document).ready(function() {

	
	/* ---------------------------------------
		Global Navigation Dropmenu
	--------------------------------------- */

	$('#sitenav li.submenu').hover(
		function () {
			$(this).addClass('hover');
			$('ul', this).show();
		}, 
		function () {
			$('ul', this).stop(true, true);
			$(this).removeClass('hover');
			$('ul', this).hide();		
		}
	);
	
	$('a.toplink').click(function(e){
		e.preventDefault();
	})
	
	/* ---------------------------------------
		Homepage slideshow
	--------------------------------------- */
	
	
	if(document.getElementById('feature-slideshow')) {
	
		$('#slideshow-controls').show();	
		
		$('.slideshow').cycle({ 
		    fx:      'scrollLeft', 
		    speed:	 1000, 
		    timeout: 9000,
		    prev:	 '#prevbtn',
		    next:	 '#nextbtn'
		});
	
	}
	
	if(document.getElementById('feature-fma')) {
	
		var fmacol = $('.fma-col');
		var fmatxt = $('.fma-details');
		var fmaimg = $('.fma-col img');
	
		/* css styles */
		
		fmacol.css({
			'height'	: '150px',
			'overflow'	: 'hidden',
			'position'	: 'relative'
			})
		fmatxt.css({
			'position'	: 'absolute',
			'top'		: 0,
			'left'		: 0,
			'marginTop'	: 0
			})
		fmaimg.css({
			'position'	: 'absolute',
			'top'		: 0,
			'left'		: 0
			})
		
		/* actions */
	
	
		fmacol.hover(
			function(){
				$(this).find('img').stop(true, false).animate({
		    		top: '140'
					}, 300);
			},
			function(){
				$(this).find('img').stop(true, false).animate({
		    		top: '0'
					}, 300);
			}
		)
	}
	
	/* ---------------------------------------
		Simple Toggle Dropmenu
	--------------------------------------- */
	
	$('#togglelist').css({
		'position'	: 'absolute',
		'top'		: $('#togglenav').outerHeight(),
		'left'		: 0,
		'z-index'	: 100
		});
	$('#togglelist').hide();
    
    
    $('#togglenav').hover(
    	function() {
    		$(this).css({'cursor' : 'pointer'});
    		$("#togglelist").show();
    		$("#togglenav").addClass('menu-open');
    		$("#menu-filteropts").stop(true,false).fadeTo(150,0.2);
    	},
    	function() {
   
    	}
    )
    $('#menutoggle').hover(
    	function() {
    	},
    	function() {
   			$("#togglelist").hide();
    		$("#togglenav").removeClass('menu-open');
    		$("#menu-filteropts").stop(true,false).fadeTo(150,1.0);
    	}
    )
    $('#togglelist li').hover(
    	function() {
    		$(this).css({'cursor' : 'pointer'});
    		$(this).addClass('hover');
    		//$(this).children('span').fadeTo(150,1.0);
    	},
    	function() {
    		$(this).removeClass('hover');
    		//$(this).children('span').fadeTo(150,0.6);
    	}
    )
    $('#togglelist li').click(function() {
    
    	var listItemUrl = $(this).find('a').attr('href');
    	
    	
    	top.location = listItemUrl;  
  		return false;
    
    });
    
    /* ---------------------------------------
		Return to Top
	--------------------------------------- */
	
	if(document.getElementById('topreturn')) {
	
		$('#topreturn').css({ 'top' : -210 });
	
		$('#topreturn').hover(
	    	function() {
	    		$(this).animate({top: '+=5'}, 300);
	    	},
	    	function() {
	    		$(this).animate({top: '-=5'}, 300);
	    	}
	    )
    }
    
    
    if(document.getElementById('menu')) {
    	var _scrollActive = true;
    	$(window).scroll(function(e){
    		
			if (_scrollActive == true){
				//checkForScroll();
			}
	    });
		//checkForScroll();
    }
   
});

function checkForScroll(){
	/*
if (isScrolledIntoView($("#menu-cat2")) <= 200){
		var elem = $("li#opt-cat2");
		if(!elem.hasClass('activeCat')) {
		
			// loop thru list items and reset position
			
			resetMenuOptions();
			
			// animate selected cateogry list item
		
			elem.find('span:first').fadeTo(listFadeDur,listFadeUp, function() {
				elem.removeClass('lgray').addClass('cyan').end();
			});
			elem.find('span:first').stop(true, false).animate({
	    		marginLeft: listSlideXPos
				}, listSlideDur, function() {
				elem.parent('li').addClass('activeCat');
			});
			
			// get menu category id and set as current
			
			var selectedCat = elem.attr('id');
				selectedCat = selectedCat.replace('opt','menu');
				selectedCat = '#' + selectedCat;
						
			currCategory = selectedCat;
			
			// scroll page target
			//scrollMenu('animate');
		}
	}
*/
}

function isScrolledIntoView(elem){
    var fromTop = $(window).scrollTop();
	var elemYpos = elem.position().top;
		elemYpos = elemYpos - fromTop;
	//console.log(fromTop +" from top / Element "+elem.attr("id")+" is "+elemYpos+" from top");
    var val = elemYpos;
	return val;
}

function resetOthers(elem){
	$('ul#sitenav li').children().not(elem).removeClass('curr');
}

// Scroll detection to auto show/hide top link

var scrollCnt = 0;

$(window).scroll(function(){
	
	var fromTop = $(window).scrollTop();
	var elemYpos = $('#content').position().top;
		elemYpos = elemYpos - fromTop;
	
	if(elemYpos > -100) {
		$('#topreturn').toggleTopReturnLink("hide");
	} else {
		$('#topreturn').toggleTopReturnLink("show");
	}
	
});


// Function for Return Top Link

jQuery.fn.toggleTopReturnLink = function(str) {
	if(str == "show") {
		return this.animate({top: '-130'}, {queue:false, duration:300});
	} else {
		return this.animate({top: '-210'}, {queue:false, duration:300});
	}
};

// Function for fade toggle

jQuery.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);
}; 

