// JavaScript Document

function getid(id) {
    return document.getElementById(id);
}

$(document).ready(function(){
	bookmark();
	accord();
	printPage();
	autoFill($(".searchkeyword"), "Search");
	newWin();
	lightBoxMap();
});

/*****************************BookMark************************/

function bookmark(){
	// add a "rel" attrib if Opera 7+
	if(window.opera) {
		if ($("a.bookmark").attr("rel") != ""){
			$("a.bookmark").attr("rel","sidebar");
		} 
	}
	$("a.bookmark").click(function(event){
		event.preventDefault();
		var url = this.href;
		var title = this.title;
		
		if (window.sidebar) { // Mozilla Firefox Bookmark
			window.sidebar.addPanel(title, url,"");
		} else if( window.external ) { // IE Favorite
			window.external.AddFavorite( url, title);
		} else if(window.opera) { // Opera 7+
			return false; // do nothing
		} else { 
			 alert('The browser does not support this action,'
			 + ' please bookmark this page manually.');
		}
	
	});
};

/*****************************Accordin************************/

function accord(){
	$(".accordin h2:first").addClass("active");
	$(".accordin .desc:not(:first)").hide();

	$(".accordin h2").click(function(){
		$(this).next(".desc").slideToggle("fast")
		.siblings(".desc:visible").slideUp("fast");
		$(this).toggleClass("active");
		$(this).siblings("h2").removeClass("active");
	});
}

/*****************************Print************************/
function printPage(){
	$('.print').click(function(){
		window.print();
	});
}

/*********************Light Box***********************************/
function pagecenter() {
    if (typeof window.innerWidth != 'undefined') {
        viewportWidth = window.innerWidth;
        viewportHeight = window.innerHeight;
    } else if (document.documentElement != 'undefined') {
        viewportWidth = document.documentElement.clientWidth;
        viewportHeight = document.documentElement.clientHeight;
    } else {
        viewportWidth = document.body.clientWidth;
        viewportHeight = document.body.clientHeight;
    }
    bodyHeight = document.documentElement.scrollHeight;
    if (bodyHeight > viewportHeight) {
        bodyHeightN = bodyHeight;
        if (typeof window.innerWidth != 'undefined') {
            viewportWidth = viewportWidth - 17;
            scrollTop = document.documentElement.scrollTop;

            if (navigator.userAgent.indexOf('Safari') != -1) {
                scrollTop = document.body.scrollTop;
            }
        } else if ((document.documentElement != 'undefined')) {
            viewportWidth = viewportWidth;
            scrollTop = document.documentElement.scrollTop;
        } else {
            viewportWidth = viewportWidth;
            scrollTop = document.body.scrollTop;
        }
    } else {
        bodyHeightN = viewportHeight;
        scrollTop = 0;
    }
    getid('cover').style.display = 'block';
    getid('cover').style.width = viewportWidth + 'px';
    getid('cover').style.height = bodyHeight + 'px';
}

var fadecoverspeed = 3;
var strSts = 0;
function changeSize(sts) {
    if (sts == -1) {
        if (strSts != sts) {
            fadecover(document['showmap'], [10], [70], 100, 20, 10, fadecoverspeed, 0);
        }
    } else {
        if (strSts != sts) {
            fadecover(document['showmap'], [10], [70], 100, 20, 10, fadecoverspeed, 0);
        }
    }
    strSts = sts;
}
function fadecover(elem, startmar, endmar, finmar, steps, intervals, powr, stat) {

    if (elem.bgFadeInt) window.clearInterval(elem.bgFadeInt);
    var actStep = 0;
    elem.bgFadeInt = window.setInterval(
	function() {
	    elem.style.opacity = easeInOut(startmar[0], endmar[0], steps, actStep, powr) / 100;
	    elem.style.MozOpacity = easeInOut(startmar[0], endmar[0], steps, actStep, powr) / 100;
	    elem.style.KhtmlOpacity = easeInOut(startmar[0], endmar[0], steps, actStep, powr) / 100;
	    elem.style.filter = "alpha(opacity=" + easeInOut(startmar[0], endmar[0], steps, actStep, powr) + ")";
	    actStep++;
	    if (actStep > steps) {
	        elem.style.opacity = finmar / 100;
	        elem.style.MozOpacity = finmar / 100;
	        elem.style.KhtmlOpacity = finmar / 100;
	        elem.style.filter = "alpha(opacity=" + finmar + ")";
	        window.clearInterval(elem.bgFadeInt);
	    }
	}, intervals)
}
function easeInOut(minValue, maxValue, totalSteps, actualStep, powr) {
    var delta = maxValue - minValue;
    var stepp = minValue + (Math.pow(((1 / totalSteps) * actualStep), powr) * delta);
    return Math.ceil(stepp)
}

