/**
* Javascript
* JS: App
*
* @author Heiko Pfefferkorn (i-fabrik GmbH)
* @copyright 2010 i-fabrik GmbH
* @version $Id: app.min.js,v 1.5 2010-11-10 15:39:59 heiko Exp $
*
*/

jQuery.fn.tagName = function() {
	var tagNames = null;

	if(1 === this.length) {
		tagNames = this[0].tagName.toLowerCase();
	} else {

		tagNames = [];

		this.each(function(i, el) {
			tagNames[i] = el.tagName.toLowerCase();
		});

	}

	return tagNames;
};

/**
 * Global application namespace.
 */
var APP = {
	settings: {
		language: 'en',
		paths   : {
			theme  : 'resources/themes/',
			plugins: 'resources/js/plugins/',
			skins  : 'skins/'
		},
		theme: {
			name: 'default',
			skin: ''
		},
		translation: {}
	}
};

/**
 * Erstes und letztes Element makieren
 *
 * @param m Mixed Selector oder das Element selbst
 * @param cF String Klassenname fÃ¼r erstes Kindelement
 * @param cL String Klassenname fÃ¼r letztes Kindelement
 *
 * @return /
 */
APP.markFirstLastChildren = function(s) {
	if(!s) {
		s = 'ul, dl';
	}

	$(s)
	.find('>:first').addClass('is-first').end()
	.find('>:last').addClass('is-last').end();
};

/**
 * Parse Opacity
 *
 * Transparenz automatisch per CSS-Klasse setzen.
 * <p class="set-opacity_{n}" />
 * n = 1-10 (0=0, 1=0.1, ..., 10=1)
 *
 * @param m Mixed Selector oder das Element selbst
 *
 * @return /
 */
APP.parseOpacity = function(m) {
	if(!m) {
		m = document.body;
	}

	for(var i=0; i<=10; i++) {
		$(m).find('.set-opacity_' + i).css('opacity', i/10);
	}
};

/**
 * Mouseenter und -leave für Listen (ul > li)
 *
 * @param m Mixed Selector oder das Element selbst
 * @param p Object Parameter
 *
 * @return /
 */
APP.appendHover = function(m, p) {
	if(!m) {
		m = $(document.body).find('.append-hover').not(".append-hover[class~='hover']");
	}

	if(m.length > 0) {

		var settings = $.extend({
			classHover: 'hover',
			onHover   : $.noop(),
			onLeave   : $.noop()
		}, p||{});

		m.hover(function() {
			$(this).addClass(settings.classHover);

			if($.isFunction(settings.onHover)) {
				settings.onHover($(this));
			}
		}, function() {
			$(this).removeClass(settings.classHover);

			if($.isFunction(settings.onLeave)) {
				settings.onLeave($(this));
			}
		});

	}
};

/**
 * Galerie initialisieren
 */
APP.gallery = function() {
	if($('.gallery').length>0 && $('.gallery').find('.gal-page').length>1) {

		$('.gallery .gal-content .gal-page_info').each(function() {
			$(this).hide().bind({
				'doHide': function() {
					$(this).fadeOut(300, function() {
						$(this).hide();
					});
				},
				'doShow': function() {
					$(this).slideDown(1000, function() {});
				}
			});
		});

		// Galerie '#feature' initialisieren.
		$('.gallery .gal-content').scrollable({
			circular  : true,
			easing    : 'easeInOutQuart',//'easeOutBounce',
			items     : '.gal-page',
			keyboard  : 'static',
			mousewheel: false,
			next      : '.gal-next',
			onBeforeSeek: function() {
				var idx = this.getIndex(),
					it = this.getItems().eq(idx).find('.gal-page_info');
				$(it).trigger('doHide');
			},
			onSeek    : function() {
				var idx = this.getIndex(),
					it = this.getItems().eq(idx).find('.gal-page_info');
				$(it).trigger('doShow');
			},
			prev      : '.gal-prev',
			speed     : 1200,
			vertical  : false
		}).navigator('.gal-nav').autoscroll({
			autoplay: true,
			interval: 6000,
			steps   : 1
		});
		var gallery_api = $('.gallery .gal-content').data("scrollable");
		gallery_api.seekTo(0, 1600);

	}
};

/**
 *
 */
