Global = {
    clickedRef: '',
    init: function() {
       Global.cufonReplace();
        
		Global.emptyDefaultFormValue();
		Global.menuHover();

        Global.wizardBtnHover();
        
        /* set session cookie if no cookie exists:  */
        if (Global.getCookie("disableWizard") == undefined) Global.disableWizard({checked: false});
		
    },
    
    getCookie : function (name) {
	    var theCookies = document.cookie.split(/[; ]+/);
	    for (var i = 0 ; i < theCookies.length; i++) {
	        var aName = theCookies[i].substring(0,theCookies[i].indexOf('='));
	        if (aName == name) {
	            return theCookies[i];
	        }
	    }
	},

    /* flashreplacement_homepage: */
    homeNoneFlash: function() {
		
		$('img.img1').attr('src', $('.alter-images input[name=img1]').val());
		$('img.img2').attr('src', $('.alter-images input[name=img2]').val());
		$('img.img3').attr('src', $('.alter-images input[name=img3]').val());
		$('img.img4').attr('src', $('.alter-images input[name=img4]').val());
		$('img.subimg1').attr('src', $('.alter-sub-page input[name=subimg1]').val());
		$('img.subimg2').attr('src', $('.alter-sub-page input[name=subimg2]').val());
		$('img.subimg3').attr('src', $('.alter-sub-page input[name=subimg3]').val());

        $('#link-overview .alter-images span').click( function () {
            var showId = '#'+$(this).attr('class');
            $('#link-overview').hide();
            $(showId).show();
            Global.homeNoneFlashBackClick($(showId));
        });
    },
    
    homeNoneFlashBackClick: function(el) {
        $(el).find('.alter-back').click( function (){
            $('.alter-back').parent().hide();
            $('#link-overview').show();
            $('.alter-back').unbind('click');

        });
        
    },
    
    switchTab: function(sIdentifier, toId) {
    	var jqeOldTab		= $('#'+sIdentifier+'-tabpanel-tabs .tabpanel-tab-active');
    	var jqeNewTab		= $('#'+sIdentifier+'-tabpanel-tab-'+toId);
    	
    	if (jqeOldTab.attr('id') == jqeNewTab.attr('id')) return;
    	
    	var jqeOldContent	= $('#'+sIdentifier+'-tabpanel .tabpanel-content-active');
    	var jqeNewContent	= $('#'+sIdentifier+'-tabpanel-content-'+toId);
    	
    	$('#'+sIdentifier+'-tabpanel .tabpanel-content').stop(true,true);
    	
    	jqeNewTab.addClass('tabpanel-tab-active').css({cursor:'default'});
    	jqeOldTab.removeClass('tabpanel-tab-active').css({cursor:'pointer'});
    	
		/* turn animation off in IE because opacity animation renders ugly text */
    	if ($.browser.msie /*&&& $.browser.version == '6.0'*/) {
    		jqeOldContent.removeClass('tabpanel-content-active');
    		jqeNewContent.addClass('tabpanel-content-active');
    	} else {
	    	jqeOldContent.css({opacity:1,display:'block'});
	    	$('#'+sIdentifier+'-tabpanel .tabpanel-content').not(jqeNewContent).animate({opacity:0},{duration:300,complete:function(){ $(this).removeClass('tabpanel-content-active'); }});
	    	jqeNewContent.css({opacity:0,display:'block'}).animate({opacity:1},{duration:300,complete:function(){ $(this).addClass('tabpanel-content-active'); }});
    	}
    },
    
    closeWizard : function () {
    	$('#wizard-overlay').remove();
    	$('#wizard-modal').animate({width: 0, height: 0, top: '100px', left : $(window).width()/2 + 450 + 'px'}, {duration:500,easing:'easeOutQuad'});
    	$(window).unbind('resize');
    },

    openWizard: function() {
    	if ($.browser.msie && $.browser.version == '6.0') {
	        $('#wizard-modal').modal({
	            closeClass:"close-link",
	        	overlayClose:true
	        });

    	} else {
    		Global.topAdd = -30;
    		if ($.browser.msie == true) Global.topAdd = -180;

	    	$('<div>')
		    	.attr('id', 'wizard-overlay')
		    	.css('position', 'absolute')
		    	.css('top', 0)
		    	.css('left', 0)
		    	.css('height', '100%')
		    	.css('width', '100%')
		    	.css('z-index', 21)
		    	.prependTo('body').click(function () { 
		    		Global.closeWizard();
		    	});

	        $('#wizard-modal')
		        .css('z-index', '22')
		        .css('width', '0')
		        .css('height', '0')
		        .css('position', 'absolute')
		        .css('top', '100px')
		        .css('display', 'block')
		        .css('left', $(window).width()/2 + 450 + 'px');
	        
	        $('#wizard-modal').animate({
	        	width: '488px', 
	        	height: '562px', 
	        	top: '160px',
	        	left : $(window).width()/2 - 220 + 'px'}, {duration:500,easing:'easeOutQuad'});
	        
	        $(window).bind('resize', function () {
	        	$('#wizard-modal').css('left', $(window).width()/2 - 220 + 'px');
	        });

    	}
  	
    },
    
    wizardBtnHover: function() {

        $('#wizard-btn').bind('mouseenter',function(){
            $(this).addClass('wizard-btn-hover');
            $('#wizard-tooltip span').addClass('show-tooltip');
        }).bind('mouseleave',function(){
            $(this).removeClass('wizard-btn-hover');
            $('#wizard-tooltip span').removeClass('show-tooltip');
        });
        
    },
    
    wizardChoice: 0,
    
    wizardStep: function(iChoice) {
    	iNewPos = 0 - (iChoice * 400);
    	$('#wizard-steps-inner').animate({left:iNewPos},{duration:300});
    },
    
    disableWizard: function(elmCaller) {
    	var bDisable	= elmCaller.checked;
    	var dExpire		= new Date();
    	if (bDisable) {
    		dExpire.setDate(dExpire.getDate() + 365);
    		document.cookie = 'disableWizard=true;expires='+dExpire.toGMTString()+';path='+cmsGlobal.baseUri;
    	} else {
    		document.cookie = 'disableWizard=session;path='+cmsGlobal.baseUri;
    	}
    },
    
    referenceAccordeon: function() {
        $('.reference-items .block').each(function() {
            if ($(this).hasClass('active-ref')) {
                // do nothing
            } else {
                $(this).find('.reference-wrapper').hide();
            }
                
        });
        
    },
    
    downloadSlideOut:function() {
        $('.download-block .toggle-downloads').toggle(function() {
            $(this).parent().find('ul ul').slideDown('normal');
            $(this).find('.toggle-downloads-text').html('Minder');
        }, function() {
            $(this).parent().find('ul ul').slideUp('normal');
            $(this).find('.toggle-downloads-text').html('Meer');
        }
        );

        
    },
    
    cufonReplace: function() {
        Cufon.replace('.topphone-wrapper, .block h5, .block h6, .main-title-light, .download-block h2, .product-block h2 ', { fontFamily: 'futuralight' });
        Cufon.replace('.general-tabs ul li, .main-title, .webshop h5, .contact-block h5, .title-highlight, .btn-download, .contact-title', { fontFamily: 'futuramedium' });
        Cufon.replace('.general-tabs ul li span.item-fullname', { fontFamily: 'futuraheavy' });
        if($('.newsheader').length > 0) {
			Cufon.replace('.newsheader', { fontFamily: 'futuralight' });
		}
		if($('.headHome').length > 0) {
		        Cufon.replace('.headHome ', { fontFamily: 'futuralight' });

			Cufon.replace('.headHome .title-highlight', { fontFamily: 'futuramedium' });

		}
        
    },
    
    emptyDefaultFormValue: function() {
        $('.topsearch').focus(function(){
            if ($(this).val() == 'Zoeken' ) { 
                $(this).val(''); 
            }
        }).blur(function(){
            if ($(this).val() == '' ) { 
                $(this).val('Zoeken');
                $(this).addClass('default-value');
            }
        });
        
        $('.extranet .username-text').focus(function(){
            if ($(this).val() == 'Gebruikersnaam' ) { 
                $(this).val(''); 
            }
        }).blur(function(){
            if ($(this).val() == '' ) { 
                $(this).val('Gebruikersnaam');
                $(this).addClass('default-value');
            }
        });
        $('.extranet .password-text').focus(function(){
            $(this).hide();
            $('.extranet .input-password').show();
            $('.extranet .input-password').focus();
            
        });
		
		$('.newsletter .input-text').focus(function(){
            if ($(this).val() == 'Uw e-mail adres' ) { 
                $(this).val(''); 
            }
        }).blur(function(){
            if ($(this).val() == '' ) { 
                $(this).val('Uw e-mail adres');
                $(this).addClass('default-value');
            }
        });
        
        
    },
    
    menuHover: function() {
        $('.menu-wrapper ul li a').bind('mouseenter',function(){
            $(this).addClass('hover');
        }).bind('mouseleave',function(){
            $(this).removeClass('hover');
        });        
    },
    
    showLargeImage: function(el) {
        var imgSrc = $(el).find('input').attr('value');
        
        var sectionClass = $('#main-wrapper').attr('class');
        
        var modalHtml  = '<div class="large-img-modal '+ sectionClass +'">';
            modalHtml += '<div class="large-img-modal-content" >';
            modalHtml += '<div class="close-link"><a href="#" title="Sluiten"><span class="icon">&nbsp;</span></a></div>';
            modalHtml += '<img src="'+ imgSrc +'" />';
            modalHtml += '</div></div>';
            
	    $('<div>')
	    	.attr('id', 'wizard-overlay')
	    	.css('position', 'absolute')
	    	.css('top', 0)
	    	.css('left', 0)
	    	.css('height', '100%')
	    	.css('width', '100%')
	    	.css('z-index', '3000')
	    	.prependTo('body').click(function () { 
	    		$('#wizard-overlay').remove();
	    		Global.modalWindow.close();
	    });

        Global.modalWindow = $.modal(modalHtml,{
            closeClass:'close-link',
        	containerId: 'simplemodal-container-img' 
        });

        $('#simplemodal-container-img').click(function(){
        	$('#wizard-overlay').remove();
        	Global.modalWindow.close();
        });

        if ($.browser.msie && $.browser.version == '6.0') {
	        $('.large-img-modal').css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=http://192.168.2.200/_users/maarten/svncheckouts/zetacom.nl/public_html/website/img/img-large-bg.png, sizingMethod=\'noscale\')');
	        $('.large-img-modal').css('background', 'none');
        }
    }
}

