/**
 * @author masa
 */
$(function(){
    // big menu is locimg variable
    locimg = locimg || "";
    var host = location.href.match(/^http[s]*:\/\/[a-zA-Z0-9.\-]+\//)[0];
    var headtmp = host + "/header_template.html";
    var header = $('#header');
    
    $('img').each(function(){
        if($(this).attr('src').match('_off')){
            var img = $(this);
            // if off, set hover event
            img.hover(function(){
                if($(this).attr('src').match('_off')){
                $(this).attr("src", $(this).attr("src").replace("_off.", "_on."));
            }},function(){
                if($(this).attr('src').match('_on')){
                    jQuery(this).attr("src", jQuery(this).attr("src").replace("_on.", "_off."));
                }
            });
        }
    });

    $.get(headtmp,function(response){
        header.html(response);
        var menus = $(".headmenu > li");
        if(locimg){
            menus.each(function(i,el){
                if(locimg != $(this).attr('id')){
                    $(this).hover(function(){
                        var src = $('img:eq(0)', this).attr('src');
                        $('img:eq(0)', this).attr('src', src.replace('_off', '_on'));
                        if ($('.submenu-bg', el).length) {
                            $('.submenu-bg', el).show();
                        }
                    },function(){
                        var src = $('img:eq(0)', this).attr('src');
                        $('img:eq(0)', this).attr('src', src.replace('_on', '_off'));
                        if ($('.submenu-bg', el).length) {
                            $('.submenu-bg', el).hide();
                        }
                    });
                } else {
                    var src = $('img:eq(0)', this).attr('src');
                    $('img:eq(0)', this).attr('src', src.replace('_off', '_on'));
                    $(this).hover(function(){
                        if ($('.submenu-bg', el).length) {
                            $('.submenu-bg', el).show();
                        }
                    },function(){
                        if ($('.submenu-bg', el).length) {
                            $('.submenu-bg', el).hide();
                        }
                    });
                }
            });
        } else {
            menus.each(function(i, el){
                $(el).mouseover(function(){
                    var src = $('img:eq(0)', this).attr('src');
                    $('img:eq(0)', this).attr('src', src.replace('_off', '_on'));
                    if ($(".submenu-bg", el).length) {
                        $(".submenu-bg", el).show();
                    }
                });
                $(el).mouseout(function(){
                    var src = $('img:eq(0)', this).attr('src');
                    $('img:eq(0)', this).attr('src', src.replace('_on', '_off'));
                    if ($(".submenu-bg", el).length) {
                        $(".submenu-bg", el).hide();
                    }
                });
            });
        }
    });
	
    if($("#footer").length){
        var foottmp = host + "/footer_template.html";
        var footer = $('#footer');
        if (navigator.userAgent.match(/iPad|iPhone/)) {
            footer.css({ position: 'relative' });
        }
        $.get(foottmp,function(response){
            footer.html(response);
        });
    }		
});

