﻿// JavaScript Document

/*----------------------------------------
initial primary menu
----------------------------------------*/
var timeDelay = Array(null, null, null, null);
var activeItem = Array(null, null, null, null);
var isDisplay = Array(false, false, false, false);

function init_primary_menu(obj) {
    init_menu(obj, 1);
}

function init_menu(obj, level) {
    $(obj).children('li').each(function () {
        if ($(this).attr('class') == 'first') return;

        //var current = $(this).attr('className');
        var child = $(this).children('ul');
        var id = $(this).attr('id');

        //set action
        $(this).mouseover(function () {
            active_menu(this, level, true);

            //display child menu
            if (child.length > 0) {
                //make sure that previous menu in same level is hide
                if ((activeItem[level] != null) && (activeItem[level] != id)) {
                    if ($('#' + activeItem[level]).children('ul').is(':visible')) $('#' + activeItem[level]).children('ul').hide();
                }

                isDisplay[level] = true;
                activeItem[level] = id;
                display_child_menu(child, level, true);
            }
        })
		.mouseout(function () {
		    if (child.length > 0) {
		        if ($(child).is(':visible') && (activeItem[level] == id) && (isDisplay[level] == true)) {
		            display_child_menu(child, level, false);
		        }
		    }
		    else {
		        active_menu(this, level, false);
		    }
		});

        //init sub menu
        if ((child.length > 0) && (child.attr('className').indexOf('tree-columns') < 0)) {
            init_menu(child, level + 1);
        }
    });
}

function display_child_menu(obj, level, show) {
    if (show == false) {
        isDisplay[level] = false;
        timeDelay[level] = setTimeout("hide_menu(" + level + ")", 250);
    }
    else {
        if (!$(obj).is(':visible')) {
            $(obj).show();
        }
    }
}

function hide_menu(level) {
    var menu = '#' + activeItem[level];
    if (isDisplay[level] == true) {
        if (timeDelay[level]) clearTimeout(timeDelay[level]);
        return;
    }

    $(menu).children('ul').hide();
    active_menu(menu, level, false);
}

function active_menu(menu, level, is_active) {
    switch (is_active) {
        case true:
            //make sure that previous menu in same level is not active
            if (activeItem[level] != null) active_menu('#' + activeItem[level], level, false);

            if (level == 1) {
                //check if menu is already active?
                if (($(menu).attr('className').indexOf('active') < 0) && ($(menu).attr('className').indexOf('hover') < 0)) {
                    if ($(menu).attr('className').indexOf('first') >= 0) {
                        $(menu).attr('className', 'first hover');
                    }
                    else {
                        $(menu).attr('className', 'hover');
                    }
                    $(menu).css({ backgroundImage: 'url(' + imgMainMenuOver.src + ')' });
                }
            }
            else if ((level == 2) && ($(menu).attr('className').indexOf('hover') < 0)) {
                $(menu).attr('className', 'hover');
            }
            break;

        case false:
            var menu_class = $(menu).attr('className');
            if (menu_class != "") {
                if (menu_class.indexOf('active') >= 0) {
                    if ($(menu).attr('className').indexOf('first') >= 0) {
                        $(menu).attr('className', 'first active');
                    }
                    else {
                        $(menu).attr('className', 'active');
                    }
                }
                else {
                    if ($(menu).attr('className').indexOf('first') >= 0) {
                        $(menu).attr('className', 'first');
                    }
                    else {
                        $(menu).removeAttr('class');
                    }
                    $(menu).removeAttr('style');
                }
            }
            break;
    }
}

/*----------------------------------------
initial secondary menu
----------------------------------------*/
function init_secondary_menu(obj) {
    $(obj).children('li').each(function () {
        var li = this;
        var child = $(this).children('ul');

        //show/hide menu lv2
        $('.action', this).click(function () {
            if (($(child).length > 0) && (!$(child).is(':visible'))) {
                $(child).show('slow');

                //expand
                $('div:first', li).attr('className', 'action expand');
            }
            else if (($(child).length > 0) && ($(child).is(':visible'))) {
                $(child).hide('slow');

                //collapse
                $('div:first', li).attr('className', 'action collapse');
            }
        });

        //auto expand if menu level 2 is active
        if ((($(li).attr('className').indexOf('active') >= 0) && ($(child).length > 0)) || (has_activemenu(child))) {
            $(child).show('slow');

            //expand
            $('div:first', li).attr('className', 'action expand');
        }

    });
}

