
/* Copyright (c) 2011 der|Design */

(function($) {

// DOM Ready

queue_ready_fn(function() {
	
	init_environment();

	enhance_menu();

	ui_enhancements();

	form_enhancements();
	
	init_lightbox();

	init_shortcodes();

	apple_device_fixes();
	
});

// Private Functions

function handle_resize_event() {

	/* Update layout */

	var body = Der.get('cache', 'body');

	var width = Der.window.width();

	if ( width < 1215 ) {

		body.addClass('compact'); 

		if ( Der.sidebar.visible ) {Der.sidebar.hide();}

	} else {

		body.removeClass('compact');

	}

	if ( width < 960 ) {body.css('width', '960 !important');} else {body.css('width', 'auto !important');}

}

function handle_background() {

	/* Make sure bgimage is not undefined */

	var bgimage = Der.get('bgimage', 'object');

	if ( bgimage == undefined ) {return;} // Second check

	/* Update background */

	Der.bg.resize_bgimage(bgimage);

}

function enhance_menu() {

	var nav = $('ul#navigation');

	nav.find('> li > ul').each(function() {

		/* Use .menu-parent on menu items that have submenus */

		$(this).parent().addClass('menu-parent');

		$(this).find('li:first').prepend('<span class="arrow"></span>');

		$(this).find('li ul').each(function() {$(this).parent().addClass('arrow');});

	});

	var timeout = null;
	
	/* Only bind the hover event to elements that really need it (improving performance) */
	
	if ( Der.is('modern_browser') ) {
	
		nav.find('li.menu-parent').add('ul#navigation > li ul li.arrow').hover(function() {

			var elem = this;

			clearTimeout(timeout);

			timeout = setTimeout(function() {$(elem).find('ul:first').fadeIn( (Der.is('modern_browser') ) ? 250 : 0 );}, 200);

		}, function() {

			clearTimeout(timeout);

			$(this).find('ul:first').hide();

		});
	
	}
		
}

function init_environment() {

	/* Configure Background Image */

	setup_background_image();

	/* Perform resizing operations on load */

	handle_resize_event();

}

function setup_background_image() {

	var container = $('#background-image');

	var img = container.find('img');

	Der.set('bgimage', 'container', container);

	$.cacheImage(img.attr('src'), {
		load: function() {
			Der.set('bgimage', 'width', img[0].width);
			Der.set('bgimage', 'height', img[0].height);
			Der.set('bgimage', 'object', img);
			Der.bg.resize_bgimage(img);
		}
	});

}

function ui_enhancements() {

	/* Bind screen resize events */

	Der.window.bind('resize', handle_background);

	Der.window.bind('resize', handle_resize_event);

	/* Show Widget Controls */

	$('#right-sidebar .widget ul.controls').show();

	/* Fade effect for .header-icons */

	if ( Der.is('modern_browser') ) {
		
		$('#header-bar ul.header-icons > li a').hover(function() {

			$(this).stop().animate({opacity: 1}, 250);

		}, function() {

			$(this).stop().animate({opacity: 0.6}, 50);

		});
		
	}
	
	/* Add Lightbox Feature */
	
	$('.add-lightbox').each(function(i) {
		
		$(this).find('img').each(function() {
			
			var parent = $(this).parent();

			if ( parent.is('a') && parent.attr('href') != undefined ) {
				
				parent.attr('rel', 'lightbox[post-lightbox-' + i + ']');
				
			}
			
		});
		
	});
	
}

function form_enhancements() {

	/* Header Search */

	new Der.classes.EnhancedSearch('#header-bar #search', 'input:text', 'span.close');

	var submit = $('#header-bar #search input:submit');

	if ( Der.is('modern_browser') ) {
		
		$('#header-bar #search input:text').focus(function() {

			submit.css('opacity', '0.8');

		}).blur(function() {

			submit.css('opacity', '0.6');

		});
		
	}
	
	/* MW-Form enhancements */
	
	$('form.mw-form input:text').each(function() {
		
		$(this).data('title', $(this).attr('title') ).removeAttr('title').focus(function() {
			
			var val = $.trim( $(this).val() );
			
			if ( val == $(this).data('title') ) { 
			
				$(this).stop().animate({textIndent: '-100px'}, 250, 'easeInQuad', function() { $(this).val('').css('text-indent', '0'); });
		
			}
			
		}).blur(function() {  
		
			var val = $.trim( $(this).val() );
			
			if ( val.length == 0 ) { $(this).val( $(this).data('title') ); }
	
		});
		
	});
	
	$('form.mw-form a.close').click(function() {
		
		$(this).closest('.notification').fadeOut(300, function() { $(this).remove(); });
		
	});

}

function init_lightbox() {

	if ( ! Der.options.lightbox_disable ) {
		
		$("a[rel^='lightbox']").prettyPhoto({
			animation_speed: 'fast', /* fast/slow/normal */
			slideshow: 3000, /* false OR interval time in ms */
			autoplay_slideshow: false, /* true/false */
			opacity: 0.80, /* Value between 0 and 1 */
			show_title: true, /* true/false */
			allow_resize: true, /* Resize the photos bigger than viewport. true/false */
			default_width: 500,
			default_height: 344,
			counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
			theme: 'pp_default', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
			horizontal_padding: 20, /* The padding on each side of the picture */
			hideflash: true, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
			wmode: 'opaque', /* Set the flash wmode attribute */
			autoplay: true, /* Automatically start videos: True/False */
			modal: false, /* If set to true, only the close button will close the window */
			deeplinking: false, /* Allow prettyPhoto to update the url to enable deeplinking. */
			overlay_gallery: Der.options.lightbox_use_overlay_gallery, /* If set to true, a gallery will overlay the fullscreen image on mouse over */
			keyboard_shortcuts: true, /* Set to false if you open forms inside prettyPhoto */
			changepicturecallback: function(){}, /* Called everytime an item is shown/changed */
			callback: function(){}, /* Called when prettyPhoto is closed */
			ie6_fallback: true,
			social_tools: false
		});
		
	} 

}

function apple_device_fixes() {
	
	if ( Der.is('apple_device') ) {
		
		$('#background-image').hide();
		
		Der.window.trigger('resize');
		
		Der.set('cache', 'bgs', $('#right-bg, #content-bg, #left-bg') );
		
	}
	
}

function init_shortcodes() {
	
	/* Expandable Content */
	
	$('.expandable-content').each(function() {
		var busy = false;
		$(this).find('> a.title').data('hidden', true).show().click(function() {
			if (busy) { return false; } else { busy = true; }
			if ( $(this).data('hidden') ) {
				$(this).find('small.status').html('&#9650;');
				$(this).data('hidden',false).siblings('.content').fadeIn(Der.fallback(200), function() { busy=false; }); 
			} else {
				$(this).find('small.status').html('&#9660;');
				$(this).data('hidden', true).siblings('.content').fadeOut(Der.fallback(150), function() { busy=false; }); 
			}
			return false;
		});
		$(this).find('> .content').hide();
	});
	
	/* Tabbed Content */

	var tab_groups = [];
	var container = $('<div class="der-tab-group"></div>');
	var head = $('<ul class="head"></ul>');

	$('.der-tab-group ul.head li.tab').live('click', function() {
		
		var busy = false;
		
		if ( ! $(this).hasClass('active') ) {
			if ( busy ) { return false; } else { busy = true; }
			$(this).addClass('active').siblings('.active').removeClass('active');
			$(this).closest('.der-tab-group').find('div[rel=' + $(this).attr('rel') + ']').show().siblings('.tab-content').hide();
		}
		
	});

	$('.der-tab').each(function(i) {
		$(this).attr('id', 'der-tab-' + i);
		
			// New tab collection
			
			var tab = { id: $(this).attr('id') };
			var h4 = $(this).find('h4.title:first')
			tab.title = h4.html(); h4.remove();
			tab.content = $(this).html();
			tab.object = $(this);
			tab_groups.push(tab);
			
			// Tab below ?
			
			var tab_below = $(this).next().hasClass('der-tab');
			
			if ( ! tab_below ) {
				
				// Generate structure & Reset tab_groups
				
				var _container = container.clone();
				var _head = head.clone();
				var _body = '';
				
				for (var j=0; j < tab_groups.length; j++) {
					tab = tab_groups[j];
					_head.append('<li class="tab" rel="' + tab.id + '">' + tab.title + '</li>');
					_body += '<div class="tab-content" rel="' + tab.id + '" style="display: none;">' + tab.content + '</div>';
				}
				
				_head.append('<li class="clear"></li>');
				_head.children(':first').addClass('active');
				_container.append(_head).append(_body);
				_container.find('.tab-content:first').show().each(function() { $(this).find('> *:last-child').css('margin', 0); });
				
				for (var j=0; j < tab_groups.length; j++) {
					if (j==0) { tab_groups[j].object.replaceWith(_container); }
					else { tab_groups[j].object.remove(); }
				}
				
				tab_groups = []; // Reset tab groups, to start over
			}
	});
	
}

})(jQuery);
