$(document).ready(function(){
    /*var rezin;
	if($('#right_area').height()>$('#center_area').height()){
		rezin = $('#right_area').height();
	}else{
		rezin = $('#center_area').height();
	}*/
    window.setInterval(
	    function(){
		    rezin = $('#right_area').height();
		    $('#center_area').height(rezin-74);
	        $('#left_bottom').height(rezin-$('#left_top').height()-17);
		},
	1000); 
});


function opensubmenu( obj, num )
{
	showSubmenu( num );
//	alert( document.getElementById( "top_" + num ).offsetLeft );
	obj.className = obj.className + " active_menu" ; 
	if( document.getElementById( "div_sub_menu_" + num ) != null )
		document.getElementById( "div_sub_menu_" + num ).style.visibility = "visible" ;
//	setsubMenuSize( obj, num, true );
	LichangeOpenStyle( num );
}
function LichangeOpenStyle( num )
{
	var objleft = document.getElementById( "root_" + num + "_0" );
	var objcenter = document.getElementById( "root_" + num + "_1" );
	var objright = document.getElementById( "root_" + num + "_2" );
	objleft.className = objleft.className + " active_left_1";
	objcenter.className = objcenter.className + " active_left_1";
	objright.className = objright.className + " active_left_1";
}
function closesubmenu( obj, num )
{
/*
	if( obj.className.indexOf( "active_menu" ) )
	{
		obj.className = obj.className.split( "active_menu" )[0] ; 
	}
	LichangeCloseStyle( num );
*/
}
function LichangeCloseStyle( num )
{
	var objleft = document.getElementById( "root_" + num + "_0" );
	var objcenter = document.getElementById( "root_" + num + "_1" );
	var objright = document.getElementById( "root_" + num + "_2" );
	if( objleft.className.indexOf( "active_left_1" ) )
	{
		objleft.className = objleft.className.split( "active_left_1" )[0];
		objcenter.className = objcenter.className.split( "active_left_1" )[0];
		objright.className = objright.className.split( "active_left_1" )[0];
	}
	
}
var INTERVALTIME = 8000;
var INTERVALID = null;
var imgArray = new Array();
var cpos = 0;
var count = 7; 

function intialize()
{
	cpos = 0;
	setBorderSize();
//	setsubMenuSize( null, 1, false );//sub menu filtering and set size of every div and table and td.
	setMenusizeInitial( null, 1 );
	setMenusizeInitial( null, 2 );
	setMenusizeInitial( null, 3 );
	setMenusizeInitial( null, 4 );
	setMenusizeInitial( null, 5 );

	eventDefinition();
}
function setTimer()
{
	intialize();
	this.INTERVALID = setInterval( FadeIn, this.INTERVALTIME );
}
// Timer for set opacity of image to zero
function setCurrentPos( )
{
	if( this.cpos < count -1 )
	{
		this.cpos += 1; 
	}
	else
	{
		this.cpos = 0;
	}
}

function FadeOut()
{

//	var element = $( "slidecontainer" );
//	removeChildObj( element );
	setCurrentPos();
	var curid = "#slide_" + cpos;
	document.getElementById( "slide_" + cpos ).style.display = "block";
	  $( curid ).animate({
			opacity:1	 
	  }, 2000, function() {
	  });
}

function FadeIn()
{
	var element = $( "slidecontainer" );
	var curid = "#slide_" + cpos;
	document.getElementById( "slide_" + cpos ).style.display = "block";
/*
	new Effect.Appear( element, {
				duration: 1.5
				,from:1
				,to: 0
			  });	
*/
  $( curid ).animate({
		opacity:0	 
  }, 2000, function() {
		this.style.display = "none";
  });
	FadeOut();
/*
	{
		this.DELETEID = setTimeout( FadeOut, 100 );
	}
*/
}
function ClearTimeout()
{
	if( this.STARTID != null )
	{
		clearTimeout( this.STARTID );
	}
}
function removeChildObj( obj )
{
	while (  obj.hasChildNodes() ) 
	{
		obj.removeChild( obj.lastChild );
	}
}
function setBorderSize()
{
	var centerobj = document.getElementById( "center_area" );
	var leftobj = document.getElementById( "left_area" );
	var left_bottom_obj = document.getElementById( "left_bottom" );
	var left_top_obj = document.getElementById( "left_top" );
	var rightobj = document.getElementById( "right_area" );
	var content_footerobj = document.getElementById( "content_footer_img" );
	var footer_txtobj = document.getElementById( "footer_txt" );
	var centersize = centerobj.offsetHeight;
	var leftsize = leftobj.offsetHeight;
	var rightsize = rightobj.offsetHeight;
	var bigger = getmaxLength(centersize, leftsize, rightsize );

	rightobj.style.height = bigger + footer_txtobj.offsetHeight + footer_txtobj.offsetHeight + "px";
	left_bottom_obj.style.height = bigger + footer_txtobj.offsetHeight + footer_txtobj.offsetHeight - left_top_obj.offsetHeight - 16 + "px";
	centerobj.style.height = bigger + "px";

}
function getmaxLength( a, b, c )
{
	var bigger = a;
	if( bigger < b )
	{
		bigger = b;
		if( bigger < c )
		{
			bigger = c; 
		}
	}
	return bigger;
}

