/*
	------------------------------------------------------------------------------------------
	Date format
	------------------------------------------------------------------------------------------
*/
Date.prototype.format = function(format)
{
	var o = {
			"M+" : this.getMonth(), //month
			"d+" : this.getDate(),    //day
			"h+" : this.getHours(),   //hour
			"m+" : this.getMinutes(), //minute
			"s+" : this.getSeconds(), //second
			"q+" : Math.floor((this.getMonth()+3)/3),  //quarter
			"S" : this.getMilliseconds() //millisecond
		}
	if(/(y+)/.test(format)) format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));
	for(var k in o)if(new RegExp("("+ k +")").test(format))format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] :("00"+ o[k]).substr((""+ o[k]).length));
	return format;
}

/*
	------------------------------------------------------------------------------------------
	StringFormat
	------------------------------------------------------------------------------------------
*/
StringFormat = function(format)
{
    if ( arguments.length == 0 )
    {
        return '';
    }
    if ( arguments.length == 1 )
    {
        return String(format);
    }

    var strOutput = '';
    for ( var i=0 ; i < format.length-1 ; )
    {
        if ( format.charAt(i) == '{' && format.charAt(i+1) != '{' )
        {
            var index = 0, indexStart = i+1;
            for ( var j=indexStart ; j <= format.length-2 ; ++j )
            {
                var ch = format.charAt(j);
                if ( ch < '0' || ch > '9' ) break;
            }
            if ( j > indexStart )
            {
                if ( format.charAt(j) == '}' && format.charAt(j+1) != '}' )
                {
                     for ( var k=j-1 ; k >= indexStart ; k-- )
                     {
                         index += (format.charCodeAt(k)-48)*Math.pow(10, j-1-k);
                     }  
                    var swapArg = arguments[index+1];
                    strOutput += swapArg;
                    i += j-indexStart+2;
                    continue;
                }
            }
            strOutput += format.charAt(i);
            i++;
        }
        else
        {
            if ( ( format.charAt(i) == '{' && format.charAt(i+1) == '{' ) || ( format.charAt(i) == '}' && format.charAt(i+1) == '}' ) )
            {
                i++
            }
            strOutput += format.charAt(i);
            i++;
        }
    }
    strOutput += format.substr(i);
    return strOutput;
 }
 
	/*
	------------------------------------------------------------------------------------------
	FilerSpecial
	------------------------------------------------------------------------------------------
	*/
