jQuery.noConflict();

var navigation = function(target){
	var timer;
	var oldSub = -1;
	var curSub;
	var curImg;
	var oldImg;
	var curSubidx;
	var oldSubidx = -1;
	var curSubimg;
	var oldSubimg;
	var sub = jQuery(target+">li>ul");
	this.depth1 = jQuery(target+">li> a");
	this.depth2 = jQuery(target+">li>ul>li>a");

	this.run = function(){

		if (oldSub == -1){
			sub.hide();
		}
		
		this.depth1.bind("mouseover focus",function(){
			curSub= jQuery(target+">li> a").index(this);
			sub.eq(curSub).show();
			
			if(oldSub !=-1 && oldSub != curSub){
				sub.eq(oldSub).hide();
			}
			oldSub = curSub;
		});

		this.depth2.bind("mouseover focus",function(){
			curSubidx = jQuery(target+">li>ul>li>a").index(this);
			
			//if(oldSubidx !=-1 && curSubidx != oldSubidx){
			//}						
			oldSubidx = curSubidx;
		});
	}
}

var tooltip = function(target){

	var obj = jQuery(target);
	var tObj = jQuery(jQuery(obj).attr("href"));
	
	this.run = function(){
		
		obj.bind("mouseover focus", function(){		
			jQuery(tObj).fadeIn(300);
		});

		obj.bind("mouseout blur", function(){
			jQuery(tObj).fadeOut(10);
		});
	}
}

var tabnavi = function(target){
	var old=jQuery(target+" ul").eq(0);
	var cur;
	var curimg;
	var oldimg;
	var cidx;
	var oidx = -1;
	this.tabmenu = jQuery(target+" .title a");		
	
	this.run = function(){
		this.tabmenu.click(function(){
			cur = jQuery(this).attr("href");				
			curimg = jQuery("img:first",jQuery(this));				
			cidx = jQuery(this).parent().parent().index();
			
			curimg.attr("src", curimg.attr("src").replace("_off","_on"));						
			if(oidx != -1 && oidx !=cidx){
				oldimg = jQuery("img:first",jQuery(target+" .title a").eq(oidx));
				oldimg.attr("src", oldimg.attr("src").replace("_on","_off"));
			}
			if(oidx == -1 && cidx != 0){
				jQuery("img:first", jQuery(target+" .title").eq(0)).attr("src", jQuery("img:first", jQuery(target+" .title").eq(0)).attr("src").replace("_on","_off"));
			}
			if(jQuery(old) && old!=cur){
				jQuery(old).removeClass("open")
			}
			jQuery(cur).addClass("open");
			old=cur;
			oidx = cidx;
			return false;
		});
	}
}

var FAQ = function(o){

	var obj = jQuery(o);
	var oldObj = null;
	var question = "";

	jQuery(obj).each(function(){			
		this.link = jQuery(question+" a",this).eq(0);
		this.con = jQuery(jQuery(this.link).attr("href"));
	});

	this.run = function(){		
		
		jQuery(obj).each(function(){
			
			var thisObj = this;
			
			jQuery(this.link).bind("click",function(e){
				jQuery(this).addClass("bold");

				if(jQuery(thisObj.con).css("display") == "block"){
					jQuery(thisObj.con).hide();
					jQuery(this).removeClass("bold");
				} else {
					jQuery(thisObj.con).slideDown(80);
				}

				if(oldObj == thisObj){
					//jQuery(thisObj.con).hide();
				}

				oldObj = thisObj;
				e.preventDefault();
			});

		});
	};
}


