
$(document).ready(function() {
    resize();
    mycarousel()
    slide();
    nudge();
    links();
});

function links() {
    if ($('#subMenuNotSel27_1').length > 0) { $('#subMenuNotSel27_1 a').attr('rel', 'external'); }
    if ($('#subMenuNotSel27_2').length > 0) { $('#subMenuNotSel27_2 a').attr('rel', 'external'); }
}

function mycarousel() {
    jQuery('#mycarousel').jcarousel({
        scroll: 1,
        initCallback: mycarousel_initCallback
    });
}
function mycarousel_initCallback(carousel) {
    jQuery('#mycarousel a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(this.id.replace('item', '')));
        return false;
    });
};

$(window).bind('resize', function(){resize();});

function resize() {

	var startwidth = 1300;
	var startheight = 870;
	var minratiosize = 0.5;
	var scrollwidth = 20;
	
	var minwidth = minratiosize*startwidth;
	var minheight = minratiosize*startheight;
	var ratio = startwidth/startheight;
	var bg = $('img#bg');
	
	var imagewidth = $(bg).width();
	var imageheight = $(bg).height();
	var windowwidth = $(window).width();
	var windowheight = $(window).height();
	
	var browserwidth = windowwidth+scrollwidth;
	var browserheight = windowheight;
	
	//Check for minimum dimensions
	if ((browserheight < minheight) && (browserwidth < minwidth)){
		$(bg).height(minheight);
		$(bg).width(minwidth);
	}
	else{	
		//browser is taller	
		if (browserheight > browserwidth){
		    imageheight = browserheight;
		    $(bg).height(browserheight);
		    imagewidth = browserheight*ratio;
		    $(bg).width(imagewidth);
	        if (browserwidth > imagewidth){
		    	imagewidth = browserwidth;
		    	$(bg).width(browserwidth);
		    	imageheight = browserwidth/ratio;
		    	$(bg).height(imageheight);
		    }
		}
		//browser is wider
		if (browserwidth >= browserheight){
		    imagewidth = browserwidth;
		    $(bg).width(browserwidth);
		    imageheight = browserwidth/ratio;
		    $(bg).height(imageheight);
		    if (browserheight > imageheight){
		    	imageheight = browserheight;
		    	$(bg).height(browserheight);
		    	imagewidth = browserheight*ratio;
		    	$(bg).width(imagewidth);
		    }
		}
	}
	
	$('div#bgcontainer').width = windowwidth;
	$('div#bgcontainer').height = windowheight;
	
	var mainheight  = 334; //$('div#main').height(); ie6 needs value
	var slideheight  = 84; //$('div#slide').height();
	var totalcontentheight = mainheight; 
	var topheight = $('div#top').height();
	var slideheight = slideheight;
	var footerheight = $('div#footer').height();
	var mainmargintop = (windowheight-slideheight-totalcontentheight-topheight-(footerheight))/2;
	var neededheight = totalcontentheight+slideheight+topheight+(footerheight)+1+48; //1 margin, 43 margin-bottom container + 5 margins
	if (browserheight <= neededheight){mainmargintop=topheight+1;} //1 margin
	$('div#slide').css('margin-top', mainmargintop);

    var mainnavwidth = $('div#mainnav').width()+1;
    var mainwidth = mainnavwidth + (($('div.box').width())*2)+2;
	
	if (browserwidth<=mainwidth) {
	        $('#frowlast').hide();
	        $('#srowlast').width(mainwidth-mainnavwidth);
	    }else{
	        $('#frowlast').show();
	        $('#frowlast').width(browserwidth-mainwidth+scrollwidth);
	        $('#srowlast').width(browserwidth-mainnavwidth+scrollwidth);
	}


	var neededwidth = 711;

	if ($('#inner1').length > 0) {
	    var innercontent = $('div#innercontent');
	    var contentholder = $('div#contentholder');
	    var innercontentwidth = browserwidth-mainnavwidth-scrollwidth;
        if (innercontentwidth<neededwidth) {innercontentwidth = neededwidth; } 
        
        innercontent.width(innercontentwidth);
        contentholder.width(innercontentwidth-116); //padding
        
        if (browserheight > contentholder.height()){
            if (browserheight <= neededheight){
           
                contentholder.height(neededheight-99);   
            } else {
            
                contentholder.height(browserheight-topheight-footerheight-2-27);
            }
        } else {

            innercontent.height(contentholder.height()+70);
        }
	}
		
	
//	if ($('#inner2').length > 0) {
//	    var topcontent = $('div#topinnercontent');
//	    var bottomcontent = $('div#bottominnercontent');
//	    var innercontentwidth = browserwidth-mainnavwidth-scrollwidth;
//        if (innercontentwidth<neededwidth) {innercontentwidth = neededwidth; } 
//        
//        topcontent.width(innercontentwidth);
//        bottomcontent.width(innercontentwidth);
//	    
//	    if ((topcontent.height()+bottomcontent.height()+1) < browserheight){
//	        if (browserheight <= neededheight){
//	            var contentheight = neededheight-topheight-footerheight-5;
//	            topcontent.height((contentheight/2)-9);
//	            bottomcontent.height((contentheight/2)+8);
//	        } else {
//	            var contentheight = browserheight-topheight-footerheight-2;
//	            topcontent.height((contentheight/2)-12);
//	            bottomcontent.height((contentheight/2)+11);
//	        }
//	    }
//	    
//	}
	
	$('#loader').css('top', (browserheight/2)-($('#loader').height()/2));
	$('#loader').css('left', (browserwidth/2)-($('#loader').width()/2));
	
};


function slide() {
   $('#mycarousel a').click(function(){     
      swapImg($('img#bg'), this.href);
      return false;
    });
    $('a#bglink').click(function(){
        $('a#bglink').css('cursor', 'default');
        $("#top").fadeIn(400);
        $("#main").fadeIn(400);
        $("#innercontent").fadeIn(400);
    });
}

function swapImg($img, src) {

    var actions = 4;
    if ($('#innercontent').length > 0) { actions = 5 };
    $('<img src="' + src + '" />').load(next);

    $('#loader').fadeIn(400, function() {
        $img.fadeOut(600, next);
        $('#top').fadeOut(600, next);
        $('#main').fadeOut(600, next);

        if ($('#innercontent').length > 0) { $('#innercontent').fadeOut(600, next); }

    });
    
    function next() {
        if(--actions) return;

        $img.attr({src: src}).fadeIn(600, function(){
            $('#bglink').css('cursor', 'url(/images/cursor.gif), pointer');
            $('#loader').fadeOut(400);
        });
            

    }
}


function nudge() {
    $('.boxholder .sub li a').hover(function() { //mouse in
        $(this).animate({ paddingLeft: '8px'}, 100).addClass('selected');
    }, function() { //mouse out
        $(this).animate({ paddingLeft: 0}, 100).removeClass('selected');
    }); 
}