// JavaScript Document

$(document).ready(function() {
	if (jQuery.browser.webkit && document.readyState != "complete") {
		setTimeout( arguments.callee, 100 );
		return;
	}
	// Slider
	$("#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 = 300;
	var max_height = 350;
	var mid_width = Math.ceil($("#slideshow .img-container").width()/2);
	var mid_height = Math.ceil($("#slideshow .img-container").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 max_width2 = 205;
	var max_height2 = 160;
	var mid_width2 = Math.ceil($("#news-ticker .art-img").width()/2);
	$("#news-ticker img").each(function(i) {
		if (($(this).width()/$(this).height() < 1)) {
			//alert("Portrait");
			var w = max_width2;
		    var h = Math.ceil($(this).height() / $(this).width() * max_width2);
			var mid_height2 = -10;
			var halfHeight2 = 0;
		} else {
			//alert("Landscape");
			var h = max_height2;
			var w = Math.ceil($(this).width() / $(this).height() * max_height2);
			var mid_height2 = Math.ceil($("#news-ticker .art-img").height()/2);
			var halfHeight2 = Math.ceil(-h/2);
		}
		var halfWidth2 = Math.ceil(-w/2);
		$(this).css({ 'height': h, 'width': w, 'top': mid_height2, 'margin-top': halfHeight2, 'left': mid_width2, 'margin-left': halfWidth2 });
	});
	/* News ticker */
	$(".carousel .jCarouselLite").jCarouselLite({
		btnNext: ".carousel .next",
		btnPrev: ".carousel .prev",
		visible: 5,
		circular: false
	});
	/* Tab Panel */
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content

	//On Click Event
	$("ul.tabs li").click(function() {
		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content
		var activeTab = $(this).find("a.tabs1").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});
	/* Tab Panel */
	$(".tab_content2").hide(); //Hide all content
	$("ul.tabs2 li:first").addClass("active").show(); //Activate first tab
	$(".tab_content2:first").show(); //Show first tab content

	//On Click Event
	$("ul.tabs2 li").click(function() {
		$("ul.tabs2 li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content2").hide(); //Hide all tab content
		var activeTab2 = $(this).find("a.tabs2").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab2).fadeIn(); //Fade in the active ID content
		return false;
	});
	/* Detect first and last item of article attributes */
	$("ul.article-attributes li:first").addClass("first"); //Activate first item
	$("ul.article-attributes li:last").addClass("last"); //Activate last item
	/* Collapsible panel in Directory Level 1 */
	$("#directory-l1 li > *:not(h3)").hide();
	$("#directory-l1 h3 span").css("cursor","pointer").click(function (){
		//select all answers
		var answer = $(this).parent("h3").siblings();
		//show/hide answers
		answer.slideToggle("fast");
		$(this).toggleClass("expanded");
	});
	/* Calculate odd and even numbers in Directory Level 3 Related Categories List */
	if($(".commentary.related ul").children("li").length % 2 == 1) {
		$(".commentary.related ul li:last").addClass("last");
	}
	/* Bookmarking codes */
	$("a.article-bookmark").click(function(e){
		e.preventDefault(); // this will prevent the anchor tag from going the user off to the link
		var bookmarkUrl = this.href;
		var bookmarkTitle = this.title;
		
		if (window.sidebar) { // For Mozilla Firefox Bookmark
			window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,"");
		} else if( window.external || document.all) { // For IE Favorite
			window.external.AddFavorite( bookmarkUrl, bookmarkTitle);
		} else if(window.opera) { // For Opera Browsers
			$("a.jQueryBookmark").attr("href",bookmarkUrl);
			$("a.jQueryBookmark").attr("title",bookmarkTitle);
			$("a.jQueryBookmark").attr("rel","sidebar");
		} else { // for other browsers which does not support
			alert('Your browser does not support this bookmark action');
			return false;
		}
	});
});

// jCarousel

