



SBH = {};

SBH.Main = function(){

	var main_config = {};
	
	return {
		init: function(config){
		  	main_config = config;
		},		
		initAlert: function() {
			if ($('#alert').length > 0) {
				$('#alert').jqm({
			        overlay: 50, 
			        modal: true, 
			        trigger: false
			    });
			}
			
			if ($('#confirm').length > 0) {
			    $('#confirm').jqm({
			        overlay: 50, 
			        modal: true, 
			        trigger: false
			    });
			}
		},
		toggleTitleBar: function(obj, e) {			
			this.showHide(e);

			this.toggleClass(obj, "minus", "plus");
			
			if (gadgets.window) {
		    	    gadgets.window.getViewportDimensions()
			    gadgets.window.adjustHeight();
			}
		},
		showHide: function(e){
			var e = document.getElementById(e);
			
			if (e) {
				if (e.style.display=="none" ) {
					e.style.display="block";
				} else {
					e.style.display="none";
				}
			}
		},			
		show:function(e) {
			var e = document.getElementById(e);
			
			if (e) {
				e.style.display="block";
			}
		},
		hide:function(e) {
			var e = document.getElementById(e);
			if (e) {
				e.style.display="none";
			}
		},					
		toggleClass:function(e,cl1,cl2) {
			if (e.className==cl1) {
				e.className=cl2;
			} else {
				e.className=cl1;
			}
		},
		toggleGadgetColumns: function() {
			
			if ($('body').hasClass('wide')) {
				var proceed = false;
				
				if(shindig.container.gadgetsInColumns([3])) {
					proceed = confirm('Gadgets zullen opnieuw worden gerangschikt als u het aantal kolommen aanpast.' +' Weet u zeker dat u wilt doorgaan?', '', function(proceed) {
						
						if(proceed == 'OK') {
							$('body').removeClass('wide');
							$('#toggleColumnsCheck').attr('checked', false);
							shindig.container.setColumns(3);
						}
					});
				} else {
					$('body').removeClass('wide');
					$('#toggleColumnsCheck').attr('checked', false);
					shindig.container.setColumns(3);
				}			
					
			} else {
				$('body').addClass('wide');
				$('#toggleColumnsCheck').attr('checked', true);
				shindig.container.setColumns(4);
			}			
		},
		redirectAction: function(ri, action){
			var postdata = {
				action: action,
				params: {
					ri: ri
				}
			};			

//			if (new_window != null) {
//				var cmd = (this.current_config.opensocial ? 'parent.' : '') + 'window.open(\'' + this.current_config[gadget_id].web_root + 'login/?origin=' + $.toJSON(postdata) + '\')';
//			}
//			else {
				var cmd = 'window.open(\'' + main_config.web_root + 'login/?origin=' + $.toJSON(postdata) + '\');';
//			}
			eval(cmd);
		},
		verversPagina: function() {
			
			setTimeout(function() {
				window.location = ''+main_config.web_root+'Persoonlijke-pagina/'; //window.location;			
			}, 3000);
		}
	}
}();
function toggleWidgetUI() {
	var d = document.getElementById("footerwrapper");
	if (d) {
		if (d.style.display=="block")  {
			d.style.display="none";
			d = document.getElementById("tab");
			if (d) {
				d.className="tabclosed";
			}
		} else {
			d.style.display="block";
			document.location="#widgetfooter";
			d = document.getElementById("tab");
			
			if (d) {
				d.className="tabopen";
			}
		}
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

$(document).ready(function() {
    //Voeg automatisch een print onclick event toe aan de Print link in het topmenu
    $("#topmenu ul li a[title='Print']").attr('onclick','javascript:print();');
    
    SBH.Main.initAlert();
    
    if ($('#alert').length > 0) {
    
    	var verwerkVraag = function(value, config) {
	        alert('U heeft geklikt op \''+value+'\'', config.title);
	    };
	  
	    $('a.alert').click(function() { 
	        alert('U heeft geen e-mailadres ingevuld!', 'E-mailadres toevoegen', verwerkVraag);
	        return false;
	    });
	  
	    $('a.confirm').click(function() { 
	        confirm('Wilt u deze gegevens opslaan?', 'E-mailadres toevoegen', verwerkVraag);
	        return false;
	    });
    }

    function cookieResize(selector, defaultSize) {

        var lastSize = defaultSize;

        function write(name, value, days) {
            var expires = "";
            if (days) {
                var date = new Date();
                date.setTime(date.getTime() + (days * 86400000));
                expires = "; expires=" + date.toGMTString();
            }
            document.cookie = name + "=" + value + expires + "; path=/";
        }

        function read(name) {
            name += "=";
            var ca = document.cookie.split(';');
            for(var i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) == ' ') c = c.substring(1, c.length);
                if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
            }
            return null;
        }

        function updateSizeFromCookie() {
            $('body').removeClass(lastSize);
            lastSize = read('size') || defaultSize;
            $('body').addClass(lastSize);
        }

        jQuery(selector).click(function() {
            write('size', this.className,365);
            updateSizeFromCookie();
            return false;
        });

        updateSizeFromCookie();
        

//    	$('#confirm').jqm({
//    		overlay: 0, 
//    	    modal: true, 
//    	    trigger: false
//    	});
    }
    cookieResize('#topmenu a.resize', 'medium');
});

function alert(msg, titel) {
  $('#alert')
    .jqmShow()    
    .find('div.jqmTitle h1')
      .html((titel ? titel : ''))
    .end()
    .find('div.jqmContent')
      .html(msg)
    .end()
    .find(':submit:visible')
    .click(function(){
        $('#alert').jqmHide();
    });
};
function confirm(msg, title, callback) {
    $('#confirm')
    .jqmShow()
    .find('p.jqmConfirmMsg')
      .html(msg)     
    .end()
    .find('div.jqmTitle h1')
      .html((title ? title : ''))
    .end()
    .find(':submit:visible')
    .unbind('click')
    .click(function(){
        $('#confirm').jqmHide();
                
        if (typeof callback == 'string') {
            window.location.href = callback;
        } else {
        	if (callback) {
	            callback.call(this, this.value, {
	                msg: msg,
	                title: title   
	            });
        	}
        }
    });
};
