/* Scripts for Minus 9 design */

$(document).ready(function(){
	
	var winWidth = 960;
	var winHeight = 560;
	
	if ( (isIE) ? isIE == true : false ){
		var css_right = 'margin-right';
		var css_bottom = 'margin-bottom';
		winHeight = 500;
	} else {
		var css_right = 'right';
		var css_bottom = 'bottom';
	}
	
	if ($("#intuitive_system_home").length != 0){
		var int_width = $("#intuitive_system_home").width();
		var int_height = $("#intuitive_system_home").height();
	} else if ($("#intuitive_system").length != 0) {
		var int_width = $("#intuitive_system").width();
		var int_height = $("#intuitive_system").height();
	}
	
	var final_h_difference = 0;
	var final_v_difference = 0;
	
	var final_extra = 0;
	
	$(".label").each(function(){
		
		var pos = $(this).position();
		var width = $(this).width();
		var height = $(this).height();
		
		var rightness = pos.left + width;
		var downness = pos.top + height;
		
		if (rightness > int_width){
			var difference = Math.round(rightness - int_width);
			if (difference > final_h_difference){
				final_h_difference = difference;
			}
		}
		if (downness > int_height){
			var difference = Math.round(downness - int_height);
			if (difference > final_v_difference){
				final_v_difference = difference;
			}
		}
	});
	
	$(".selected_label_holder").each(function(){
		
		var width = $(this).width();
		var height = $(this).height();
		
		$(this).css({
			'position':'absolute',
			'float':''
		});
		
		var pos = $(this).position();
		var rightness = pos.left + width;
		var downness = pos.top + height;
		
		if (rightness > int_width){
			//var test = $(this).css('left');
			//if (test > test_pcent){
				//test_pcent = test;
			//}
			var difference = Math.round(rightness - int_width);
			var extra = Math.round( (int_width / pos.left) * 100 );
			if (extra > final_extra){
				final_extra = extra;
			}
			if (difference > final_h_difference){
				final_h_difference = difference;
			}
		}
		
		if (downness > int_height){
			var difference = Math.round(downness - int_height);
			if (difference > final_v_difference){
				final_v_difference = difference;
			}
		}
		
		$(this).width(width + 5);
	});
	
	if (final_h_difference > 0){
		if(isIE == true){
			if ($("#intuitive_system_home").length != 0){
				final_h_difference = final_h_difference + 60 + 280 + "px";
				$("#intuitive_system_home").css(css_right, final_h_difference);
			} else if ($("#intuitive_system").length != 0){
				final_h_difference = final_h_difference + 60 + final_extra + "px";
				$("#intuitive_system").css(css_right, final_h_difference);
				
			}
		} else {
			final_h_difference = final_h_difference + 30 + final_extra +"px";
			if ($("#intuitive_system_home").length != 0){
				$("#intuitive_system_home").css(css_right, final_h_difference);
			} else if ($("#intuitive_system").length != 0){
				$("#intuitive_system").css(css_right, final_h_difference);
			}
		}
	}
	
	if (final_v_difference > 0){
		final_v_difference = final_v_difference + 60 + "px";
		if ($("#intuitive_system_home").length != 0){
			$("#intuitive_system_home").css(css_bottom, final_v_difference);
		} else if ($("#intuitive_system").length != 0){
			$("#intuitive_system").css(css_bottom, final_v_difference);
		}
	}
	
	$(".intuitive_entry_image").each(function(){
		var pos = $(this).position();
		var width = $(this).width();
		var height = $(this).height();
		var rightness = pos.left + width;
		var downness = pos.top + height;
		if (rightness > winWidth){
			var left = Math.round(100 - (100 / (winWidth / width))) + "%";
			$(this).css('left',left);
		}
		if(downness > winHeight){
			var top = Math.round(100 - (100 / (winHeight / height))) + "%";
			$(this).css('top',top);
		}
	});
	
	$(".intuitive_entry_video").each(function(){
		var pos = $(this).position();
		var width = $(this).width();
		var height = $(this).height();
		var rightness = pos.left + width;
		var downness = pos.top + height;
		if(rightness > winWidth){
			var left = Math.round(100 - (100 / (winWidth / width))) + "%";
			$(this).css('left',left);
		}
		if(downness > winHeight){
			var top = Math.round(100 - (100 / (winHeight / height))) + "%";
			$(this).css('top',top);
		}
	});
	
	$(".intuitive_entry_field").each(function(){
		var pos = $(this).position();
		//var width = $(this).width();
		var width = 400;
		var height = $(this).outerHeight();
		var rightness = pos.left + width;
		var downness = pos.top + height;
		if(rightness > winWidth){
			var left = 100 - (100 / (winWidth / width)) + "%";
			$(this).css('left',left);
		}
		if(downness > winHeight){
			var top = 100 - (100 / (winHeight / height)) + "%";
			$(this).css('top',top);
		}
	});
	
	$(".intuitive_entry_title").each(function(){
		var width = $(this).width();
		var height = $(this).height();
		$(this).css({
			'position':'absolute',
			'float':''
		});
		var pos = $(this).position();
		var rightness = pos.left + width;
		var downness = pos.top + height;
		if(rightness > winWidth){		
			var left = 100 - (100 / (winWidth / width)) + "%";
			$(this).css('left',left);
		}
		if(downness > winHeight){
			var top = 100 - (100 / (winHeight / height)) + "%";
			$(this).css('top',top);
		}
		$(this).width(width);
	});
	
	$(".intuitive_entry_field .content").hide();
	
	$(".intuitive_entry_field").live('mouseover',function(){
		$(this).children(".content").show();
		$(this).css(
			'z-index','11000'
		);
	}).live('mouseout',function(){
		$(this).children(".content").hide();
		$(this).css(
			'z-index','10000'
		);
	});
	
	// show all contents of asterisks when you rollover the main title one
	$(".intuitive_entry_title .asterisk").live('mouseover',function(){
		$(".intuitive_entry_field").children(".content").show();
	}).live('mouseout',function(){
		$(".intuitive_entry_field").children(".content").hide();
	});
	
	/* adjust widths of columns based on content, in rational system */
	$(".nav_medium").width($(".nav_medium").width() + 40);
	$(".nav_small").width($(".nav_small").width() + 25);
	$(".nav_tiny").width($(".nav_tiny").width() + 20);
	
	var awidth = $(".nav_medium").width() + $(".nav_small").width() + $(".nav_tiny").width();
	var width = $(".nav_large").width();
	
	$(".nav_large").css({
		"left" : awidth
	});
	$("#rational_content").css({
		"left" : awidth
	});
	/* end that */
	
	$(".index").live('mouseover',function(){
		$(this).css(
			'z-index','5000'
		);
		$(this).children(".index_entry").css({
			"border-top" : "1px solid #DDD",
			"border-bottom" : "1px solid #DDD",
			"color" : "#888"
		});
		$(this).children(".index_entry").children(".index_entry_title").css(
			"color" , "#000"
		);
	}).live('mouseout',function(){
		$(this).css(
			'z-index',''
		);
		$(this).children(".index_entry").css({
			"border-top":"1px solid #fffffa",
			"border-bottom":"1px solid #fffffa",
			"color" : "#CCC"
		});
		$(this).children(".index_entry").children(".index_entry_title").css(
			"color" , "#888"
		);
	});
	
	$(".index_label").live('mouseover',function(){
		$(this).css(
			"color" , "#000"
		);
	}).live('mouseout',function(){
		$(this).css(
			"color" , ""
		);
	});
	
	
	$(".intuitive_entry_image").live('mouseover',function(){
		$(this).css(
			'z-index','5000'
		);
	}).live('mouseout',function(){
		$(this).css(
			'z-index',''
		);
	});
	
	$("embed").mouseenter( function(){
		$(this).parent().parent().css({
			'z-index':'5000'
		});
		$(this).parent().css({
			'z-index':'5001'
		});
		$(this).css({
			'z-index':'5002'
		});
	}).mouseleave( function(){
		$("embed").css({
			'z-index':''
		});
		$("embed").parent().css({
			'z-index':''
		});
		$("embed").parent().parent().css({
			'z-index':''
		});
	});
	
});