jQuery.fn.sort = function() {  
	return this.pushStack( [].sort.apply( this, arguments ), []);  
};  
   
Download = {
	// Variables:
	iRowsPerPage : 0,
	iTotalDownloads : 0,
	iCurrentPageIndex : 0,
	iDownloadItemsHeight : 0,
	iNumPages : 0,
	bLocked : false,
	sSortBy : '',
	
	init : function () {
		Download.sortList('name');
	},
	
	sortDesc : function (a, b){  
		 return $(a).find('input[name="'+Download.sSortBy+'"]').val() < $(b).find('input[name="'+Download.sSortBy+'"]').val() ? 1 : -1;  
	},
	  
	sortAsc : function (a, b){  
		return $(a).find('input[name="'+Download.sSortBy+'"]').val() > $(b).find('input[name="'+Download.sSortBy+'"]').val() ? 1 : -1; 
	},
	
	sortList : function (sortBy) {
		// Lock sorting and pagination buttons during sorting.
		if (Download.bLocked) return;
		Download.bLocked = true;
		
		// Determine sort order, reverse for next time
		Download.sSortBy = sortBy;
		
		mySort = (Download.sortOrder == sortBy + '-desc') ? Download.sortDesc : Download.sortAsc;
		Download.sortOrder = (Download.sortOrder == sortBy + '-desc') ? sortBy +  '-asc' : sortBy +  '-desc';

		// set active sort label
		$('.sort-label-active').removeClass('sort-label-active');
		$('.sort-label-' + sortBy).addClass('sort-label-active');
		
		// Sort list of downloads.
		$('.download-store div.block').sort(mySort).appendTo('.download-store'); 
		
		// Renumber downloads
		Download.reNumber = 0;
		$('.download-store div.block').each(function () {
			$(this).attr('class', 'block download-item-' + Download.reNumber++);
		});
		
		// Re-show visible downloads
		Download.setCurrentPage('refresh');
	},
	
	setCurrentPage : function (iPageIndex) {
		// Do nothing if current button is clicked.
		if (iPageIndex == Download.iCurrentPageIndex) return;
		
		// Return if object is locked, but continue if this is a call from the sort function.
		if (Download.bLocked && iPageIndex != 'refresh') return;

		// Handle forward/backward buttons, set direction.
		if (iPageIndex == '+') {
			if (Download.iCurrentPageIndex == Download.iNumPages - 1) return;
			iPageIndex = Download.iCurrentPageIndex + 1;
			Download.direction = 'next';
		} else if (iPageIndex == '-') {
			if (Download.iCurrentPageIndex == 0) return;
			iPageIndex = Download.iCurrentPageIndex - 1;
			Download.direction = 'prev';
		} else if (iPageIndex == 'refresh') {
			iPageIndex = 0;
			Download.direction = 'refresh';
		} else if (iPageIndex > Download.iCurrentPageIndex) {
			Download.direction = 'next';
		} else {
			Download.direction = 'prev';
		}

		Download.bLocked = true;			
		
		// Hide prev/next buttons that we cannot use.
		var buttons = $('.info-downloads .pagination .btn-prev, .info-downloads .pagination .btn-next');
		buttons.removeClass('inactive');
		if (iPageIndex == 0) $('.info-downloads .pagination .btn-prev').addClass('inactive');
		if (iPageIndex == Download.iNumPages - 1) $('.info-downloads .pagination .btn-next').addClass('inactive');
		
		// Show active class.
		$('.info-downloads .pagination .download-page-selector-' + Download.iCurrentPageIndex).removeClass('active');
		$('.info-downloads .pagination .download-page-selector-' + iPageIndex).addClass('active');
		
		// Prepare DOM and set parameters for animation.
		if (Download.direction == 'refresh') {
			// Initialize DOM for this animation
			$('<div class="download-display-new">').css('height', 0).css('width', '580px').prependTo('div.download-display-container');
			$('div.download-display-current').css('float', 'none');
			
			// Set parameters for animation
			animateParams = {height: Download.iDownloadItemsHeight + 'px'};
			animateTarget = '.download-display-new';
		} else if (Download.direction == 'next') {	
			// Initialize DOM for this animation
			$('<div class="download-display-new">').css('height', Download.iDownloadItemsHeight + 'px').css('width', '580px')
			.css('float', 'left').appendTo('div.download-display-container');
			$('div.download-display-current').css('float', 'left');
			
			// Set parameters for animation
			animateParams = {width: 0};
			animateTarget = '.download-display-current';
		} else if (Download.direction == 'prev') {	
			// Initialize DOM for this animation
			$('<div class="download-display-new">').css('height', Download.iDownloadItemsHeight + 'px').css('width', 0)
			.css('float', 'left').prependTo('div.download-display-container');
			$('div.download-display-current').css('float', 'left');
			
			// Set parameters for animation
			animateParams = {width: '580px'};
			animateTarget = '.download-display-new';
		}	
		
		// Show new downloads, and remove old ones after animation is finished.
		for (i = 0; i < Download.iRowsPerPage; i++) {
			$('.download-store .download-item-' + (i + iPageIndex*Download.iRowsPerPage))
			.clone() // Clone the download that is to be added to the visible downloads.
			.appendTo('div.download-display-container div.download-display-new'); // Add element to visible downloads.
		}
		
		$(animateTarget).animate(animateParams, 500, 'linear' , function () {
			$('.download-display-current').remove();
			$('div.download-display-container div.download-display-new').addClass('download-display-current').removeClass('download-display-new');
			Download.bLocked = false;
		});

		// Update curret page index
		Download.iCurrentPageIndex = iPageIndex;
	}
	
}

