var Vanoordt = Class.create({

    // options
    options : {
        backend: false,
        sDbLanguage: ''
    },

    initialize : function(options) {

        // setup options
        this.options = options;
        this.initLanguage();
        this.setMenuHover();
        this.clearInputs();
        this.initToTop();
        this.initAutoComplete();
        this.initIntranet();
        this.initForms();
        this.initCufonRightCol();
    },

    initCufonRightCol : function () {
        if (!this.options.backend){
            Cufon.replace('#text_right h2', {
                fontFamily: 'Interstate'
            });
            Cufon.replace('#column_1 h1, #column_2 h1, #column_3 h1', {
                fontFamily: 'Interstate'
            });
            Cufon.replace('#column_1 h2, #column_2 h2, #column_3 h2', {
                fontFamily: 'Interstate-normal'
            });
        }	
    },
    
    initCycle : function() {
        var bFirstSlide = true;
        $(function() {
            $('#slider').cycle({
                requeueTimeout: 500,
                timeout: 8000,
                speed : 1500,
                cleartype: 1,
                fx : 'scrollBothWays',
                next : '#next',
                prev : '#prev'
            });
        });

        $(document).bind('keypress', function(e) {
            if (e.keyCode == 37) {
                $('#slider').cycle('prev');
            } else if (e.keyCode == 39) {
                $('#slider').cycle('next');
            }
        });
    },

    initLanguage : function() {
        $('#language a#language-arrow').click(function() {
            if ($('#language-arrow img').attr('src') == 'images/template/language-arrow.png') {
                $('#language').animate({
                    height : '45px'
                }, 300, function() {
                    $('#language-arrow img').attr('src', 'images/template/language-arrow-up.png');
                });
                $('#language a').animate({
                    height : '20px'
                }, 300, function() {
                    //animation complete
                    });
                $('#language a').fadeTo(300, 1);
            } else {
                $('#language').animate({
                    height : '26px'
                }, 300, function() {
                    $('#language-arrow img').attr('src', 'images/template/language-arrow.png');
                });
                $('#language a').animate({
                    height : '20px'
                }, 300, function() {
                    //animation complete
                    $('#language a#' + sDbLanguage).fadeTo(300, 0);
                    $('#language a#' + sDbLanguage).css('display', 'none');
                });
            }
        });
    },
	
    setMenuHover: function () {
        $('header nav > ul#menu > li.has-submenu').hover(
            function(){
                $(this).find('.font-bold').css('color', '#D81F2A');
                Cufon.refresh();
            },
            function(){
                $(this).find('.font-bold').css('color', '#FFF');
                Cufon.refresh();
            }
            );
        $('header nav > ul#menu > li:not(.has-submenu)').hover(
            function(){
                $(this).find('.font-bold').css('color', '#ADB4C1');
                Cufon.refresh();
            },
            function(){
                $(this).find('.font-bold').css('color', '#FFF');
                Cufon.refresh();
            }
            );
            
        $('header nav > ul#menu > li.active').unbind('mouseenter mouseleave');
        
        $('header nav > ul#menu > li.active').hover(
            function(){
                $(this).find('.font-bold').css('color', '#ADB4C1');
                Cufon.refresh();
            },
            function(){
                $(this).find('.font-bold').css('color', '#ADB4C1');
                Cufon.refresh();
            }
            );
        $('header nav > ul#menu > li.active.has-submenu').hover(
            function(){
                $(this).find('.font-bold').css('color', '#D81F2A');
                Cufon.refresh();
            },
            function(){
                $(this).find('.font-bold').css('color', '#ADB4C1');
                Cufon.refresh();
            }
            );
        
        
    },
	
    clearInputs: function () {
        $('.fade').focus(function() {
            $(this).select();
            $(this).next().fadeTo(150, 0);
        });
        $('.fade').blur(function() {
            if($(this).val() == '' ) {
                $(this).next().fadeTo(150, 1);
            }
        });
    },
    
    initToTop: function () {
        $('.totop-darkblue').click(function(){
            $.scrollTo($('#container'), 300);
        });
    },
    
    initAutoComplete: function () {
        $('input#autocomplete').autocomplete('/zoeken/autocomplete', {
            formatItem: function(data) {
                return '<a href="javascript://">'+data[0]+'</a>';
            },
            minChars: 3,
            max: 10
        }).result(function(e,data,formatted){
            location.href = data[1];
            return false;
        });
    },
	
    initArchiveScroller: function () {
    	
        $('#scrollup').click(function () {
            if ($('.archive dl').css('top') != '0px') {
                $('.archive dl').animate({
                    top: '+=' + '172px'
                });
            }
        });
        $('#scrolldown').click(function () {
            var archiveHeight = parseInt($('.archive dl').css('height').replace('px',''));
            var archiveTop = parseInt($('.archive dl').css('top').replace('px','')) - 172;
            if (!((archiveTop - (archiveTop*2)) > archiveHeight)) {
                $('.archive dl').animate({
                    top: '-=' + '172px'
                });
            }
        });
    },
	
    initFilter: function () {
        this.customSelect();
        this.initProductgroepSelect();
        this.initProductSelect();
        this.initVerpakkingSelect();
        var zIndex = 9999;
        $('div.entry').each(function(){
            $(this).css('z-index',zIndex);
            zIndex--;
        });
    },
    
    customSelect: function() {
        //return;
        $('select#productgroep').unbind('change');
        $('select#product').unbind('change');
        $('select#verpakking').unbind('change');
        $("select").each(function(){
		$(this).wrap("<div class='selector'></div>");
		
		var spanText = $(this).attr('title');
		var span = "<span class='select'>" + spanText + "</span>";
		
		$(this).before(span)
		.fadeTo(1, 0)
		.focus(function(){
			$(this).parents(".selector").addClass("focus");
		})
		.change(function(){
			$(this).siblings("span").text($(this).children(":selected").text());
		}).click ( function (){
			$(this).siblings("span").text($(this).children(":selected").text());		
                        $(this).siblings("span").css('color', '#C22F16');
		}) 
		.blur(function(){
			$(this).parents(".selector").removeClass("focus");
		});	
	});
	$('select').trigger("change");
    },
    
    initProductgroepSelect : function () {
        $('select#productgroep').change($.proxy(function(evt){
            var id = $(evt.target).attr('value');
            if (id > 0) {
                $.ajax({
                    url: "assortiment/filter",
                    type: "POST",
                    data: "type=productgroep&id=" + id,
                    success: $.proxy(function(data){
                        var oHtml = jQuery.parseJSON(data);
                        $('#product').parent().html(oHtml.producten);
                        $('#verpakking').parent().html(oHtml.verpakkingen);
                        $('#merk').parent().html(oHtml.merken);
                        this.customSelect();
                        this.initProductgroepSelect();
                        this.initProductSelect();
                        this.initVerpakkingSelect();
                    }, this)
                });
            }
        }, this));
    },
    
    initProductSelect : function () {
        $('select#product').change($.proxy(function(evt){
            var id = $(evt.target).attr('value');
            if (id > 0) {
                $.ajax({
                    url: "assortiment/filter",
                    type: "POST",
                    data: "type=product&id=" + id,
                    success: $.proxy(function(data){
                        var oHtml = jQuery.parseJSON(data);
                        $('#verpakking').parent().html(oHtml.verpakkingen);
                        $('#merk').parent().html(oHtml.merken);
                        this.customSelect();
                        this.initProductSelect();
                        this.initVerpakkingSelect();
                    }, this)
                });
            }
        }, this));
    },
    
    initVerpakkingSelect : function () {
        $('select#verpakking').change($.proxy(function(evt){
            var id = $(evt.target).attr('value');
            if (id > 0) {
                $.ajax({
                    url: "assortiment/filter",
                    type: "POST",
                    data: "type=verpakking&id=" + id,
                    success: $.proxy(function(data){
                        var oHtml = jQuery.parseJSON(data);
                        $('#merk').parent().html(oHtml.merken);
                        this.customSelect();
                        this.initVerpakkingSelect();
                        //t.initMerkenSelect();
                    }, this)
                });
            }
        }, this));
    },
    
    initLogin: function () {
        $('a.forgot-password').click(function () {
            $('#inlogform').css('display', 'none');
            $('#passwordform').css('display', 'block');
        });
        $('a.login').click(function () {
            $('#inlogform').css('display', 'block');
            $('#passwordform').css('display', 'none');
        });
        if (this.options.sDbLanguage == '_en') {
            $('div.submit input').css('background', 'transparent url(http://www.webkey6.nl/klant/vanoordt/images/template/verzenden_en.jpg) no-repeat 0 0');
        }
    },
    
    initContact: function () {
        $('div.select select').selectbox();
        var zIndex = 9999;
        $('div.form div.entry').each(function(){
            $(this).css('z-index',zIndex);
            zIndex--;
        });
        
        $('p.readmore a').click(function () {
            $('contactform').trigger('submit');
        });
    },
    
    validateForm: function (p_sForm, messages) {
        switch(p_sForm) {
            case 'registerform':
                var rules = {};
                rules['sUsername'] = {
                    required: true,
                    notags: true,
                    minlength: 2
                };
                rules['sEmail'] = {
                    required: true,
                    email: true,
                    notags: true
                };
                rules['sVoornaam'] = {
                    required: true,
                    notags: true,
                    minlength: 2
                };
                rules['sTussenvoegsel'] = {
                    notags: true,
                    minlength: 2
                };
                rules['sAchternaam'] = {
                    required: true,
                    notags: true,
                    minlength: 2
                };
                rules['sPassword'] = {
                    required: true,
                    notags: true,
                    minlength: 2
                };
                rules['sPasswordRepeat'] = {
                    required: true,
                    notags: true,
                    minlength: 2
                };
                
                $('#'+p_sForm).validate({
                    rules: rules,
                    messages: messages,
                    errorPlacement: function(error, element) {
                        error.insertAfter(element.parents('.input, .textarea'));
                    //error.insertAfter(element);						
                    },
                    success: function(label) {
                        label.remove();
                    },
                    submitHandler: function(form) {
                        form.submit();
                    }
                });	
                break;
            case 'contactform':
                var rules = {};
                rules['sNaam'] = {
                    required: true,
                    notags: true,
                    minlength: 2
                };
                rules['sEmail'] = {
                    required: true,
                    email: true,
                    notags: true
                };
                rules['sTelefoon'] = {
                    required: true,
                    notags: true
                };
                rules['sAfdeling'] = {
                    required: true,
                    notags: true
                };
                rules['sBericht'] = {
                    required: true,
                    notags: true
                };
			
                $('#'+p_sForm).validate({
                    rules: rules,
                    messages: messages,
                    errorPlacement: function(error, element) {
                        error.insertAfter(element.parents('.input, .textarea'));
                    //error.insertAfter(element);						
                    },
                    success: function(label) {
                        label.remove();
                    },
                    submitHandler: function(form) {
                        form.submit();
                    }
                });	
                break;
        }
    },
    
    initIntranet: function (sDomain) {
        t = this;
        $('#intranet').click(function () {
            Shadowbox.open({
                content:    '<div id="login">'+
                '<section class="login-form">'+
                '<h1 class="font-bold darkblue">Inloggen extranet</h1>'+
                '<p>Vul hieronder uw gebruikersnaam en wachtwoord in en klik op inloggen om toegang te krijgen tot het extranet van Van Oordt.</p>'+
                '<form action="/klant/vanoordt/inloggen" id="inlogform" method="post">'+
                '<div class="input">'+
                '<label for="sName">'+
                '<input type="hidden" name="sForm" value="login">'+
                '<input type="text" class="fade" name="sName" id="sName" value="" />'+
                '<em>Typ hier uw gebruikersnaam...</em>'+
                '</label>'+
                '</div>'+
                '<div class="input">'+
                '<label for="sPassword">'+
                '<input type="password" class="fade" name="sPassword" id="sPassword" value="" />'+
                '<em>Typ hier uw wachtwoord...</em>'+
                '</label>'+
                '</div>'+
                '</form>'+
                '<p class="readmore readmore-inloggen"><a href="javascript:void(0);" onclick="$(\'#inlogform\').submit();" class="login" title="Inloggen">Inloggen</a></p>'+
                '</section>'+
                '</div>',
                player:     "html",
                //displayNav: false,
                height:     290,
                width:      557,
                options: {
                    onFinish: function(){
                        Cufon.refresh();
                        $('.fade').focus(function() {
                            $(this).select();
                            $(this).next().fadeTo(150, 0);
                        });
                        $('.fade').blur(function() {
                            if($(this).val() == '' ) {
                                $(this).next().fadeTo(150, 1);
                            }
                        });
                    }
                } 
            });
        });
    },
    
    initForms: function () {
        var sDbLanguage = this.options.sDbLanguage;
        $('div.form.generator form').each(function () {
            var oForm = new FormHandler({
                form: this, 
                sDbLanguage: sDbLanguage
            });
        });
        $('div.select').each(function () {
            $(this).parent().addClass('selectHolder');
        });
        $('div.checkbox').each(function () {
            $(this).addClass('checkboxHolder');
        });
    }
});
