// JavaScript Document
$(document).ready(function(){
	//Carousel
	$("#slideshow").carousel( {
		direction: "horizontal", // you can change this to vertical
		loop: true,
		pagination: true, // setting this to true adds the pagination 1, 2, 3 etc.
		autoSlide: true, // automatic rotation between the banners
		autoSlideInterval: 5000, // how long in miliseconds till next rotation
		delayAutoSlide: 3000 // how long to pause on each banner
	});

	//Resize Image
	var max_width = 290;
	var max_height = 270;
	var mid_width = Math.ceil($(".featured-thumb").width()/2);
	var mid_height = Math.ceil($(".featured-thumb").height()/2);
	$("#slideshow img").each(function(i) {
		if (($(this).width()/$(this).height() < 1)) {
			//alert("Portrait");
			var w = max_width;
		    var h = Math.ceil($(this).height() / $(this).width() * max_width);
		} else {
			//alert("Landscape");
			var h = max_height;
			var w = Math.ceil($(this).width() / $(this).height() * max_height);
		}
		var halfHeight = Math.ceil(-h/2);
		var halfWidth = Math.ceil(-w/2);
		$(this).css({ 'height': h, 'width': w, 'top': mid_height, 'margin-top': halfHeight, 'left': mid_width, 'margin-left': halfWidth });
	});

	//Resize Image
	var tab_img = 80;
	$("img.article-img").each(function(i) {
		if ($(this).height() > $(this).width()) {
			//var w = Math.ceil($(this).width() / $(this).height() * tab_img);
			var w = tab_img;
		} else {
			var w = tab_img;
		}
		$(this).css({ 'width': '80px' });
		$("#biz-panel .biz-content .story-entry .entry-thumb").css({ 'height': 80, 'overflow': 'hidden' });
	});

	/* Tab Panel */
	//Get the height of the first item
	$('#mask').css({'height':$('#panel-1').height()});	
	//Calculate the total width - sum of all sub-panels width
	//Width is generated according to the width of #mask * total of sub-panels
	$('#panel').width(parseInt($('#mask').width() * $('#panel div').length));
	//Set the sub-panel width according to the #mask width (width of #mask and sub-panel must be same)
	$('#panel div').width($('#mask').width());
	//Get all the links with rel as panel
	$('a[rel=panel]').click(function () {
		//Get the height of the sub-panel
		var panelheight = $($(this).attr('href')).height();
		//Set class for the selected item
		$('a[rel=panel]').removeClass('selected');
		$(this).addClass('selected');
		//Resize the height
		$('#mask').animate({'height':panelheight},{queue:false, duration:500});			
		//Scroll to the correct panel, the panel id is grabbed from the href attribute of the anchor
		$('#mask').scrollTo($(this).attr('href'), 800);		
		//Discard the link default behavior
		return false;
	});
	
	/* Add "drop" class for menu items with child items */
	$("#header .menu li").find("ul.child").parent().find("a").addClass("drop");
	$("#header .menu li").find("ul.child").find("a").removeClass("drop");
	$("#register #reg-bar li").find("ul.child").parent().find("a").addClass("drop");
	$("#register #reg-bar li").find("ul.child").find("a").removeClass("drop");
	
	/* <img> inline style */
	$('.editable-content img').each(function (index) {
		var imgFloat = $(this).css('float');
		if (imgFloat == 'left') {
			$(this).css({ 'margin': '5px 10px 0 0' })
		} else if (imgFloat == 'right') {
			$(this).css({ 'margin': '5px 0 0 10px' })
		}
	});
});