function has_activemenu(obj) {
    var has = false;
    $(obj).children('li').each(function () {
        if ($(this).attr('className').indexOf('active') >= 0) {
            has = true;
        }
    });
    return has;
}


/*----------------------------------------
Custom Search
----------------------------------------*/
var googleSearchIframeName = "cse-search-results";
var googleSearchFormName = "cse-search-box";
var googleSearchDomain = "www.google.com";
var googleSearchPath = "/cse";
var googleSearchFrameWidth = 920;

function Search() {
    var keyword = document.getElementById('textfield');
    window.location.href = '/specialpages/Search-Result.aspx?cx=002552486380778138527:4t5fgdfhdki&cof=FORID:9&q=' + keyword.value;
}

/*----------------------------------------
Banner Popup
----------------------------------------*/
function banner_popup(obj,bannerDetail) {
	var tmp = bannerDetail.split(",");
	
    var page_name = $.trim(tmp[0]);
	var b_width = $.trim(tmp[1]);
	var b_height = $.trim(tmp[2]);
	var isModal =  $.trim(tmp[3]).toString().toLowerCase();
	var url = $.trim(tmp[4]);
    var x = $.trim(tmp[5]);
    var y = $.trim(tmp[6]);

    if(bannerHasPopup(page_name)) return;
    setCookie(page_name,url,null)

    if (isModal.toLowerCase() == "true") {
        PopupUrl(url, 'html', '', b_width, b_height, '.banner-tranformation');
        //remove background title
        $('.title ', '.modal-popup').css({
            backgroundImage:'none'
        });
    }
    else {
        NoModalPopupUrl(url,b_width, b_height, '.banner-tranformation',x,y);
    }
}

/*----------------------------------------
Load url for display page in no-modal popup
----------------------------------------*/
function NoModalPopupUrl(url, width, height, obj, x, y){
    $('.modal-popup').remove();
    if (width != 'auto') width += 'px';
    if (height != 'auto') height += 'px';

    var popup = $('<div class="no-modal-popup"></div>');
    $(popup).css({
        display: 'block',
        backgroundColor: '#FFFFFF',
        width: width,
        height: height,
        position: 'absolute',
        left: x + 'px',
        top: y + 'px',
        zIndex: '10000'
    });
    var close = $('<a title="Close" class="modalCloseImg simplemodal-close"></a>').click(function(){
                    $(popup).hide();
                });
    $(popup).append($('<div class="title"></div>').append(close));

    var body = $('<div class="body"></div>');
    $(body).append('<div id="popupLoad" style="text-align:center;width:auto;height:auto;"><img src="/CMSTemplates/BIHSite/images/main-site/template/loadinfo.net.gif" /></div>');
    $(body).append('<div id="popupText" style="display:none"></div>');

    $(popup).append(body);
    $('.page').append(popup);
    $(popup).show();

    /*load data*/
    $.ajax({
            url: url,
            success: function (data) {
                $('#popupText').html($(data).find(obj).html());
                $('#popupText').show();
                $('#popupLoad').hide();
            }
    });
}

