$(function(){
    $('#password-clear').show();
    $('#password-password').hide();

    $('#password-clear').focus(function() {
        $(this).hide();
        $('#password-password').show();
        $('#password-password').focus();
    });
    
    $('#password-password').blur(function() {
        if($(this).val() == '') {
            $('#password-clear').show();
            $(this).hide();
        }
    });
    
    $("#article_search input").focus(function(){
        $(this).select();
    });

    $(".section .menu").bind("click", function(){
        $(this).siblings().toggle();
        
        if(this.className != "info menu open"){
            $(this).addClass("open");
        }else{
            $(this).removeClass("open");
        }
    });
    
    $(".section a").bind("click", function(){
        $(".section a").removeClass("here");
        $(this).addClass("here");
    });
    
    $("#language-choice span").bind("click", function(){
        window.location = location.pathname + '?language='+ $(this).html();
    });
    
    $("#article_search button").bind("click", function(){
        kyani.login_fx();
        $("#article_form").submit();
    });
    
    $("#login-form input").keydown(function(event){
        if(event.keyCode == 13){
            kyani.login_fx();
            $("#login-form").submit();
        }
    });
    
    kyani.resize_frame();
    
    $(window).bind("resize", function(){
        return kyani.resize_frame();
    });
    
    $(".event .head").bind("click", function(){
        $(this).siblings().toggle();
    });
    
    $("table.download-data tr:even td").addClass("alt");
    
    $("#col2 select").bind("change", function(){
        $(this).parent().submit();
    });
    
    kyani.load.flowplayer();
    
    if($("div.dashboard").length > 0){
        kyani.load.dashboard();
    }
});

kyani = {
    processing_text: "PROCESSING ..."
};
kyani.resize_frame = function(){
    $("#iframe-holder").each(function(){ 
        $(this).height("auto"); // reset height
        //should be only one on the page
        w = $(window).height();
        d = $(document).height();
        if(w >= d){
            v = w-220;
        }else{
            v = d-220;
        }
        $(this).height(v+"px");
    });
}
kyani.login_fx = function(){
    $("#article_search button").text(kyani.processing_text);
}
kyani.load = {};
kyani.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);
};
kyani.load.flowplayer_status = false;
kyani.load.loading_img = '<div id="loading"><img src="/assets/images/ajax-loader.gif"/></div>';
kyani.load.flowplayer = function(){
    if($("a.vidblock").length == 0){
        return false;
    }
    
    // load flowplayer js if not already loaded
    if(!this.flowplayer_status){
        $.ajaxSetup({async: false});
        $.getScript('/js/flowplayer/flowplayer-3.1.4.min.js');
        $.ajaxSetup({async: true});
        this.flowplayer_status = true;
    }
    
    //Flash Streaming Skin
    $f("a.vidblock","/js/flowplayer/flowplayer-3.1.5.swf",{
        clip:{autoPlay:false}
    });
};
kyani.load.dashboard = function(){
    $.ajaxSetup({async: false});
    $.getScript('/assets/js/dashboard.js');
    $.ajaxSetup({async: true});
}
