jQuery(document).ready(function() {
	
	if(jQuery('.nav-container').position() == null) {
		return;
	}	

	//left menu
    var sidebar_position_top_max = 190;
    jQuery('#navigation_sidebar').css('position', 'fixed');
   
    //left menu share newsletter container
	var v_menu_height = jQuery('.nav-container').position().top + jQuery('.nav-container').height() + 90; //90 pix for artist menu
	var v_win_height = jQuery(window).height();
	
	var v_top = 0;
	
	if((v_win_height - 100) > (v_menu_height + 20) ) {
		v_top = v_win_height - 100;
	} else {	
	 	v_top = v_menu_height + 20;
	}

	jQuery('#left-menu-share-container').css({ position: "fixed", top: v_top });	
	
	jQuery(window).scroll(function() {
		ucon_scroll_menu();
		if(jQuery(window).scrollTop() == 0) {
			jQuery('#navigation_sidebar').stop().animate({top: sidebar_position_top_max}, 1000);
		}
	});
	
	function ucon_scroll_menu() {
		var top = jQuery(window).scrollTop();
		var top_of_menu = jQuery('#navigation_sidebar').position().top;		

		
		
		if((top < sidebar_position_top_max) && (top_of_menu <= 10)) {
			jQuery('#navigation_sidebar').stop().animate({top: sidebar_position_top_max}, 1000);
		}

		if((top > sidebar_position_top_max) && (top_of_menu >= 100)) {
			jQuery('#navigation_sidebar').stop().animate({top: 0}, 1000);
		}
	}
	
	/* slider */
	var stunt_slideshow = function() {
		var sliderId;
		var imageWidth;
		var imageSum;
		var imageReelWidth;
		var rotationTime = 5000;
		var regularRotationTime = 5000;
		var play;
		
		return {
			rotate: function() {
				var triggerID = $active.attr("rel") - 1; //Get number of times to slide
				var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

				jQuery(sliderId + " #slider-navigation a").removeClass('active'); //Remove all active class
				$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
				
				//Slider Animation
				jQuery(sliderId + " div:first").animate({ 
					left: -image_reelPosition
				}, 500 );
			},
			rotateSwitch: function() {
				play = setInterval(function(){ //Set timer - this will repeat itself every N seconds
					$active = jQuery('#slider-navigation a.active').next();
					if ( $active.length === 0) { //If paging reaches the end...
						$active = jQuery('#slider-navigation a:first'); //go back to first
					}
					stunt_slideshow.rotate(); //Trigger the paging and slider function
					rotationTime = regularRotationTime;
					clearInterval(play);
					stunt_slideshow.rotateSwitch();
				}, rotationTime); //Timer speed in milliseconds
			},
			setActive: function(element) {
				$active = jQuery(element); //Activate the clicked paging
				
				clearInterval(play); //Stop the rotation
				stunt_slideshow.rotate(); //Trigger rotation immediately
				rotationTime = 9000;
				stunt_slideshow.rotateSwitch(); // Resume rotation
			},
			init: function(sliderIdentifier) {
				sliderId = sliderIdentifier;
				
				if(jQuery(sliderId).size() == 0) {
					return;
				}
				
				imageWidth = jQuery(sliderId).width();
				imageSum = jQuery(sliderId + " :first-child img").size();
				imageReelWidth = imageWidth * imageSum;
				
				jQuery(sliderId + " #slider-navigation a:first").addClass("active");
				jQuery(sliderId + " div:first").css({'width' : imageReelWidth});
				stunt_slideshow.rotateSwitch();
				
				jQuery(sliderId + " #slider-navigation a").click(function() {
					stunt_slideshow.setActive(this);
					return false;
				});
			}
		}
	}();

	stunt_slideshow.init('#slider');
	/* end of slider */
 });