FilerSpecial = function(str)
{
	str = str.replace(/</g,"&lt;");
	str = str.replace(/>/g,"&gt;");
	str = str.replace(/\"/g,"&quot;");
	return str;
}

UnFilerSpecial = function(str)
{
	str = str.replace(/&lt;/g,"<");
	str = str.replace(/&gt;/g,">");
	str = str.replace(/&quot;/g,"\"");
	return str;
}

	/*
	------------------------------------------------------------------------------------------
	ConvertTo Int
	------------------------------------------------------------------------------------------
	*/
ToInt = function(nVar)
{
	return (nVar < 0 ? Math.ceil ( nVar ) : Math.floor ( nVar )); 
}


	/*
	------------------------------------------------------------------------------------------
	get mouse x,y
	------------------------------------------------------------------------------------------
	*/
	this.mouse_X;
	this.mouse_Y;

	function myGetBrowser()
	{
		myN6 = document.getElementById;  // N6 or IE
		myIE = document.all;             // IE
		myN4 = document.layers;          // N4

		if (myIE){                       // IE?
			document.onmousemove = myMoveIE;
		}else if (myN6){                 // N6?
			window.addEventListener("mousemove",myMoveN6,true);
		}else if (myN4){                 // N4?
			window.captureEvents(Event.MOUSEMOVE);
			window.onmousemove = myMoveN4;
		}
	}

	function myMoveOP(){ // Opera
		mouse_X = window.event.clientX;
		mouse_Y = window.event.clientY;
	}
	function myMoveN6(myEvent){  //Netscape6 or FireFox
		mouse_X = myEvent.clientX + window.pageXOffset;
		mouse_Y = myEvent.clientY + window.pageYOffset;
	}
	function myMoveIE(){ // InternetExplorer6 or InternetExplorer7
		mouse_X = window.event.clientX + document.documentElement.scrollLeft;;
		mouse_Y = window.event.clientY + document.documentElement.scrollTop;;
	}
	function myMoveN4(myEvent){ // Netscape4x
		mouse_X = myEvent.x ;
		mouse_Y = myEvent.y ;
	}
	var bodyTag = document.getElementsByTagName('body');
	bodyTag.onload = myGetBrowser();
	function getX()
	{
	    return mouse_X;
	}
	function getY()
	{
	    return mouse_Y;
	} 

	/*
	------------------------------------------------------------------------------------------
	get element offset x,y
	------------------------------------------------------------------------------------------
	*/
var Position = {
	cumulativeOffset: function(element) {
		var valueT = 0, valueL = 0;
		do {
			valueT += element.offsetTop  || 0;
			valueL += element.offsetLeft || 0;
			element = element.offsetParent;
		} while (element);
		return {"x":valueL,"y":valueT};
	}
}

	/*
	------------------------------------------------------------------------------------------
	scroll google map
	------------------------------------------------------------------------------------------
	*/

var headall = null;
var footall = null;
var defaultHeadPos = 0;
var defaultFootPos = 0;

function posGoogleMap(mapDiv, headDiv, footDiv){
	headall = headDiv;
	footall = footDiv;
	defaultHeadPos = Position.cumulativeOffset(headDiv).y;
	defaultFootPos = Position.cumulativeOffset(footDiv).y;
	
	var defaultMapHeight = $(mapDiv).height();
	
	window.onscroll = function(){
		if(defaultHeadPos + defaultMapHeight > defaultFootPos) return;
		
		var posX = 0;
		var posY = 0;
		
		if (window.innerHeight) {
			posX = window.pageXOffset;
			posY = window.pageYOffset;
		}
		else if (document.documentElement && document.documentElement.scrollTop) {
			posX = document.documentElement.scrollLeft;
			posY = document.documentElement.scrollTop;
		}
		else if (document.body) {
			posX = document.body.scrollLeft;
			posY = document.body.scrollTop;
		}
		
		var popdiv = document.getElementById('popdiv');
		var mapDiv1 = document.getElementById("topSearchMap");
		
		$(mapDiv).css("position", "absolute");	
		$(mapDiv).css("top", posY + 5);
		if( popdiv != null ){
			if( deltaY == 0 ){
				popdiv.style.top = (posY + 5 + mapDiv1.style.height.replace(/px/g, '')/2) + 'px';
			}else{
				popdiv.style.top = (posY + 5 + deltaY) + 'px';
			}
		}		
		
		if(posY < defaultHeadPos){
			$(mapDiv).css("position", "absolute");	
			$(mapDiv).css("top", defaultHeadPos);
			if( popdiv != null ){
				if( deltaY == 0 ){
					popdiv.style.top = (defaultHeadPos + mapDiv1.style.height.replace(/px/g, '')/2) + 'px';
				}else{
					popdiv.style.top = (defaultHeadPos + deltaY) + 'px';
				}
			}
			if (typeof resetPopDivContainerPos !== "undefined")resetPopDivContainerPos();
			return;
		}
		
		if(posY + defaultMapHeight > defaultFootPos){
			$(mapDiv).css("position", "absolute");	
			var tmpPosY = defaultFootPos - defaultMapHeight;
			$(mapDiv).css("top", tmpPosY);
			if( popdiv != null ){
				if( deltaY == 0 ){
					popdiv.style.top = (tmpPosY + mapDiv1.style.height.replace(/px/g, '')/2) + 'px';
				}else{
					popdiv.style.top = (tmpPosY + deltaY) + 'px';
				}
			}
			if (typeof resetPopDivContainerPos !== "undefined")resetPopDivContainerPos();
			return;
		}
		if (typeof resetPopDivContainerPos !== "undefined")resetPopDivContainerPos();
	}
}

function resetGoogleMapPos(){
	if( headall == null || footall == null ) return;
	defaultHeadPos = Position.cumulativeOffset(headall).y;
	defaultFootPos = Position.cumulativeOffset(footall).y;
	window.onscroll();
}

/*
	------------------------------------------------------------------------------------------
	disableEnter
	------------------------------------------------------------------------------------------
*/
function disableEnter(event){
	 var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	 if (keyCode == 13){
	  return false;
	 }
}

/*
	------------------------------------------------------------------------------------------
	jQuery plugins
	------------------------------------------------------------------------------------------
*/
jQuery.fn.changeStyle = function(style, speed, callback) {
	return this.animate(style, speed, callback);
};

jQuery.fn.pos = function() {
	return Position.cumulativeOffset(this.get(0));
};

jQuery.fn.offsetPos = function() {
	return {x:this.get(0).offsetLeft || 0, y:this.get(0).offsetTop || 0};;
};

jQuery.fn.offsetSize = function() {
	return {"width":this.get(0).offsetWidth, "height":this.get(0).offsetHeight};
};
