/**
 * custom scripts for dimitri-plugin
 * @author Martin Sprekelsen <martin@sintesilabs.eu>
 */

/*---------------*
 | DOM-READY     |
 *---------------*/
$(document).ready(function () {
	
	// menu-fixes
	dimitriMenu();
	
	// scroll-buttons for blog
	dimitriBlogScrollTo();
	
	// align description-text in collection-detail-view
	dimitriFixCollectionsDetailText();
	
	// align agb to online-shop iFrame
	$('#dimitriShopCanvas').iframeAutoHeight({
		callback: function(o){
					console.log(o.newFrameHeight);
					$('#dmtrShopMenu').vAlignTo('#dimitriShopCanvas',{'targetAnchor':'bottom'});			
		}
		});
	
	
	
	// lightbox for press-view
	Shadowbox.init({
		'overlayColor':'#ffffff',
		'overlayOpacity':0.9,
		'resizeDuration':0.1,
		'viewportPadding':60
	});
});

/*---------------*
 | WINDOW LOADED |
 *---------------*/
$(window).load(function () {
	
	// intro-animation
	$('#dmtrInto').dimitriIntro().init();
	
	
});


function dimitriFixCollectionsDetailText(){
	if($('#dmtrCollectionsDetailText').length == false){
		return null;
	}
	
	//wrap in alignment-container
	$('#dmtrCollectionsDetailText').wrap('<div id="dmtrCollectionsDetailTextWrapper"></div>');
	
	//align with image
	var left = $('.dimitriCollectionsDetailThumb').position().left + $('.dimitriCollectionsDetailThumb>img').outerWidth() - 850;
	var height  = $('.dimitriCollectionsDetailThumb>img').outerHeight();
	$('#dmtrCollectionsDetailTextWrapper').css({'margin-left':left,'height':height});
}



/**
 * dimitriMenu
 */
function dimitriMenu(){
	
	// remove all levels greater than L3
	$('#mnu li.L4').remove();
	
	
	// hide expanded submenu
	$('#mnu ul.L2').hide();
	
	// sliding submenu
	
	var watcher = new HoverWatcher('li.L1');
	
	$('#mnu li.L1').on('mouseenter',function(el){
			var next = $(this).children('ul.L2');
			next.stop(true).fadeIn(150);
	}).on('mouseleave',function(el){	
		if($(el.target).not('a.L1')){
			var next = $(this).children('ul.L2');
			next.stop(true,true).fadeOut(150);
		}	
	});
	
	$('#mnu ul.L2').on('mouseover',function(){
		$(this).stop(true,true).show();
	});
	$('#mnu ul.L2').on('mouseout',function(){
		var me = $(this);

		setTimeout(function(){
			if(watcher.isHoveringOver() == false){
				me.stop().fadeOut(150);
			}
		}, 50);
	});
		
	// remove href from L1-menu-entries with submenus
	$('#mnu ul.L1>ul').each(function(){
		$(this).prev().find('a').attr('href','#');
	});
	
	$('#mnu li.L1').clickHelper('','.ffWdgetTitle, .ffWidget, .ffIcon, input');
	
	// shortcut to avoid reloading of page to show L3
	// (avoid this, if we're logged in)
	if($('#isLoggedIn').length){
		return null ;
	}
	
	$('#mnu li.L2').on('hover',function(){
		
		var positive01 = $(this).parent().prev().hasClass('ffMenuWithinActives');
		//if(window.location.href.find('feature=') == -1);
		var q = window.location.href;
		
		if(q.search('feature=') == -1){
			var positive02 = true;
		} else {
			var positive02 = false;
		}
		
		var negative01 = $('li.L3.ffMenuWithinActives').length;
		
		if((positive01 == true || positive02 == true) && negative01 == false){
			
			$(this).children('a').attr('href','#');
			
			$(this).click(function(e){

				// get content from L3 to be copied 
				var L3 = $(this).children('ul').clone();
				L3.show();
				
				// get title to be copied
				var title = $(this).children('a').text();
				
				// transfer to L3
				if($('#mnuL3').length){
					$('#mnuL3').empty();
				} else {
					$('<div id="mnuL3" class="ffPresenter ffMenu"></div>').prependTo('body');
				}
				$('#mnuL3').append(L3);
				
				// transfer title
				if($('#dmtrCollectionsSelector').length){
					$('#dmtrCollectionsSelector .ffFolderNameBlock').empty();
				} else {
					$('<div id="dmtrCollectionsSelector" class="ffPresenter ffMenu"><div class="ffFolderNameBlock"></div></div>').prependTo('body');
				}
				$('#dmtrCollectionsSelector .ffFolderNameBlock').append(title);
				
			});	
		}
	});
}

