$(document).ready(function() {

	/* fancybox settings */
	$("a.group").fancybox({
		'speedIn'		:	300, 
		'speedOut'		:	300, 
		'overlayShow'	:	true,
		'titlePosition'	:	'outside',
		'autoScale'		:	'false'
	});

	$("a.iframe").fancybox({
		'type'			: 	'iframe',
		'height'		:	'80%',
		'width'			:	'80%'
	});

	// big buttons
	$('a.btn_big').hover(function() {
			
			// disable animation queue
			if($(this).find('span.header').is(':animated')) {
				return;
			}
			
			$(this).find('span.header').first().animate({ 'margin-top': '30'});
			$(this).find('span.descr').first().css('display', 'block').fadeIn('slow');
		},
		function() {
			$(this).find('span.header').first().animate({ 'margin-top': '55'});
			$(this).find('span.descr').first().hide();
		}
	);

	// subnav positions
	$.each($('nav>ul>li>ul.subnav'), function(id, elem) {
		
		left = ($(elem).offset()['left']);
		var width_screen = $(window).width();
		var width_elem = $(elem).css('width');
		var overlap = 0;

		if(parseInt(left) + parseInt(width_elem) + 100 > width_screen) {
			overlap = width_screen - parseInt(width_elem) - left;
		}
		
		offset_left = overlap - 100;
		$(elem).css({
			'left': offset_left
		});
		
		// adjust display properties
		$(elem).css({'display': 'none', 'opacity': 0.9, 'alpha': 'filter(opacity=90)'});
		
	});
	
	// init nivo slider
	if($('#slider').length) {
	    $('#slider').nivoSlider({
	        effect:'fade', //Specify sets like: 'fold,fade,sliceDown'
	        slices:1,
	        animSpeed:500, //Slide transition speed
	        pauseTime:10000,
	        startSlide:0, //Set starting Slide (0 index)
	        directionNav:false, //Next & Prev
	        directionNavHide:false, //Only show on hover
	        controlNav:true, //1,2,3...
	        controlNavThumbs:false, //Use thumbnails for Control Nav
	        controlNavThumbsFromRel:false, //Use image rel for thumbs
	        controlNavThumbsSearch: '.jpg', //Replace this with...
	        controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src
	        keyboardNav:false, //Use left & right arrows
	        pauseOnHover:true, //Stop animation while hovering
	        manualAdvance:false, //Force manual transitions
	        captionOpacity:1, //Universal caption opacity
	        beforeChange: function(){},
	        afterChange: function(){},
	        slideshowEnd: function(){}, //Triggers after all slides have been shown
	        lastSlide: function(){}, //Triggers when last slide is shown
	        afterLoad: function(){} //Triggers when slider has loaded
	    });
	}
	
    // dropdown menu
    cur_elem = false;
    interval = 1000;
    timeout = null;
    
    $("ul>li.hasChildren .textItem").wrap('<span>');
    $("ul>li.hasChildren span").click(function() { //When trigger is clicked...

		window.clearTimeout(timeout);
		cur_elem = $(this).parent().attr('id');

		$("ul>li.hasChildren>ul.subnav").hide();
		$(this).parent().find("ul.subnav").show();

		// change zindex
		add_zindex();
		
        //Following events are applied to the subnav itself (moving subnav up and down)
        $(this).parent().find("ul.subnav").first().fadeIn(100); //Drop down the subnav on click
		$(this).find('div.textItem').addClass('on');
		
        $(this).parent().find("ul.subnav").first().hover(function() {
				window.clearTimeout(timeout);
				timeout = null;
        	},
        	function() {
            	elem = this;
        		timeout = window.setTimeout('$(elem).parent().find("ul.subnav").first().fadeOut(300, remove_zindex)', interval);
        		$('div.on').removeClass('on');
        	}
        );

    });

});

function add_zindex() {
	
	$('.btn_big').css('z-index', '-2');
	$('#slider_holder').css('z-index', '-2');
	$('#pageimage_holder').css('z-index', '-2');
	$('#pageinfo_holder').css('z-index', '-1');
}

function remove_zindex() {
	
	$('.btn_big').css('z-index', '0');
	$('#slider_holder').css({'z-index': '100'});
	$('#pageimage_holder').css('z-index', '0');
	$('#pageinfo_holder').css('z-index', '1');
}

