$(function(){
	
	var theme_url = $('body').attr('data-theme-url');

	function k_menu()
	{
		jQuery("#nav a, .subnav a").removeAttr('title');
		jQuery(" #nav ul ").css({display: "none"}); // Opera Fix

		jQuery("#nav li").each(function()
		{	

			var $sublist = jQuery(this).find('ul:first');

			jQuery(this).hover(function()
			{	
				$sublist.stop().css({overflow:"hidden", height:"auto", display:"none"}).slideDown(400, function()
				{
					jQuery(this).css({overflow:"visible", height:"auto"});
				});	
			},
			function()
			{	
				$sublist.stop().slideUp(400, function()
				{	
					jQuery(this).css({overflow:"hidden", display:"none"});
				});
			});	
		});
	}
	k_menu();
	
	
	/* Fancybox configuration */
	$("a.fancybox").fancybox({
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'titlePosition' 	: 'inside'
	});
	
	$('a.iframe75').fancybox({
		'width'				: '75%',
		'height'			: '75%',
		'autoScale'			: 'false',
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'type'				: 'iframe'
	});

	$('a.iframe90').fancybox({
		'width'				: '90%',
		'height'			: '90%',
		'autoScale'			: 'false',
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'type'				: 'iframe'
	});
	$('a.iframe800x600').fancybox({
		'width'				: '800',
		'height'			: '600',
		'autoScale'			: 'false',
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'type'				: 'iframe'
	});
	
	/* Slideshow (Start page) */
	$('#slideshow #buttons').find('a').each(function(index) {
		if (index == 0) { $(this).addClass('current'); }
		$buttons = $('#slideshow #buttons').find('a');
		var $this = $(this);
		$images = $('#slideshow').find('img');
		var $img = $($images[index]);
		$this.bind('click', function(){
			$images.each(function(index){
				$(this).fadeOut();
			});
			$img.fadeIn();
			$buttons.each(function(index){
				$(this).removeClass('current');
			});
			$this.addClass('current');
		});
	});
	
	$buttons = $('#buttons a');
	if ($buttons.length > 1)
	{
		$buttons[0].click();
		var clicksLeft = 20;
		var currentButtonIndex = 1;
		setTimeout(function(){
			
			var interval = setInterval(function(){
				clicksLeft = clicksLeft - 1;
				if (clicksLeft == 0)
				{
					clearInterval(interval);
				}
				if (currentButtonIndex > $buttons.length-1)
				{
					currentButtonIndex = 0;
				}
				$buttons[currentButtonIndex].click();
				currentButtonIndex = currentButtonIndex + 1;
			}, 3000);
			
		}, 100);
	}
	
	/* Placeholder for inputs, search box mainly. Example <input type="text" name="whatever" placeholder="Example text"> */
	$('input').each(function(){
		var $this = $(this);
		if ($this.attr('placeholder') && $this.val() == "")
		{
			$this.val($this.attr('placeholder'));
			$this.addClass('placeholder');
			$this.bind('focus', function(){
				if ($this.val() == '' || $this.val() == $this.attr('placeholder'))
				{
					$this.val('');
					$this.removeClass('placeholder');
				}
			});
			$this.bind('blur', function(){
				if ($this.val() == '' || $this.val() == $this.attr('placeholder'))
				{
					$this.val($this.attr('placeholder'));
					$this.addClass('placeholder');
				}
			});
		}
	});
	$('input[type="submit"]').each(function(){
		$(this).bind('click', function(){
			$('input.placeholder').val('');
			return true;
		});
	});
	
	/* Opening and closing expandables */
	$('a.expandable_toggler').each(function(index) {
		var $this = $(this);
		var $target = $('#'+$this.attr('data-container'));
		$this.bind('click', function(event) {
			if ($this.attr('data-open'))
			{
				$target.slideUp();
				$this.removeAttr('data-open');
				$this.parents('li').removeClass('current');
			}
			else
			{
				$('a[data-open=yes]').click();
				$target.slideDown();
				$this.attr('data-open', 'yes');
				$this.parents('li').addClass('current');
			}
		});
	});
	
	/* Image Slider */
	$('.image-flipper').each(function(index){
		var $this = $(this);
		var $container = $this.find('.slides-container');
		var $slider = $this.find('.slides');
		var $slides = $this.find('.slide');
		
		var container_width = $container.width();
		var slides_width = 0;
		var max_offset = 0;
		var step_width = container_width / 3;
		var current_offset = 0;
		
		var calculated = 0;
		
		$slides.parent('div').css('width', container_width*2);
		
		$this.find('.arrow-left').bind('click', function() {
			if (current_offset > 0)
			{
				if (current_offset - step_width < 0)
				{
					current_offset = 0;
				}
				else
				{
					current_offset -= step_width;
				}
				redraw();
			}
		});
		$this.find('.arrow-right').bind('click', function() {
			if (calculated == 0) calculate_full_width($slides);
			if (current_offset < max_offset)
			{
				if (current_offset + step_width > max_offset)
				{
					current_offset = max_offset;
				}
				else
				{
					current_offset += step_width;
				}
				redraw();
			}
		});
		function calculate_full_width ($slides) {
			$slides.each(function(){
				slides_width += $(this).width() + 37;
			});
			max_offset = slides_width - container_width;
			$slider.css('width', slides_width);
			calculated = 1;
		}
		function redraw () {
			$slider.animate({"margin-left": -current_offset}, 200);
		}
	});
	
});
