


var TimelineFlash = {

    settings: {
        id: "home_phase2",
        contentFrameId: "ContentFrame",
        postFrameId: "ContentFrame",
        postURL: "/Templates/Pages/YourProfile.aspx?id=2697&iframe=1&timelinepostid=",
        publicPostURL: "/Templates/Pages/PublicProfilePage.aspx?id=2696&iframe=1&timelinepostid=",
        yourBrandURL: "http://www.libero.ru/Your-Libero/"
    },

    timeoutTries: 0,
    initialized: false,
    initFromFrame: false,

    onInit: function() { /*abstract*/ },

    init: function() {

        this.log("TimelineFlash.init() called (initFromFrame = " + this.initFromFrame + ")");

        try {
            if(window.top.TimelineFlashLocked === undefined){
                window.top.TimelineFlashLocked = false;
                window.top.TimelineFlashPublishing = false;
            }
        } catch (err) { this.log(err); }

        this.initialized = true;
        this.prepare();
        this.onInit();
        this.initFrame();

    },
    
    setLocked: function(bLock){ 
        window.top.TimelineFlashLocked = bLock;
    },
    
    isLocked: function(){
        return window.top.TimelineFlashLocked === true ? true : false;
    },
    
    setPublishing: function(bPublishing){  
        window.top.TimelineFlashPublishing = bPublishing;
    },
    
    isPublishing: function(){
        return window.top.TimelineFlashPublishing === true ? true : false;
    },    

    unlockOnUnload: function(){
        if(TimelineFlash.isLocked() && !TimelineFlash.isPublishing()){
            this.newPostCancel(); 
            this.setLocked(false);
        }
    },

    prepare: function() {

        this.setFlash();

        if (this.foundFlash()) {
            if (this.initialized){
                this.onInit();
            }
                
            $(window).unload(function(){                
                TimelineFlash.unlockOnUnload();
            });
                
        }

    },

    setFlash: function() {

        this.o = $("#" + this.settings.id)[0];

        if (!this.foundFlash()) {
            if (typeof (window.parent.$) != 'undefined') {
                var objects = window.parent.$("#" + this.settings.id);
                if (objects.length > 0) {
                    this.o = objects[0];
                    if (typeof (window.parent) != 'undefined'){
                        if (typeof (window.parent.TimelineFlash) != 'undefined'){
                            this.initialized = window.parent.TimelineFlash.initialized;
                        }
                    }
                }
            }
        }

    },

    foundFlash: function() {
        return typeof (this.o) != 'undefined' ? true : false;
    },

    initFrame: function() {
        try {
            if (window.frames.length > 0) {
                var frame = window.frames[this.settings.contentFrameId];
                if (typeof (frame) != 'undefined') {
                    if (typeof (frame.TimelineFlash) != 'undefined') {
                        frame.TimelineFlash.initFromFrame = true;
                        frame.TimelineFlash.init();
                        this.timeoutTries = 0;
                    } else {
                        if (this.timeoutTries < 30) {
                            this.timeoutTries++;
                            setTimeout("TimelineFlash.initFrame();", 1000);
                        }
                    }
                }
            }
        } catch (err) { this.log(err); }
    },
    
    getStageID: function(stageName) {
        
        var stages = ["trying", "pregnant", "baby", "toddler"];
        return stages.indexOf(stageName.toLowerCase());
        
    },

    log: function(msg) {
        if (typeof (console) != 'undefined'){
            console.log(msg);
        }
    },
    
    
    /* Flash Methods */

    updateSection: function(stage, period) {
        if (this.foundFlash()) {
            try {
                if (typeof (this.o.updateSection) == 'function') {
                    this.o.updateSection(stage, period);
                    this.log("TimelineFlash.updateSection('" + stage + "', " + period + ")");
                }else{
                    this.log("ERROR! updateSection is not available in timelineflash");
                }
            } catch (err) { this.log(err); }
        }
    },

    updateUserData: function() {
        if (this.foundFlash()) {
            try {
                if (typeof (this.o.updateUserData) == 'function') {
                    this.o.updateUserData();
                    this.log("TimelineFlash.updateUserData()");                
                }else{
                    this.log("ERROR! updateUserData is not available in timelineflash");
                }                
            } catch (err) { this.log(err); }
        }
    },
    
    newPostCancel: function() {
        if (this.foundFlash()) {
            try {
                if (typeof (this.o.newPostCancel) == 'function') {
                    this.o.newPostCancel();
                    this.setLocked(false);
                    this.setPublishing(false);
                    this.log("TimelineFlash.newPostCancel()");                
                }else{
                    this.log("ERROR! newPostCancel is not available in timelineflash");
                }
            } catch (err) { this.log(err); }
        }
    },
    
    newPostComplete: function(strXML) {
        if (this.foundFlash()) {
            try {
                if (typeof (this.o.newPostComplete) == 'function') {
                    this.o.newPostComplete(strXML);
                    this.setLocked(false);
                    this.setPublishing(false);
                    this.log("TimelineFlash.newPostComplete('" + strXML + "')");
                }else{
                    this.log("ERROR! newPostComplete is not available in timelineflash");
                }
            } catch (err) { this.log(err); }
        }
    },
    
    postChanged: function(strXML) {
        if (this.foundFlash()) {
            try {
                if (typeof (this.o.postChanged) == 'function') {
                    this.o.postChanged(strXML);
                    this.log("TimelineFlash.postChanged('" + strXML + "')");
                }else{
                    this.log("ERROR! postChanged is not available in timelineflash");
                }
            } catch (err) { this.log(err); }
        }
    },        
    
    addNewPost: function(stage, period) {
        if (this.foundFlash()) {
            try {
                if (typeof (this.o.addNewPost) == 'function') {
                    this.o.addNewPost(stage, period);
                    this.log("TimelineFlash.addNewPost('" + stage + "', " + period + ")");
                    this.setLocked(true);
                }else{
                    this.log("ERROR! addNewPost is not available in timelineflash");
                }
            } catch (err) { this.log(err); }
        }
    },    
    
    
    /* Flash Events */
    
    onInitNewPost: function(stage, period, child, date) {
        try {
            
            if(isNaN(stage)){
                stage = this.getStageID(stage);
            }
                
            if(isNaN(child) || child == "1"){
                child = "";
            }
            
            var frame = window.top.frames[this.settings.postFrameId];
            frame.newPost(stage, period, child, date);
            this.setLocked(true);
            
            this.log("TimelineFlash.onInitNewPost('" + stage + "', '" + period + "', '" + child + "', '" + date + "')");
            
        } catch (err) { this.log(err); }
    },
    
    onPostClick: function(postID){
        try {
                        
            var frame = window.top.frames[this.settings.contentFrameId];

            if(window.location.href.indexOf("userId=") > -1){               
                frame.document.location.href = this.settings.publicPostURL + postID;
            }
            else {
                frame.document.location.href = this.settings.postURL + postID;
            }
                        
            this.log("TimelineFlash.onPostClick('" + postID + "')");
            
        } catch (err) { this.log(err); }
    },
    
    onChangePostPeriod: function(stage, period, child){
        try {
            var frame = window.top.frames[this.settings.postFrameId];
            
            if (typeof (frame.onChangePostPeriodListener) == 'function') {
               frame.onChangePostPeriodListener(stage, period, child);
            }
            
            this.log("TimelineFlash.onChangePostPeriod('" + stage + "', '" + period + "', '" + child + "')");
            
        } catch (err) { this.log(err); }
    },
    
    onChangeTimeline: function(stage, period, child){
        try {
        
            var a = "'" + stage + "','" + period + "', '" + child + "'";
            this.log("TimelineFlash.onChangeTimeline(" + a + ")");
            
        } catch (err) { this.log(err); }
    }

};

