
// remap jQuery to $
(function($){

})(this.jQuery);



// usage: log('inside coolFunc',this,arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  if(this.console){
    console.log( Array.prototype.slice.call(arguments) );
  }
};


// catch all document.write() calls
(function(doc){
  var write = doc.write;
  doc.write = function(q){ 
    log('document.write(): ',arguments); 
    if (/docwriteregexwhitelist/.test(q)) write.apply(doc,arguments);  
  };
})(document);


//
// Functions
//

var isNumber = function isNumber(value) {
	return typeof value === 'number' && isFinite(value);
}


//
// Define our UI-Global
//

var DSUI = {
	
};



//
// Add mailto: Links to e-mail adresses
//

DSUI.emailSetLinks = function ( node ) {
	var new_addr =  node.text().split('').reverse().join('');
	node.attr('href', 'mailto:' + new_addr);
	node.html( new_addr );
};


//
// Kontaktformular Scripts
//

DSUI.KFormSetup = function() {
	
	// Hover labels
	$('#KForm .text input').add('#KForm .text textarea').focus( function() {
		$(this).parent('label').addClass('active');
	})
		.blur( function() {
			$(this).parent('label').removeClass('active');
		});
	
	
	// Submit Button
	$('#FormSubmit').html( $('#SubmitBtn').val() )
	.mouseenter(function(){
		$(this).removeClass('FormSubmitActive');
		$(this).addClass('FormSubmitHover');
	})
	.mouseleave(function(){
		$(this).removeClass('FormSubmitHover');
		$(this).removeClass('FormSubmitActive');
	})
	.mousedown(function(){
		$(this).removeClass('FormSubmitHover');
		$(this).addClass('FormSubmitActive');
	})
	.mouseup(function(){
		$(this).removeClass('FormSubmitActive');
		$(this).addClass('FormSubmitHover');
	})
	.click( function() {
		$('#KForm').submit();
	});
	
	
	/* Fade-out errors on focus */
	$('input').focus( function() {
		$(this).prev().children('.error').fadeOut(300);
	});
	
	
};





//
// Slideshow - Home
//

DSUI.homeSlides = {
	
	wrap  : $('#Home-Slides')
	,nodes  : []
	,current : 0
	,interval : 0
	
	// ------------------------------------------
	
	// Methods
	
	,init : function() {
		
		// get images
		this.wrap.children('li').map( function() {
			DSUI.homeSlides.nodes.push( $(this) );
			$(this).css('display', 'none');
		});
		if ( this.nodes.length <= 1 ) {
			return;
		}
		// shuffle images
		//this.nodes = this.shuffle( this.nodes );
		
		// display bullet-nav
		var i, 
			hsnav = '', 
			len = this.nodes.length;
			
		for ( i=0; i<len; i++ ) {
			hsnav += '<span id="hs'+i+'"></span>';
		}
		$('<div id="Home-Slides-Nav">'+hsnav+'</div>').insertAfter( this.wrap );
		
		$('#Home-Slides-Nav span').mouseenter( function() {
			var slidenumber = $(this).attr('id').substr(2);
			DSUI.homeSlides.gotoSlide(slidenumber);
		});
		
	}
	
	,start : function() {
				
		// show first
		this.nodes[this.current].css('display', 'block');
		
		this.setNav();
		
		this.setInterval();
		
	}
	
	,setInterval : function() {
		// start slideshow
		this.interval = window.setInterval( 'DSUI.homeSlides.nextSlide()', 3000 );
	}
	
	,clearInterval : function() {
		window.clearInterval( DSUI.homeSlides.interval );
	}
	
	,nextSlide : function() {
		// hide current image
		this.stopAnim();
		this.nodes[this.current].fadeOut( 1400 );
		// next slide
		this.current += 1;
		if ( this.current >= this.nodes.length ) {
			this.current = 0;
		}
		// fade-in
		this.nodes[this.current].fadeIn( 1400 );
		this.setNav();
	}
	
	,gotoSlide : function(slidenr) {
		if ( slidenr != this.current && slidenr < this.nodes.length && slidenr >= 0 ) {
			this.clearInterval();
			this.stopAnim();
			// hide current image
			this.nodes[this.current].fadeOut( 400 );
			// set current
			this.current = slidenr *1;
			// fade-in
			this.nodes[this.current].fadeIn( 400 );
			
			this.setNav();
			
			this.setInterval();
		}
	}
	
	,stopAnim : function() {
		var i, len = this.nodes.length;
		for ( i=0; i<len; i++ ) {
			this.nodes[i].stop(true,true);
		}
	}
	
	,setNav : function() {
		$('#Home-Slides-Nav span').removeClass('active');
		$( '#hs' + this.current ).addClass('active');
	}
		
	,shuffle : function ( a ) {
		var tmp, cur, top = a.length;
	
	    if(top) while(--top) {
	        cur = Math.floor(Math.random() * (top + 1));
	        tmp = a[cur];
	        a[cur] = a[top];
	        a[top] = tmp;
	    }
	    return a;
	    
	}
	
}; // DSUI.homeSlides



function shuffle(array) {
    var tmp, current, top = array.length;

    if(top) while(--top) {
        current = Math.floor(Math.random() * (top + 1));
        tmp = array[current];
        array[current] = array[top];
        array[top] = tmp;
    }

    return array;
}



// 
// Picture-Slider
//