Training = {
		
	selectItem: function (elt) {
		$(elt).toggleClass('training-item-active');
	},
	
	requestInfo: function (iTrainingId) {
		
		var sText = 'Stuur mij s.v.p. meer informatie over de volgende training:'+"\n";
		
		$form = (typeof(iTrainingId) == 'undefined')? $('#form_training') : $('#form_training_' +iTrainingId);
		
		$elmTrainingRows = $('.training-row', $form);
		if($elmTrainingRows.length) {
			var elmSelected = $('.training-row input:radio[checked=true]', $form);
			
			if (elmSelected.length == 0) {
				alert('Selecteer a.u.b. een cursusdag');
				return;
			}

			var iDateId = elmSelected.val();
			var iTrainingId = $('#date-trainingid-'+iDateId, $form).val();
			
			sText += $('#training-name-'+iTrainingId, $form).val();
			sText += "\n\n"+'Datum: '+$('#training-date-'+iDateId, $form).text();
		} else {
			sText += 'Training "'+$('#training-name-'+iTrainingId, $form).val()+'"';
		}
		
		$('#contactform_info', $form).val(sText);
		
		$form.submit();
	}

}

trackExitLink = function (anchor) {
	var sDest = ((anchor.href.indexOf(location.hostname) == -1) && (anchor.href.indexOf('http') != -1 )) ? 'extern' : 'intern';
	if (typeof(_gaq) != 'undefined') _gaq.push(['_trackPageview','/sidetrack/' +  sDest + '/' + anchor.href]);
}


// imageSliderContiniuos
var imageSliderContinious = {

	init: function(){
		if ($('#bar').length > 0) {
			$('#bar').plagScroller({ scrollChildren:'.img-logo-bg', top:0 });
		}
		if ($('#foot').length > 0) {
			$('#foot').plagScroller({ scrollChildren:'.footer-img-wrapper', top:0 });
		}

	}
	
}


// Run any/all init functions.
$(document).ready(function() {
    Global.init();
    Download.init();
	
});
