var Site = {
    groupScoreSet : false,
    
    fromCalendar : null,
    tillCalendar : null,    
    
    init : function() {
        $().ready(function(){        	
            Site.formatWeight();
            Site.reportByMail();
            Site.groupScore();
            Site.mailAFriend();
            Site.mailAFriendSended();
            Site.reportByMailSended();
            Site.formatRequiredError();
            Site.formatWeightError();
            Site.formatWeightSumError();
            Site.goMeter();
            Site.toggleGroupScore();
    		Site.setCalendars();
    		Site.criteriaResultsForm();            
        });
    },
    
    openInFrame : function(hash) {        
        var $trigger = $(hash.t);        
        var $modalWindow = $(hash.w);
        var $modalContainer = $('iframe', $modalWindow);        
        var myUrl = $trigger.attr('href');        
        var myTitle = $trigger.attr('title');        
        var newWidth = 0, newHeight = 0, newLeft = 0, newTop = 0;
        $modalContainer.html('').attr('src', myUrl);        
        $('#jqmTitleText').text(myTitle);        
        myUrl = (myUrl.lastIndexOf("#") > -1) ? myUrl.slice(0, myUrl.lastIndexOf("#")) : myUrl;
        var queryString = (myUrl.indexOf("?") > -1) ? myUrl.substr(myUrl.indexOf("?") + 1) : null;        
        if (queryString != null && typeof queryString != 'undefined')
        {
            var queryVarsArray = queryString.split("&");
            for (var i = 0; i < queryVarsArray.length; i++)
            {
                if (unescape(queryVarsArray[i].split("=")[0]) == 'width')
                {
                    var newWidth = queryVarsArray[i].split("=")[1];
                }
                if (escape(unescape(queryVarsArray[i].split("=")[0])) == 'height')
                {
                    var newHeight = queryVarsArray[i].split("=")[1];
                }
                if (escape(unescape(queryVarsArray[i].split("=")[0])) == 'jqmRefresh')
                {
                    // if true, launches a "refresh parent window" order after the modal is closed.

                    hash.refreshAfterClose = queryVarsArray[i].split("=")[1]
                } else
                {

                    hash.refreshAfterClose = false;
                }
            }
            // let's run through all possible values: 90%, nothing or a value in pixel
            if (newHeight != 0)
            {
                if (newHeight.indexOf('%') > -1)
                {

                    newHeight = Math.floor(parseInt($(window).height()) * (parseInt(newHeight) / 100));

                }
                var newTop = Math.floor(parseInt($(window).height() - newHeight) / 2);
            }
            else
            {
                newHeight = $modalWindow.height();
            }
            if (newWidth != 0)
            {
                if (newWidth.indexOf('%') > -1)
                {
                    newWidth = Math.floor(parseInt($(window).width() / 100) * parseInt(newWidth));
                }
                var newLeft = Math.floor(parseInt($(window).width() / 2) - parseInt(newWidth) / 2);

            }
            else
            {
                newWidth = $modalWindow.width();
            }

            // do the animation so that the windows stays on center of screen despite resizing
            $modalWindow.css({
                width: newWidth,
                height: newHeight,
                opacity: 0
            }).jqmShow().animate({
                width: newWidth,
                height: newHeight,
                top: newTop,
                left: newLeft,
                marginLeft: 0,
                opacity: 1
            }, 'slow');
        }
        else
        {
            // don't do animations
            $modalWindow.jqmShow();
        }

    },    
    
    formatWeight : function() {
        if($('#weightInfo').length > 0) {
            $('#weightInfo').jqm();
            $('#weightInfo a').click(function(){
                $('#weightInfo').jqmHide();
            });
            $('.weightTrigger').click(function(){
                $('#weightInfo').jqmShow();
            });
        }
    },    
    
    reportByMail : function() {
        if($('#reportByMail').length > 0) {
            $('#reportByMail').jqm({
                overlay: 70,
                target: '#jqmContent',
                modal: true,            
                trigger: 'a.reportTrigger',
                onShow: Site.openInFrame     
            });
            $('#reportByMail a').click(function(){
                $('#reportByMail').jqmHide();
            });
            $('.reportTrigger').click(function(){
                $('#reportByMail').jqmShow();
            });
        }
    },
    
    groupScore : function() {
        if($('#groupScore').length > 0) {
            $('#groupScore').jqm();
            
            var minValue = 0;
            var maxValue = 270;
            var maxScore = 4;
            var value, destination;
            
            $('#groupScore a').click(function(){
                $('#groupScore').jqmHide();
            });
            $('.groupScoreTrigger').click(function(){
                $('#groupScore').jqmShow();
                
                if(!Site.groupScoreSet) { 
                    $('#groupScore .meterArrow').rotate(minValue);
                    
                    value = parseFloat($('#groupScore .meterValue').html());
                    destination = (maxValue / maxScore) * value;
                    $('#groupScore .meterArrow').rotate(destination);
                    
                    Site.groupScoreSet = true;
                }
            });
        }
    },    
    
    mailAFriend : function() {
        if($('#mailAFriend').length > 0) {
            $('#mailAFriend').jqm();
            $('#mailAFriend a').click(function(){
                $('#mailAFriend').jqmHide();
            });
            $('.mailAFriendTrigger').click(function(){
                $('#mailAFriend').jqmShow();
            });
        }
    },
    
    mailAFriendSended : function() {
        if($('#mailAFriendSended').length > 0) {
            $('#mailAFriendSended').jqm();
            $('#mailAFriendSended a').click(function(){
                $('#mailAFriendSended').jqmHide();
            });
        }
    },
    
    reportByMailSended : function() {
        if($('#reportByMailSended').length > 0) {
            $('#reportByMailSended').jqm();
            $('#reportByMailSended a').click(function(){
                $('#reportByMailSended').jqmHide();
            });
        }
    },       
    
    formatRequiredError : function() {
        if($('#requiredError').length > 0) {
            $('#requiredError').jqm();
            $('#requiredError a').click(function(){
                $('#requiredError').jqmHide();
            });
        }
    },
    
    formatWeightError : function() {
        if($('#weightError').length > 0) {
            $('#weightError').jqm();
            $('#weightError a').click(function(){
                $('#weightError').jqmHide();
            });
        }
    },
    
    formatWeightSumError : function() {
        if($('#weightSumError').length > 0) {
            $('#weightSumError').jqm();
            $('#weightSumError a').click(function(){
                $('#weightSumError').jqmHide();
            });
        }
    },   
        
    goMeter : function() {
        if($('.meterValue').length > 0) {
            var minValue = 0;
            var maxValue = 270;
            var maxScore = 4;
            var value, destination;
            
            $('.resultMeter:not(.popupMeter) .meterArrow').rotate(minValue);
            
            $('.resultMeter:not(.popupMeter) .meterValue').each(function() {
                value = parseFloat($(this).html());
                destination = (maxValue / maxScore) * value;
                $('.meterArrow', $(this).parent().parent()).rotateAnimation(destination);
            });
        }
    },
    
    toggleGroupScore : function() {
        $('#toggleGroupScore').click(function(){
            $(this).hide();
            $('#groupScore').show();
        });
    },
    
    setCalendars : function() {
    	if($('#from_date').length > 0) {
	        var calOptions = {
	            dayNamesMin: dayNamesMin,
	            monthNames : monthNames,
	            firstDay   : 1,
	            showWeek   : false,
	            nextText   : '>',
	            prevText   : '<',
	            altFormat  : 'dd/mm/yy'
	        };
	        Site.fromCalendar = $('#fromDateCalendar')
	            .datepicker(calOptions)
	            .datepicker('option', 'altField', '#fromDate');
	            
	        Site.tillCalendar = $('#tillDateCalendar')
	            .datepicker(calOptions)
	            .datepicker('option', 'altField', '#tillDate');
    	}
    },
    
    getFromDate : function() {
        return new Date(Site.fromCalendar.datepicker('getDate'));
    },
    
    getTillDate : function() {
        return new Date(Site.tillCalendar.datepicker('getDate'));
    },
    
    checkCalendars : function() {
        var today = new Date();
        today.setHours(0, 0, 0, 0);
        
        if(Site.getFromDate() > Site.getTillDate()) {
            alert('van datum is voor tot en met datum');
            return false;
        }       
        
        if(Site.getFromDate()>today) {
        	alert('van datum ligt in de toekomst');
        	return false
        }
        
        if(Site.getTillDate()>today) {
        	alert('tot en met datum ligt in de toekomst');
        	return false
        }        
        
        return true;
    },
    
    criteriaResultsForm : function() {
    	if($('#from_date').length > 0) {
	        $('.dateTimeSelector #fromDateCalendar')
	            .datepicker('option', 'onSelect', function(){
	                $(this).hide();                
	            })
	            .datepicker('option', 'altField', '#from_date');
	        
	        $('.dateTimeSelector #tillDateCalendar')
	            .datepicker('option', 'onSelect', function(){
	                $(this).hide();
	            })
	            .datepicker('option', 'altField', '#till_date');
	        
	        $('#fromDateSelector').click(function(){
	            $('#tillDateCalendar').hide();
	            $('#fromDateCalendar').toggle();
	        });
	        $('#tillDateSelector').click(function(){
	            $('#fromDateCalendar').hide();
	            $('#tillDateCalendar').toggle();
	        });
    	}
    }    
};

Site.init();

function decreaseStep() {
	if(document.getElementById('step') && document.getElementById('formPost')) {
		var step = document.getElementById('step').value;
		step=step-2;
		document.getElementById('step').value = step;
		document.getElementById('back').value = 'true';
	}	
	
	document.forms['surveyFrm'].submit();
}

function bookmark(url, bookmarktitel) {
    if(bookmarktitel == '' || bookmarktitel == undefined) {
        bookmarktitel = document.title;
    }
    var bookmarklink = url;

    if(document.all) {
        //MSIE
        window.external.AddFavorite(bookmarklink, bookmarktitel);
    }
    else {
        // firefox
        window.sidebar.addPanel(bookmarktitel, bookmarklink,"");
    }
}