DSUI.picSlider = {
	
	// Wrapper
	wrap : '#Picture-List'
	,prev_class : '.pic-prev'
	,next_class : '.pic-next'
	
	,slider : {}
	
	// number of picts per screen
	,max_pics : 4
	
	// height and width incl. padding/margin
	,p_height : 136
	,p_width : 194
	,width: 0
	,pict_count : 0
	,current_screen : 0
	,screen_width : 0
	,screen_count : 0
	
	
	
	,init : function() {
		
		
		this.slider = $( DSUI.picSlider.wrap + ' ul' );
		this.pict_count = this.slider.children('li').length;
		this.screen_count = Math.ceil( this.pict_count / this.max_pics);
		this.screen_width = this.max_pics * this.p_width;
		this.width = this.p_width * this.pict_count;
		
		// set width of slider
		this.slider.css({
			'width' : DSUI.picSlider.width
		});
		
		// show pic-navigation
		$('<div id="pic-nav"><span class="pic-nav-item pic-prev png_bg">prev</span><span class="pic-nav-item pic-next png_bg">next</span></div>').insertAfter( $( DSUI.picSlider.wrap ) );
		
		// Next/Prev Buttons
		$('#pic-nav .pic-next').mouseenter( function() {
			$(this).addClass('pic-next-active');
		})
		.mouseleave( function() {
			$(this).removeClass('pic-next-active');
		})
		.click( function() {
			DSUI.picSlider.next();
		});
		$('#pic-nav .pic-prev').mouseenter( function() {
			$(this).addClass('pic-prev-active');
		})
		.mouseleave( function() {
			$(this).removeClass('pic-prev-active');
		})
		.click( function() {
			DSUI.picSlider.prev();
		});
		
		// set rollovers for pics
		$('.pic-roll').mouseenter( function() {
			if ( $(this).children('img').hasClass('portrait') ) {
				$(this).children('img').animate({
					'width' : '86px'
					,'height' : '125px'
					,'left' : '53px'
					,'top' : '5px'
				},200);
			}
			else {
				$(this).children('img').animate({
					'width' : '183px'
					,'height' : '125px'
					,'left' : '5px'
					,'top' : '5px'
				},200);
			}
		})
		.mouseleave( function() {
			if ( $(this).children('img').hasClass('portrait') ) {
				$(this).children('img').animate({
					'width' : '76px'
					,'height' : '115px'
					,'left' : '58px'
					,'top' : '10px'
				},200);
			}
			else {
				$(this).children('img').animate({
					'width' : '173px'
					,'height' : '115px'
					,'left' : '10px'
					,'top' : '10px'
				},200);
			}
		});
		
		// hide prev-button
		$( DSUI.picSlider.prev_class ).css('display', 'none');
	}
	
	,next : function() {
		
		var new_l = 0;
		this.current_screen += 1;
		
		// get next screen position (start or + 1)
		if ( this.current_screen < this.screen_count ) {
			new_l = this.current_screen * this.screen_width;
		}
		else {
			this.current_screen = 0;
		}
		
		// animate the slider
		this.slide(new_l);
	}
	
	,prev : function() {
		
		this.current_screen -= 1;
		var new_l = this.current_screen * this.screen_width;
		
		// get next screen position (start or + 1)
		if ( this.current_screen < 0 ) {
			//this.current_screen = this.screen_count - 1;
			//new_l = this.current_screen * this.screen_width;
			this.current_screen = 0;
			new_l = 0;
		}
		
		// animate the slider
		this.slide(new_l);
	}
	
	,slide : function(new_l) {
		// animate slider
		this.slider.animate({
			'left' : '-' + new_l + 'px'
		},600);
		
		// show/hide navigation
		var pr = $(DSUI.picSlider.prev_class);
		var ne = $(DSUI.picSlider.next_class);
		
		// show prev
		if ( this.current_screen > 0 && pr.css('display') === 'none' ) {
			pr.fadeIn(400);
		}
		// hide prev
		if ( this.current_screen === 0 && pr.css('display') === 'block' ) {
			pr.fadeOut(400);
		}
		// show next
		if ( this.current_screen < this.screen_count && ne.css('display') === 'none' ) {
			ne.fadeIn(400);
		}
		// hide next
		if ( this.current_screen === this.screen_count - 1 && ne.css('display') === 'block' ) {
			ne.fadeOut(400);
		}
	}
	
};


//
// Big Image
//

DSUI.bigImage = {
	
	init : function() {
		
		// fade-in image
		$('#Big-Image img').css({
			'opacity' : 0
			,'visibility' : 'visible'
		})
		.animate({
			'opacity' : 1
		}, 600);
		
		// buttons
		
		
	}
	
};



//
// Quip Comments
//

DSUI.quipVis = {
	
	init : function() {
		
		// Change behaviour of Comment-Link to display comments
		$('#Comment-Link a').click( function() {
			if ( $('#Quip-Comments').css('display') == 'none' ) {
				$('#Quip-Comments').slideDown(300, function() {
					// scroll to new position
					$('html,body').animate( { scrollTop: $('#Quip-Comments').offset().top }, 'slow' );
				});
			}
			else {
				$('#Quip-Comments').slideUp(300);
			}
			return false;
		});
		
		// check GET-Request and hash-tag for quip-related stuff
		var hash = document.location.hash;
		var query = document.location.search;
		if ( hash != "" || query != "" ) {
			
			$('#Quip-Comments').css('display', 'block');
		}
		
	}
	
};