function HoverWatcher(selector){
	this.hovering = false;
	var self = this; 

	this.isHoveringOver = function() { 
		return self.hovering; 
	} ;

	$(selector).hover(function() { 
		self.hovering = true; 
	}, function() { 
		self.hovering = false; 
	});
} 

/**
 * dimitriBlogScrollTo
 * smooth scrolling for dimitris blog
 */
function dimitriBlogScrollTo(){
	$('.dimitriBlogBtnNextEntry').find('a').remove();
	$('.dimitriBlogBtnNextEntry').css('cursor','pointer');
	$('.dimitriBlogBtnNextEntry').on('click',function(e){
		var scrollTargetId = $(this).attr('id').substring(3,$(this).attr('id').lenght);
		var scrollTarget   = $('#' + scrollTargetId);
		if(scrollTarget.length){
			var d = Math.round(scrollTarget.offset().top);
			$('html, body').animate({scrollTop: d}, '5000');
		}
	});
}

/*!	
* dimitriIntro 1.0
*
*/
(function($){
	$.fn.dimitriIntro = function() {
		
		var start = function(){
			//console.log('intro start!');
			//$('#symbolStart').animate({'opacity':0.01},400,function(){
			//	$('#logoStart').animate({'width':259,'height':41},  500);
			//	$('#introLogo').animate({'top':-235,'margin-top':0,'width':259,'margin-left':-130},500,function(){
			//		$('#dmtrIntro').fadeOut(1000);
			//	});
				$('#dmtrIntro').fadeOut(1200);
			//});
			
		};
		
		this.init = function(){
			//console.log('intro init!');
			setTimeout(function(){
				start();
			}, 1000);
		};
		return this;
	};
})(jQuery);



/*global jQuery */
/*!	
* vAlignTo 1.1
*
* vertically align an element to another element 
* Copyright 2011, Martin Sprekelsen martin@sintesilabs.eu
*
* please contact the author for licensing
*
*/
(function($){
	$.fn.vAlignTo = function(target, options) {
		
		// settings
		var settings = $.extend({
			'myAnchor'     : 'top',
			'targetAnchor' : 'top',
			'margin'       : 0
		}, options);

		return this.each(function(){
			if(settings.targetAnchor == 'bottom'){
				var anchor = $(target).offset().top + $(target).outerHeight() - $(window).scrollTop();
				console.log('aligning to bottom');
			} else {
				var anchor = $(target).offset().top - $(window).scrollTop();
				console.log('aligning to top');
			}
			console.log('anchor: ' +anchor);
			$(this).css(settings.myAnchor,anchor);
			
		});

		return this;
	};
})(jQuery);


/*global jQuery */
/*!	
* clickHelper 1.2
*
* Copyright 2011, Martin Sprekelsen martin@sintesilabs.eu
* please contact the author for licensing
*
*/
(function($){
	$.fn.clickHelper = function(href, exception) {
		
		return this.each(function(){

			// crawl for first a as href if not set
			if(href == null || typeof(href)!='undefined'){
				href = $(this).find('a').attr('href');
				target = $(this).find('a').attr('target');
			} else {
				target = null;
			}
			
			if(typeof(href)!='undefined'){
				$(this).css('cursor','pointer');
			}
			
			this.target = target;
			this.href = href;
			$(this).click(function(e){
				if(   $(e.target).is('a')
				   || $(e.target).is(exception))
				{
					// do nothing.
				} else{
					if(typeof(this.href)!='undefined'){
						if(this.target=='_blank'){
							window.open(this.href);
						} else {
							window.location.href = this.href;
						}
					}
				}
			});
		});
		return this;
	};
})(jQuery);