function onInitNewPost(stage, period, child, date){
    TimelineFlash.onInitNewPost(stage, period, child, date);    
}

function onPostClick(postId){
    TimelineFlash.onPostClick(postId);    
}

function onChangePostPeriod(stage, period, child){
    TimelineFlash.onChangePostPeriod(stage, period, child);    
}

function onChangeTimeline(stage, period, child){
    TimelineFlash.onChangeTimeline(stage, period, child);    
}


function hideNoneFlashNavi() {
    if ($('#container').find('#home_phase2').length > 0) {
        $('#tabs-trying, #date-selector, #calculate-due-date, #add-your-due-date').hide();
    }
    else if ($('#wrapper').find('.nav-main-stage').length > 0) {
        $('#tabs-trying, #date-selector, #calculate-due-date, #add-your-due-date').show();
    }
}


$(function() {

    if (window.top == window && $("#flash").length > 0) {

        var userToDisplay = "current";
        var timelineMode = "private";
        
        if(window.location.href.indexOf(TimelineFlash.settings.yourBrandURL) > -1){
            
            timelineMode = "public";
            
        }else if(window.location.href.indexOf("userId=") > -1){
            
            var queryUserId = GBW.GetQueryParam("userId");
            
            if(queryUserId != ""){
                userToDisplay = queryUserId;
                timelineMode = "public";
            }
            
        }
     
        var flashvars = {
            iso: 'ru-RU',
            asset_path: '/TimelineFlash/Version1/',
            params_url: '/TimelineFlash/Parameters.aspx',
            loader_font: '/TimelineFlash/Version1/swf/fonts/progress_font_karlson.swf',
            primary_user_id: userToDisplay,
            timeline_mode: timelineMode,
            header: '/TimelineFlash/Images/Libero-split-header-1.png,/TimelineFlash/Images/Libero-split-header-2.png,/TimelineFlash/Images/Libero-split-header-3.png',
            header_link: '',
            header_hitarea: '0,0,0,0,0',
            version: '1.5.0.6'
        };
        var params = {
            wmode: "window",
            menu: "false"
        };
        var attributes = {
            id: "home_phase2",
            name: "home_phase2",
            type: "application/x-shockwave-flash" 
        };
    
        swfobject.embedSWF("/TimelineFlash/Version1/swf/index.swf", "flash", "958", "345", "9.0.0", false, flashvars, params, attributes);
        swfobject.createCSS("#home_phase2", "outline:none"); // border fix.
        swfobject.addDomLoadEvent(hideNoneFlashNavi);

    }

});

$(document).ready(function() {

    TimelineFlash.prepare();

});