function setMenusizeInitial( obj, num )
{
	var pliobj = document.getElementById( "top_" + num );
	var subobj = document.getElementById( "div_sub_menu_" + num );
	var leftrightwidth = 24;// most left and right
	var offset = 3; // left offset of li background;
	var leftWidth =  document.getElementById( "menu_left" ).offsetWidth;
	var rightWidth = document.getElementById( "menu_right" ).offsetWidth;
	var rightLeft = document.getElementById( "menu_right" ).offsetLeft;
	var leftoffset = 67; 
	var contentWidth = rightWidth + rightLeft;//1024px
	if( subobj != null )
	{
		var submenuobj = document.getElementById( "sub_menu_" + num );
		var submenuwidth = getsubMenuLength( submenuobj, "li" );
		var liobjwidth =  pliobj.offsetWidth;
		var subconobj = document.getElementById( "drop_table_0_" + num );//submenu top container( for decoration )
		var subconobj_btm = document.getElementById( "drop_table_1_" + num );//submenu container
		subconobj.style.width = submenuwidth + leftrightwidth + "px" ;// set table width.
		subconobj_btm.style.width = submenuwidth + leftrightwidth + "px" ;
		var conWidth = subconobj.offsetWidth;
		var realCotentWidth = submenuwidth + leftrightwidth ;
		var submenuleftobj = document.getElementById( "top_left_1_" + num );// second most left
		var submenurightobj = document.getElementById( "top_left_3_" + num );// second most left
		var whitebkobj = document.getElementById( "top_left_2_" + num );
		whitebkobj.style.width = pliobj.offsetWidth - 7 + "px";// set white bk length here 7 means li tag border.
		var whitebkwidth = whitebkobj.offsetWidth;
		var leftorange = parseInt( (realCotentWidth - whitebkwidth - leftrightwidth)/2 );
		var leftorangeWhole = (realCotentWidth - whitebkwidth - leftrightwidth)/2;

		var offsetLeft = Math.abs( pliobj.offsetLeft - document.getElementById( "menu_left" ).offsetWidth );
		var offsetRight = contentWidth - pliobj.offsetLeft  - pliobj.offsetWidth - rightWidth - leftWidth - leftoffset; 
//normal action
		if( (conWidth - whitebkwidth - leftrightwidth)%2 == 0 )
		{
			submenuleftobj.style.width = parseInt( (conWidth - whitebkwidth - leftrightwidth)/2 )  + "px"; 
			submenurightobj.style.width = parseInt( (conWidth - whitebkwidth - leftrightwidth)/2 )  + "px";
		}
		else
		{
			submenuleftobj.style.width = parseInt( (conWidth - whitebkwidth - leftrightwidth)/2 ) + 1 + "px"; 
			submenurightobj.style.width = parseInt( (conWidth - whitebkwidth - leftrightwidth)/2 ) - 1 + "px";
		}
//innormal action 
		if( (leftorange + leftrightwidth/2) > offsetLeft && (leftorange + leftrightwidth/2) < offsetRight )
		{
			submenuleftobj.style.width = leftorange - offsetLeft - leftrightwidth/2  + "px"; 
			submenurightobj.style.width = 2 * leftorange - submenuleftobj.offsetWidth + "px";
//if right background larger than left position 
			if( submenurightobj.offsetWidth > offsetRight )
			{
				submenuleftobj.style.width = pliobj.offsetLeft  - leftrightwidth/2 + "px";
				submenurightobj.style.width = 2 * leftorangeWhole - submenuleftobj.offsetWidth + "px";
			}
			else//if right background larger than left position 
			{
				submenuleftobj.style.width = pliobj.offsetLeft  - leftrightwidth/2 + "px";
				submenurightobj.style.width = 2 * leftorangeWhole - submenuleftobj.offsetWidth + "px";
			}
		}
		else if( (leftorange + leftrightwidth/2) > offsetRight )
		{
			if( num != 5 )
			{
				submenurightobj.style.width = offsetRight - leftrightwidth/2 + "px";
				submenuleftobj.style.width = 2 * leftorangeWhole - submenurightobj.offsetWidth + "px"; 
			}
		}
		if( num == 5 )
		{
			submenurightobj.style.width =  0 + "px";
			submenuleftobj.style.width = 2 * leftorangeWhole - submenurightobj.offsetWidth + "px"; 
		}
//		if( num != 5 )
			subobj.style.marginLeft = -1 * (whitebkobj.offsetLeft) + offset  + "px";
//		else if( num == 5 )
//			subobj.style.left = -200 + "px";
//			subobj.style.marginLeft = -390 + "px";
		
/*
		var liobjwidth =  pliobj.offsetWidth;
		var subconobj = document.getElementById( "drop_table_" + num );//submenu container
		var childtopobj = document.getElementById( "drop_table_0_" + num );//submenu top table container
		subconobj.style.width = submenuwidth + leftrightwidth + "px" ;// set table width.
		var realCotentWidth = submenuwidth + leftrightwidth ;
//alert( submenuwidth +  "*" + subconobj.offsetWidth );
		var submenuleftobj = document.getElementById( "top_left_1_" + num );// second most left
		var submenurightobj = document.getElementById( "top_left_3_" + num );// second most left
		var whitebkobj = document.getElementById( "top_left_2_" + num );
		childtopobj.style.width  = submenuwidth + leftrightwidth + "px" ;

		whitebkobj.style.width = pliobj.offsetWidth - 7 + "px";// set white bk length here 7 means li tag border.
		var whitebkwidth = whitebkobj.offsetWidth;
		var leftorange = parseInt( (realCotentWidth - whitebkwidth - leftrightwidth)/2 );
		var leftorangeWhole = (realCotentWidth - whitebkwidth - leftrightwidth)/2;
//		submenuleftobj.style.width = parseInt( (childtopobj - whitebkwidth - leftrightwidth)/2 )  + "px"; 
//      submenurightobj.style.width = parseInt( (childtopobj - whitebkwidth - leftrightwidth)/2 )  + "px";; 
//		alert( submenurightobj.offsetWidth );
		var offsetLeft = Math.abs( pliobj.offsetLeft - document.getElementById( "menu_left" ).offsetWidth );
		var offsetRight = contentWidth - pliobj.offsetLeft  - pliobj.offsetWidth - rightWidth - leftWidth - leftoffset; 
//		alert( offsetRight );
		var lengthtoRight  = pliobj.offsetLeft  + pliobj.offsetWidth + leftorange + leftrightwidth/2;//right side.
//		var offsetRight = Math.abs( pliobj.offsetLeft - document.getElementById( "menu_left" ).offsetWidth );
	//		var offsetRight = 
//		alert( leftorange + leftrightwidth/2 + "*"  +  offsetRight );
		if( (leftorange + leftrightwidth/2) > offsetLeft && (leftorange + leftrightwidth/2) < offsetRight )
		{
			submenuleftobj.style.width = leftorange - offsetLeft - leftrightwidth/2  - 10 + "px"; 
			submenurightobj.style.width = 2 * leftorange - submenuleftobj.offsetWidth + "px";
			if( submenurightobj.offsetWidth > offsetRight )
			{
//		alert( ( realCotentWidth - whitebkwidth - leftrightwidth ) + "*" + ( pliobj.offsetLeft - leftrightwidth/2 ));
				submenuleftobj.style.width = pliobj.offsetLeft  - leftrightwidth/2 + 3 - 30 + "px";
//				submenurightobj.style.width = 2 * leftorangeWhole - submenuleftobj.offsetWidth - leftrightwidth + "px";
//				submenurightobj.style.width = submenuwidth - whitebkwidth - submenuleftobj.offsetWidth + 4  + "px";
//alert( ( pliobj.offsetLeft  - leftrightwidth/2 + 3 + whitebkwidth +  submenuwidth - whitebkwidth - submenuleftobj.offsetWidth + 4) + "*" +  submenuwidth );
alert( submenuleftobj.offsetWidth + "*" + ( pliobj.offsetLeft  - leftrightwidth/2 + 3 ) );

//				alert( pliobj.offsetLeft - leftrightwidth/2 + 2 * leftorangeWhole - submenuleftobj.offsetWidth - leftrightwidth + "" + 2 * leftorangeWhole);
//				alert( submenuleftobj.offsetWidth + submenurightobj.offsetWidth + "*" +  leftorange * 2 );
			}
		}
		else
		{
		}
//		else if( (leftorange + leftrightwidth/2) < offsetLeft )
		if( parseInt(submenuwidth - liobjwidth - leftrightwidth)%2 == 0 )
			subobj.style.marginLeft = -1 * (whitebkobj.offsetLeft) + offset  + "px";
		else
		{
			subobj.style.marginLeft = -1 * (whitebkobj.offsetLeft) + offset + "px";
		}
*/
	}

}
function setsubMenuSize( obj, num, flag )
{
//	alert( document.getElementById( "top_" + num ).offsetWidth ) ;
	var pliobj = document.getElementById( "top_" + num );
	var subobj = document.getElementById( "div_sub_menu_" + num );
	var leftrightwidth = 24;// most left and right
	var offset = 3; // left offset of li background;
	if( subobj != null )
	{
		if( flag )
		{
			subobj.style.visibility = "visible";
		}
		var submenuobj = document.getElementById( "sub_menu_" + num );
		var submenuwidth = getsubMenuLength( submenuobj, "li" );;
		var liobjwidth =  pliobj.offsetWidth;
		var subconobj = document.getElementById( "drop_table_" + num );//submenu container
		var childtopobj = document.getElementById( "drop_table_0_" + num );//submenu top table container
		var submenuleftobj = document.getElementById( "top_left_1_" + num );// second most left
		var submenurightobj = document.getElementById( "top_left_3_" + num );// second most left
		var whitebkobj = document.getElementById( "top_left_2_" + num );

		subconobj.style.width = submenuwidth + leftrightwidth + "px" ;// set table width.
		childtopobj.style.width  = submenuwidth + leftrightwidth + "px" ;
//		alert( subconobj.offsetWidth + "#" + submenuwidth );
		whitebkobj.style.width = pliobj.offsetWidth - 7 + "px";// set white bk length here 7 means li tag border.
		var whitebkwidth = whitebkobj.offsetWidth;
		var leftorange = parseInt( (childtopobj.offsetWidth - whitebkwidth - leftrightwidth)/2 );
		submenuleftobj.style.width = parseInt( (childtopobj - whitebkwidth - leftrightwidth)/2 )  + "px"; 
		submenurightobj.style.width = parseInt( (childtopobj - whitebkwidth - leftrightwidth)/2 )  + "px";; 
/*size control again */
		var offsetLeft = Math.abs( pliobj.offsetLeft - document.getElementById( "menu_left" ).offsetWidth );
//		var offsetRight = 

		if( (leftorange + leftrightwidth/2) > offsetLeft )
		{
			submenuleftobj.style.width = leftorange - offsetLeft - leftrightwidth/2  - 10 + "px"; 
//			submenurightobj.style.width = 2 * leftorange - offsetLeft + leftrightwidth/2 + 10 + "px";
			submenurightobj.style.width = 2 * leftorange - submenuleftobj.offsetWidth + "px";
		}
//		alert( submenuleftobj.offsetWidth + submenurightobj.offsetWidth + "*" +  leftorange * 2 );

		if( flag )
		{
			if( parseInt(submenuwidth - liobjwidth - leftrightwidth)%2 == 0 )
				subobj.style.marginLeft = -1 * (whitebkobj.offsetLeft) + offset  + "px";
			else
			{
				subobj.style.marginLeft = -1 * (whitebkobj.offsetLeft) + offset + "px";
			}
		}
	}

}