var bannerClass = function(o){
	var thisObj = jQuery(o);
	var obj = jQuery(".list ul", o);
	var banner = jQuery("li", o);
	var bannerLink = jQuery("a", o);
	var len = banner.length;
	var btnNext = ".next";
	var btnPrev = ".prev";
	
	this.auto = true;
	this.loop = true;
	this.speed = 800;
	this.pause = 2000;
	this.clickspeed = 400;
	
	var auto, loop, speed , pause, clickspeed;

	//var w = banner.width()+parseInt(banner.css("margin-right").substr(0,1));
	var w = banner.width();

	var idx = 0;						//	current index
	var pidx = 0;					//	previous index
	var click = false;				//	click state
	var outInterval = 3000;	//	mouseout intervel time
	var mouseOutTimer = 0;
	var ismove = true;

	var timeout = 0;
	var over_timer = null;
	var out_timer = null;
	var overstate = false;

	this.start = function(){
		
		auto = this.auto;
		loop = this.loop;
		speed = this.speed;
		pause = this.pause;
		clickspeed = this.clickspeed;		

		auto == true ? ismove = true : ismove = false;

		if (w*len <= thisObj.width()) return false;
		jQuery(obj).css({width: w*(len)});		

		//if auto is ture, animation start
		if(auto){
			timer = setTimeout(function(){
				animate("next");
			}, pause);
		};		
		

		//banner control button bind click event
		//jQuery(btnNext, thisObj).bind("click","next", control);
		//jQuery(btnPrev, thisObj).bind("click","prev", control);

		jQuery(obj).hover(overEvt, outEvt);
	
	}	

	//banner animation
	animate = function(dir){

			var p = (w*-1);
			pidx = idx;
		
			switch(dir){
				case "next":
					idx = (pidx > len-1) ? (loop ? idx+1 : len-1) : idx+1;						
					break;
				case "prev":
					idx = (idx<=0) ? (loop ? idx-1 : Math.abs(len-idx)) : idx-1;
					break; 
				case "stop":
					idx = (idx ==0) ? 0 : idx-1;
					break; 
				case "last":
					idx = len-1;
					break; 
				default:
					idx = dir;
					break; 
			}

			//jsDebug(idx);

			var diff = Math.abs(pidx-idx);
			var sp = diff*speed;			

			clearTimeout(timeout);	
			
			//alert(diff+"/"+p); return false;			
			
			if ( dir == "prev" ){						
				jQuery(obj).prepend(jQuery("li:last-child", obj).clone());
				jQuery("li:last-child", obj).eq(0).remove();
				jQuery(obj).css("margin-left", p+"px");
				jQuery(obj).animate(
					{ marginLeft: 0 }, 
					{ queue:false, duration:sp, complete:scrollprev }
				);
			} else {
				jQuery(obj).append(jQuery("li:first-child", obj).clone());
				jQuery(obj).css({width: w*(len+1)});
				jQuery(obj).css("margin-left","0px");
				jQuery(obj).animate(
					{ marginLeft: p }, 
					{ queue:false, duration:sp, complete:bannerscroll }
				);
			}
			
			if(click) clearTimeout(timeout);

			//loop animation

			if(auto && dir=="next" && !click){
				timer = setTimeout(function(){
					animate("next");
				},pause+speed);
			};
			
	}

	//animation complete function
	bannerscroll = function (){		
		if (idx >= len) idx =0;		
		jQuery("li:first-child", obj).eq(0).remove();
		jQuery(obj).css("margin-left","0px");
		jQuery(obj).css({width: w*(len)});
		if (click) click= false;
	};


	//previous move function
	scrollprev = function (){
		if (idx >= len) idx =0;
		jQuery(obj).css("margin-left","0px");
		jQuery(obj).css({width: w*(len)});
		if (click) click = false;
		ismove = false;
	}

	//event function
	control = function (e){
		switch(e.data){
			
			case "start":
				if(!loop) loop = true;
				if(!auto) auto = true;		
				animate("next");
				return false;
				break;

			case "stop":
				if(loop) loop = false;
				if(auto) auto = false;
				animate("stop");
				return false;
				break;
			
			case "next":
				loop = false;
				click = true;
				animate(e.data);				
				return false;
				break;

			case "prev":
				loop = false;
				click = true;
				animate(e.data);		
				return false;
				break;

			default:
				return false;
				break;
		};

		e.preventDefault();
	}

	overEvt = function(e){
		if(ismove) ismove = false;
		click = true;
		loop = false;
		auto = false;
		//if(loop) loop = false;
		//if(auto) auto = false;							
		clearTimeout(timeout);
		//jQuery(obj).stop();
		return false;
	}

	outEvt = function(e){
		if (!ismove){	
			clearTimeout(mouseOutTimer);
			mouseOutTimer = setTimeout(function(){
				ismove = true;
				loop = true;
				auto = true;
				click= false;
				animate("next");
			}, outInterval);
			return false;
		}
	}
}

