function isArray2(obj) {
    if(obj.constructor.toString().indexOf("Array") == -1)return false;
    else return true;
}

function str_replace2(search, replace, subject){
    if(isArray2(subject)){
        return subject[0].replace(new RegExp(search,"gi"), replace);
    }else{
        return subject.replace(new RegExp(search,"g"), replace);
    }
}


if(window.$j===undefined){
    var $j = jQuery.noConflict();
}

$j(document).ready(function() {
    $j(".ikodu_group_link").hover(function(){
        $j(".ikodu_group_link").each(function(){
            $j(this).children("h1").hide();
            $j(this).children("img").attr("src",function(){
                return str_replace2('active','passive',$j(this).attr("src"));
            });            
        });        
        $j(this).children("img").attr("src",function(){
                return str_replace2('passive','active',$j(this).attr("src"));
        });
        $j(this).children("h1").show();        
    },function(){
        $j(".ikodu_group_link").each(function(){
            $j(this).children("h1").hide();
            $j(this).children("img").attr("src",function(){
                return str_replace2('active','passive',$j(this).attr("src"));
            });            
        });
        $j(".ikodu_center").find(".active").children(".ikodu_label").show();        
        $j(".ikodu_center").find(".active").children("img").attr('src',function(){
            return str_replace2('passive','active',$j(this).attr("src"));
        });
    });   
    checkLeftMargin();
});
function checkLeftMargin(){
    $j(".ikodu_label").each(function(){
        var w = parseInt($j(this).width());
        var m = 17-(w/2);
        if(m > -200){
            if($j.browser.msie){
                if($j("body#wp_body").length){
                    $j(this).css({'margin-top':39+'px','top':39+'px'});
                }else if($j("body#tv_body").length){
                    $j(this).css({'margin-top':34+'px','top':34+'px'});
                }else if($j("body#phpbb").length){    
                    $j(this).css({'margin-top':36+'px','top':36+'px'});
                }else{                   
                   $j(this).css({'margin-top':38+'px','top':38+'px'});
                }
                m = m - 35;
            }else if($j.browser.mozilla){
                if($j("body#wp_body").length){
                    $j(this).css({'margin-top':39+'px','top':39+'px'});
                }else if($j("body#tv_body").length){
                    $j(this).css({'margin-top':34+'px','top':34+'px'});
                }else if($j("body#phpbb").length){    
                    $j(this).css({'margin-top':36+'px','top':36+'px'});
                    $j("#site-description").find("img").height(107);
                }
            }            
            $j(this).css({'margin-left':m,'opacity':1});
        }else{
            setTimeout(checkLeftMargin,100);
        }
    });
}


function handle_front_page_highlight(){
    var module_list = new Array;
    var total_list_height = 50;
    $j(".module_content").parent("div.module").each(function(i){
        var id = $j(this).attr("id");
        id = id.split("module_")[1];
        module_list[i] = new Object;
        module_list[i].id = id;
        
        var rh = $j(this).height();
        if(id=="ikodu_tv"){
            rh = 691;
        }
        module_list[i].height = rh;
        total_list_height += rh;
        module_list[i].fromTop = total_list_height;
        id = '#module_navigation__'+id;

        $j(this).hover(function(){
            $j(".ikodu_group_link").each(function(){
                $j(this).children("h1").hide();
                $j(this).children("img").attr("src",function(){
                    return str_replace2('active','passive',$j(this).attr("src"));
                });            
            });
            
            
            $j(id).children("img").attr("src",function(){
                return str_replace2('passive','active',$j(this).attr("src"));
            });
            $j(id).children("h1").show();
            
            $j(".module_content").parent("div.module.active").css("background-color","#EEE");            
            $j(this).css("background-color","#FFF");
        },function(){
            $j(".ikodu_group_link").each(function(){
                $j(this).children("h1").hide();
                $j(this).children("img").attr("src",function(){
                    return str_replace2('active','passive',$j(this).attr("src"));
                });            
            });
            $j(".ikodu_center").find(".active").children(".ikodu_label").show();        
            $j(".ikodu_center").find(".active").children("img").attr('src',function(){
                return str_replace2('passive','active',$j(this).attr("src"));
            });
            $j(this).css("background-color","#EEE");
            $j(".module_content").parent("div.module.active").css("background-color","#FFF");
        });    
    });
    
    $j(window).scroll(function(){
        var selected = 0;
        var last_active = $j(".module_content").parent("div.module.active");
        var screen_middle_from_top = document.viewport.getScrollOffsets().top + (document.viewport.getHeight())/2;
        $j(".ikodu_group_link").each(function(){
                $j(this).removeClass("active");
                $j(this).children("h1").hide();
                $j(this).children("img").attr("src",function(){
                    return str_replace2('active','passive',$j(this).attr("src"));
                });            
        });
        var adjust = -150;
        if ( $j.browser.msie ) {
            adjust = -200;
        }
        $j.each(module_list,function(i){
            $j('#module_'+$(this).id).removeClass("active").css("background-color","#EEE");
            
            if(screen_middle_from_top - ($(this).height/2) < (parseInt($(this).fromTop)+parseInt(adjust)) && selected==0){
                $j('#module_'+$(this).id).addClass("active").css("background-color","#FFF");
                $j('#module_navigation__'+$(this).id).children("img").attr("src",function(){
                    return str_replace2('passive','active',$j(this).attr("src"));
                });
                $j('#module_navigation__'+$(this).id).addClass("active");
                $j('#module_navigation__'+$(this).id).children("h1").show();
                selected = 1;
            }
        });
        if(!selected){
            last_active.addClass("active").css("background-color","#FFF");
        }
    });
}
