

VNLS.addController("IndexController",function(){
    function IndexController(){
        // Models
        
        this.prefsModel = false;
        // Rest of declarations
        this.datatable = false;
        this.editor = false;
        
        return this;
    }
    
    IndexController.prototype = VNLS.getObject("EventEmitter");
    IndexController.prototype.constructor = IndexController;
    
    IndexController.prototype.init = function(){
        // Load the nessery models
        // this.prefsModel = VNLS.getModel("prefs");
       //  this.loadTransportation();
    }
    
    // Set the view.
    IndexController.prototype.initView = function(document){
        
        jQuery("#notify-container").notify();
        
        
        // Init TABS
        $( "#tabs" ).tabs();
        jQuery("#featured").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 4000, true);
        
        jQuery( "#tabs" ).tabs({
            select: function(event, ui) { 
                 if(jQuery(ui.tab).attr("href")==="#tabs-reading"){
                    // jQuery('#ticker1').rssfeed('http://blog.jorr.net/rss',{}, function(e) {
		            // jQuery(e).find('div.rssBody').vTicker({showItems: 1});
                    // });
                    jQuery('#ticker2').rssfeed('https://github.com/jorritd/node-watch/commits/master.atom',{}, function(e) {
		             jQuery(e).find('div.rssBody').vTicker({showItems: 1});
                    });
                 }
            }
        });
        jQuery( "#customers_login" ).button({
            icons: {
                primary: "ui-icon-person"
            },
            text: true     
            }).click(function(){
                window.location = "//my.jorr.net";
            });
     jQuery( "#customers_login" ).css("float", "right");      
     jQuery( "#send_contact" ).button({
            icons: {
                primary: "ui-icon-mail-closed"
            },
            text: true}).click(function(){
                // Get the form serialzed
                // Get a timestamp
                // And post the lot
                jQuery(".validateTips").removeClass( "ui-state-error" ).removeClass("ui-state-highlight").text("");
                var fdata = jQuery("#form_cntct").serializeArray();
                var name = VNLS.trim(fdata[0].value);
                var email = VNLS.trim(fdata[1].value);
                if(name===""){
                // Need an error
                jQuery(".validateTips").addClass( "ui-state-error" ).text("Vul uw naam in!");
                    // console.log("Vul uw naam in");
                    return false;
                };
                if(email===""){
                    // Need an error
                   //  console.log("Vul uw email in");
                   jQuery(".validateTips").addClass( "ui-state-error" ).text("Vul uw emailadres in");
                    return false;
                }
                if(!VNLS.checkEmail(email)){
                    // Need an error
                    jQuery(".validateTips").addClass( "ui-state-error" ).text("Vul een geldig emailadres in");
                    return false;
                };
                var ts = new Date().getTime();
                var doc = jQuery("#form_cntct").serialize();
                //console.log("Zover?");
               jQuery.post("api/contact?_ts="+ts, doc,
                   
                   function(data) {
                       if(typeof(data)=="object"){
                           if(data.status && data.status === "ok"){
                                // We have a winner
                                // console.log(data);
                                 jQuery("#notify-container").notify("create", "notify-template-info", {
                                   title: 'Success',
                                   text: 'We hebben uw contactaanvraag ontvangen'
                                });
                                jQuery(".validateTips").addClass( "ui-state-highlight" ).text("We hebben uw bericht ontvangen.");
                                jQuery("#form_cntct").fadeOut("slow");
                                jQuery("#send_contact").fadeOut("slow");
                                jQuery("input:text").val("");
                                // Set the reset form 
                                // VnlsAppController.resetForm();
                                return;
                           }else{
                               // Er is iets fout gegaan
                               jQuery("#notify-container").notify("create", "notify-template-error", {
                                   title: 'Foutmelding',
                                   text: 'Er heeft een fout plaatsgevonden, welke weet ik niet :) <i>'+data.message+'</i>'
                               });
                           }
                       }
                      
               },"json");
           });
        // 
        this._init_view_ready();
        return this;
    };
    
   
    
    // # -------------- EVENTS -----------------------------
    IndexController.prototype._init_view_ready = function(){
        this.emit("view_ready");
    };
    
    
    return IndexController;
});