/*----------------------------------------
Check banner has popup for this browser already
----------------------------------------*/
function bannerHasPopup(c_name){
    var bannerCookie = getCookie(c_name);
    if(bannerCookie == ""){
        return false;
        
    }
    else{
        return true;
    }
}
/*----------------------------------------
Manage cookie
----------------------------------------*/
function setCookie(c_name,value,expiredays){
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function getCookie(c_name){
    if (document.cookie.length>0)
      {
      c_start=document.cookie.indexOf(c_name + "=");
      if (c_start!=-1)
        {
        c_start=c_start + c_name.length+1;
        c_end=document.cookie.indexOf(";",c_start);
        if (c_end==-1) c_end=document.cookie.length;
        return unescape(document.cookie.substring(c_start,c_end));
        }
      }
    return "";
}

/*----------------------------------------
Load url for display page in modal popup
----------------------------------------*/
function PopupUrl(url, type, title, width, height, obj) {
    $('.modal-popup').remove();
    if (width != 'auto') {
        width += 'px';
    }
    else {
        width = '150px';
    }
    if (height == "") {
        height = 'auto';
    }
    else if (height != 'auto') {
        height += 'px';
    }

    var popup = $('<div class="modal-popup" style="text-align:center; width:' + width + ';height:' + height + '"></div>');
    $(popup).append($('<div class="title"><div>' + title + '</div></div>'));

    var body = $('<div class="body"></div>');
    $(body).append('<div id="popupLoad" style="display:block;margin-left:auto; margin-right:auto;width:50px;height:50px;"><img src="/CMSTemplates/BIHSite/images/main-site/template/loadinfo.net.gif" /></div>');
    $(body).append('<div id="popupText" style="display:none"></div>');
    $(popup).append(body);
    $('.page').append(popup);
    $(popup).modal({ overlayClose: true });

    //initial container
    $('#simplemodal-container').css({
        display: 'block',
        height: '150px',
        width: width
    });

    /*load data*/
    if (type == 'image') {
        //url = "http://www.bumrungradinternational.com/images/main-site/about-us/AMI-Program-L.aspx";

        $('#popupText').html('<img class=\"img-popup\" src="' + url + '"/>');
        $('img', '#popupText').load(function () {
            $('#popupText').show();
            $('#popupLoad').hide();

            positionModal('#simplemodal-container', '.img-popup', width); //position image again after load finished
        });
    }
    else if (type == 'flash') {
        $('#popupText').html(url);
        $('#popupLoad').hide();
        $('#popupText').show();
    }
    else {
        $.ajax({
            url: url,
            context: document.body,
            dataType: "html",
            success: function (data) {
                $('#popupText').html($(obj, data).html());
                $('#popupText').show();
                $('#popupLoad').hide();

                positionModal('#simplemodal-container', '#popupText', width);
            }
        });
    }

}
function createFlash(url, title, width, height) {
    var html = '<object height="202" width="306" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" title="' + title + '">';
    html += '<param name="movie" value="' + url + '">';
    html += '<param name="quality" value="high">';
    html += '<param name="wmode" value="opaque">';
    html += '<embed height="' + height + '" width="' + width + '" src="' + url + '" quality="high" wmode="opaque" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash">';
    html += '</object>';

    return html;
}
var countPost = 0;
var timePostion = null;
function positionModal(obj, target, width) {
    if (($(target, obj).width() == 0) || ($(target, obj).width() == null)) {
        if (countPost < 5) {
            timePostion = setTimeout("positionModal('" + obj + "','" + target + "','" + width + "')", 500);
            countPost++;
        }
        return;
    }
    if (timePostion != null) clearTimeout(timePostion);

    //
    // .modal-popup
    //
    $('.modal-popup').css({
        width: $(target, obj).width() + 20 + 'px'
    });

    //
    // #simplemodal-container
    //
    var h = 100; //default
    var w = 100; //default
    if ($(target, obj).width()){
        h = $(target, obj).height() + 70;
        w = $(target, obj).width() + 32;
    }
    $(obj).css({
        display: 'block',
        height: h + 'px',
        width: w + 'px'
    });

    //detect position top for browser ie6
    if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
        $(obj).css({
            left: (($(window).width() - $(target, obj).width()) / 2) + 'px'
        });
    }
    else {
        $(obj).css({
            left: (($(window).width() - $(target, obj).width()) / 2) + 'px',
            top: (($(window).height() - $(target, obj).height()) / 2 - 30) + 'px'
        });
    }
}

/*----------------------------------------
Email - Web Form Popup
----------------------------------------*/
function EmailForm(email, subject) {
   var url = "/specialpages/Email-Webform.aspx?email="+email+"&subject="+subject;

   CreateWebformPopup(url, 600,450,500, 400,"no");}
/*----------------------------------------
Send to friend (real cost page 3) - Web Form Popup
----------------------------------------*/
//function SendToFriendForm(parentUrl, lang) {
function SendToFriendForm(pid,realpid, lang) {
   /*var parentUrl = window.location;*/
    //var url = "/" + lang + "/specialpages/tell-a-friend?url=" + parentUrl;
    var url = "/" + lang + "/specialpages/tell-a-friend?Pid=" + pid + "&RealcostPid=" + realpid;

   CreateWebformPopup(url, 600,450,500, 400,"no");
}
/*----------------------------------------
Representative - Web Form Popup
----------------------------------------*/
function InquiryRepresentativeForm(email, subject, country, city, lang) {

    if (lang == "ja-JP") 
    {
        lang = "jp";
    } else 
    {
        lang = "en";
    }

    var url = "/"+ lang +"/specialpages/inquiry-rep-webform?email=" + email + "&subject=" + subject + "&country=" + country + "&city=" + city;
    CreateWebformPopup(url, 770,450,740, 400,"yes");
}
/*----------------------------------------
Representative - Auto create webform modal popup
----------------------------------------*/
function CreateWebformPopup(url, width, height,iwidth,iheight,scroll) {
    $('#webform').remove();

    var webform = $('<div id="webform" class="modal-popup"></div>').css({ width: width + 'px', height: height + 'px' });
    var emailform = $('<div id="email-form"><div class="language-popup"><div class="title">&nbsp;</div><div class="body"><div class="box-content-detail"></div></div></div></div>');
    
	$('.box-content-detail', emailform).append('<iframe width="' + iwidth + '" height="' + iheight + '" frameborder="0" scrolling="' + scroll + '" src="' + url + '"></iframe>');
    $('.page').append($(webform).append(emailform));

    $(webform).modal();

    //initial container
    var obj = '#simplemodal-container';
    $(obj).css({
        display: 'block',
        width: (width + 10) + 'px',
        height: (height + 20) + 'px'
    });

    //detect position top for browser ie6
    if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
        $(obj).css({
            left: (($(window).width() - $(obj).width()) / 2) + 'px'
        });
    }
    else {
        $(obj).css({
            left: (($(window).width() - $(obj).width()) / 2) + 'px',
            top: (($(window).height() - $(obj).height()) / 2 - 30) + 'px'
        });
    }

}

