﻿//got to step public class

(function(){

    //gets elements used through the class/object
    var S1 = $("#new_event_step_1");
    var S2 = $("#new_event_step_2");
    var S3 = $("#new_event_step_3");

    FNG.INV.GoToStep = {
        one : function(){
            //insrt event
            S1.show();
            S2.hide();
            S3.hide();
            
            $(".steps span").removeClass("current");
            $("#step_1 span").addClass("current");
            
             //window.location.hash = "one";
        },
        two : function(){
        
            //inport emails/send message screen
            $(".steps span").removeClass("current");
            $("#step_2 span").addClass("current");
            
            //TODO
            //could store the vals into hash
            
            //'session variables from previous step
            var t = $("#template_id").val();
            var i = $("#event_id").val();
            var g = $("#event_guid").val();
            

            
            if(t == "" || i == "" || g == ""){
                S1.hide();
                FNG.AjaxUtil.Message("<strong>Sesson was lost!</strong> ", "#invite_from_wrp");
            }
            else{
                S1.hide();
                S3.hide();
                S2.show();
            }
            
            //updates hash
            window.location.hash = "two";
        },
        three : function(){
            //login/register screen

            $(".steps span").removeClass("current");
            $("#step_3 span").addClass("current");
        
            S1.hide();
            S2.hide();
            S3.show();
            
            window.location.hash = "three";
        },
        done : function(){
            //thank you screen screen
            $(".steps span").removeClass("current"); 
            $("#step_done span").addClass("current"); 
            
            //hide all
            S1.hide();
            S2.hide();
            S3.hide();
            
            window.location.hash = "done";
        }
    }
}());