function getsubMenuLength( obj, tagname )
{
	var childobj = obj.parentNode.getElementsByTagName( tagname );
	var len = 0;
	for( var i = 0; i < childobj.length; i ++ )
	{
		len += childobj[i].offsetWidth; 
	}
	return len;
}
function closeSubmenu( num )
{
	var submenuobj = document.getElementById( "div_sub_menu_" + num  );
	if( submenuobj != null )
	{
		submenuobj.style.display = "none";
	}
}
function showSubmenu( num )
{
	var submenuobj = document.getElementById( "div_sub_menu_" + num  );
	if( submenuobj != null )
	{
		submenuobj.style.display = "block";
	}
}
var isLiEnter = false;
var isAEnter = false;
function eventDefinition()
{
	$('#div_sub_menu_1')
		.mouseout(function(){
//			document.getElementById( "div_sub_menu_1" ).style.display = "block";
		});
	$('#menu_content > li')
		.hover(function(){
//			document.getElementById( "div_sub_menu_1" ).style.visibility = "visible";
		}, function(){

			var num = this.id.split( "_" )[1];
//			alert( "#" + this.id + " >  a");
			var id = "root_" + num + "_1";
			var obj = $( "#" + id + " > a"  )[0];
			var subobj = document.getElementById( "div_sub_menu_" + num );
			if( obj.className.indexOf( "active_menu" ) )
			{
				obj.className = obj.className.split( "active_menu" )[0] ; 
			}
			LichangeCloseStyle( num );
			if( subobj != null )
				subobj.style.display = "none";
		}); 
}