function setJsDebugger(){
	if(jQuery("#msgbox") !=null){
		jQuery("body").append("<div id='msgbox'></div>");	
		jQuery("#msgbox").css({'width':'200px', 'height':'400px', 'position':'absolute', 'left':'0','top':'0','overflow':'auto'});
		jQuery("#msgbox").css({'border':'2px solid #DDDDDD', 'background-color':'#FFFFFF', 'padding':'20px'});
	}
}

function jsDebug(msg){
	jQuery("#msgbox").html(msg+"<br />");
}

function setInputStyle(o){
	var obj = jQuery(o);
	var inp = jQuery("input:text,input:password", obj);
	
	jQuery(inp).each(function(){

		jQuery(this).bind("focus", function(){
			jQuery(this).css({'background-color':'#FFFBCF'});
		})

		jQuery(this).bind("blur", function(){
			jQuery(this).css({'background-color':'#FFFFFF'});
		})
		
	});
}

var tabContents = function(target){
	var old=jQuery(target+" li").eq(0);
	var cur;
	var curimg;
	var oldimg;
	var cidx;
	var oidx = -1;
	var objlist = jQuery("div.list", jQuery("#asso"));
	var gho = jQuery("div.generalhospital",  jQuery("#asso"));
	var hpo = jQuery("div.hospital",  jQuery("#asso"));
	var sco = jQuery("div.specialclinic",  jQuery("#asso"));
	this.tabmenu = jQuery(target+" li a",  jQuery("#asso"));

	this.run = function(){
		this.tabmenu.click(function(){
			cur = jQuery(this).attr("class");	
			cidx = jQuery(this).parent().index();

			if(oidx == -1 && cidx != 0){
				jQuery(target+" li").eq(cidx).addClass("over");
				jQuery(target+" li").eq(0).removeClass();
				jQuery(target).attr("class","ms"+cidx);

				if(cur=="all"){
					jQuery(objlist).show();
				} else if (cur=="generalhospital"){
					jQuery(gho).show();
					jQuery(hpo).hide();
					jQuery(sco).hide();
				} else if (cur=="hospital"){
					jQuery(gho).hide();
					jQuery(hpo).show();
					jQuery(sco).hide();
				} else if (cur=="specialclinic"){
					jQuery(gho).hide();
					jQuery(hpo).hide();
					jQuery(sco).show();
				}
			}

			if(oidx != -1 & oidx!=cidx){

				jQuery(target+" li").eq(cidx).addClass("over");
				jQuery(target+" li").eq(oidx).removeClass();
				jQuery(target).attr("class","ms"+cidx);

				if(cur=="all"){
					jQuery(objlist).show();
				} else if (cur=="generalhospital"){
					jQuery(gho).show();
					jQuery(hpo).hide();
					jQuery(sco).hide();
				} else if (cur=="hospital"){
					jQuery(gho).hide();
					jQuery(hpo).show();
					jQuery(sco).hide();
				} else if (cur=="specialclinic"){
					jQuery(gho).hide();
					jQuery(hpo).hide();
					jQuery(sco).show();
				}			
			}
			oidx = cidx;
			return false;
		});
	}
}

function resetForm(obj){
	jQuery(obj).each(function(){
		this.reset();
	});
	//document.getElementById(obj).reset();
}