jQuery(window).resize(function() {
  	
    if(jQuery('.nav-container').position() == null) {
		return;
	}

	var v_menu_height = jQuery('.nav-container').position().top + jQuery('.nav-container').height() + 90; //90 pix za artist meni
	var v_win_height = jQuery(window).height() + jQuery(window).scrollTop();
	
	var v_top = 0;
	
	if((v_win_height - 100) > (v_menu_height + 20) ) {
		v_top = v_win_height - 100;
	} else {	
	 	v_top = v_menu_height + 20;
	}

	jQuery('#left-menu-share-container').css({ position: "fixed", top: v_top });

});



function moveDown(element) {
	var sign = -1; 
	
	if(!$(element).visible()) {
		sign = 1;
	}	

	new Effect.toggle($(element),'Blind', {duration:0.6});
	var add = $(element).getHeight() * sign;
	
	if(jQuery('.nav-container').position() == null) {
		return;
	}	
   
    //left menu share newsletter container
	var v_menu_height = jQuery('.nav-container').position().top + jQuery('.nav-container').height() + add;
	var v_win_height = jQuery(window).height() + jQuery(window).scrollTop();
	
	var v_top = 0;
	if((v_win_height - 100) > (v_menu_height + 20) ) {
		v_top = v_win_height - 100;
	} else {	
	 	v_top = v_menu_height + 20;
	}

	jQuery('#left-menu-share-container').stop().animate({top: v_top}, 600);
	//jQuery('#left-menu-share-container').css({ position: "fixed", top: v_top });

}

/* effects for mouseover */
var uconHover = function() {
	var hoverArray = new Array();
	var hoverLock = new Array();
	var hasForm = '';
	
	return {
		setLock: function(element, state) {
			var parent = jQuery(element).parents('li.item').filter(':first');
			hoverLock[jQuery(parent).attr('id')] = state;
		},
		hoverOn: function(element) {
			var parent = jQuery(element).parents('li.item').filter(':first');
			hoverArray[parent] = false;
			if(hasForm != '') {
				parent = jQuery(parent).children('form:first');
			}
			
			jQuery(parent).children('.item-table-cell').animate({ opacity: 0.4 }, 500, function() {  });
			jQuery(parent).children('.item-name-price').animate({ opacity: 1 }, 500, function() {  });
			jQuery(parent).children('.wholesale').animate({ opacity: 1 }, 500, function() {  });
			jQuery(parent).children('.retail').animate({ opacity: 1 }, 500, function() {  });
			
			if(jQuery(parent).children('.reorder-item-hover').size() > 0) {
				jQuery(parent).children('.reorder-item-hover').animate({ opacity: 1 }, 500, function() {  });
			}
		},
		holdHover: function(element) {
			var parent = jQuery(element).parents('li.item').filter(':first');
			hoverArray[parent] = true;
			//uconHover.hoverOn(element);
		},
		releaseHover: function(element) {
			var parent = jQuery(element).parents('li.item').filter(':first');
			hoverArray[parent] = false;
			//uconHover.hoverOff(element);
		},
		hoverOff: function(element) {
			setTimeout( function() { uconHover.hoverHide(element); }, 200);
		},
		hoverHide: function(element) {
			var parent = jQuery(element).parents('li.item').filter(':first');
			if(hasForm != '') {
				parent = jQuery(parent).children('form:first');
			}
			
			if(hoverArray[parent] == true) {
				return;
			}
			if(hoverLock[jQuery(parent).attr('id')] == true) {
				return;
			}
			
			jQuery(parent).children('.item-table-cell').animate({ opacity: 0 }, 500, function() {  });
			jQuery(parent).children('.item-name-price').animate({ opacity: 0 }, 500, function() {  });
			jQuery(parent).children('.wholesale').animate({ opacity: 0 }, 500, function() {  });
			jQuery(parent).children('.retail').animate({ opacity: 0 }, 500, function() {  });
			
			if(jQuery(parent).children('.reorder-item-hover').size() > 0) {
				jQuery(parent).children('.reorder-item-hover').animate({ opacity: 0 }, 500, function() {  });
			}
		},
		setHasForm: function() {
			hasForm = 'form.order-product-form ';
		}
	};
}();
