var helpWindow = null;
$(document).ready(function(){
	var datalinks = $('.datalink');
	datalinks.each(function(){
		var datalink = $(this);
		var classText = $(this).attr('class');
		var startIndex = classText.indexOf('ref.')+4;
		var reference = classText.charAt(startIndex);
		// alert(reference);
		// Good so far!
		var datasources = $('.datasource');	
		for(var i = 0; i < datasources.length; i++){
			if($(datasources[i]).attr('class').indexOf('ref.'+reference)!=-1){
				// alert('found: '+reference);
				// Good so far!
				var sourceVal = $(datasources[i]).find('input').val();
				sourceVal = parseFloat(sourceVal);
				if(isNaN(sourceVal))
					sourceVal = 0;
				datalink.html(sourceVal);
				$(datasources[i]).change(function(){
					var sourceVal = $(this).find('input').val();
					sourceVal = parseFloat(sourceVal);
					if(isNaN(sourceVal))
						sourceVal = 0;
					datalink.html(sourceVal)+$(this).find('input').text();
					//alert();
				});
				$(datasources[i]).keyup(function(){
					var sourceVal = $(this).find('input').val();
					sourceVal = parseFloat(sourceVal);
					if(isNaN(sourceVal))
						sourceVal = 0;
					datalink.html(sourceVal)+$(this).find('input').text();
					//alert();
				});
			}
		}
		
	});
	var sliders = $('.slider .sliderContent');
	sliders.each(function(){
		var sliderHeader = $(this).parent().find('h2');
		sliderHeader.append($('<img src="'+BASE_URL+'images/famicons/bullet_toggle_plus.png" />'));
		// sliderHeader.wrap($('<a href="#" onclick="animatedcollapse.toggle(\''+$(this).attr('id')+'\');return false;">'));
		// This doesn't work in IE, use adopt instead
		//sliderHeader.wrap($(document.createElement('a')).attr('href','#').click(function(){animatedcollapse.toggle($(this).attr('id'));return false;)});
		var sliderLink = $(document.createElement('a'));
		sliderLink.attr('href','#');
		sliderLink.mouseover(function(){
			alert('a');
		});
		sliderHeader.wrap(sliderLink);
		var sliderId = $(this).attr('id');
		sliderHeader.parent().click(function(){
			animatedcollapse.toggle(sliderId);
			return false;
		});
		animatedcollapse.addDiv($(this).attr('id'), 'fade=1'+($(this).hasClass('startClosed')?',hide=1':''));
	});
	
	animatedcollapse.ontoggle=function($, divobj, state){ //fires each time a DIV is expanded/contracted
		//$: Access to jQuery
		//divobj: DOM reference to DIV being expanded/ collapsed. Use "divobj.id" to get its ID
		//state: "block" or "none", depending on state
		if(state=='block'){
			$(divobj).parent().find('h2 img').attr('src',BASE_URL+'images/famicons/bullet_toggle_minus.png');
		}
		else{
			$(divobj).parent().find('h2 img').attr('src',BASE_URL+'images/famicons/bullet_toggle_plus.png');
		}
	}
	
	animatedcollapse.init()
	
	var helpLinks = $('.helpref a, a.helpref');
	helpLinks.click(function(){
		if(helpWindow==null || helpWindow.closed){
			helpWindow = window.open(this.href,'helpwindow','width=600,height=700,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no');
		}
		else{
			helpWindow.location = this.href;
			helpWindow.focus();
		}
		return false;
	});
	
	/*
	var helpItems = $(".help");
	helpItems.each(function(){
		var helpIcon = $("<img />").attr({
			src : BASE_URL+"images/famicons/help.png",
			helpref : $(this).attr("helpref")
		}).addClass("helpIcon");
		helpIcon.tooltip({
			track: true, 
			delay: 0, 
			showURL: false, 
			bodyHandler : function(){
				var refName = $(this).attr("helpref");
				return $("#help #ref"+refName).html();
			}
		});
		$(this).append(helpIcon);
	});
	*/
});// JavaScript Document
