// JavaScript Document
jQuery(document).ready(function(){
	jQuery('#day-num li.txt-orange').css({
		'cursor':'pointer'
	});
	
	// Top Highlight UI
	if ($('#highlight-shownews .content-inner-show > div').size() <= 3 ){
		$('#highlight-shownews > #bt-random').hide();
	}
	
	// Multimedia
	jQuery('.active:eq(0)').show();
	jQuery('a[href="#video"]').click(function(){
		vt=jQuery('a[name="video"]').position().top;
		jQuery('html,body').animate({
			scrollTop:vt+'px'
		},{
			duration:800,
			easing:'expoinout'
		});
		return false;
	});
	
	// Menu-top
	jQuery('#submenu').find('div[class|="menu"]').mouseenter(function(){
		var minW=jQuery(this).width(); //min height
		var thisPos=jQuery(this).offset();
		subMenuL=thisPos.left-1;
		jQuery(this).find('div').show();
	}).mouseleave(function(){
		jQuery(this).find('div').hide()
	});
	
	// Tooltip home
	toolObj=jQuery('.ico-home');
	var toolPos=toolObj.offset();
	jQuery('#hometooltip').css({
		'position':'absolute',
		//'left':(toolPos.left+9)+'px',
		//'top':(toolPos.top-25)+'px'
		'left':'24px',
		'top':'-15px'
	});
	toolObj.mouseenter(function(){
		jQuery('#hometooltip').show();
	}).mouseleave(function(){
		jQuery('#hometooltip').hide();
	});

   // Switch Language
	var url = window.location.href;
	if(navigator.appName == "Microsoft Internet Explorer") {
		url = window.location;
	}
	var currentLanguage = window.location.href.substring(window.location.href.lastIndexOf("/"), window.location.href.lastIndexOf("/")-2);
	var pageFileName = window.location.href.substring(window.location.href.lastIndexOf("/")+1, url.length);
	if (pageFileName == "") {
		pageFileName = "index.php";
	}
	if (currentLanguage=="th") {
		$("a.menu-language").attr("href", "../en/"+pageFileName)
	} else {
		$("a.menu-language").attr("href", "../th/"+pageFileName)
	}
	
	// Calendar: Event Popup
	$(".calendar-month td[class^=\"has-event\"] div").each(function(){	
		$(this).click(function() {
			$(".calendar-month td.has-event").removeClass("active-popup");
			$(this).parent().addClass("active-popup");
			var popupPosTop = $(this).offset().top+22;
			var popupPosLeft = $(this).offset().left;
			if(navigator.appName == "Microsoft Internet Explorer") {
				if (jQuery.browser.version.substr(0,1) == "6") {
					popupPosLeft += 6;
				} else {
					popupPosLeft += 0;
				}
			}
			$(".calendar-event-detail-popup h4").html($(this).find("h4").html());
			$(".calendar-event-detail-popup div").html($(this).find("div").html());
			$(".calendar-event-detail-popup div p").after("<p class=\"hr\"></p>");
			$(".calendar-event-detail-popup div p:last-child p.hr").remove();
			$(".calendar-event-detail-popup")
				.css({"top": popupPosTop+"px", "left": popupPosLeft+"px"})
				.stop(true,true)
				.show()
				.css({"top": popupPosTop+"px", "left": popupPosLeft+"px"});
				return false;
			
		});	
	});	
	$(".month-control a").click(function(){
		$(".calendar-month td.has-event").removeClass("active-popup");
		$(".calendar-event-detail-popup").stop(true,true).hide();		
	});
	$(".calendar-event-detail-popup").parent().mouseleave(function() {
		 $(".calendar-month td.has-event").removeClass("active-popup");
		$(this).children(".calendar-event-detail-popup").stop(true,true).hide();						
	});	
	
	// Interest Loan
    $('.dott-top-loan > div').each(function(){
		$(this).attr("noActive",$(this).attr("class"));
	});
	$('.box-loan-01').attr("class","box-loan-01-active");
   	$('.content-loan-02').hide();
	$('.content-loan-03').hide();
	$('.dott-top-loan > div').click(function() {
		$('.dott-top-loan > div').each(function(){
			$(this).attr("class",$(this).attr("noActive"));
		});	
		var contentItem = $(this).attr("class").substr($(this).attr("class").length-2,2);
		contentItem = ".content-loan-"+contentItem;
		$('div[class*="content-loan"]').fadeOut(250);
		$(contentItem).fadeIn(250);
		$(this).attr("class",$(this).attr("class")+"-active");
	});	
	// New Accordion Side Menu
	$('#menuSection > ul > li > ul').hide();
	$('#menuSection > ul > li > a.txt-orange').next("ul").show();
	$('#menuSection > ul > li').each(function(){
		if ($(this).children("ul").html() == null) {
			$(this).children("a").addClass("no-sub-menu");
		}
	});
	$('#menuSection > ul > li > a').click(function(){
		if ($(this).hasClass("no-sub-menu") == false){
			if ($(this).parent().hasClass("expanded") == false) {
				$('#menuSection > ul > li > ul').slideUp(500);	
				$('#menuSection > ul > li').removeClass("expanded");
				$(this).next("ul").slideDown(500).parent().addClass("expanded");
			}
			return false;
		}
	});
	
	// Manipulate Style in Content Table
	$('table.service tr td:first-child').css("border-left","none");
	$('table.service tr td:last-child').css("border-right","none");
	
	// Branch Accordion Fixed 
	$('#list-branch > div:eq(0)').hide();
	$('#list-branch > div:even').live("click",function(){
		$('#list-branch > div.ui-accordion-header').slideDown();
		$(this).slideUp();
	});
	$('#list-branch2 > div:eq(0)').hide();
	$('#list-branch2 > div:even').live("click",function(){
		$('#list-branch2 > div.ui-accordion-header').slideDown();
		$(this).slideUp();
	});
});

