function isEan(string) {     
    var sum = 0;

    if(!string.match(/^\d{13,13}$/)) { return false; }

    for(var i = 0; i < 12; i++) {
        if(i % 2) { sum += parseInt(string.charAt(i), 10) * 3; } else { sum += parseInt(string.charAt(i), 10); }
    }
    
    sum = 10 - (sum % 10);
    if(sum === 10) { sum = 0; }
    if(sum  === parseInt(string.charAt(12), 10)) {  return true; }
    
    return false; 
}

/* Page flip function */

var flipTimer;

function pageWobble() {
		$('#pageflip img').animate({ width: 84, height: 84 }, 500, function () {
			$('#pageflip img').animate({ width: 72, height: 72 }, 500); });
		$('#pageflip .msg_block').animate({ width: 80, height: 77 }, 500, function () {
			$('#pageflip .msg_block').animate({ width: 70, height: 65 }, 500); });				
		
		flipTimer = setTimeout("pageWobble()", 4000);
} 		 

//For test
function c(text) { return console.log(text); }

$(document).ready(function() {

/**********************************************
 * BOTTOM CONTENT FUNCTIONS
 **********************************************/

    var generalFunctions = {
        init: function() {},
        bottomContent: function() {
            /**** Bottom content - float right if under 3 elements ****/
        	if ($('.content-bottom .box-32').length == 2) {
        		$('.content-bottom .box-32:first').css('margin-left', '324px');
        		$('.content-bottom .box-32:last').removeClass('margin-r24');
        	}
        	else 
        		if ($('.content-bottom .box-32').length == 1) {
        			$('.content-bottom .box-32:last').removeClass('margin-r24');
        			$('.content-bottom .box-32:first').css('float', 'right');
        		}
        		else 
        			if ($('.content-bottom .box-32').length > 2) {
        				$('.content-bottom .box-32').each(function(index){
        					if((index+1) % 3 == 0) { 
        						$(this).removeClass('margin-r24').next().css('clear', 'left'); 
        					}
        				});
        			}
		},
		style1: function() {
            /**** bd borders - style1 ****/
            $('.box-32 .style1 .side-shadows').each(function(){
                $(this).find('.bd:first').css('border-top', 'none');
                $(this).find('.bd:last').css('border-bottom', 'none');
            });
        },
        staff: function() {
            /**** Staff margin ****/
            $('.staff-wrapper:nth-child(2n)').removeClass('margin-r24').after('<div class="clear-fix"></div>');
            $('.staff .bd').find('.staff-img:nth-child(2n)').removeClass('margin-r24').after('<div class="clear-fix"></div>');
        },
        linkOverlay: function() {
            /**** Link functionality ****/
            $('.link-overlay').css('cursor', 'pointer').click(function() {
                window.open($(this).find('a').attr('href'), '_self');
                return false;
            });            
        },
        clearInput: function() {
            /**** Clear input fields ****/
            //Clear input
            $('.clear-input').each(function(){
                $(this).focus(function(){
                    if($(this).val() == $(this).attr('title') || $(this).val() == 'indtast din emailadresse') {
                        $(this).attr('value', '');
                    }
                }); 
            });
        },
        focusInput: function() {
            //'Tilmelding' fields style on focus & blur
            $('.input01:not(.single-input)').find('input, textarea').focus(function(){
                if ($(this).is('input')) {
                    $(this).addClass('tilmelding-input-focus');
                    $(this).parent('.input01').addClass('tilmelding-span-focus');
                } else { $(this).parent('.textarea-wrapper').addClass('textarea-focus') }
            }).blur(function(){
                if ($(this).is('input')) {
                    $(this).removeClass('tilmelding-input-focus');
                    $(this).parent('.input01').removeClass('tilmelding-span-focus');
                } else { $(this).parent('.textarea-wrapper').removeClass('textarea-focus'); }
            });
        },
        tipAFriend: function() {
            /**** 'Tip a friend' Fancybox ****/
         /*   $("a#send-email-link").fancybox({
                'centerOnScroll' : 'true',
                'transitionIn' : 'none',
                'transitionOut' : 'none',
                'onClosed' : function(){
                    $('#send-mail-completion').remove();
                    $('#send-email-wrapper').find('form').css('display', 'block');
                    $('#send-email-wrapper').find('.send-input').attr('value', '');
                }
            });*/
            
            //$('#fancybox-custom-close').click(function(){ $.fancybox.close(); });            
            
            /**** 'Tip a friend' ajax functionality ****/
 /*           $('#tip-a-friend-form').submit(function(){
                    $.ajax({
                        type: 'POST',
                        url: "http://" + window.location.host + "/ajax/tip-a-friend.json",
                        data: ({
                            from: $('#send-email-wrapper').find('.email').val(),
                            name: $('#send-email-wrapper').find('.name').val(),
                            to: $('#send-email-wrapper').find('.send-to').val(),
                            msg: $('#send-email-wrapper').find('.msg').val(),
                            url: $('#send-email-wrapper').find('.url').val() 
                        }),
                        success: function(data){
                            if (data.response == 'success') {
                                $('#send-email-wrapper').find('#tip-a-friend-form').css('display', 'none').siblings('h2').after('<p id="send-mail-completion">' + data.messages[0] + '<br/><br/>' + data.messages[1] + '</p>');
                                _gaq.push(['_trackPageview', '/tipenven/sent']);                        
                                return false;
                            }
                            if (data.response == 'error') {
                                var tempMsg = '';
                                for(var i in data.errors) {
                                    tempMsg = tempMsg + data.errors[i] + '\n';
                                }
                                alert(tempMsg);
                                return false;
                            }
                        },
                        dataType: 'json'
                    });
                    return false;
            });       */      
            
        },
        relatedCourses: function() {
            /* Related activities */
            $('.mRelatedCourses').find('li:last a').css('border', 'none');
            $('.mRelatedCourses').find('li').hover(function(){
                $(this).prev().children('a').css('border-color', '#fff');
            }, function(){
                $(this).prev().children('a').css('border-color', '#e4e4e4');
            });
        }
	}
	if($('.content-bottom').length) { generalFunctions.bottomContent(); }
	if($('.box-32 .style1 .side-shadows').length) { generalFunctions.style1(); }
	if($('.staff').length) { generalFunctions.staff(); }
	if($('.link-overlay').length) { generalFunctions.linkOverlay(); }
    if($('.clear-input').length) { generalFunctions.clearInput(); }
    if($('.input01').length) { generalFunctions.focusInput(); }
	//if($('#send-email-link').length) { generalFunctions.tipAFriend(); }
	if($('.mRelatedCourses').length) { generalFunctions.relatedCourses(); }

/********************************************************
 * TAB FUNCTIONS
 ********************************************************/

    var tabFunctions = {
        init: function() {},
        tabsFrontpage: function() {
            /**** Tabs on the frontpage - box-32 ****/
            //Click
            $('.box-32 ul.tabs li').click(function(){
                if($(this).hasClass('active')) {
                } else {
                    var index = $(this).index();
                    
                    $(this).addClass('active');
                    $(this).siblings().removeClass('active');
                    $('.tab-wrapper').find('.tab-content:eq('+index+')').addClass('active').siblings().removeClass('active');
                    $('.tab-wrapper').find('.ft a:eq('+index+')').addClass('active').siblings().removeClass('active');
                }
            });
        },
        tabsCourseClick: function() {
            /**** Tabs on course/activity pages - box-66 ****/
            //When clicking a tab 
            $('.box-66 ul.tabs li').click(function(){
                if($(this).hasClass('active')) {
                } else {
                    var tab = $(this).attr('id');
                    
                    switch (tab) {
                        case 'tab-info': 
                            window.location.hash = '#info'; 
                            $('.tab-info, .tab-program').removeClass('tab-noprint');
                            $('.tab-tilmelding').removeClass('tab-print');
                            $('.tab-rekvirer').removeClass('tab-print');
                            break;
                        case 'tab-program': 
                            window.location.hash = '#program'; 
                            $('.tab-info, .tab-program').removeClass('tab-noprint');
                            $('.tab-tilmelding').removeClass('tab-print');
                            $('.tab-rekvirer').removeClass('tab-print');
                            break;
                        case 'tab-tilmelding': 
                            window.location.hash = '#tilmelding'; 
                            $('#botCheat').val('true'); 
                            $('.tab-info, .tab-program').addClass('tab-noprint');
                            $('.tab-tilmelding').addClass('tab-print');
                            $('.tab-rekvirer').removeClass('tab-print');
                            break;
                        case 'tab-rekvirer': window.location.hash = '#rekvirer'; 
                            $('#botCheat2').val('true'); 
                            $('.tab-info, .tab-program').addClass('tab-noprint');
                            $('.tab-tilmelding').removeClass('tab-print');
                            $('.tab-rekvirer').addClass('tab-print');
                            break;
                        default: window.location.hash = '#info';
                    }
                    
                    if(tab == 'tab-program' && typeof program_url != "undefined") {
                        window.open(program_url, "_self");
                    } else {
                        $(this).addClass('active').siblings().removeClass('active');
                        $('.tab-wrapper').find('.tab-content.'+tab+'').addClass('active').siblings().removeClass('active');
                    }
                }
            });
        },
        tabsCourseLoad: function() {
            //When loading
            if (window.location.hash) {
                var hash = window.location.hash;
                
                switch (hash) {
                    case '#info': 
                        this.loadTab('tab-info'); 
                        $('.tab-info, .tab-program').removeClass('tab-noprint');
                        $('.tab-tilmelding').removeClass('tab-print');
                        $('.tab-rekvirer').removeClass('tab-print');
                        break;
                    case '#program': 
                        this.loadTab('tab-program'); 
                        $('.tab-info, .tab-program').removeClass('tab-noprint');
                        $('.tab-tilmelding').removeClass('tab-print');
                        $('.tab-rekvirer').removeClass('tab-print');                
                        break;
                    case '#tilmelding': 
                        $('#botCheat').val('true'); 
                        this.loadTab('tab-tilmelding'); 
                        $('.tab-info, .tab-program').addClass('tab-noprint');
                        $('.tab-tilmelding').addClass('tab-print');
                        $('.tab-rekvirer').removeClass('tab-print');                
                        break; 
                    case '#rekvirer': $('#botCheat2').val('true'); 
                        this.loadTab('tab-rekvirer'); 
                        $('.tab-info, .tab-program').addClass('tab-noprint');
                        $('.tab-tilmelding').removeClass('tab-print');
                        $('.tab-rekvirer').addClass('tab-print');               
                        break;
                    default: this.loadTab('info');
                }
            }
        },
        loadTab: function(tab) {
            $('ul.tabs li#'+tab+'').addClass('active'); 
            $('ul.tabs li#'+tab+'').siblings().removeClass('active'); 
            $('.tab-wrapper').find('.tab-content.'+ tab +'').addClass('active').siblings().removeClass('active');
        
            //Variable for cheating bots
            $('#botCheat').val('true');
        }        
    } //Tabfunctions END
	
	if($('.box-32 .tab-wrapper').length) { tabFunctions.tabsFrontpage(); };
	if($('.node-course .tab-wrapper').length) { tabFunctions.tabsCourseClick(); tabFunctions.tabsCourseLoad(); };
	

/**************************************** 
 * TILMELDING OG VALIDATIONS FUNCTIONS
*****************************************/	
	
	var signupFunctions = {
	    init: function() {},
	    signupFunctions: function() {
            /**** 'Tilmelding' validation & submit ****/

            // If a user has signed up, then show 'thank you' page 
            if(signup == true) { //The variable 'signup' is set inline
                $('ul.tabs li#tab-tilmelding').addClass('active').siblings().removeClass('active'); 
                $('.tab-wrapper').find('.tab-content.tab-tilmelding').addClass('active').siblings().removeClass('active');          
                $('#step02').show().siblings('div').hide();
                $('.tilmelding ul').find('.step02').addClass('active').siblings('li').removeClass('active');
    
                _gaq.push(['_trackPageview', track]);
            }
            
            // Submit form
            $('#course-signup-form').submit(function(){
                var nextStep = true;
                
                $('#step01').find('.signup-input:visible').each(function() {
                    var $this = $(this),
                        val = $this.val();
                    if (val == '' && $this.hasClass('mandatory')) {
                        nextStep = false;
                        $this.parent().siblings('.error-text').css('display', 'block');
                    } else if($this.hasClass('cprvalidate') && !val.match(/^\d{10,10}$/)) {
                        nextStep = false;
                        $this.parent().siblings('.error-text').css('display', 'block');
                    } else if($this.hasClass('eanvalidate') && !isEan(val)) {
                        nextStep = false;
                        $this.parent().siblings('.error-text').css('display', 'block');
                    } else {
                        $this.parent().siblings('.error-text').css('display', 'none');
                    }
                
                });
                
                // 'Tilmelding' with an 'accept' checkbox
                if ($('.accept-checkbox').length != 0) {
                    if ($('.accept-checkbox').is(':checked')) {
                        $('.accept-checkbox').siblings('.error-text').css('display', 'none');
                    }
                    else {
                        nextStep = false;
                        $('.accept-checkbox').siblings('.error-text').css('display', 'block');
                    }
                }
    
                if($('#botCheat').attr('value') == 'true') {
                //Submit if true
                if (nextStep) { return true; }
          }
    
                window.scrollTo(0, $('#step01 .error-text:visible').first().offset().top);
                
                return false;
            });
            
            // 'Tilmelding' visual form control
            var inputFieldsId = Array();
            var inputFieldsClass = Array();
            var className;
    
            $('#course-signup-form fieldset #step01').children('div:not(.steps-content):not(.question-help)').each(function(index) {  
                inputFieldsId[index] = $(this).attr('id'); 
                
                if($(this).attr('class') && !$(this).hasClass('mFBLike')) {
                    var tempClass = $(this).attr('class');
                    inputFieldsClass[index] = $(this).attr('class');
                    $(this).hide();
                    
                    $('#'+tempClass).find('input').blur(function(){
                        if($.inArray($(this).val(), window[tempClass+'Arr']) != -1) { 
                            $('.'+tempClass).css('display', 'block'); 
                        } else { 
                            $('.'+tempClass).css('display', 'none'); 
                        }
                    });
                    $(this).addClass('questionWrap');
                } else {                 
                    inputFieldsClass[index] = 'false';
                    $(this).addClass('questionWrap');
                }
            });
            
            // Help popup for questions
            $('.question-help').hover(function(){ 
			$('.primary-dropdown').find('ul > li > ul:first').css('display', 'none');
			$('.primary-dropdown').find('ul li:first').removeClass('active');
			$('.primary-dropdown').find('li.bottom').css('display', 'none');
			$('.primary-dropdown').find('ul li:first ul:first').css({
					'overflow' : 'hidden',
					'height' : 'auto'
			});
			$(this).css('z-index', '55').parent().css('z-index', '2').siblings('.questionWrap').css('z-index', '1');
                $(this).children('div').css('display', 'block'); 
            }, function(){ 
                $(this).css('z-index', '54').parent().css('z-index', '1');
                $(this).children('div').css('display', 'none'); 
            });
	    },
	    /**** Newsletter un/subscription & Rekvirer validation & submit****/
	    newsletterContactFunctions: function() {
            $('#contactform-form, #newsletter-signup-form, #contactform-addon-form').submit(function(){
                $('#newsletter-email-error').css('display', 'none');
                $('#newsletter-name-error').css('display', 'none');
                $('#newsletter-muncipality-error').css('display', 'none');
                $('#newsletter-department-error').css('display', 'none');
                var formValidate = true;    
                var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
                if (!emailReg.test($('#newsletter-email-input').val()) || $('#newsletter-email-input').val() == '') {
                    $('#newsletter-email-error').css('display', 'block');
                    formValidate = false;
                }
                if ($('#newsletter-name-input').val() == '') {
                    $('#newsletter-name-error').css('display', 'block');
                    formValidate = false;
                }
                if ($('#newsletter-muncipality-input').val() == '') {
                    $('#newsletter-muncipality-error').css('display', 'block');
                    formValidate = false;
                }
                if ($('#newsletter-department-input').val() == '') {
                    $('#newsletter-department-error').css('display', 'block');
                    formValidate = false;
                }
                
                if (formValidate == false) {
                    window.scrollTo(0, $('.error-text:visible').first().offset().top); 
                    
                    return false;
                }
                else {
                    if($('#botCheat2').attr('value') == 'false' && $('#newsletter-signup-form').length == 0) {
                        return false;
                    }
                    
                    if($('#contactform-form').length)
                        _gaq.push(['_trackPageview', '/rekvirer']);
                    
                    return true;
                }
            });	        
	        
            $('#unsubscribe-newsletter-form').submit(function(){
                $('#cancel-newsletter-email-error').css('display', 'none');
                var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
                var formValidate = true;
                if (!emailReg.test($('#cancel-newsletter-email-input').val()) || $('#cancel-newsletter-email-input').val() == '') {
                    $('#cancel-newsletter-email-error').css('display', 'block');
                    formValidate = false;
                }
                if (formValidate == false) {
                    return false;
                }
                else {
                    return true;
                }
            });	        
	        
	        
	    },
       /**** Newsletter un/subscription & Rekvirer validation & submit****/
        rekvirerBtn: function() {
            /**** 'Tilmelding' & 'Rekvirer' buttons ****/
            $('#rekvirer-btn').click(function(){  window.location.href=$(this).attr('href'); window.location.reload(true); });            
        }
	}
    if ($('.tab-tilmelding').length) { signupFunctions.signupFunctions(); }
	if ($('.newsletter-subscribe').length != 0) { signupFunctions.newsletterContactFunctions(); }
	if ($('#rekvirer-btn').length) { signupFunctions.rekvirerBtn(); }


/******************************************
 *  DROPDOWN FUNCTIONS
 ******************************************/

    var dropdownFunctions = {
        init: function() {},
        dropdown: function() {
            
			$('.primary-dropdown').css('display', 'block');
			$('.primary-dropdown-no-js').css('display', 'none');
			
                $('.primary-dropdown').each(function() {
                    var _this = $(this);
                    var ddLen = $(this).find('ul ul li').length;
        
                    $(_this).find('ul li:first').click(function() {
					
						_this.parent().css('z-index', '2').siblings().css('z-index', '1');
					
                        if($(this).hasClass('active')) {
                            $(_this).find('ul li ul:first').css('display', 'none');
                            $(_this).find('ul li:first').removeClass('active');
                            $(_this).find('li.bottom').css('display', 'none');
                            $(this).find('ul:first').css({
                                    'overflow' : 'hidden',
                                    'height' : 'auto'
                                });
                        } else {
                            if( $('.primary-dropdown').not(_this).find('ul li:first').hasClass('active') ) {
                                $('.primary-dropdown').not(_this).find('ul li ul:first').css('display', 'none');
                                $('.primary-dropdown').not(_this).find('ul li:first').removeClass('active');
                                $('.primary-dropdown').not(_this).find('li.bottom').css('display', 'none');
                                $('.primary-dropdown').not(_this).find('ul ul:first').css({
                                        'overflow' : 'hidden',
                                        'height' : 'auto'
                                });             
                            }
                            
                            $(this).addClass('active');
                            $(this).find('ul:first').width($(this).width());
                            $(this).find('ul li:not(.bottom)').width($(this).width() - 4);
                            $(this).find('ul:first').css('display' ,'block');
                            $(this).find('span:first').css('border', 'none');
							
                            var distTop = $(_this).offset().top - $(window).scrollTop();
                            var ulHeight = document.documentElement.clientHeight - distTop - 50;
                            
                            if($(this).find('ul:first').height() > ulHeight) {
                                $(this).find('ul:first').css({
                                    'overflow-x' : 'hidden',
                                    'overflow-y' : 'scroll',
                                    'height' : ulHeight + 'px'
                                });
                            } else {
                                $(this).find('ul:first').css({
                                    'overflow-y' : 'hidden'
                                });
                            }
                            
							if($.browser.msie && $.browser.version == 7) {
								if($(_this).parents().hasClass('steps-content'))
									$(_this).find('li.bottom').css({'display' : 'block', 'bottom' :  -($(this).find('ul:first').height()-3) + 'px'});
								else
									$(_this).find('li.bottom').css({'display' : 'block', 'bottom' :  -($(this).find('ul:first').height()+7) + 'px'});
                            } else {
                                if($(_this).parents().hasClass('steps-content'))
									$(_this).find('li.bottom').css({'display' : 'block', 'bottom' :  -($(this).find('ul:first').height()-3) + 'px'});
								else
									$(_this).find('li.bottom').css({'display' : 'block', 'bottom' :  -($(this).find('ul:first').height()+40) + 'px'});
                            }
                            
                            $(_this).mouseenter( function() {
                                $('body').unbind('click');              
                            }).mouseleave(function(){
                                $('body').bind('click', function(){
                                    $('body').unbind('click');
                                    $(_this).find('ul > li > ul:first').css('display', 'none');
                                    $(_this).find('ul li:first').removeClass('active');
                                    $(_this).find('li.bottom').css('display', 'none');
                                    $(_this).find('ul li:first ul:first').css({
                                            'overflow' : 'hidden',
                                            'height' : 'auto'
                                    });
                                });                                     
                            });
                        }
                    });
        
                    if ($(this).hasClass('no-load')) {
                        $(this).next('.primary-dropdown-no-js').find('option').each(function(){
                            if ($(this).attr('selected')) {
								if($('.workAreaInput').length)
									$('.workAreaInput').val($(this).val());
									
								if($('.where').length)
									$('.where').val($(this).val());
									
								if($('.newsletterInput').length) 
									$('.newsletterInput').val($(this).val());									
									
								if($('.hidden-select').length) 
									_this.next('.hidden-select').val($(this).val());
								
                                $(_this).find(' ul li div:first').html($(this).html());
                            }
                        });
        
                        $(this).find('span a').click(function(){
                            var index02 = $(this).parent('span').parent('li').index();
							var $a = $(this);
							
							if($('.workAreaInput').length)
								$('.workAreaInput').val($a.attr('href'));
								
							if($('.where').length)
								$('.where').val($a.attr('href'));
								
							if($('.newsletterInput').length)
								$('.newsletterInput').val($a.attr('href'));								
							
							if($('.hidden-select').length)
								_this.siblings('.hidden-select').val($a.attr('href'));
							
                            $(_this).find('ul li div:first').html($(this).html());
                            $(_this).next('.primary-dropdown-no-js').find('select option:eq(' + index02 + ')').attr('selected', 'selected').siblings().removeAttr('selected');
							$(_this).find('ul > li > ul:first').css('display', 'none');
                            $(_this).find('ul li:first').removeClass('active');
                            $(_this).find('li.bottom').css('display', 'none');
                            $(_this).find('ul li:first ul:first').css({
                                    'overflow' : 'hidden',
                                    'height' : 'auto'
                            });
							
                            return false;
                        });
                    }
                });

        }
    }
	if ($('.primary-dropdown').length) { dropdownFunctions.dropdown(); }
		

/***************************************************
 * SEARCHPAGE FUNCTIONS
 ***************************************************/

	/*** Searching ***/
	var searchFunctions = {
		init: function() {
		    $this = this;
		    
			if ($.browser.msie && $.browser.version == 7)
			    this.ie7Fix();
			    
			this.searchClick();
		},
		/****  IE7 FIX ****/
		ie7Fix: function() {
			var newVal = $.trim($('.search-input').val());
			var oldVal = $.trim($('#input-value').val());
		
			$('.search-submit').click(function() { 
				newVal = $.trim($('.search-input').val());
				oldVal = $.trim($('#input-value').val());
			
				if(newVal == oldVal)
					return false;
			});
			$('.search-input').keydown(function(event) {
				newVal = $.trim($('.search-input').val());
				oldVal = $.trim($('#input-value').val());

				if(event.keyCode == 13 && newVal == oldVal) 
					return false;
			});
		},
		/**** Search page - 'more results' ****/
		searchClick: function() {
            $('.site-search .consulent .ft a').click(function(){ $this.getMoreResults('consulent', $(this).attr('id'), $(this)); return false; });
            $('.site-search .education .ft a').click(function(){ $this.getMoreResults('courses_axapta', $(this).attr('id'), $(this)); return false; });
            $('.site-search .kommunom .ft a').click(function(){ $this.getMoreResults('courses_planbook', $(this).attr('id'), $(this)); return false; });
            $('.site-search .results .ft a').click(function(){ $this.getMoreResults('textpages', $(this).attr('id'), $(this)); return false; });  		    
		},
        getMoreResults: function(searchType, searchVal, id) {
            $.ajax({
                type: 'POST',
                url: 'http://' + window.location.host + '/sitesearch/getAll/' + searchType + '/' + searchVal,
                success: function(data){
                    var tempSearch = '';
                    
                    for(var i in data.res) {
                        tempSearch = tempSearch + '<h3><a href="'+data.res[i].path+'" title="'+data.res[i].title+'">'+data.res[i].title+'</a></h3><p>'+data.res[i].text+'</p>' 
                    }
                    $(id).parents('.ft').siblings('.bd').append(tempSearch);
                    $(id).css('display', 'none');
                },
                error: function(data) {
                    return false;
                },
                dataType: 'json'
            });
        }		
	}
	if ($('.site-search').length != 0) { searchFunctions.init(); }



/*************************************************
 * FRONTPAGE SLIDESHOW
 *************************************************/	
	
	function galleryFade(sel) {
		var active = 0;
		var change = 1;
		var max = $('.frontpage-slideshow').find('.slideshow-element').length;
		var timer, timer2;

		$(sel + '.slideshow-image::eq(0)').css('z-index', '5');			
		$(sel + '.slideshow-image:not(.slideshow-image:eq(0))').css(
		{
			'opacity': '0',
			'zIndex' : '4',
			'display' : 'none'
		});
		$(sel + '.slideshow-text-content:eq(0)').css({'z-index' : '10', 'display' : 'block'});

		//$(sel + '.slideshow-text-content:eq(0)').css();
		$(sel + '.btn02:eq(0)').css('display', 'inline-block');

		if (max > 1) {
			for(i=0; i < max-1; i++)
				$('.slideshow-nav-radiobuttons ul').append('<li><span></span>');
			
			function timeout() {
				clearTimeout(timer2);
				timer2 = setTimeout(function(){
					interval();
				}, 4000);
			}
			
			function interval(){
				timer = setInterval(function(){
					change = active;
					change++;
					if (change == max) {
						change = 0;
					}
					changeActive();
				}, 6000);
			}
			
			function changeActive(){
				$(sel + '.slideshow-image:eq(' + active + ')').css('zIndex', '4');
				$(sel + '.slideshow-image:eq(' + change + ')').css('zIndex', '5');
				$(sel + '.slideshow-image:eq(' + active + ')').stop().animate({ opacity: 0 }, 500, function() { $(this).css('display', 'none'); });
				$(sel + '.slideshow-text-content:eq(' + active + ')').css({'display' : 'none', 'z-index' : '9'});
				$(sel + '.btn02:eq(' + active + ')').css('display', 'none');
				$(sel + '.slideshow-image:eq(' + change + ')').css('display', 'block').stop().animate({ opacity: 1 }, 1000);
				$(sel + '.slideshow-text-content:eq(' + change + ')').css({'display' : 'block', 'z-index' : '10'});
				$(sel + '.btn02:eq(' + change + ')').css('display', 'inline-block');

				changeRadioBtn();
				active = change;
			};

			$(sel + '.slideshow-nav .next').mousedown(function(){
				clearInterval(timer);
				change = active;
				change++;
				if (change == max) 
					change = 0;
				changeActive();
			});

			$(sel + '.slideshow-nav .prev').mousedown(function(){
				clearInterval(timer);
				change = active;
				if (change == 0) {
					change = max-1;
					changeActive();
				} else {
					change--;
					changeActive();				
				}
			});
			
			$(sel + '.slideshow-nav-radiobuttons li').mousedown(function(){
				if($(this).index() != active) {
					change = $(this).index();
					changeActive();
				}
			});			
			
			$(sel).mouseover(function(){ clearInterval(timer); });
			$(sel).mouseleave(function(){ timeout(); });
			
			function changeRadioBtn() {
				$('.slideshow-nav-radiobuttons li:eq('+ change +')').addClass('active');
				$('.slideshow-nav-radiobuttons li:eq('+ change +')').siblings().removeClass('active');
			}
			
			timeout();
			
		} // End (if  > max)
		
		$(sel).css('display', 'block');
	}	
    if ($('.frontpage-slideshow').length != 0) { galleryFade($('.frontpage-slideshow')); }

/******************************************************
 * GALLERY SLIDER
 ******************************************************/

	function gallerySlider(sel, sliderW) {
		var numBoxes = $(sel).find('li').length;
		var currentPage = 1;
		var maxPages = 1;
		var slideDis;				
					
		if (numBoxes > 3) {
			maxPages = Math.ceil(numBoxes/3);

			//$(sel + '')			
			navVisibility();

			$('.box-slideshow-nav-right').mousedown(function() {
				slideDis = sliderW*currentPage;
				$(sel).find('ul').animate({
					marginLeft: -(sliderW*currentPage)
				}, 750);

				if(currentPage != maxPages)
					currentPage++;
				navVisibility();
			});
			$('.box-slideshow-nav-left').mousedown(function() {
				if(currentPage != 1)
					currentPage--;
				navVisibility();

				$(sel).find('ul').animate({
					marginLeft: -(sliderW*(currentPage-1))
				}, 750);
			});
		}
		
		function navVisibility(){
			if (currentPage == 1) {
				$('.box-slideshow-nav-left').css('display', 'none');
				$('.box-slideshow-nav-right').css('display', 'block');
			}
			else 
				if (currentPage == maxPages) {
					$('.box-slideshow-nav-right').css('display', 'none');
					$('.box-slideshow-nav-left').css('display', 'block');
				}
				else {
					$('.box-slideshow-nav-left').css('display', 'block');
					$('.box-slideshow-nav-right').css('display', 'block');
				}
		}		
		 
	}
    if ($('.box-slideshow').length != 0) { gallerySlider($('.box-slideshow'), 944); }	


/***************************************************************
 * PAGEFLIP
 ***************************************************************/

    var pageFlip = {
        init: function() {
        	/* PAGEFLIP */
        	$("#pageflip").hover(function() { //On hover...
        		clearTimeout(flipTimer);
        		$("#pageflip img, .msg_block").stop()
        			.animate({ 
        				width: '370px',
        				height: '360px'
        			}, 500);
        		$(".msg_block").stop()
        			.animate({ 
        				width: '360px',
        				height: '315px'
        			}, 500);				
        	} , function() {
        		$("#pageflip img").stop() 
        			.animate({
        				width: '72px',
        				height: '72px'
        			}, 220,function() { pageWobble(); });
        			
        		$(".msg_block").stop() 
        			.animate({
        				width: '70px',
        				height: '65px'
        			}, 200); 
        	});
        
        	$('#pageflip img').animate({ width: 84, height: 84 }, 1000, function (){ pageWobble(); }); 
        	$('#pageflip .msg_block').animate({ width: 80, height: 77 }, 1000, function (){ }); 
        }
	}
    if ($('#pageflip').length) { pageFlip.init(); }   
	
});


