var BrAgent = navigator.userAgent.toLowerCase();

var mainNavi = {
	mnObj : null,
	mnItems:new Array(),
	currentSeq:null,currentSeq2:null,
	initSeq:null,initSeq2:null,
	Timer:null,Time2:null,
	isOver:false,isOver2:false,
	init:function(objId,seq,seq2){

		
		var this_s  = this;
		this.mnObj = $("#" + objId);
		this.mnItems = $("> li > a ",$(this.mnObj)) ;		

		this.mnItemsImg = $("> li > a  img ",$(this.mnObj)) ;

		//이미지 로딩이 완료되었는지 체크 후 재 로드(사파리,크롬에서 이미지 로딩되기 전에 사이즈 계산하는 오류)
		if(BrAgent.indexOf("msie 6")!=-1){
			//ie6일때는 오버효과 빼버릴까..ㅠㅠ		
			this.ie6 = true;
		}else{
			this.ie6=false;
			
			var chkImgLoad = true;
			for (i=0;i<this.mnItemsImg.length ;i++ )
			{
				if(!this.mnItemsImg[i].complete) chkImgLoad = false;
			}
			if(!chkImgLoad) {this.Loader = setTimeout("mainNavi.init('"+objId+"',"+seq+","+seq2+")",50);return;}
			else{
				clearTimeout(this.Loader);
			}
			
		}

		
		this.initSeq = seq;
		this.initSeq2 = seq2;
		

		$("a",$(this.mnObj)).bind("mouseover focus", function(){	 this_s.isOver = true;	});
		$("> li > ul",$(this.mnObj)).bind("mouseover focus", function(){	 this_s.isOver = true;	});

		$("a",$(this.mnObj)).bind(" blur", function(){	this_s.setMenuOut();	});
		$("> li > ul",$(this.mnObj)).bind(" blur", function(){	this_s.setMenuOut();	});


		//2차메뉴 오버 액션
		var mnSubItems = $(".mn_a2",$(this.mnObj));

		//대메뉴 설정 
		for (var i=0; i < this.mnItems.length ; i++ ){
			this.mnItems[i].seq = i + 1;
			$(this.mnItems[i]).bind("mouseover focus",function(){ this_s.setMenuOn(this.seq);})

			//2차메뉴 설정
			var subMn = $("> li:eq("+i+") > ul > li >a ",$(this.mnObj)) ;		//$(":parent .mn_a2",this.mnItems[i]);
			for (var j=0;j<subMn.length ;j++ )
			{
				$(subMn[j]).attr("seq", i+1);
				$(subMn[j]).attr("seq2",j+1);

				$(subMn[j]).bind("mouseover focus",function(){ 
					this_s.isOver2 = true;
					this_s.setSubMenuOn($(this).attr("seq"),$(this).attr("seq2")); 
				});
				$(subMn[j]).bind("mouseout blur",function(){ 

					this_s.setSubMenuOut(); 
				});
			}
			
			//alert(subMn.html());
		}

		if(this.initSeq<1) {
		
		}else{
			this.setMenuOn(this.initSeq,this.initSeq2);
		}

		
	},
	setMenuOn:function(){
		clearTimeout(this.Timer );
		var this_s = this;
		var s  = new Array();
		for(var i=0; i<arguments.length;i++){
			s[i] = arguments[i];	
		}
		
		var thisMenu = $("> li:eq(" + ( s[0]-1 ) +")", this.mnObj);
		var subMenu = $("> ul",$(thisMenu));
		var otherMenu = $("> li:not(:eq(" + ( s[0]-1 ) +"))", this.mnObj);
		var otherSubMenu = $("> ul",$(otherMenu));

		var thisMnMask = $("a.mn_a1 .ovMask", thisMenu);
		var otherMnMask = $("a.mn_a1 .ovMask", otherMenu);
		
		$(thisMenu).addClass("over");
		$(thisMnMask).show("fast");

		$(subMenu).slideDown();
		
		$(otherMenu).removeClass("over");
		$(otherSubMenu).hide("fast");
		
		$(otherMnMask).hide("fast");
		
//alert(s[1]);
		if(s[1]>0){this.setSubMenuOn(this.initSeq,s[1]); }
		this.currentSeq = s[0];

		
	},
	
	setMenuOut:function(){
		clearTimeout(this.Timer );
		var this_s = this;
		this.isOver = false;	
		this.Timer = setTimeout(function(){
			if(this_s.isOver==false) {
				this_s.setMenuOn(this_s.initSeq,this_s.initSeq2);
			}
		},400);
	},


	setSubMenuOn:function(){
		clearTimeout(this.Timer2 );
		var this_s = this;
		var s  = new Array();
		for(var i=0; i<arguments.length;i++){
			s[i] = arguments[i];	
		}
		
		//모든 서브메뉴들
		//var thisSubMenus  = $("> li:eq("+(this_s.currentSeq -1)+") > ul > li > a",$(this.mnObj)) ;	
		var thisSubMenus  = $("> li > ul > li > a",$(this.mnObj)) ;	
		for (var i=0;i< thisSubMenus.length; i++)
		{
			var seq = $(thisSubMenus[i]).attr("seq");
			var seq2 = $(thisSubMenus[i]).attr("seq2");
			if(seq2==s[1] && seq==s[0])  $(".ovMask",$(thisSubMenus[i])).show("fast");
			else $(".ovMask",$(thisSubMenus[i])).hide("fast");
		}
		this.currentSeq2 = s[1];
		
		
	},
	
	setSubMenuOut:function(){
		clearTimeout(this.Timer );
		var this_s = this;
		this.isOver2 = false;	
		this.Timer = setTimeout(function(){
			if(this_s.isOver2==false) {

				this_s.setSubMenuOn(this_s.initSeq, this_s.initSeq2);
			}
		},200);
	}
	

}

function initNavigation(seq,seq2) {

if(parseInt(seq)==0) seq = 1;
if(parseInt(seq2)==0) seq2 = 0;
//alert(parseInt(seq))

mainNavi.init("mainMenu",parseInt(seq),parseInt(seq2));

	

}



