var div_mainSubNav = 0;

function Show_MainNavSub(tb,css,current_page)
{
    var temp_page = current_page.split("/");
    current_page = temp_page[0];
    
    $('main_td_'+tb).className=css;
    clearTimeout(div_mainSubNav);
    close_others_menu_sub_menu(tb,current_page);
    resize_main_menu_Layer(tb);
    $('table_'+tb).show();
    $('mainNavSub_menu').show();
}

function Close_MainNavSub(tb,css)
{
    //close_sub_menu(tb);
    div_mainSubNav = setTimeout("close_sub_menu('"+tb+"','"+css+"')",500);
}
function close_sub_menu(tb,css) {
    // for change mome pic start
    change_default_home_pic(true);
    // for change mome pic end
    
    $('main_td_'+tb).className=css;
    $('mainNavSub_menu').hide();
    $('table_'+tb).hide();
}

function resize_main_menu_Layer(tb)
{
    var obj = $('mainNav_td').positionedOffset();
    var top = obj.top+33;
    var left = obj.left;
    //$('mainNavSub_menu').setStyle({left:left+"px",top:top+"px"});


    var obj2 = $('main_td_'+tb).positionedOffset();
    var left2 = obj2.left;
    /*
    // for over the width start
    var main_width = $('mainNav_td').getWidth();
    var sub_width = $('mainNavSub_menu').getWidth();    
    if (left2 + sub_width > main_width) {
        left2 = main_width - sub_width;        
    }
    // for over the width end
    */
    $('mainNavSub_menu').setStyle({left:left2 + left+"px",top:top+"px"});
    
}

function close_others_menu_sub_menu(tb,current_page) {
    for (var i=0; i < all_menu_tb.length; i++) {
        $('table_'+all_menu_tb[i]).hide();
        if (all_menu_tb[i] != tb && all_menu_tb[i] != current_page) {
            $('main_td_'+all_menu_tb[i]).className=all_menu_normal_css[i];
        }
    }
}


function change_home_pic(main_id) {

    if ($('home_menu_image') != null) {
        var pic_obj = $('home_img_path_'+main_id);

        var blend_pic = "";
        if (pic_obj.value != "") {
            var preload = new Image();
            //preload.src = "/autoresize.aspx?iP="+pic_obj.value+"&size_w=565&size_h=450";
            preload.src = pic_obj.value;
    
            blend_pic = preload.src;
            //$('home_menu_image').width=preload.width;
            //$('home_menu_image').height=preload.height;
        } else {
            blend_pic = "/templates/default/en/images/spacer.gif";
            //$('home_menu_image').width='565';
            //$('home_menu_image').height='450';
        }
    
        change_blend_img('home_menu_image',blend_pic,true);
        $('current_default_home_img').value = main_id;
    }
    
}

function change_default_home_pic(change) {

    if (change == true) {
        if ($('home_menu_image') != null) {
            var pic_obj = $('default_home_img');

            var blend_pic = "";
            if (pic_obj.value != "") {
                var preload = new Image();
                //preload.src = "/autoresize.aspx?iP="+pic_obj.value+"&size_w=565&size_h=450";
                preload.src = pic_obj.value;
        
                blend_pic = preload.src;
                //$('home_menu_image').width=preload.width;
                //$('home_menu_image').height=preload.height;
            } else {
                blend_pic = "/templates/default/en/images/spacer.gif";
                //$('home_menu_image').width='565';
                //$('home_menu_image').height='450';
            }
    
            change_blend_img('home_menu_image',blend_pic,true);
            $('current_default_home_img').value = "home_pic";
        }
    }
    
}

function check_current_home_img(main_id) {

    if ($('home_menu_image') != null) {
        if ($('current_default_home_img').value != main_id) {
            change_home_pic(main_id);
        }
    }

}


var isf = { 'clock' : null, 'fade' : true, 'count' : 1 , 'speed' : 1}
function change_blend_img(target_id,pic,new_start) {

    if (new_start == true) {
        clearInterval(isf.clock);
        isf.clock=null;
        isf.fade = true;
    }
    
    c_img = document.getElementById(target_id);

	if(isf.clock == null)
	{
		if(typeof c_img.style.opacity != 'undefined')
		{
			isf.type = 'w3c';
		}
		else if(typeof c_img.style.MozOpacity != 'undefined')
		{
			isf.type = 'moz';
		}
		else if(typeof c_img.style.KhtmlOpacity != 'undefined')
		{
			isf.type = 'khtml';
		}
		else if(typeof c_img.filters == 'object')
		{
			isf.type = (c_img.filters.length > 0 && typeof c_img.filters.alpha == 'object' && typeof c_img.filters.alpha.opacity == 'number') ? 'ie' : 'none';
		}
		else
		{
			isf.type = 'none';
		}
		

		//if any kind of opacity is supported
		if(isf.type != 'none')
		{
			isf.length = isf.speed * 10;
			
			isf.resolution = 5;

			//start the timer
			isf.clock = setInterval("change_blend_img('"+target_id+"','"+pic+"',false)", isf.length/isf.resolution);
		}
		
		//otherwise if opacity is not supported
		else
		{
            c_img.src = pic;
		}
		
	} // if clock
	else 
	{
	    //increase or reduce the counter on an exponential scale
	    isf.count = (isf.fade) ? isf.count * 0.8 : (isf.count * (1/0.8)); 
	

	    //if the counter has reached the bottom
	    if(isf.count < (1 / isf.resolution))
	    {
    		//clear the timer
		    clearInterval(isf.clock);
		    isf.clock = null;
    
    	    c_img.src = pic;

		    //reverse the fade direction flag
		    isf.fade = false;

		    //restart the timer
		    isf.clock = setInterval("change_blend_img('"+target_id+"','"+pic+"',false)", isf.length/isf.resolution);

	    }
	
	    //if the counter has reached the top
	    if(isf.count > (1 - (1 / isf.resolution)))
	    {
    		//clear the timer
		    clearInterval(isf.clock);
		    isf.clock = null;
    
		    //reset the fade direction flag
		    isf.fade = true;
    		
		    //reset the counter
		    isf.count = 1;
	    }

	    //set new opacity value on element
	    //using whatever method is supported

	    switch(isf.type)
	    {
    		case 'ie' :
			    c_img.filters.alpha.opacity = isf.count * 100;
			    break;
    			
		    case 'khtml' :
    			c_img.style.KhtmlOpacity = isf.count;
			    break;
    			
		    case 'moz' : 
    			//restrict max opacity to prevent a visual popping effect in firefox
			    c_img.style.MozOpacity = (isf.count == 1 ? 1.0 : isf.count);
			    break;
    			
		    default : 
    			//restrict max opacity to prevent a visual popping effect in firefox
			    c_img.style.opacity = (isf.count == 1 ? 1.0 : isf.count);
	    }
	}
    
} // end change
