// on load actions
$(function(){
    // Replace plain character text input with password field on focus
    $('#password-clear').show();
    $('#password-password').hide();
    $('#password-clear').focus(function() {
        $(this).hide();
        $('#password-password').show().focus();
    });
    $('#password-password').blur(function() {
        if($(this).val() == '') {
            $('#password-clear').show();
            $(this).hide();
        }
    });
    // auto remove content in input fields on focus
    $("#article_search input").focus(function(){
        if(!kyani.focus_username){
            $(this).val("");
            kyani.focus_username = true;
        }
    });
    // toggle sub menus in sub navigation; left panel
    $(".section .menu").bind("click", function(){
        $(this).siblings().toggle();
        if(this.className != "info menu open"){
            $(this).addClass("open");
        }else{
            $(this).removeClass("open");
        }
    });
    // add navigation graphic to sub navigation; left panel
    $(".section a").bind("click", function(){
        $(".section a").removeClass("here");
        $(this).addClass("here");
    });
    // display language choice change
    $("#language-choice span").bind("click", function(){
        window.location = location.pathname + '?language='+ $(this).html();
    });
    // replace button text and submit form when button clicked
    $("#article_search button").bind("click", function(){
        kyani.login_fx();
        $("#article_form").submit();
    });
    // "enter" key in login inputs submits form
    $("#login-form input").keydown(function(event){
        if(event.keyCode == 13){
            kyani.login_fx();
            $("#login-form").submit();
        }
    });
    // iframe resize for framed content
    kyani.resize_frame();
    // binding "resize" in the window object breaks IE
    if ( !$.browser.msie ) {
        $(window).bind("resize", function(){
            kyani.resize_frame(); return true;
        });
    }
    // event information toggle; event section; office
    $(".event .head").bind("click", function(){
        $(this).siblings().toggle();
    });
    // table stripes; download section
    $("table.download-data tr:even td").addClass("alt");
    // download category choice control
    $("#col2 select").bind("change", function(){
        $(this).parent().submit();
    });
    // flash player dynamic load; KSA, articles
    kyani.load.flowplayer();
    // init dashboard; office
    if($("div.dashboard").length > 0){
        if($.browser.msie){
            $(".vert").addClass("vert-ie").removeClass("vert");
            $(".vert-rep").addClass("vert-rep-ie").removeClass("vert-rep");
        }
        $.ajax(kyani.dash.params("volume",true));
        //$.ajax(kyani.dash.params("recruit",true));
        $.ajax(kyani.dash.params("orders",true));
        $.ajax(kyani.dash.params("customers",true));
        $.ajax(kyani.dash.params("rank",false));
        $.ajax(kyani.dash.params("sys_date",true));
        $.ajax(kyani.dash.params("paygate",false));
        $.ajax(kyani.dash.params("contest",false));
        // dynamic tooltips and overlays
    }
    // rounded corners on promo images on public
    if(!$.browser.msie){
        $("div.promo").each(function(){
            img = $("img", this).get();
            $(img).load(function(){
                h = $(this).height(); w = $(this).width();
                sc = "url("+$(this).attr("src")+")";
                if(h > 0 && w > 0){
                    $(this).parent().css({height:h,width:w,"background-image":sc});
                    $(this).remove();
                }
            });
        });
    }
    // change css for Asian languages
    if(kyani.lonograph == true){
        $("body").addClass("lonograph");
    }
    // random header banners
    kyani.banners.process();
});
// Kyani Code
kyani = {
    processing_text: "PROCESSING ...",
    lonograph: false,
    focus_username: false,
    log: {},
    contest: 0,
    
    resize_frame: function(){
        if ( $("#iframe-holder").length != 1 ) {
            return false;
        }
        $("#iframe-holder").height("auto"); // reset height
        w = $(window).height();
        d = $(document).height();
        if(w >= d){
            v = w-220;
        }else{
            v = d-220;
        }
        $("#iframe-holder").height(v+"px");
        return true;
    },
    
    safari_message: function() {
        if((/Safari/.test(navigator.userAgent)) && !(/Chrome/.test(navigator.userAgent))){
            html = "<div class='safari_message'>";
            html += "Currently we cannot fully support the Safari web browser. Legacy pages are not correctly showing session information. We are working with our vendors to correct the problem. <a href='http://firefox.com' target='_blank'>Firefox</a> is a free download and is fully supported.";
            html += "</div>";
            
            $("body").prepend(html);
            return true;
        }
        return false;
    },
    
    login_fx: function() {
        $("#article_search button").text(kyani.processing_text);
    },
    
    addCommas: function(nStr){
        nStr += '';
        x = nStr.split('.');
        x1 = x[0];
        x2 = x.length > 1 ? '.' + x[1] : '';
        var rgx = /(\d+)(\d{3})/;
        while(rgx.test(x1)){
            x1 = x1.replace(rgx, '$1' + ',' + '$2');
        }
        return x1 + x2;
    },
    
    formatNum: function(num){
       result = !Number(num) ? 0 : Number(num);
       result = result.toFixed(0);
       return this.addCommas(result); 
    },
    
    // Dyanmic loading controls
    load: {
        ksa: function(title){
            $("#content_space").animate({opacity:"0.5"});
            $("body").append(kyani.load.loading_img);
            var o = {
                dataType: "html",
                url: "ajax/ksa.get_article.php",
                data: {ksa:title},
                success: function(r){
                    $("#content_space").html(r);
                    kyani.load.flowplayer();
                },
                error: function(){
                    alert("Error pulling page info. Please contact support.");
                },
                complete: function(){
                    $("#content_space").animate({opacity:"1"});
                    $("#loading").remove();
                }
            };
            $.ajax(o);
        },
        
        flowplayer_status: false,
        
        loading_img: '<div id="loading"><img src="/assets/images/ajax-loader.gif"/></div>',
        
        flowplayer: function(){
            if($("a.vidblock").length == 0 && $("a.audioblock").length == 0){
                return false;
            }
            // load flowplayer js if not already loaded
            if(!this.flowplayer_status){
                $.ajaxSetup({async: false});
                $.getScript('/assets/js/flowplayer/flowplayer-3.1.4.min.js');
                $.ajaxSetup({async: true});
                this.flowplayer_status = true;
            }
            //Flash Streaming Skin
            $f("a.vidblock","/assets/js/flowplayer/flowplayer-3.1.5.swf",{
                clip:{autoPlay:false}
            });
            
            $f("a.audioblock","/assets/js/flowplayer/flowplayer-3.1.5.swf",{
                plugins:{
                    controls: {
                        fullscreen: false,
                        height: 30
                    }
                },
                clip: {
                    autoPlay: false,
                    onBeforeBegin: function(){
                        $f("player").close();
                    }
                }
            });
            
        },
        
        tools_status: false,
        tools: function(){
            // load tools js if not already loaded
            if(!this.tools_status){
                $.ajaxSetup({async: false});
                $.getScript('/assets/js/jquery.tools.min.js');
                $.getScript('/assets/js/jquery.scrollable.js');
                $.ajaxSetup({async: true});
                this.tools_status = true;
            }
            
            // initialize tooltip
            $("[title]").tooltip({
            
               // tweak the position
               offset: [10, 2],
            
               // use the "slide" effect
               effect: 'slide',
               
               // delay the closing
               delay: 200
            
            // add dynamic plugin with optional configuration for bottom edge
            })
            .dynamic({ bottom: { direction: 'down', bounce: true } });
        }
    },
    
    // Dashboard Controls
    dash: {
        leg_count: 0,
        params: function(location, table){
            var selector = "."+location+" .bd";
            if(table){ selector += " tbody"}
            var dashboard_date = $("#dashboard_date").val();
            return {
                data: {date:dashboard_date},
                success: function(r){
                    kyani.dash.process(location, r, selector);
                },
                error: function(xml){
                    var mes = kyani.dash.templates.error("http error");
                    $(selector+" img").after(mes);
                    $(selector+" img").remove();
                },
                dataType: "json",
                url: "ajax/dash_"+location+".php",
                type: "POST"
            };
        },
        
        process: function(loc, r, sel){
            if(r.status != "success"){
                var mes = this.templates.error(r.description);
                $(sel).html(mes);
                return false;
            }
            var data = this.templates.fill(loc, r);
            if(data){
                $(sel).html(data);
            }
        },
        
        Tracker: function(o){
            var self = {
                params: o,
                ani_speed: 1000,
                tests_passed: 0
            };
            
            // Processing Scripts
            self.populate = {
                fill_calc: function(box, fill_percent, direction){
                    direction = (direction)? direction : "up";
                    percent = Number(fill_percent);
                    
                    switch(direction){
                        case "lr":
                        case "rl":
                            box = $(box).parent().width();
                            break;
                        default:
                            box = $(box).parent().height();
                    }
                    
                    if(percent >= 1.0){
                        return (Number(box) + 5).toString() + 'px';
                    }
                    
                    if(percent > 0.9){
                        percent = 0.9;
                    }
                    
                    if(percent < 0.05){
                        percent = 0.05;
                    }
                    
                    // IE requires actual pixels. Will not work with % for height.
                    calc_h = Number(box) * Number(percent);
                    percent_txt = calc_h.toString() + 'px';
                    
                    return percent_txt;
                },
                title: function(info){
                    o = {};
                    o.name = (info.name)? info.name : "";
                    o.id = (info.id)? " ("+info.id+"): " : "";
                    o.vol = (info.vol)? kyani.formatNum(info.vol) : "0";
                    o.cap = (info.cap)? kyani.formatNum(info.cap) : "0";
                    
                    if(info.percent){
                        value = Number(info.vol)/Number(info.cap)*100;
                        o.percent = (value > 100)? 100 : kyani.formatNum(value);
                    }else{
                        o.percent = 0;
                    }
                    
                    o.percent = "["+percent+"%]";
                    
                    o.difference = Number(info.cap) - Number(info.vol);
                    o.diff = (o.difference >= 0)? kyani.formatNum(o.difference) : "0";
                    
                    switch(info.template){
                        case "goal":
                            html = kyani.dash.templates.goal_tooltip(o);
                            break;
                        default:
                            html = kyani.dash.templates.cap_tooltip(o);
                            break;
                    }
                    
                    return html;
                },
                power_leg: function(){
                    context = self.params.context;
                    rep = self.params.data.frontline[0];
                    
                    loc = {
                        leg: $('.power .fill', context).get()
                    };
                    
                    if(!rep){
                        $(loc.leg).parent().remove();
                        return false;
                    }
                    
                    if(self.params.data.goal == 300){
                        cap = 0;
                        cap_amount = 200;
                    }else{
                        cap = 60;
                        cap_amount = (cap/100)*Number(self.params.data.goal);
                    }
                    
                    fill_percent = rep.contribution/cap_amount;
                    
                    px = this.fill_calc(loc.leg, fill_percent);
                    
                    $(loc.leg).animate({height: px}, self.ani_speed);
                    
                    rollover = this.title({
                        name: rep.name,
                        id: rep.id,
                        vol: rep.contribution,
                        cap: cap_amount,
                        percent: fill_percent
                    });
                    
                    $(loc.leg).parent().attr({title:rollover});
                    
                    return {
                        cap: cap,
                        cap_amount: cap_amount,
                        fill_percent: fill_percent,
                        fill_height: px
                    };
                },
                second_leg: function(){
                    rep = (self.params.data.frontline[1]) ? self.params.data.frontline[1] : false;
                    percentages = self.params.data.percentages;
                    context = self.params.context;
                    
                    c = {
                        leg: $('.second .fill', context).get()
                    };
                    
                    if(!rep){
                        $(c.leg).parent().parent().remove();
                        return false;
                    }
                    
                    min_percent = 47.5;
                    power_percent = Number(percentages[0]) ? Number(percentages[0]) : 0;
                    
                    if(power_percent > min_percent){
                        cap = min_percent - (power_percent - min_percent);
                    }else{
                        cap = min_percent;
                    }
                    
                    cap_amount = (cap/100)*Number(self.params.data.goal);
                    
                    fill_percent = rep.contribution/cap_amount;
                    
                    px = this.fill_calc(c.leg, fill_percent);
                    
                    $(c.leg).animate({height: px}, self.ani_speed);
                    
                    rollover = this.title({
                        name: rep.name,
                        id: rep.id,
                        vol: rep.contribution,
                        cap: cap_amount,
                        percent: fill_percent
                    });
                    
                    $(c.leg).parent().attr({title:rollover});
                    
                    return {
                        power_percent: power_percent,
                        min_percent: min_percent,
                        fill_percent: fill_percent,
                        fill_height: px
                    };
                },
                other_leg: function(){
                    total = self.params.other_legs_amount;
                    context = self.params.context;
                    
                    c = {
                        leg: $('.others .fill', context).get()
                    };
                    
                    percent = total/Number(self.params.data.goal);
                    
                    px = this.fill_calc(c.leg, percent);
                    
                    $(c.leg).animate({height: px}, self.ani_speed);
                    
                    rollover = this.title({
                        vol: total,
                        cap: self.params.data.goal,
                        percent: percent
                    });
                    
                    $(c.leg).parent().attr({title:rollover});
                    
                    return {
                        fill_height: px,
                        percent: percent,
                        total: total,
                        goal:self.params.data.goal
                    };
                },
                total: function(){
                    pv = Number(self.params.data.volume.PV);
                    total = self.params.total_amount + pv;
                    context = self.params.context;
                    req = Number(self.params.QV_requirement);
                    
                    c = {
                        leg: $('.goal .fill', context).get(),
                        next_rank: $('.goal .next', context).get(),
                        current_rank: $('.goal .current', context).get()
                    };
                    
                    percent = total/self.params.data.goal;
                    
                    px = this.fill_calc(c.leg, percent, "lr");
                    
                    $(c.leg).animate({width: px}, self.ani_speed);
                    $(c.next_rank).html(self.params.data.next_rank);
                    $(c.current_rank).html(self.params.data.current_rank);
                    
                    if(pv < req){
                        $(c.next_rank).addClass("pv_low");
                    }
                    
                    rollover = this.title({
                        vol: total,
                        cap: self.params.data.goal,
                        percent: percent,
                        template: "goal"
                    });
                    
                    $(c.leg).parent().attr({title:rollover});
                    
                    return {
                        percent: percent,
                        fill_height: px
                    };
                },
                pv: function(){
                    context = self.params.context;
                    pv = Number(self.params.data.volume.PV);
                    req = Number(self.params.QV_requirement);
                    
                    loc = {
                        leg: $(".pqv .fill", context).get(),
                        text: $(".pqv .label", context).get()
                    };
                    
                    fill_percent = pv/Number(req);
                    
                    px = this.fill_calc(loc.leg, fill_percent);
                    
                    $(loc.leg).animate({height:px}, self.ani_speed);
                    
                    if(pv < req){
                        $(loc.text).addClass("pv_low");
                    }
                    
                    rollover = this.title({
                        vol: pv,
                        cap: req,
                        percent: fill_percent,
                        template: "goal"
                    });
                    
                    $(loc.leg).parent().attr({title:rollover});
                    
                    return {
                        pv: pv,
                        req: req,
                        fill_percent: fill_percent,
                        fill_height: px
                    };
                },
                gate_power_part: function(){
                    context = self.params.context;
                    rep = self.params.data.frontline.power;
                    cap = self.params.data.frontline.gate.power;
                    
                    loc = {
                        leg: $('.power .fill', context).get()
                    };
                    
                    if(!rep){
                        $(loc.leg).parent().parent().remove();
                        return false;
                    }
                    
                    if(Number(rep.volume) >= Number(cap)){
                        fill_percent = 1;
                        rep.volume = cap;
                    }else{
                        fill_percent = Number(rep.volume)/Number(cap);
                    }
                    
                    px = this.fill_calc(loc.leg, fill_percent);
                    
                    $(loc.leg).animate({height: px}, self.ani_speed);
                    
                    rollover = this.title({
                        name: rep.name,
                        id: rep.id,
                        vol: rep.volume,
                        cap: cap,
                        percent: fill_percent,
                        template: "goal"
                    });
                    
                    $(loc.leg).parent().attr({title:rollover});
                    
                    return {
                        vol: rep.volume,
                        cap: cap,
                        fill_percent: fill_percent,
                        fill_height: px
                    };
                },
                gate_other_part: function(){
                    context = self.params.context;
                    others = self.params.data.frontline.others;
                    cap = self.params.data.frontline.gate.small;
                    
                    loc = {
                        leg: $('.outside .fill', context).get()
                    };
                    
                    fill_percent = (others >= cap)? 1: Number(others)/Number(cap);
                    
                    px = this.fill_calc(loc.leg, fill_percent);
                    
                    $(loc.leg).animate({height: px}, self.ani_speed);
                    
                    rollover = this.title({
                        vol: others,
                        cap: cap,
                        percent: percent,
                        template: "goal"
                    });
                    
                    $(loc.leg).parent().attr({title:rollover});
                    
                    return {
                        vol: others,
                        cap: cap,
                        fill_percent: fill_percent,
                        fill_height: px
                    };
                    
                },
                gate_third_part: function(){
                    context = self.params.context;
                    third = self.params.data.frontline.third;
                    cap = Number(self.params.data.frontline.gate.third);
                    vol = (third == null)? 0 : Number(third.volume);
                    t_name = (third == null)? "" : third.name;
                    t_id = (third == null)? "" : third.id;
                    
                    loc = {
                        leg: $(".third .fill", context).get()
                    };
                    
                    fill_percent = (vol >= cap) ? 1.0 : vol/cap;
                    
                    px = this.fill_calc(loc.leg, fill_percent);
                    
                    $(loc.leg).animate({height: px}, self.ani_speed);
                    
                    rollover = this.title({
                        name: t_name,
                        id: t_id,
                        vol: vol,
                        cap: cap,
                        percent: fill_percent,
                        template: "goal"
                    });
                    
                    $(loc.leg).parent().attr({title:rollover});
                    
                    return {
                        cap: cap,
                        vol: vol,
                        fill_percent: fill_percent,
                        fill_height: px
                    };
                },
                gate: function(){
                    context = self.params.context;
                    next = "Gate " + self.params.data.frontline.gate.gate_id;
                    current_id = Number(self.params.data.frontline.gate.gate_id) - 1;
                    current = "Gate " + current_id;
                    passed = 0;
                    power_req = Number(self.params.data.frontline.gate.power);
                    power_vol = Number(self.params.data.frontline.power.volume);
                    second_req = Number(self.params.data.frontline.gate.small);
                    second_vol = Number(self.params.data.frontline.others);
                    third_req = Number(self.params.data.frontline.gate.third);
                    pv_req = Number(self.params.QV_requirement);
                    pv_vol = Number(self.params.data.volume.PV);
                    
                    if(self.params.data.frontline.third == null){
                        third_vol = 0;
                    }else{
                        third_vol = self.params.data.frontline.third.volume;
                    }
                    
                    if(power_vol >= power_req){
                        passed++;
                    }
                    
                    if(second_vol >= second_req){
                        passed++;
                    }
                    
                    if(third_vol >= third_req){
                        passed++;
                    }
                    
                    if(pv_vol >= pv_req){
                        passed++;
                    }else{
                        $(".goal .next", context).addClass("pv_low");
                    }
                    
                    fill_percent = passed/4;
                    
                    leg = $(".goal .fill", context).get();
                    
                    px = this.fill_calc(leg, fill_percent, "lr");
                    
                    $(leg).animate({width:px}, self.ani_speed);
                    
                    $(".goal .current", context).html(current);
                    $(".goal .next", context).html(next);
                    
                    rollover = this.title({
                        vol: passed,
                        cap: 4,
                        percent: fill_percent,
                        template: "goal"
                    });
                    
                    $(leg).parent().attr({title:rollover});
                    
                    return {
                        current: current,
                        next: next,
                        passed: passed,
                        fill_percent: fill_percent,
                        fill_height: px
                    };
                },
                
                goal: function(){
                    var context = self.params.context;
                    var start = self.params.data.contest_info.start_date;
                    var end = self.params.data.contest_info.end_date;
                    var goal = self.params.data.goal;
                    
                    c = {
                        leg: $('.goal .fill', context).get(),
                        start_date: $('.goal .current', context).get(),
                        end_date: $('.goal .next', context).get()
                    };
                    
                    $(c.start_date).html(start);
                    $(c.end_date).html(end);
                    
                    return {
                        start: start,
                        end: end,
                        goal: goal
                    };
                },
                
                frontline: function(){
                    var frontline = self.params.data.frontline;
                    var html = "";
                    
                    // loop through frontline
                    $.each(frontline, function(i,rep){
                        // create frontline element
                        html += kyani.dash.templates.frontline_bar(rep);
                        // start getting frontline contribution
                        $.ajax({
                            url: 'ajax/dash_contest.php',
                            dataType: 'json',
                            data: {id:rep.id},
                            success: function(r){
                                sel = ".frontline_"+rep.id+" .label span";
                                if(r.status != "success"){
                                    $(".frontline_"+rep.id).remove();
                                    kyani.dash.leg_count++;
                                    return false;
                                }
                                kyani.dash.process("contest_frontline", r, sel);
                            },
                            error: function(xml){
                                console.log(xml);
                            }
                        });
                    });
                    
                    $(".bd .items", self.params.context).append(html);
                                        
                    return {
                        html: html
                    };
                },
                
                frontline_data: function(){
                    var cap = Number(self.params.data.cap);
                    var contribution = Number(self.params.data.contribution);
                    var location = ".frontline_"+self.params.data.id;
                    
                    var bar = $('.fill',location).get();
                    
                    var percent = contribution/cap;
                    
                    var px = this.fill_calc(bar, percent);
                    
                    $(bar).animate({height: px}, self.ani_speed);
                    
                    var rollover = this.title({
                        name: self.params.data.name,
                        id: self.params.data.id,
                        vol: contribution,
                        cap: cap,
                        percent: percent
                    });
                    
                    $(bar).parent().attr({title:rollover});
                    
                    $('.label span',location).html(self.params.data.name);
                    
                    var actual = (contribution > cap) ? cap : contribution;
                    this.update_total(actual);
                    
                    kyani.load.tools();
                    
                    $('.contest .bd .scrollable').scrollable({size:4});
                    
                    if(kyani.dash.leg_count > 4){
                        $('.contest .bd a.right').removeClass('disabled');
                    }
                    
                    return {
                        cap: cap,
                        contribution: contribution,
                        fill_percent: percent,
                        fill_height: px,
                        location: location
                    };
                },
                
                update_total: function(add){
                    kyani.contest += Number(add);
                    var goal = kyani.log.contest.header.goal;
                    
                    percent = Number(kyani.contest)/Number(goal);
                    
                    var fill = $(".contest .goal .fill").get();
                    
                    px = this.fill_calc(fill, percent, "lr");
                    
                    kyani.dash.leg_count++;
                    
                    $(fill).animate({width: px}, self.ani_speed);
                    
                    if(kyani.dash.leg_count >= kyani.log.contest.params.data.frontline.length)
                    {   
                        rollover = this.title({
                            vol: kyani.contest,
                            cap: goal,
                            percent: percent,
                            template: "goal"
                        });
                        
                        $(fill).parent().attr({title:rollover});
                    }
                }
            };
            
            return self;
        },
        
        // Templates
        templates: {
            fill: function(loc, o){
                switch(loc){
                    case "customers":
                        html = this.customers(o.data);
                        break;
                    case "orders":
                        html = this.orders(o.data);
                        break;
                    case "promotions":
                        html = this.promotions(o.data);
                        break;
                    case "recruit":
                        html = this.recruit(o.data);
                        break;
                    case "volume":
                        html = this.volume(o.data);
                        break;
                    case "events":
                        html = this.events(o.data);
                        break;
                    case "rank":
                        html = this.rank(o);
                        break;
                    case "sys_date":
                        html = this.sys_date(o);
                        break;
                    case "paygate":
                        html = this.paygate(o);
                        break;
                    case "contest":
                        html = this.contest(o);
                        break;
                    case "contest_frontline":
                        html = this.contest_frontline(o);
                        break;
                    default:
                        html = "Did not recognize: "+loc;
                }
                return html;
            },
            
            error: function(desc){
                return "<span class='error'>"+desc+"</span>";
            },
            
            cap_tooltip: function(o){
                html = '<div class="rep_name">'+o.name+'</div>';
                html +='<table>';
                html +='<tr><td>Maximum:</td><td class="numbers">'+o.cap+'</td></tr>';
                html +='<tr><td>Current:</td><td class="numbers">'+o.vol+'</td></tr>';
                html +='<tr class="diff"><td>Difference:</td><td class="numbers">'+o.diff+'</td></tr>'
                html +='</table>';
                return html;
            },
            
            goal_tooltip: function(o){
                html = '<div class="rep_name">'+o.name+'</div>';
                html += '<table>';
                html += '<tr><td>Goal:</td><td class="numbers">'+o.cap+'</td></tr>';
                html += '<tr><td>Current:</td><td class="numbers">'+o.vol+'</td></tr>';
                html += '<tr class="diff"><td>Requires:</td><td class="numbers">'+o.diff+'</td></tr>';
                html += '</table>';
                return html;
            },
            
            customers: function(o){
                var html = ""; var pv = 0; var cv = 0;
                for(i=0;i<o.length;i++){
                    html += "<tr>\n";
                    //html += "<td><a href='javascript: alert(\""+ o[i].id +"\")'>";
                    //html += o[i].name +"</a></td>\n";
                    html += "<td>"+ o[i].id + " " + o[i].name +"</td>\n";
                    html += "<td>"+ o[i].type +"</td>\n";
                    html += "<td class='numbers'>"+ kyani.formatNum(o[i].pv) +"</td>\n";
                    html += "<td class='numbers'>"+ kyani.formatNum(o[i].cv) +"</td>\n";
                    html += "</tr>\n";
                    pv += Number(o[i].pv);
                    cv += Number(o[i].cv);
                }
                html += "<tr>";
                html += "<td style='border-top:1px solid #ccc'>"+o.length+"</td>";
                html += "<td></td>";
                html += "<td style='border-top:1px solid #ccc'>"+kyani.formatNum(pv)+"</td>";
                html += "<td style='border-top:1px solid #ccc'>"+kyani.formatNum(cv)+"</td>";
                html += "</tr>";
                
                return html;
            },
            
            orders: function(o){
                var html = "";
                for(i=0;i<o.length;i++){
                    html += "<tr>\n";
                    //html += "<td><a href='javascript: alert(\""+ o[i].id +"\")'>"+ o[i].id +"</a></td>\n";
                    html += "<td>"+ o[i].id +"</td>\n";
                    html += "<td>"+ o[i].status +"</td>\n";
                    html += "<td class='numbers'>"+ kyani.formatNum(o[i].volumes) +"</td>\n";
                    html += "<td>"+ o[i].date +"</td>\n";
                    html += "</tr>\n";
                }
                return html;
            },
            
            promotions: function(o){
                var html = "";
                for(i=0;i<o.length;i++){
                    html += "<tr>\n";
                    html += "<td>"+ o[i].name +" ("+ o[i].id +")</td>\n";
                    html += "<td>"+ o[i].from +"</td>\n";
                    html += "<td>"+ o[i].to +"</td>\n";
                    html += "<td>"+ o[i].date +"</td>\n";
                    html += "</tr>\n";
                }
                return html;
            },
            
            recruit: function(o){
                var html = "";
                for(i=0;i<o.length;i++){
                    html += "<tr>\n";
                    html += "<td>"+ o[i].name +" ("+ o[i].id +")</td>\n";
                    //html += "<td><a href='javascript: alert(\""+ o[i].id +"\")'>"+ o[i].recruits +"</a></td>\n";
                    html += "<td class='numbers'>"+ o[i].recruits +"</td>\n";
                    html += "</tr>\n";
                }
                return html;
            },
            
            volume: function(o){
                var html = "";
                for(i=0;i<o.length;i++){
                    html += "<tr>\n";
                    html += "<td>"+ o[i].name +" ("+ o[i].id +")</td>\n";
                    //html += "<td><a href='javascript: alert(\""+ o[i].id +"\")'>"+ o[i].gv +"</a></td>\n";
                    html += "<td class='numbers'>"+ kyani.formatNum(o[i].gv) +"</td>\n";
                    html += "</tr>\n";
                }
                return html;
            },
            
            events: function(o){
                var html = "<ul>\n";
                for(i=0;i<o.length;i++){
                    html += "<li>"+ o[i].date +": "+ o[i].description +" ("+ o[i].name +")</li>\n";
                }
                return html;
            },
            
            sys_date: function(o){
                var html = "<tr class='sys_date_data'><td>"+ o.date +"</td></tr>\n";
                return html;
            },
            
            rank: function(o){
                if(!o.status || o.status != "success"){
                    return false;
                }
                
                total = 0;
                other_legs_total = 0;
                
                for(i=0;i<o.frontline.length;i++){
                    if(i>1){
                        other_legs_total += Number(o.frontline[i].contribution);
                    }
                    total = total + Number(o.frontline[i].contribution);
                }
                
                params = {
                    context: ".rank",
                    data: o,
                    other_legs_amount: other_legs_total,
                    total_amount: total,
                    QV_requirement: 100
                };  
                
                var rank = new kyani.dash.Tracker(params);
                
                var debug = {
                    power_leg: rank.populate.power_leg(),
                    second_leg: rank.populate.second_leg(),
                    other_leg: rank.populate.other_leg(),
                    total: rank.populate.total(),
                    pv: rank.populate.pv(),
                    params: params
                };
                
                kyani.log.rank_tracker = debug;
                
                kyani.load.tools();
                
                return false; // force calling function to not replace the html
            },
            
            paygate: function(o){
                if(!o.status || o.status != "success"){
                    return false;
                }
                
                params = {
                    context: ".paygate",
                    data: o,
                    QV_requirement: 100
                };
                
                var gate = new kyani.dash.Tracker(params);
                
                var debug = {
                    power_leg: gate.populate.gate_power_part(),
                    gate: gate.populate.gate(),
                    others: gate.populate.gate_other_part(),
                    pv: gate.populate.pv(),
                    third: gate.populate.gate_third_part(),
                    params: params
                };
                
                kyani.log.paygate_tracker = debug;
                
                kyani.load.tools();
                
                return false; // force calling function to not replace the html
            },
            
            contest: function(o){
                if(!o.status || o.status != "success"){
                    return false;
                }
                
                params = {
                    context: ".contest",
                    data: o
                };
                
                var contest = new kyani.dash.Tracker(params);
                
                var debug = {
                    header: contest.populate.goal(),
                    frontline: contest.populate.frontline(),
                    params: params
                }
                
                kyani.log.contest = debug;
                
                kyani.load.tools();
                
                return false; // force calling function to not replace the html
            },
            
            frontline_bar: function(o){
                html = '<span class="req frontline_'+o.id+'">';
                html +='<div class="label"><span><img src="/assets/images/ajax-loader.gif" /></span></div>';
                html +='<div class="bar">';
                html +='<div class="fill">&nbsp;</div>';
                html +='</div>';
                html +='</span>';
                
                return html;
            },
            
            contest_frontline: function(o){
                var context = $(".contest .frontline_"+o.id).get();
                
                if(!context){
                    console.log(o);
                }
                
                if(!o.status || o.status != "success"){
                    return false;
                }
                
                params = {
                    data: o,
                    context: context
                };
                
                contest_leg = new kyani.dash.Tracker(params);
                
                debug = {
                    this_rep: contest_leg.populate.frontline_data()
                };
                
                return false; // force calling function to not replace the html
            }
        }
    },    
    
    // Rotating Banner System
    banners: {
        location: "/assets/images/banners/",
        images: [
            "BottleSplash1",
            "KnockoutBottle",
            "LightBottle"
        ],
        
        process: function(){
            if($("#header").height() > 110){
                banner = this.get("big");            
            }else{
                banner = this.get("small");
            }
            $("#header").html("<img src='"+banner.main+"' />");
            $("#header").css("background-image", "url("+banner.pixel+")");
        },
        
        get: function(type){
            image = this.images[Math.floor(Math.random()*this.images.length)];
            return {
                main: this.location + image + "_" + type + ".jpg",
                pixel: this.location + image + "_" + type + "_pixel.jpg"
            };
        }
    }
};
