/**
 **************************************************
 * HKF / http://relaunch.hkf-personal.de
 * userfunctions.cj.js
 *
 * @author:HDNET GmbH & Co. KG
 * @version:1.0
 * @updated:2010-11-10 (CJ)
 **************************************************
**/



$(document).ready(function(){

	var activeItem = 0;

	$('.nonsense').hide(0);

	$('p.big>img').not(':first').hide();

	$('p.big').mousemove(function(e) {

		//e.preventDefault();

		//Dimensions for the <p>-tag
		var width = $(this).width();
		var height = $(this).height();

		//the four areas of the image
		areaLeftTop = [];
		areaRightTop = [];
		areaLeftBottom = [];
		areaRightBottom = [];

		/*
		 * The array structure is always:
		 *
		 * array[width] = [min, max]
		 * array[height] = [min,max]
		 */
		areaLeftTop[0] = [0, width / 2.0];
		areaLeftTop[1] = [0, 108];

		areaRightTop[0] = [width / 2.0, width];
		areaRightTop[1] = [0, 108];

		areaLeftBottom[0] = [0, width / 2.0];
		areaLeftBottom[1] = [108, height];

		areaRightBottom[0] = [width / 2.0, width];
		areaRightBottom[1] = [108, height];

		//offset for the <p>-tag relative to the document
		var pOffset = $(this).offset();

		/*
		 * calculating the inner offset from the mouseclick
		 * to the border of the <p>-tag
		 *
		 * posX = width
		 * posY = height
		 */
		var posX = e.pageX - pOffset.left;
		var posY = e.pageY - pOffset.top;

		if(isInWidthRange(posX, areaLeftTop) && isInHeightRange(posY, areaLeftTop) && activeItem != 0) {
			$('p.big>img:visible').fadeOut(0);
			$('p.big>img').eq(0).fadeIn(0);
			activeItem = 0;
		} else if(isInWidthRange(posX, areaRightTop) && isInHeightRange(posY, areaRightTop) && activeItem != 1) {
			$('p.big>img:visible').fadeOut(0);
			$('p.big>img').eq(1).fadeIn(0);
			activeItem = 1;
		} else if(isInWidthRange(posX, areaRightBottom) && isInHeightRange(posY, areaRightBottom) && activeItem != 2) {
			$('p.big>img:visible').fadeOut(0);
			$('p.big>img').eq(2).fadeIn(0);
			activeItem = 2;
		} else if(isInWidthRange(posX, areaLeftBottom) && isInHeightRange(posY, areaLeftBottom) && activeItem != 3) {
			$('p.big>img:visible').fadeOut(0);
			$('p.big>img').eq(3).fadeIn(0);
			activeItem = 3;
		}
	});


	var once = true;

	$('p.big').click(function(e){
		e.preventDefault();

		var activeItem = 0;

		$('p.big').unbind('mousemove');

		//Dimensions for the <p>-tag
		var width = $(this).width();
		var height = $(this).height();

		//the four areas of the image
		areaLeftTop = [];
		areaRightTop = [];
		areaLeftBottom = [];
		areaRightBottom = [];

		/*
		 * The array structure is always:
		 *
		 * array[width] = [min, max]
		 * array[height] = [min,max]
		 */
		areaLeftTop[0] = [0, width / 2.0];
		areaLeftTop[1] = [0, 108];

		areaRightTop[0] = [width / 2.0, width];
		areaRightTop[1] = [0, 108];

		areaLeftBottom[0] = [0, width / 2.0];
		areaLeftBottom[1] = [108, height];

		areaRightBottom[0] = [width / 2.0, width];
		areaRightBottom[1] = [108, height];

		//offset for the <p>-tag relative to the document
		var pOffset = $(this).offset();

		/*
		 * calculating the inner offset from the mouseclick
		 * to the border of the <p>-tag
		 *
		 * posX = width
		 * posY = height
		 */
		var posX = e.pageX - pOffset.left;
		var posY = e.pageY - pOffset.top;

		$('.nonsense').slideUp(1000);

		if(!once) {
			$('p.big>img:visible').fadeOut(1000);
		} else {
			once = false;
		}

		if(isInWidthRange(posX, areaLeftTop) && isInHeightRange(posY, areaLeftTop)) {
			$('p.big>img').eq(0).fadeIn(1000);
		} else if(isInWidthRange(posX, areaRightTop) && isInHeightRange(posY, areaRightTop) && activeItem != 1) {
			$('div#menuHKF').slideDown(1000);
			$('p.big>img').eq(1).fadeIn(1000);
			activeItem = 1;
		} else if(isInWidthRange(posX, areaRightBottom) && isInHeightRange(posY, areaRightBottom) && activeItem != 2) {
			$('div#menuBewerber').slideDown(1000);
			$('p.big>img').eq(2).fadeIn(1000);
			activeItem = 2;
		} else if(isInWidthRange(posX, areaLeftBottom) && isInHeightRange(posY, areaLeftBottom) && activeItem != 3) {
			$('div#menuKunden').slideDown(1000);
			$('p.big>img').eq(3).fadeIn(1000);
			activeItem = 3;
		}
	});
	
	if ($('.jobDetail').length > 0) {
		var target = $('.jobDetail');
		$.scrollTo(target,1000);
		$('.jobDetail').css({
			opacity:0
		}).animate({
			opacity:1
		}, 1500);
	}
	
});

function isInWidthRange(intval, sector) {
	if(intval >= sector[0][0] && intval <= sector[0][1]) {
		return true;
	}
	return false;
}

function isInHeightRange(intval, sector) {
	if(intval >= sector[1][0] && intval <= sector[1][1]) {
		return true;
	}
	return false;
}

// BEN:Helpful wrapper for logging, prevents IEs from throwing errors
jQuery.log = function(s) {
	if(window.console) {
		console.log(s);
	} else {
// Only if really needed. It's quite annoying without a console...
//alert(s);
}
}
