/*
#############################################
	Client: EMBRAER
	Project: E FOR EFFICIENCY
	AGENCIA GINGA
#############################################
*/

/* PRELOAD IMAGES */
jQuery.preloadImages = function(){
	for(var i = 0; i<arguments.length; i++){
    	jQuery("<img>").attr("src", arguments[i]);
	}
}
$.preloadImages("/images/h3_menu_over.png","/images/bg_divback.gif","/images/arrow_linkback.gif","/images/bg_inpPh1.gif","/images/bg_inpPh2.gif","/images/bg_fkInptPh1.gif","/images/bg_fkInptPh2.gif","/images/h3_menu_over.png");



var assumptions =
{
	initialize: function()
	{
		jQuery(document).ready(function(){
								
		});
	},
	changeDailyUtilization :function(stageLength, annualUtilization)
	{
		jQuery.ajax({
			url: "/assumptions/daily_utilization/" + stageLength + '/' + annualUtilization,
			cache: false,
			onLoading: jQuery('#loading').show(),
			success: function(html){
					jQuery('#daily_utilization').html(html);
					jQuery('#loading').hide();	
		 	}
			});
	},
	change :function(id)
	{
		jQuery('#economic_environment_value').attr('value', id);
		jQuery.ajax({
			url: "/assumptions/change_economic_environment/" + id,
			cache: false,
			onLoading: jQuery('#loading').show(),
			success: function(html){
					jQuery('#assumptions_content').html(html);
					fakeCombos.initialize();
					jQuery('#loading').hide();	
		 	}
		});
	},
	changeStageLength :function(id)
	{
		jQuery('#stage_length_value').attr('value', id);
		assumptions.changeDailyUtilization(jQuery('#stage_length_value').attr('value'), jQuery('#annual_utilization_value').attr('value'));
	},
	changeCurrency :function(currency)
	{
		jQuery('#currency_value').attr('value', currency);
	},
	changeAnnualUtilization :function(id)
	{
		jQuery('#annual_utilization_value').attr('value', id);
		assumptions.changeDailyUtilization(jQuery('#stage_length_value').attr('value'), jQuery('#annual_utilization_value').attr('value'));
	},
	changeUnit :function(unit, type)
	{
		jQuery('#' + type + '_value').attr('value', unit);
	}

};


/* FAKE COMBO FUNCTIONS [INPUT + DIV + A] */

	function closeInputs(){
		jQuery.noConflict();
		jQuery(document).ready(function($){
			$('.fkList').hide();
			$('.inpPh2').addClass('inpPh1').removeClass('inpPh2');
			$('.fkInputUnits_Ph2').addClass('fkInputUnits_Ph1').removeClass('fkInputUnits_Ph2');
		});
	}
	function inpToggle(obj){
		input = obj
		list = document.getElementById(obj.id + "_list");
		inputClass = obj.className;
		if(inputClass.match(/.+Ph1$/)){
			closeInputs();
			list.style.display = "";
			input.className = inputClass.replace(/Ph1$/,"Ph2");
		} else {
			list.style.display = "none";
			input.className = inputClass.replace(/Ph2$/,"Ph1");
		}
	}
	function setValue(a){
		inputID = a.parentNode.id.replace(/_list$/,"");
		if(document.getElementById(inputID) == ""){
			document.getElementById(inputID).innerHTML = a.innerHTML;
			inputReal = document.getElementById(inputID + "_real");
			value = a.innerHTML.replace(/<span>(.*?)(\(.*?\))<\/span>/, "$1");
			inputReal.value = value;
			a.parentNode.style.display = "none";
			inputClass = document.getElementById(inputID).className;
			document.getElementById(inputID).className = inputClass.replace(/Ph2$/,"Ph1");
			document.getElementById(inputID).style.color = "#2E2E2A";
		} else{
			input = document.getElementById(inputID);
			inputClass = input.className;
			input.value = a.innerHTML;
			a.parentNode.style.display = "none";
			input.className = inputClass.replace(/Ph2$/,"Ph1");
			input.style.color = "#2E2E2A";
		}
	}


var fakeCombos = 
{
	initialize: function()
	{

		jQuery(".inpPh1").click(function(event){ inpToggle(this); });
		jQuery(".inpPh1").focus(function(event){ this.blur(); });
		jQuery(".fkList a").click(function(event){ setValue(this); });
		jQuery(".fkList a").focus(function(event){ this.blur(); });
		jQuery(".inpType1[@value*=Select]").css("color", "#9C9C95");
		jQuery(".fkInputUnits_Ph1").click(function(event){ inpToggle(this); });
		jQuery(".fkInputUnits_Ph1").focus(function(event){ this.blur(); });
	}
};


jQuery.noConflict();
jQuery(document).ready(function($){
	// setting HREF attribute on links to iE6 show the a:hover style.
	version = parseFloat(navigator.appVersion.split("MSIE")[1]);
	if ((version >= 5.5) && (version < 7) && (document.body.filters)) {
		$(".units").attr("href", "javascript:;");
	}
	$('td').attr("nowrap","nowrap");
	fakeCombos.initialize();	
	/*$('.fkList').bind("mouseleave", function(){
		$(".fkList").css({display:'none'});
		
	});*/

	$(document).bind('click', function(e) {
    	var $clicked=$(e.target); // get the element clicked
		if(!($clicked.is('.fkList') || $clicked.parents().is('.fkList') || $clicked.is('input') || $clicked.is('.fkInputUnits_Ph2') )){
			closeInputs();
        }
	});
});



/* HOTWORDS WINDOW SHADOW */
var gradientshadow={}
gradientshadow.depth=5 //Depth of shadow in pixels
gradientshadow.containers=[]

gradientshadow.create=function(){
	var a = document.all ? document.all : document.getElementsByTagName('*')
	for (var i = 0;i < a.length;i++) {
		if (a[i].className == "hotWindow") {
			for (var x=0; x<gradientshadow.depth; x++){
				var newSd = document.createElement("DIV")
				newSd.className = "shadow_inner"
				newSd.id="hotWindow"+gradientshadow.containers.length+"_"+x //Each shadow DIV has an id of "shadowL_X" (L=index of target element, X=index of shadow (depth) 
				if (a[i].getAttribute("rel"))
					newSd.style.background = a[i].getAttribute("rel")
				else
					newSd.style.background = "black" //default shadow color if none specified
				document.body.appendChild(newSd)
			}
		gradientshadow.containers[gradientshadow.containers.length]=a[i]
		}
	}
	gradientshadow.position()
	window.onresize=function(){
		gradientshadow.position()
	}
}

gradientshadow.position=function(){
	if (gradientshadow.containers.length>0){
		for (var i=0; i<gradientshadow.containers.length; i++){
			for (var x=0; x<gradientshadow.depth; x++){
			var shadowdiv=document.getElementById("hotWindow"+i+"_"+x)
				shadowdiv.style.width = gradientshadow.containers[i].offsetWidth + "px"
				shadowdiv.style.height = gradientshadow.containers[i].offsetHeight + "px"
				shadowdiv.style.left = gradientshadow.containers[i].offsetLeft + x + "px"
				shadowdiv.style.top = gradientshadow.containers[i].offsetTop + x + "px"
			}
		}
	}
}

if (window.addEventListener)
window.addEventListener("load", gradientshadow.create, false)
else if (window.attachEvent)
window.attachEvent("onload", gradientshadow.create)
else if (document.getElementById)
window.onload=gradientshadow.create
