// back image resize

var main_image_original_width = 1500;
var main_image_original_height = 1000;
var pic_width = main_image_original_width;
var pic_height = main_image_original_height;


function resize_image_with_cropping() {
	var win_width = jQuery(window).width();
	var win_height = jQuery(window).height();
	var win_aspect_ratio = win_width / win_height;

	current_width = jQuery('#main_image').width();
	if (current_width > 1) {
		pic_width = current_width;
	}	

	current_height = jQuery('#main_image').height();
	if (current_height > 1) {
		pic_height = current_height;
	}
	
	pic_aspect_ratio = pic_width / pic_height;
	
	if (win_aspect_ratio > pic_aspect_ratio) {
		// scale pic to fit window width
		var new_height = (win_width / pic_width) * pic_height;
		var new_width = win_width;
	} else {
		// scale pic to fit window height		
		var new_height = win_height;
		var new_width = (win_height / pic_height) * pic_width;
	}
	
	// center image
//	new_top = 0 - ((new_height - win_height) / 2);
//	new_left =  0 - ((new_width - win_width) / 2);
	
	// center image only horizontal
	new_top = 0 ;
	new_left =  0 - ((new_width - win_width) / 2);

	
	
//	alert('new_top: ' + new_top + 'new_left: ' + new_left);
	jQuery('#main_image').css({top: new_top, left: new_left});

	jQuery('#main_image').css({height: new_height, width: new_width});
	jQuery('#bkg-wrapper').css({height: win_height, width: win_width});
	jQuery('#bkg-wrapper').css({visibility:"visible", display:"inline"});
	
}


// horizontal carousel

$(document).ready(function(){

	$("#page-flip").jFlow({
		slides: "#slides",
		controller: ".page", // must be class, use . sign
		slideWrapper : "#h-carousel", // must be id, use # sign
		selectedWrapper: "selected",  // just pure text, no sign
		width: "750px",
		height: "480px",
		duration: 400,
		prev: ".prev", // must be class, use . sign
		next: ".next" // must be class, use . sign
	});
	
});


// vertical carousel

$(function() {  
$("#content-navig-slide-list").jCarouselLite({  
        vertical: true,  
        visible: 8,  
        speed: 400,
        circular: false,		
		btnPrev: ".content-navig-prev",
        btnNext: ".content-navig-next"
     });  
});  


// footer subnavig dropdown

var obj = null;

   function checkHover() {
      if (obj) {
         obj.find('ul').fadeOut('fast');
      } //if
   } //checkHover

   $(document).ready(function() {
      $('#footer-main-navig > li').hover(function() {
         if (obj) {
            obj.find('ul').fadeOut('fast');
            obj = null;
         } //if

         $(this).find('ul').fadeIn('fast');
      }, function() {
         obj = $(this);
         setTimeout(
            "checkHover()",
            400);
      });
   });
   

// Main subnavig dropdown
var shown_menu = null;
var scheduled_change_to_original_menu = null;


function change_to_original_menu() {
    if (shown_menu) {
        original_menu = $('ul.navig-level0 > li.on').find('ul').get(0)
        if (shown_menu != original_menu) {
            $(shown_menu).fadeOut('fast');
            $(original_menu).fadeIn('fast');
            shown_menu = original_menu
        }
    }
}

function cancel_scheduled_change_to_original_menu() {
    if (scheduled_change_to_original_menu) {
        clearTimeout(scheduled_change_to_original_menu)
    }
    scheduled_change_to_original_menu = null
}

function schedule_change_to_original_menu() {
    cancel_scheduled_change_to_original_menu()
    scheduled_change_to_original_menu = setTimeout(function() {
        change_to_original_menu()
    }, 1000)
}

   $(document).ready(function() {
        // When the mouse pointer hovers over a main menu entry, we show that entry's submenu
        $('.navig-level0 > li').hover(function() {
            cancel_scheduled_change_to_original_menu()
            var menu_to_show = $(this).children('ul').get(0)
            if (shown_menu != menu_to_show) {
                if (shown_menu)   $(shown_menu).fadeOut('fast');
                if (menu_to_show) $(menu_to_show).fadeIn('fast');
                shown_menu = menu_to_show
            }
        }, function() {
            schedule_change_to_original_menu()
        });
   });

// slideshow

function slideSwitch() {
    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    // uncomment the 3 lines below to pull the images in random order
    
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