function formFocus(v,i) {
	if(v.value==i){v.value=''} 
}
function formBlur(v,i) {
	if(v.value==''){v.value=i}
}

var oldPopupCalendar=null;
function showCalerndar(box,obj){
	jQuery(document.body).unbind('click');
	if(oldPopupCalendar!=null)jQuery(oldPopupCalendar).hide();
	oldPopupCalendar=obj;
	height=jQuery(box).height();
	if(parseFloat(jQuery(box).css('padding-top'))>0)height+=parseFloat(jQuery(box).css('padding-top'));
	if(parseFloat(jQuery(box).css('padding-bottom'))>0)height+=parseFloat(jQuery(box).css('padding-bottom'));
	if(parseFloat(jQuery(box).css('margin-top'))>0)height+=parseFloat(jQuery(box).css('margin-top'));
	if(parseFloat(jQuery(box).css('margin-bottom'))>0)height+=parseFloat(jQuery(box).css('margin-bottom'));
	pos=jQuery(box).position();
	top2=Math.ceil(pos.top);
	left=Math.ceil(pos.left);
	jQuery(obj).css({
		'left':left+'px',
		'top':(top2+(height*1))+'px'
	}).fadeIn('fast',function(){
		jQuery(document.body).bind('click',function(){
			jQuery(oldPopupCalendar).hide();
			jQuery(this).unbind('click');
		});
	}).find('#btnCalendarClose').click(function(){
		jQuery(obj).hide();
	}).css({
		'cursor':'pointer'
	});
}

var oldTabClass=null;
function tabOver(t){
	oldTabClass=jQuery(t).attr('class');
	if(!(jQuery(t).hasClass('active'))){
		jQuery(t).addClass('beforeactive');
	}
}
function tabOut(t){
	jQuery(t).removeAttr('class').addClass(oldTabClass);
}
function swapTab(t,num){
	for(i=1;i<=num;i++){
		jQuery('#content'+i).hide();
		jQuery(t.parentNode).find('a:eq('+(i-1)+')').removeClass('active').removeClass('beforeactive');
	}
	oldTabClass=jQuery(t).attr('class')+' active';
	jQuery(t).addClass('active');
	jQuery(jQuery(t).attr('href')).fadeIn('fast');
	return false;
}

function getVDO(p,d,n){
	jQuery(d).html('');
	jQuery.get(p,function(html){jQuery(d).html(html);});
}
function getVDO2(p,d,n){
	jQuery(d).html('');
	jQuery.get(p,function(html){jQuery(d).html(html);});
}
// Branch Data
var oldPopupBranch=null;
var oldColorBranch=null;
var overColorBranch='#ecf7e6';
function showBranch(box,obj,str,str2){
	$('#list-branch-show .rows-branch-detail div.column-1[style], #list-branch-show .rows-branch-detail-2 div.column-1[style]').css("background-color","");
	oldColorBranch=jQuery('.'+box.parentNode.className+' .column-1').css('background-color');
	jQuery(box).css('background-color',overColorBranch);
	jQuery(document.body).unbind('click');
	if(oldPopupBranch!=null)jQuery(oldPopupBranch).hide();
	oldPopupBranch=obj;
	height=jQuery(box).height();
	if(parseFloat(jQuery(box).css('padding-top'))>0)height+=parseFloat(jQuery(box).css('padding-top'));
	if(parseFloat(jQuery(box).css('padding-bottom'))>0)height+=parseFloat(jQuery(box).css('padding-bottom'));
	if(parseFloat(jQuery(box).css('margin-top'))>0)height+=parseFloat(jQuery(box).css('margin-top'));
	if(parseFloat(jQuery(box).css('margin-bottom'))>0)height+=parseFloat(jQuery(box).css('margin-bottom'));
	pos=jQuery(box).position();
	top2=Math.ceil(pos.top);
	left=Math.ceil(pos.left);
	jQuery(obj).css({
		'left':left+'px',
		'top':(top2+(height*1))+'px'
	}).fadeIn('fast',function(){
		jQuery(document.body).bind('click',function(){
			jQuery(oldPopupBranch).hide();
			jQuery(this).unbind('click');
			jQuery(box).css('background-color',oldColorBranch);
		});
	}).find('#btnBranchClose').click(function(){
		jQuery(obj).hide();
		jQuery(box).css('background-color',oldColorBranch);
		jQuery(box).removeAttr("style");
	}).css({
		'cursor':'pointer'
	});
	
	$.ajax({
		type: "GET",
		url: "branch_detail_content.php?t=",
		data: "part_id="+str+"&bnch_type_id="+str2,
		success: function(result){
			$("#branchlist").html(result);
		}
	});
}

