var maxWidth = 0;
var maxHeight = 0;

$(document).ready(function() {
	
	$('.target').attr("target","_blank");
	
	// toggle subnav
	$('#nav a[class^="n_"]').click(function() {
		var clicked = $(this).attr('rel');
		if ($('#subnav').hasClass('active') && $(this).hasClass('sel')) {
			closeSubnav(clicked);
		} else if ($('#subnav').hasClass('active')) {
			toggleSubnav(clicked);
		} else {
			$('#sub_'+clicked).show();
			openSubnav(clicked);
		}
		return false;
	});
	
	// show thumbs
	$('.ss_toggle a').click(function() {
		if ($('#content_thumbs').hasClass('active')) {
			closeThumbs();
		} else {
			openThumbs();
		}
		return false;
	});
	
	// thumbnail hover effect
	$('#thumbs ul li a').hover(function() {
		$(this).children().stop().animate({opacity: 1}, 500);
		$(this).stop().animate({'backgroundColor':'#FFF'}, 500);
	}, function() {
		$(this).children().stop().animate({opacity: 0.4}, 500);
		$(this).stop().animate({'backgroundColor':'#FFF'}, 500);
	});
	
	$('.s_content').find('ul').first().addClass('no_margin');
	$('.blog_post').first().addClass('no_margin');
	
	maxWidth = parseInt($('.centerContent').width(),10);
	maxHeight =parseInt($('.centerContent').height(),10);
	
	$('.bp_con').find('a').attr('target','_blank');
	
	$('#scroll_post.detail').find('.bp_title a').addClass('returnFalse');
	
	$('.returnFalse').click(function(){
		return false;
	});
	
});

$(window).load(function() {
	// body click to hide the subnavigation
	$('body').bind('click', function() {
		closeSubnav();
	});
});

function toggleSubnav(clicked) {
	$('.s_division').hide();
	$('#sub_'+clicked).show();
	$('#nav ul li a').removeClass('sel');
	$('#nav a.n_'+clicked).addClass('sel');
}

function openSubnav(clicked) {
	$('#subnav').animate({marginTop: '0'}, 'slow', 'easeOutCubic');
	$('#subnav').addClass('active');
	$('#nav .n_' + clicked).addClass('sel');
}

function closeSubnav(clicked) {
	$('#subnav').animate({marginTop: '-220px'}, 'slow', 'easeInCubic', function() {
		$('.s_division').hide();
	});
	$('#subnav').removeClass('active');
	$('#nav .n_' + clicked).removeClass('sel');
}

function closeThumbs() {
	$('#content_thumbs').animate({marginTop: '613px'}, 'slow', 'easeInCubic');
	$('#content_thumbs').removeClass('active');
	$('.ss_toggle a').removeClass('sel');
	$('#ss_prev').show();
	$('#ss_next').show();
	$('#ssul').cycle('resume');
}
function openThumbs() {
	$('#content_thumbs').animate({marginTop: '0px'}, 'slow', 'easeOutCubic');
	$('#content_thumbs').addClass('active');
	$('.ss_toggle a').addClass('sel');
	$('#ss_prev').hide();
	$('#ss_next').hide();
	$('#ssul').cycle('pause');
}


function resizeImage(){

	var $content = $('.centerContent');

	for(i=0; i<$content.length; i++){
		if($content.eq(i).find('img')){
			$imgCurrent = $content.eq(i).find('img');
			var height = $imgCurrent.height();
			var width = $imgCurrent.width();
			
			if(height>width){ //portrait
				if(height < maxHeight){
				ratio = maxHeight / height;
				$imgCurrent.css("height", maxHeight+'px'); // Set new height
				$imgCurrent.css("width", width * ratio+'px'); // Scale width based on ratio
				width = width * ratio; // Reset width to match scaled image
				}
			}else { //landscape
				if(width < maxWidth){
				ratio = maxWidth / width; 
				$imgCurrent.css("width", maxWidth+'px'); // Set new width
				$imgCurrent.css("height", height * ratio+'px'); // Scale height based on ratio
				height = height * ratio; // Reset height to match scaled image
				width = width * ratio; // Reset width to match scaled image
				}
			}
			
			if(width > maxWidth){
				ratio = maxWidth / width;
				$imgCurrent.css("width", maxWidth+'px'); // Set new width
				$imgCurrent.css("height", height * ratio+'px'); // Scale height based on ratio
				height = height * ratio; // Reset height to match scaled image
				width = width * ratio; // Reset width to match scaled image
			}
			
			// Check if current height is larger than max
			if(height > maxHeight){
				ratio = maxHeight / height; 
				$imgCurrent.css("height", maxHeight+'px'); // Set new height
				$imgCurrent.css("width", width * ratio+'px'); // Scale width based on ratio
				width = width * ratio; // Reset width to match scaled image
			}
			
			if($imgCurrent.height() < $content.height()){
				$imgCurrent.css('margin-top',($content.height()-$imgCurrent.height())/2);
			}
		}
	}
}

