$(document).ready(function() {
  topnav();
  agendaTooltips();
  beritaFotoTabs();
  tabboxTabs();
  booklistHover();
  booklistTooltips();
  booklistToggler();
  $('input.help,textarea.help').formtips({
        tippedClass: 'tipped'
  });
  $('.scrolltop').click(function(){
    $('html').animate({scrollTop : 0},1000);
  });
  $('a.banner').hover(function(){
    $(this).stop().animate({'opacity':'1'},500);
  },function(){
    $(this).stop().animate({'opacity':'0.5'},500);
  });
  $('#newsticker').innerfade({ animationtype: 'slide', speed: 1000, timeout: 5000, type: 'random', containerheight: '30px' });
  $( ".tabs" ).tabs();
});

$(function datePicker(){
  $('.datepicker').datepicker({
  dateFormat:'dd/mm/yy',
  showOn: "button",
	buttonImage: "images/icon/calendar-select.png",
	buttonImageOnly: true
  });
});

function topnav(){
 $('#navigation > ul > li').hover(function(){
    $(this).append('<span class="bg">&nbsp;</span>').children('span.bg').animate({'height':'35px'},200);
  },function(){
    $(this).children('span.bg').animate({'height':'0px'},300,function(){$(this).remove()});
  });
  $('#navigation > ul > li').hoverIntent(function(){
    $(this).addClass('active');
    $(this).children('ul').slideDown(100);
  },function(){
    $(this).removeClass('active');
    $(this).children('ul').slideUp(100);
  });
}

function agendaTooltips(){
  $('#agenda-ticker ul li').hoverIntent(function(){
    var content = $(this).children('.fullcontent').html();
    var pos = $(this).offset();
    var posLeft = parseInt(pos.left)-316;
    var posTop = pos.top;
    
    $('body').append('<div class="agenda-tooltips"><div class="content"></div></div>');
    $('.agenda-tooltips').append('<img class="arrow" src="images/background/arrow-agenda.png" alt=""/>');
    
    $('.agenda-tooltips .content').html(content);
    $('.agenda-tooltips').css({'left':posLeft,'top':posTop});
  },function(){
    $('.agenda-tooltips').remove();
  });
}

function beritaFotoTabs(){
//When page loads...
	$("#berita-foto .tab_content").hide(); //Hide all content
	$("#berita-foto ul.tabs li:first").addClass("active").show(); //Activate first tab
	$("#berita-foto .tab_content:first").show(); //Show first tab content

	//On Click Event
	$("#berita-foto ul.tabs li").click(function() {

		$("#berita-foto ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$("#berita-foto .tab_content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});
  $('#berita-foto .tab_content a').hover(function(){
    var content = $(this).children('img').attr('alt');
    $(this).append('<span>'+content+'</span>');
    $(this).children('span').slideDown();
  },function(){
   $(this).children('span').slideUp(function(){
    $(this).remove();
   });
  });
}

function tabboxTabs(){
//When page loads...
	$(".tabbox .tab_content").hide(); //Hide all content
	$(".tabbox ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tabbox .tab_content:first").show(); //Show first tab content

	//On Click Event
	$(".tabbox ul.tabs li").click(function() {

		$(".tabbox ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tabbox .tab_content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});
}

function booklistHover(){
$("ul.thumb li").hover(function() {
	$(this).css({'z-index' : '10'}); /*Add a higher z-index value so this image stays on top*/ 
	$(this).find('img').addClass("hover").stop() /* Add class of "hover", then stop animation queue buildup*/
		.animate({
			marginTop: '-100px', /* The next 4 lines will vertically align this image */ 
			marginLeft: '-75px',
			top: '50%',
			left: '50%',
			width: '120px', /* Set new width */
			height: '140px', /* Set new height */
			padding: '5px'
		}, 200); /* this value of "200" is the speed of how fast/slow this hover animates */

	} , function() {
	$(this).css({'z-index' : '0'}); /* Set z-index back to 0 */
	$(this).find('img').removeClass("hover").stop()  /* Remove the "hover" class , then stop animation queue buildup*/
		.animate({
			marginTop: '0', /* Set alignment back to default */
			marginLeft: '0',
			top: '0',
			left: '0',
			width: '80px', /* Set width back to default */
			height: '100px', /* Set height back to default */
			padding: '5px'
		}, 400);
});
}

function booklistTooltips(){
  $('#booklist ul.thumb li').hoverIntent(function(){
    var content = $(this).children('.fullcontent').html();
    var pos = $(this).offset();
    var posLeft = parseInt(pos.left)-110;
    var posTop = parseInt(pos.top)-230;
    
    $('body').append('<div class="booklist-tooltips" style="display:none"><div class="content"></div></div>');
    $('.booklist-tooltips').append('<img class="arrow" src="images/background/arrow-down.png" alt=""/>');
    
    $('.booklist-tooltips').fadeIn(200);
    $('.booklist-tooltips .content').html(content);
    $('.booklist-tooltips').css({'left':posLeft,'top':posTop});
  },function(){
    $('.booklist-tooltips').remove();
  });
}

function booklistToggler(){
  $('#booklist .tabs a').click(function(){
    $('#booklist .tabs a').removeClass('active');
    $(this).addClass('active');
    $('#booklist .tabsbox').hide();
    var activeTabs = $(this).attr('href');
    $(activeTabs).show();
  });
}

$(function(){
  $("a[rel=example_group]").fancybox({
				'transitionIn'		: 'elastic',
				'transitionOut'		: 'elastic',
				'titlePosition' 	: 'over',
				'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
					return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
				}
			});
      
  $("a.fancybox").fancybox({
				'overlayShow'	: false,
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic'
			});
});

function fancymodal(id,title,message,width,height){
    $.fancybox(
		'<div class="modulebox" style="margin:0"><h4>'+title+'</h4><div class="content" style="padding:10px"><p>'+message+'</p></div></div>',
		{
            'autoDimensions'	: false,
			'overlayShow'	: false,
			'width'         	: width,
			'height'        	: height,
			'transitionIn'		: 'elastic',
			'transitionOut'		: 'elastic'
		}
	);

}
