$(document).ready(function() {
    
    // for signin box
    $('#nav-signin a.main-nav-item').click(function(){
        
        if($(this).hasClass('active')) {
            $(this).removeClass('active').addClass('inactive');
            $('#nav-signin_box').slideUp(400);
        }
        else {
            $(this).addClass('active');
            $('#nav-signin_box').slideDown(400);
        }
        return false;
    });

    $(window).load(function() {
        $('.flexslider').flexslider({
            directionNav: false,
            animationLoop: true
            });
    });
    
    markActiveLink();
    input_inactive();
    input_regform();
    UIdialog();
    faq();
    
    tooltip();
    close_window();    
});

function markActiveLink() {
    //Look through all the links in the sidebar
   $(".main-nav-item").filter(function() {

      //Take the current URL and split it into chunks at each slash
      var currentURL = window.location.toString().split("/");

      //return true if the bit after the last slash is the current page name
      return $(this).attr("href") == currentURL[currentURL.length-1];

    //when the filter function is done, you're left with the links that match.
    }).parent("li").addClass("selected");

    //Afterwards, look back through the links. If none of them were marked,
    //mark your default one.
    if($(".main-nav-item").parent("li").hasClass("selected") == false) {
    $(".main-nav-item:nth-child(2)").parent("li").addClass("selected");
  }
  
}

function input_inactive() {
    var username = $('#input-username');    
    var password = $('#input-pw');
    var mobile = $('#input-text-mobile');
    
    username.addClass('inactive-input');
    password.addClass('inactive-input');
    mobile.addClass('inactive-input');
    $(mobile).one("focus", function() {
      $(this).removeClass('inactive-input');
    });
}

function input_regform(){
    
    var reg_mobile = $('p.reg-mobile'); 
    var reg_nomobile = $('p.reg-nomobile'); 
    var reg_email = $('div.reg-ifemail');
    var captcha = $('.captchacode');
    var reg_nomobile_box = $('#input-checkbox-nomob');
    var agreement = $('#input-checkbox-agree');
    var button_submit = $('#input-submit');
    agreement.removeAttr('checked');
    
    function check_mobile(){
        var header = $('#reg-header');
        if($(this).is(':checked')) {
            show_non_mobile();
        }
        else {
            show_mobile();
        }
    }
    
    function check_agreement(){
        if ((this.checked)) {
            button_submit.removeClass('disabled').removeAttr('disabled');
        }
        else {
            button_submit.attr('disabled','disabled').addClass('disabled');
        }
    }
    
    if (button_submit.attr('disabled','disabled')){
        button_submit.addClass('disabled');
    }
    
    reg_nomobile_box.click(check_mobile);
    agreement.click(check_agreement);
    
}

function show_non_mobile() {    
    $('#reg-header').html('Register a NEW non-mobile account');
    $('p.reg-mobile').addClass('hide').css('display','none');
    $('div.reg-ifemail').fadeIn().removeClass('hide');
}

function show_mobile() {
    $('#reg-header').html('Use your Globe, Smart, or Sun mobile phone to register a NEW account');
    $('div.reg-ifemail').addClass('hide').css('display','none');
    $('p.reg-mobile').fadeIn().removeClass('hide');
}

function UIdialog() {

  $("#a-ret-pass").click(function(event) {
    $(".overlay").css("filter", "alpha(opacity=80)");
    $(".overlay").fadeIn("fast");
    $("#chikka-ui-dialog-retrievepw").delay(250).fadeIn("fast");
    $("#chikka-ui-dialog-retrievepw").center();

    event.preventDefault();
  });
  
  
  // Center UI Dialog
  jQuery.fn.center = function() {
    var container = $(window);
    var top = -this.height() / 1 + 200;
    var left = -this.width() / 2 - 10;
    return this.css('position', 'absolute').css({ 'margin-left': left + 'px', 'margin-top': + 'px', 'left': '50%', 'top': '100px' });
  }
  
  // Dialog Close Button
  $(".overlay, .close-button").click(function() {
    $(".chikka-ui-dialog").fadeOut("fast");
    $(".overlay").delay(250).fadeOut("fast", function() {
      $('#form-ret-pass').show();
      $('#p-ret-pass-msg').html('');
      $('#input-text-id').val('');
    });
  });
  
    
}

function faq(){

    $('div.faq-main > ul li div').hide();
    $('div.faq-main > ul li h4').click(function() {
        $(this).next().slideToggle('fast');
        return(false);
        });
    
    var containers = $('.faq-container'),
        general = $('#ctm-general'),
        general_link = $('#chikka_faq_link'),
        ctm_other = $('.ctm-other-link');
    
    containers.addClass('hide'); // other containers are hidden
    
    
    general_link.click(function(){
        general_sub.removeClass('hide');
    });
    
    ctm_other.click(function(){
        $('#ctm-chikka-iphone, #ctm-chikka-android, #ctm-chikka-chrome, #ctm-chikka-facebook').removeClass('hide');
        return false;
    });
    
    $('#faq-sidebar ul.left-content a').click(function(){
        var faq_id = $(this.hash);
        $('a.faq-selected').removeClass('faq-selected');
        containers.addClass('hide');
        $('#ctm-chikka-iphone, #ctm-chikka-android, #ctm-chikka-chrome, #ctm-chikka-facebook').addClass('hide');
        $(faq_id).removeClass('hide');
        $(this).addClass('faq-selected');
        return false;
    });
    
    general.removeClass('hide'); // Chikka FAQ - default visible 
    
    
}



this.tooltip = function(){  
    /* CONFIG */        
        xOffset = 10;
        yOffset = 20;       
        // these 2 variable determine popup's distance from the cursor
        // you might want to adjust to get the right result     
    /* END CONFIG */        
    $("a.tooltip").hover(function(e){                                             
        this.t = this.title;
        this.title = "";                                      
        $("body").append("<p id='tooltip'>"+ this.t +"</p>");
        $("#tooltip")
            .css("top",(e.pageY - xOffset) + "px")
            .css("left",(e.pageX + yOffset) + "px")
            .fadeIn("fast");        
    },
    function(){
        this.title = this.t;        
        $("#tooltip").remove();
    }); 
    $("a.tooltip").mousemove(function(e){
        $("#tooltip")
            .css("top",(e.pageY - xOffset) + "px")
            .css("left",(e.pageX + yOffset) + "px");
    });         
};



function close_window(){
    $('.close-open-window').click(function(){
        window.open('', '_self', '');
        window.close();
    }); 
}

