function loadCharts(){
	$('table#voorbeeld').visualize({
		type: 'pie', 
		pieMargin: 10, 
		width: 435,
		height: 150,
		colors: ['#888', '#C6AE3E']
	});	
}

addLoadEvent(loadCharts);

function toggler(){
	$(".surveyResults").click(function () {
	
		$(this).next().slideToggle();
		
		if ( $(this).next().find(".visualize").length == 0 ) {
		
			type_post = $(this).next().find("table").attr("id");
		
			$('table#'+type_post).visualize({ 
				width: 900,
				height: 150,
				colors: ['#880000', '#C6AE3E']
			});
		
		}
		
	});
	
	$("body#statistieken ul li").click(function () {
		$(this).find(".sliderTrigger").slideToggle("300");	
		$(this).toggleClass("focus"); 		
	});
	
}

addLoadEvent(toggler);

$(document).ready(function() {  	
	$('input[type="text"]').focus(function() { $(this).toggleClass("focusField"); } );
	$('input[type="text"]').blur(function() { $(this).toggleClass("focusField"); } );  
	$('textarea').focus(function() { $(this).toggleClass("focusField"); } );
	$('textarea').blur(function() { $(this).toggleClass("focusField"); } ); 
	
}); 

function prepareGallery(){
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("content")) return true;
	var list = document.getElementById("content");
	var item = list.getElementsByTagName("li");
	for ( var i=0; i < item.length; i++ ) {
		if(item[i].id) {
			$("#"+item[i].id).mouseenter( function (){ return showPic(this.id); });	
		}
		
	}
}

function callback(name) {	
	$("#img_"+name).fadeIn('slow');
}

function showPic(name){
	var std_img = document.getElementById('std');
	std_img.style.display = "none";
	
	var holder = document.getElementById('imageHolder');
	var images = holder.getElementsByTagName('img');
	var visible_images = 0;
	for(var i=0; i < images.length;i++) {
		if(images[i].style.display == 'inline') {
			if(images[i].id != 'img_'+name) {
				$("#"+images[i].id).fadeOut('fast',	callback(name) );	
			}
			
			visible_images += 1;
		}
	}

	if(name && visible_images == 0) {
		$("#img_"+name).fadeIn('slow');
	}
}

addLoadEvent(prepareGallery);
addLoadEvent(loadCufon);

function loadCufon(){
	Cufon.replace('h3');  
	Cufon.replace('h2');
	Cufon.replace('.next'); 
	Cufon.replace('ul.accounts li span.title');
	Cufon.replace('#sidebar ul', { hover: true });
	Cufon.replace('#footer');
	Cufon.replace('#budget dl');
	Cufon.replace('#progress .customHeader em');
	
}

/**
 * POPUP
 */
$( function () {
	
	$(".accounts").children("li").children("span.itembar").click( function() {
		
		//check if item is already active
		if ($(this).parent().hasClass("active")) {
			$(this).parent().toggleClass("active");
			$(this).parent().children(".popup").animate({left: "0px", opacity: 0}, 750, function(){ 
				$(this).css("display", "none");
				resetPopup($(this) );
			});
		} else {
			//check if other items are active
			$(".accounts").children("li").each( function(i) {
				$(this).children(".popup").each( function(i){
					if( $(this).parent().hasClass("active") ){
						$(this).parent().toggleClass("active");
						$(this).animate({left: "0px", opacity: 0}, 750, function(){ 
							$(this).css("display", "none");
							// reset values
							resetPopup($(this) );
						});
					}
				});
			});
			//open popup
			$(this).parent().toggleClass("active");
			
			// slide from item, not from top of page.
			var offset = $(this).parent().offset();
			var topDistance = (offset.top - 351);

			$(this).parent().children(".popup").css('top', topDistance + 'px' )
			$(this).parent().children(".popup").toggle().animate({opacity: 1, left: "478", top: topDistance}, 750, "swing", function(){
			
				if ( $(this).parent().find(".visualize").length == 0 ) {
					
					type_post = $(this).parent().find("table").attr("id");
					
					//make some charts
					$('table#'+type_post).visualize({
						type: 'pie', 
						pieMargin: 10, 
						width: 465,
						height: 180,
						colors: ['#888', '#C6AE3E']
					});
				}
			});	
// 			scroll(0,0);
		}	
	});
	
});

function addLoadEvent(func){
	var oldonload = window.onload;
	if(typeof window.onload != 'function'){
		window.onload = func;
	} else {
		window.onload = function(){
			oldonload();
			func();
		}
	}
}

function insertAfter(newElement, targetElement){
	var parent = targetElement.parentNode;
	if(parent.lastChild == targetElement) {
		parent.appenChild(newElement);
	} else {
		parent.insertBefore(newElement, targetElement.nextSibling);
	}
}

/**
 * MENU
 */

function navigateSendForm()
{
	$("li#p1").click(function (){
	$("form").append("<input type='hidden' name='pg' value='1' />");
	$("form").submit();		
	} );
	
	$("li#p2").click(function (){
	$("form").append("<input type='hidden' name='pg' value='2' />");
	$("form").submit();		
	} );	
	
	$("li#p3").click(function (){
	$("form").append("<input type='hidden' name='pg' value='3' />");
	$("form").submit();		
	} );
	
	$("li#last").click(function (){
	$("form").append("<input type='hidden' name='pg' value='4' />");
	$("form").submit();		
	} );		
}

addLoadEvent(navigateSendForm);

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split(',');
	x1 = x[0];
	x2 = x.length > 1 ? ',' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + '.' + '$2');
	}
	return x1 + x2;
}

function rmFormat(nStr)
{
	var noFormat = nStr.replace(/\./g, "");
	return noFormat;
}

function scrolToWarning()
{
	var isWarning = $("#error");
	if(typeof(isWarning) !== 'undefined') 
	{
		var offset = isWarning.offset();
		scroll(0, offset.top);
//		 alert(offset.top);
	}
}

function scrolToWarning()
{
	var isWarning = $("#error");
	if(typeof(isWarning) !== 'undefined') 
	{
		var offset = isWarning.offset();
		window.scroll(0, offset.top);
//		 alert(offset.top);
	}
}

addLoadEvent(scrolToWarning);