function checkKeycode(e) {
    var keycode;
    if (window.event) {
        keycode = window.event.keyCode
    } else if (e) {
        keycode = e.which;
    }
    if (keycode == 27) {
        closepopup();
    }
}

function closepopup() {
    if (getid('lightboxMap')) {
        getid('lightboxMap').style.display = 'none';
        getid('cover').style.display = 'none';
    }
}

var nml = 0;
var nmdl = 0;
function lightBoxMap() {
    mlinks = document.getElementsByTagName('a');
    mdivs = document.getElementsByTagName('div');

    for (ml = 0; ml < mlinks.length; ml++) {
        if (mlinks[ml].rel == 'lighboxMap') {
            nml++
            mlinks[ml].setAttribute('href', 'javascript:showLighboxMap(' + nml + ')')
        }
		if (mlinks[ml].rel == 'lighboxMapnobg') {
            nml++
            mlinks[ml].setAttribute('href', 'javascript:showLighboxMap_nobg(' + nml + ')')
        }
    }
	
    for (md = 0; md < mdivs.length; md++) {
        if (mdivs[md].className == 'LightBoxOuterContainer') {
            nmdl++
            mdivs[md].id = 'mapDiv' + nmdl;
            mdivs[md].style.display = 'none';
        }
    }
}

function showLighboxMap(nmlId) {
	getid('lightboxMap').style.display = 'block';
	mapCnt = '<div class="LightBoxOuterContainerLoad"><a href="javascript:closepopup()" class="imgClose"><img src="/images/btn-close-lb.png" /></a>' + getid('mapDiv' + nmlId).innerHTML + '</div>';
    getid('lightboxMap').innerHTML = mapCnt;
    pagecenter();
    popupLeft = (viewportWidth - getid('lightboxMap').offsetWidth) / 2;
    popupTop = (viewportHeight - getid('lightboxMap').offsetHeight) / 2;
    getid('lightboxMap').style.left = popupLeft + 'px';
    getid('lightboxMap').style.top = (popupTop + scrollTop) + 'px';
    getid('cover').onclick = closepopup;
    document.documentElement.onkeyup = checkKeycode;
}
function showLighboxMap_nobg(nmlId) {
	getid('lightboxMap').style.display = 'block';
	mapCnt = '<div class="LightBoxOuterContainerLoad1"><a href="javascript:closepopup()" class="imgClose"><img src="/images/btn-close-lb.png" /></a>' + getid('mapDiv' + nmlId).innerHTML + '</div>';
    getid('lightboxMap').innerHTML = mapCnt;
    pagecenter();
    popupLeft = (viewportWidth - getid('lightboxMap').offsetWidth) / 2;
    popupTop = (viewportHeight - getid('lightboxMap').offsetHeight) / 2;
    getid('lightboxMap').style.left = popupLeft + 'px';
    getid('lightboxMap').style.top = (popupTop + scrollTop) + 'px';
    getid('cover').onclick = closepopup;
    document.documentElement.onkeyup = checkKeycode;
}

/***************************Search*****************************/
function autoFill(el, v){ 
	$(el).css({ color: "" }).attr({ value: v }).focus(function(){
		if($(this).val()==v){
			$(this).val("").css({ color: "" });
		}
	}).blur(function(){
		if($(this).val()==""){
			$(this).css({ color: "" }).val(v);
		}
	});
}

/**************************New Window************************/
function newWin(){
	$("a[rel=ExtLink]").attr('target','_blank');
}