APP.appendCufon = function() {
	if(Cufon != 'undefined') {
		$('ul.menubar li, #navigation li, a.btn-contact, .h1, .h2, .display-index #pageContent p').addClass('has-cufon');

		Cufon.set('fontFamily', 'calibri');
		Cufon.replace('#navigation a', {
			hover: true
		});
		Cufon.replace('a.btn-contact', {
			hover     : true
		});
		Cufon.replace('ul.menubar a', { hover: true });
		Cufon.replace('.h1, .h2');
		Cufon.replace('.display-index #pageContent p');
	}
};

/**
 *
 */
APP.Overlays = function(m) {
	if(!m) {
		m = document.body;
	}

	// Video overlays
	var ovly_el = $(m).find('a.overlay-movie');

	if(ovly_el.length>0 && $('#jqtMovieOverlay').length>0 && $('#jqt-player', '#jqtMovieOverlay').length>0) {
		var ovly_ply = $f('jqt-player', {
			src    : 'resources/swf/flowplayer-3.2.5.swf',
			bgcolor: '#fff'
		});

//		var ovly_ply = $f('jqt-player', 'resources/swf/flowplayer-3.2.5.swf');
		ovly_el.overlay({
			close       : '.jqt-close',
			closeOnClick: false,
			top         : 'center',
			mask        : {
				color    : '#000000',
				loadSpeed: 200,
				opacity  : 0.6
			},
			onClose: function() {
				ovly_ply.stop().unload();
				this.getTrigger().removeClass('is-active');
			},
			onBeforeLoad: function() {
				this.getTrigger().addClass('is-active');
			},
			onLoad: function() {
				var self = this;
				//var wrap = this.getOverlay().find('.jqt-ovly_body').eq(0).end();
				var href = this.getTrigger().attr('href');
				ovly_ply.play(href);
			},
			target: '#jqtMovieOverlay'
		});

	}
};

/**
 * Main-Initialisierung.
 */
$(function() {
	APP.settings = $.extend(true, APP.settings, APP_SETTINGS||{});	APP.settings = $.extend(true, APP.settings, APP_SETTINGS||{});

	$('a').live('click', function() { // fix border arround links
		$(this).blur();
	});

	APP.markFirstLastChildren(); // ? see function

//	$('#ServiceMenu ul:first')
//	.find('>li>a').css('opacity', 0.9).end()
//	.find('>li>ul').css('opacity', 0.9).end();

	$('#ServiceMenu').find('li').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});

	APP.parseOpacity(); // ? see function
	APP.gallery(); // ? see function
	APP.Overlays(); // ? see function

	// Bild vergrößern
	if($("a[rel='use-fb']").length > 0) {

		$("a[rel='use-fb']").fancybox({
//			'transitionIn'		: 'none',
//			'transitionOut'		: 'none',
//			'titlePosition' 	: 'over',
//			'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
//			    return '<span id="fancybox-title-over">Image ' +  (currentIndex + 1) + ' / ' + currentArray.length + ' ' + title + '</span>';
//			}
			'centerOnScroll': true,
			'transitionIn'  : 'elastic',
			'transitionOut' : 'elastic',
			'speedIn'       : 600,
			'speedOut'      : 200,
			'overlayColor'  : '#000',
			'overlayShow'   : true,
			'overlayOpacity': 0.6
		});

	}

	// Dropdown - Automatic submit parent form
	if($('select.auto').length>0) {
		$('select.auto').change(function() {
			$(this).closest('form').submit();
		});
	}

	// Searchform - Toggle value
	var searchform = $('#form_search'),
		searchinput = searchform.find('input#q'),
		searchvalue = searchinput.val();

	searchinput.focus(function(){
		if ($(this).val() === searchvalue) $(this).val("");
	}).blur(function(){
		if ($(this).val() === '') $(this).val(searchvalue);
	});

	// Tables - Zebra style
	$('table.zebra').each(function() {
	      $(this).find('>tbody > tr:odd').addClass('odd');
	      $(this).find('>tbody > tr:even').addClass('even');
	});






//	var teaser = $('.page_search .teaser, .event_list .teaser');
//
//	if(teaser.length>0) {
//		teaser.each(function() {
//			var url = $(this).find('a:eq(0)').attr('href');
//
//			if(url) {
//				$(this).find('a').each(function() {
//					$(this).replaceWith('<p>'+$(this).html()+'</p>');
//				});
//
//				$(this).closest('div.teaser').click(function() {
//					document.location.href=url;
//				}).css('cursor', 'pointer');
//			}
//		});
//	}
});