/*----------------------------------------
Advance Appointment - Loader
------------------------------------*/
function AppointLoader() {
    var webform = $('<div id="webform" class="modal-popup"></div>').css({ width: '500px', height: '150px' });
    var emailform = $('<div id="email-form"><div class="language-popup"><div class="title"><h4 class="appoint-h4">Loading appointment page ...</h4></div><div class="body-appoint"><div class="box-content-detail box-content-detail-appoint"></div></div></div></div>');
    
    $('.box-content-detail', emailform).append('<div class="loading"><img src="/CMSTemplates/BIHSite/images/main-site/template/loadinfo.net.gif" /><h4>Please wait ...</h4></div><div class="body">For slower connections, <a href="/contact-us/Classic-Appointment">click here to load basic appointment page.</a></div>');
	$('.page').append($(webform).append(emailform));

    $(webform).modal();
	$(".modalCloseImg").hide();

}

/*----------------------------------------
Email - Create email format
----------------------------------------*/
function CreateEmail(user, domain, org) {
    return user + '@' + domain + '.' + org;
}
function EmailRewrite() {
    $('.email-rewrite').each(function () {
        var str_mail = $(this).text().replace(' ', '');
        if (str_mail != "") {
            var mail = str_mail.split(',');
            $(this).text(mail[0] + '@' + mail[1] + '.' + mail[2]);
        }
    });
}

/*----------------------------------------
ForgotPassword- Web Form Popup
----------------------------------------*/
function ForgotPassword(lang,form){
    if (lang == "ja-JP") 
    {
        lang = "jp";
    } 
    else if (lang == "th-TH") {
        lang = "th";
    } 
    else 
    {
        lang = "en";
    }

    var url = "/" + lang + "/specialpages/forgot-password-webform?form=" + form;

   CreateWebformPopup(url, 430, 430, 410, 375, "yes");
}

/*----------------------------------------
Font Sizing
----------------------------------------*/
function font_sizing(src, target) {
    /*content detail*/
    $(target).css('font-size', $(src).css('font-size'));
    $('p', target).css('font-size', $(src).css('font-size'));
    $('a', target).css('font-size', $(src).css('font-size'));
    $('span', target).css('font-size', $(src).css('font-size'));
    $('td', target).css('font-size', $(src).css('font-size'));
    $('li', target).css('font-size', $(src).css('font-size'));

    //change active
    var i = 1;
    $('a', '.font-size').each(function () {
        $(this).attr('className', 'font-size-' + i);
        i++;
    });

    $(src).attr('className', $(src).attr('className') + ' font-active');
}

/*---------------------------------------
Button Style - change button style from preload image
----------------------------------------*/
function change_button(obj, image) {
    $('img', obj).attr('src', image.src);
}
function change_linkbutton(obj, image) {
    $(obj).css('background-image', 'url('+ image.src+')');
}
/* remove border around radio and checkbox */
$(document).ready(function(){
	$("input:radio").css("border","none");
	$("input:checkbox").css("border","none");
});


/*----------------------------------------
Email - Web Form Popup
----------------------------------------*/
/*function EmailForm(lang, email, handler) {
    alert('xx');
    if (lang == "ja-JP") {
        lang = "jp";
    }
    else if (lang == "th-TH") {
        lang = "th";
    }
    else {
        lang = "en";
    }

    var url = "/" + lang + "/specialpages/Email-Webform.aspx?email=" + email;

    CreateWebformPopup(url, 600, 450, 500, 400, "no");
}*/