$(document).ready(function(){
    
    //local settings
    var settings = {
        texts : {
            onBeforeLeaveMsg : "Ztratis session!",
            wrongHash : "je spatna hash!"
        }
    };
    
    //populate template
     var pt = function(p){
        var l = "/ajax/getTemplatePreview/" + p + "";
        //loads template
        $.getJSON(l, function(d){
            $("#prev_temp_wrp").html(d.data.tHtml);
        });
    }
    
    var hasSession = function(){
        var l = window.location.hash;
        
        //goes to step one on default
        if(!l){
            var l = "#one";
        }
        
        //removes #
        var h = FNG.Util.String.splitHashLink(l);
        
        if(typeof h == "string"){
        
            //shorcut for go to step
            var s = FNG.INV.GoToStep[h];
        
        }
        else{
            var s = FNG.INV.GoToStep[h[0]];
            
            //prepopulatesd template
            pt(h[1]);
            $("#template").val(h[1]);
            
        }
        
        //invalit has validator
        if(s){
            s();
        }
        else{
            FNG.AjaxUtil.Message("<strong>" + h + "</strong> " + settings.texts.wrongHash,  "#invite_from_wrp");
        }
        
    }();
    

    var init = function(){
    
        //updates template placehodlers
        $(".changer").change(function(){
            var T = $(this);
            var v = T.val();
            var n = T.attr("name");
            $("#temp_" + n).text(v);
        });
    
        //do not let the user leave
        FNG.INV.LetLeave = false; //let leave
    
        //deletes record if user leaves before finishing
        var unloadCleanup = function(){
            var s = FNG.Util.String; //util shorcut
            var u = s.getRoot() + "ajax/newEventClean";
            var h = "";
            var cb = function(){
                //cleanup call back function
            }
        
            $.post(u, h, cb, "json");
        }
    
        //if user wants to leave the page
        $(window).unload(function(){
            if(FNG.INV.LetLeave == false){
                //unload cleanup
                unloadCleanup();
            }
        })
    
        //creates alert if user wants to leave
        window.onbeforeunload = function(){
            if(FNG.INV.LetLeave == false){
                return settings.texts.onBeforeLeaveMsg;
            }
        }
    }();
    
    
    
    var step1_init = function(n){
        //AJAX forms
        n('#new_event_step_1');
        
        //loads event template based on the dropdown
        //change preview template
        
        //change template
        var ct = function(){
            var R = $("#prev_temp_wrp");
            if(!R[1]){
                var T = $(this);
                var p = T.val();
                pt(p);
            }
        }
        
        //bind to event
        $("#template").change(ct);

        // Datepicker
        $('#datepicker').datepicker({
            dayNamesMin: ['Ne', 'Po', 'Ut', 'St', 'Ct', 'Pa', 'So'],
	        inline: true
        });

        $.datepicker.regional['cs'] = {
		    closeText: 'Zavřít',
		    prevText: '&#x3c;Dříve',
		    nextText: 'Později&#x3e;',
		    currentText: 'Nyní',
		    monthNames: ['leden','únor','březen','duben','květen','červen',
            'červenec','srpen','září','říjen','listopad','prosinec'],
		    monthNamesShort: ['led','úno','bře','dub','kvě','čer',
		    'čvc','srp','zář','říj','lis','pro'],
		    dayNames: ['neděle', 'pondělí', 'úterý', 'středa', 'čtvrtek', 'pátek', 'sobota'],
		    dayNamesShort: ['ne', 'po', 'út', 'st', 'čt', 'pá', 'so'],
		    dayNamesMin: ['ne','po','út','st','čt','pá','so'],
		    dateFormat: 'dd/mm/yy', firstDay: 1,
		    isRTL: false};
	    $.datepicker.setDefaults($.datepicker.regional['cs']);
        
    }(FNG.INV.NewEventFormInit);
    
    //step two: improt emails:
    var step2_init = function(settings, n){
        
        var gcr = 5;
        
        //skip step one button:
        $("#skip_step_two").click(function(){
            var eg = $("#event_guid").val();
            var h = "<div class=\"clearfix info_box\"><p>Hotovo! Pozvánka byla vytvořena! <a href=\"/na/moje-akce/" + eg + "\">Tady</a> se na ni můžeš podívat, nebo ji upravit.</p></div>";
            $(".steps").after(h); 
            FNG.INV.GoToStep["done"]();
            FNG.INV.LetLeave = true; 
        });
        
        //Tabs init
        $('#tab_wrp').FngTabs();
        $('#email_tab_wrp2').FngTabs();
        
        //adds more fileds 
        $("#more_email_fields_btn").click(function(){
        
            var ac = ""; //alt class
            
            //checks for odd/even rows
            if(gcr%2){
                ac = " alt";
            }
        
            //appens a new row
            $("#guest_invite_table").append("<tr class=\"contact_set" + ac + "\"><td>" + gcr + "</td><td><input /></td><td><input /></td><td> <input id=\"ie_" + gcr + "\" /></td><td id=\"ie_" + gcr + "_t\"></td></tr>");
            
            //bind val click for the new row:
            $("#ie_" + gcr).change(bidVal);
            
            //adds one for the future row
            gcr = gcr + 1;
            return false;
        });
        
        
        //binds validation click
        var bidVal = function(){
            var T = $(this); //this current elemnt
            var e = T.val(); //value
            var i = T.attr("id"); //id of the input
            var ET = $("#" + i + "_t"); //target indicator
            
            //check if email validates and create indicator
            if(FNG.Util.String.isEmail(e)){
                ET.html("<img src=\"../../Content/images/icons/icon_check.png\"/>");
            }
            else{
                ET.html("<img src=\"../../Content/images/icons/icon_delete.gif\"/>");
            }
        }
        
        //email validator init
        var evi = function(){
            $(".contact_set").each(function() {
                var t = $(this); //this
                var I = $('input', t); //input pair
                $(I[2]).change(bidVal);
            });
        }();
        
        //http://www.bennadel.com/blog/1504-Ask-Ben-Parsing-CSV-Strings-With-Javascript-Exec-Regular-Expression-Command.htm
        //TODO does not work
        $("#csvEmails").blur(function(){
            var T = $(this);
            var s = T.val();
            s.replace("<", "");
            s.replace(">", "");
            T.val(s);
            //return false;
        })
        
        //onBeforeSubmit of email imports
        var obs = function(){
            var e = FNG.INV.GetCsvEmails(".contact_set");
            $("#csvEmails").val(e);
        };
        
        //AJAX form init
        n('#new_event_step_2', obs);
        
    }(settings, FNG.INV.NewEventFormInit);
    
    
    var step3_init = function(n){
        //AJAX forms
        n('#login_form');
        n('#register_form');
    }(FNG.INV.NewEventFormInit);

});
