var $j = jQuery.noConflict();

$j(document).ready(function(){
	
	//
	// SCROLL TO TOP
	//
	$j('.to_top').click(function(){
		$j.scrollTo(0, 1200, {easing: 'easeOutExpo'});
	});
	
	//
	// IMAGE LOAD UP *HOME*
	//
	$j('img.loaduphome').each(function() {
		
		var width = $j(this).width();
		var height = $j(this).height();
		
		$j(this).wrap('<div class="imageloaduphome"></div>');
		
		$j(this).parent().css({
			'width' : width,
			'height' : height
		});
	
	});
	
	$j('.screen').each(function(){
		var randomdelay = Math.floor(Math.random()*1500)+1000;
		$j(this).slideUp(randomdelay);
	});		
	
	$j('div.imageloaduphome').hoverIntent(function(){$j(this).children('img.loaduphome').fadeIn();}, function(){$j(this).children('img.loaduphome').fadeOut();});
	
	// END IMAGE LOAD UP *HOME*
	
	
	//
	// IMAGE LOAD UP *SINGLE*
	//
	
	$j('#single-content .post img').each(function() {
		
		var width = $j(this).width();
		var height = $j(this).height();
		
		$j(this).wrap('<div class="single-row-float"></div>');
		
		$j(this).parent().css({
			'width' : width,
			'height' : height
		});
		
		$j(this).after('<div class="screen-single"></div>');
		
		$j(this).next('.screen-single').css({
			'width' : width,
			'height' : height,
			'top' : -height-15
		});
		
		$j('.screen-single').each(function(){
			var randomdelay = Math.floor(Math.random()*1500)+1000;
			$j(this).slideUp(randomdelay);
		});
		
	});	
	
	// END IMAGE LOAD UP *SINGLE*
	

	
	//
	// SINGLE ROW FLOATS
	//
	
	function checkFloats() {
		$j('div.post-preview').each(function(){
		
		
			// if this element is on the same position as the one before
			// and it is smaller, give it a min-height equal to the height of the
			// object before it
			
			that = $j(this);
			
			// if there is an item before it, and if the item before it is in the same row
			if ($j(that).prev('div.post-preview').length && ($j(that).position().top == $j(that).prev('div.post-preview').position().top)) {
			
				
				if ($j(that).prev('div.post-preview').height() > $j(that).height()) {
					$j(that).css({'min-height': $j(that).prev('div.post-preview').height()});
				}
				
				else if ($j(that).prev('div.post-preview').height() == $j(that).height()) {
					$j(that).css({'min-height': $j(that).prev('div.post-preview').height()});
				}
		
				else {
					
				}
			
			}
			
			// if the item before it is not in the same row, reset the min-height so what the object is
			else if ($j(that).prev('div.post-preview').length && ($j(that).position().top != $j(that).prev('div.post-preview').position().top)) {
				$j(that).css({'min-height': $j(that).children("img").height()});
			}
	
		});
		
		$j('div.single-row-float').each(function(){
		
		
			// if this element is on the same position as the one before
			// and it is smaller, give it a min-height equal to the height of the
			// object before it
			
			that = $j(this);
			
			// if there is an item before it, and if the item before it is in the same row
			if ($j(that).prev('div.single-row-float').length && ($j(that).position().top == $j(that).prev('div.single-row-float').position().top)) {
			
				
				if ($j(that).prev('div.single-row-float').height() > $j(that).height()) {
					$j(that).css({'min-height': $j(that).prev('div.single-row-float').height()});
				}
				
				else if ($j(that).prev('div.single-row-float').height() == $j(that).height()) {
					$j(that).css({'min-height': $j(that).prev('div.single-row-float').height()});
				}
		
				else {
					
				}
			
			}
			
			// if the item before it is not in the same row, reset the min-height so what the object is
			else if ($j(that).prev('div.single-row-float').length && ($j(that).position().top != $j(that).prev('div.single-row-float').position().top)) {
				$j(that).css({'min-height': $j(that).children("div.single-row-float").height()});
			}
			
	
		});
	}
	
	//If the User resizes the window, adjust the #post-preview height
	$j(window).bind("resize", resizeWindow);
	function resizeWindow( e ) {
		checkFloats();
	}
	
	//
	// END SINGLE ROW FLOATS
	//
	
	//
	// RANDOM COLOUR BOXES
	//
	function yesno() {
		if (Math.floor(Math.random()*30) < Math.floor(Math.random()*30)) {return true;}
		else {return false;}
	}
	
	
	function randomColour(i) {
		var colours = ['#2A2724','#EF4D2A','#FFA86C', '#52AD7E', '#D5BCC6', '#009CAA'];
		colour = colours[i % (colours.length)];
		return colour;
	}
	
	function randomSize() {
		var sizes = ['110','230','470'];
		size = sizes[Math.floor(Math.random()*2)];
		return size;
	}
	
	function drawRandomBox() {
		var output = "<div class='randombox' style='background:"+randomColour(Math.floor(Math.random()*30))+"; height: "+randomSize()+"px; width: "+randomSize()+"px;'></div>";
		return output;
	}
	
	//
	// NEUIGKEITEN
	//

	/**
	 * Callback function that displays the content.
	 *
	 * Gets called every time the user clicks on a pagination link.
	 *
	 * @param {int}page_index New Page index
	 * @param {jQuery} jq the container with the pagination links as a jQuery object
	 */
	function pageselectCallback(page_index, jq){
	    var new_content = $j('#neuigkeitenHidden div.neuigkeitenItem:eq('+page_index+')').clone();
	    $j('#neuigkeitenResults').html(new_content);
	    return false;
	}
	
	/** 
	 * Callback function for the AJAX content loader.
	 */
	function initPagination() {
	    var num_entries = $j('#neuigkeitenHidden div.neuigkeitenItem').length;
	    
	    // Create pagination element
	    $j("#neuigkeitenPagination").pagination(num_entries, {
	        num_edge_entries: 0,
	        num_display_entries: 0,
	        callback: pageselectCallback,
	        items_per_page:1,
	        next_text: '>',
	        prev_text: '<',
	        archive_link: '/neuigkeiten'
		});
	}
	 
	initPagination();
			
	var newsConfig = {
		sensitivity: 3,
		interval: 200,
		over: showNews,
		timeout: 500,
		out: hideNews
	}
	
	function showNews() {
		var src = $j('#nav-neuigkeiten').attr("src").replace(".png","-active.png");
		$j('#nav-neuigkeiten').attr("src", src);
		$j('#nav-neuigkeiten').removeClass("inactive");
		$j('#neuigkeitenPopup').fadeIn(250);
		$j('#neuigkeitenBack').fadeIn(250);
	}
	
	function hideNews() {
		var src = $j('#nav-neuigkeiten').attr("src").replace("-active","");
		$j('#nav-neuigkeiten').attr("src", src);
		$j('#nav-neuigkeiten').addClass("inactive");
		$j('#neuigkeitenPopup').fadeOut(250);
		$j('#neuigkeitenBack').fadeOut(250);
	}
	
	$j('#nav-neuigkeiten').toggle(function(){showNews()},function(){hideNews()});
	
	// When page initializes, check the single row floats
	checkFloats();	
	
	// give images hover fades
	$j('div.imageloaduphome').hoverIntent(function(){$j(this).children('img.loaduphome').fadeIn();}, function(){$j(this).children('img.loaduphome').fadeOut();});
	
	// preload menu images
	$j('#menu img').preload({
	    find: '.png',
	    replace: '-active.png'
	});
	
	$j('#footer img').preload({
	    find: '.png',
	    replace: '-active.png'
	});
	
	// menu rollovers
	$j('#menu .inactive img').hover(
		function() {
			this.src = this.src.replace(".png","-active.png");
		},
		function() {
			this.src = this.src.replace("-active","");

	});
	
	$j('#footer img').hover(
		function() {
			this.src = this.src.replace(".png","-active.png");
		},
		function() {
			this.src = this.src.replace("-active","");

	});
	
	// kill titles
	$j('a').attr("title", "");
	$j('img').attr("title", "");
	
	
	//
	// BACK LINKS
	//
	$j('.backlink').click(function(){
	
		// SET THE CURRENT DOMAIN
		var currentDomain = "k-produktdesign.de";
		
		// If it was linked from the site, go back in history
		if(document.referrer.indexOf(currentDomain) != -1) {
			history.go(-1);
		}
		
		// otherwise go to homepage
		else {
			window.location=currentDomain;
		}
	
	
	});
	
	
});
