
/* Browser */
var userAgent = navigator.userAgent.toLowerCase();
var browser = {
    name : userAgent,
    version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1],
    safari: /webkit/.test( userAgent ),
    opera: /opera/.test( userAgent ),
    msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
    mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
};

function addEventHandler(oTarget, sEventType, fnHandler) 
{	
	if( !oTarget )
		return;
	if( oTarget.addEventListener ){
		oTarget.addEventListener(sEventType, fnHandler, false);
	} else if ( oTarget.attachEvent ){
		oTarget.attachEvent("on" + sEventType, fnHandler);
	} else {
		oTarget["on" + sEventType] = fnHandler;
	}
}


/* Common function */
function AddFavorite( url, title )
{
	window.external.AddFavorite(url, title);
}

function $( arg )
{
  try {
    if( typeof(arg) == 'string' )
    	return document.getElementById(arg);
    return arg;
  } catch ( e ) {}
}

function show( arg )
{
	if( !arg ) return;
  $(arg).style.display = "";
}

function hide( arg )
{
	if( !arg ) return;
  $(arg).style.display = "none";
}

function isEmail( id ) 
{
	return $(id).value != '' && /^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/i.test( $(id).value );
}

function PreviewImg(id, value, w, h)
{  
	w = w || 120;
	h = h || 120;
  var newPreview = $(id);
  newPreview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = value;    
  newPreview.style.width = w + 'px';
  newPreview.style.height = h + 'px';
}

function getScript( html )
{
  var script;
  var scripts = new Array;
  var regexp = /<script[^>]*>([\s\S]*?)<\/script>/gi;
  while (script = regexp.exec( html )) 
    scripts.push( script[1] );
  return scripts;
}

var Mouse = function( event ){ 
  this.event = event || window.event;
};

Mouse.prototype = {
  pageX : function() {
    return this.event.pageX || this.event.clientX + document.documentElement.scrollLeft;
  },
  
  pageY : function() {
    return this.event.pageY || this.event.clientY + document.documentElement.scrollTop;
  }
};


/*  Ajax */
var Ajax = function( options ){
  this._url = options.url || null;
  this._type = options.type || 'get';
  this._async = options.async || true;
  this._params = options.params || '';
  this._dataType = options.dataType || '';
  this._loading = options.loading;
  this._success = options.success;
  this._failure = options.failure;
};

Ajax.prototype = {
  send : function() {
    if( !this._url ) return;
    var self = this.setTransport();
    self.transport.open( self._type, self._url, self._async );
    self.transport.onreadystatechange = function(){ self.stateChange() };
    self.transport.send( self._params );
  },
  
  stateChange : function() {
    if( this.transport.readyState == 4 || this.transport.readyState == 'complete' ){
      if( this.transport.status == 200 ) {
        this.response = {
          'text': this.transport.responseText,
          'xml': this.transport.responseXML
        };
        if( this._success ){
          this._success( this.response );
          this._dataType == 'script' ? this._evalScript( this.response ) : null;
        }
      } else {
        this._failure ? this._failure() : null;
      }
    } else {
    	this.running ? this.running() : null;
    }
  },
  
  setTransport : function() {
    this.transport = window.XMLHttpRequest ? new XMLHttpRequest() : (window.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : null);
    return this;
  },
  
  _evalScript : function( res ){
    var resText = res.text ? res.text : res.xml;
    var scripts = getScript( resText );
    eval( scripts.join('\n') );
  }
};


/* Scroller */
var Scroller = function( scroller, content, options ){
  this.oScroller = scroller || null;
  this.oContent = content || null;
	if( !this.oScroller || !this.oContent ) return;
  this._step = options.step || 1;
  this._speed = options.speed || 30;
  this._pause = options.pause || 0;
  this._direction = options.direction || 'up';
  this._intermission = false;
  this._timer = null;
  
  var self = this;
	addEventHandler( self.oScroller, "mouseover", function() { if( !self._intermission ) self.Stop(); } );
	addEventHandler( self.oScroller, "mouseout", function() { if( !self._intermission ) self.Start(); } );
	
	this._widthContent = parseInt(this.oContent.style.width) || this.oContent.offsetWidth;
	this._heightContent = parseInt(this.oContent.style.height) || this.oContent.offsetHeight;
	this.oContent.appendChild( this.oContent.cloneNode(true) );
};
 
Scroller.prototype = {
	GetScroll : function(iScroll, iContent) {
		this._isPause = false;
		var _step = this._step * this._stepUp;
		if( this._stepUp > 0 ){
			if( (iContent - iScroll) <= 0 ){ iScroll -= iContent; _step = 0; this._isPause = true; }
		} else {
			if( iScroll <= 0 ){ iScroll += iContent; _step = 0; this._isPause = true; }
		}
		return (iScroll + _step);
  },
  
	Intermission : function(iScroll, iContent) {
		if( this._stepUp > 0 ){
			if( (iContent - iScroll) <= 0 ){ iScroll -= iContent; }
		} else {
			if( iScroll <= 0 ){ iScroll += iContent; }
		}
		return iScroll;
	},
  
  ScrollUpDown : function() {
    var self = this;
		this.oScroller.scrollTop = this.GetScroll(this.oScroller.scrollTop, this._heightContent);		
		if( this._isPause ){
			if( this._intermission ) { this.Stop(); }
			if( this._pause && !this._intermission ) { this.Pause(); }
		}
  },
  
  ScrollLeftRight : function() {
    var self = this;
		this.oScroller.scrollLeft = this.GetScroll(this.oScroller.scrollLeft, this._widthContent);
		if( this._isPause ){
			if( this._intermission ) { this.Stop(); }
			if( this._pause && !this._intermission ) { this.Pause(); }
		}
  },
  
	Pause : function() {
		this.Stop();
    var self = this;
		this._timer = setTimeout( function() { self.Start() }, self._pause );
	},
  
  Start : function() {
		if( !this.oScroller || !this.oContent ) return;
		switch( this._direction.toLowerCase() ) {  
			case 'up' : 
				this._stepUp = 1;
				this._Scrolling = this.ScrollUpDown;
		    if( this._intermission )
					this.oScroller.scrollTop = this.Intermission(this.oScroller.scrollTop, this._heightContent);
			  break;
			
			case 'down' : 
				this._stepUp = -1;
				this._Scrolling = this.ScrollUpDown;
		    if( this._intermission )
					this.oScroller.scrollTop = this.Intermission(this.oScroller.scrollTop, this._heightContent);
			  break;
			  
			case 'left' : 
				this._stepUp = 1;
				this._Scrolling = this.ScrollLeftRight;
		    if( this._intermission )
					this.oScroller.scrollLeft = this.Intermission(this.oScroller.scrollLeft, this._widthContent);
			  break;
			
			case 'right' : 
				this._stepUp = -1;
				this._Scrolling = this.ScrollLeftRight;
		    if( this._intermission )
					this.oScroller.scrollLeft = this.Intermission(this.oScroller.scrollLeft, this._widthContent);
			  break;
	  }
	  this.Stop();
    var self = this;
    this._timer = setInterval( function(){ self._Scrolling() }, self._speed );
  },
  
  Stop : function() {
    clearInterval( this._timer );
  }
};


/* UItabs */
var UItabs = function( options ){
  this._ui_id = options.ui_id || null;
  if( !this._ui_id  ) return;
  this._delay = options.delay || 200;
  this._active = options.active || '';
  this._normal = options.normal || '';
  this._event = options.event || 'mouseover'; //click
	this._navs = this.getNavs();
	this._tabs = this.getTabs();
	this.init();
};

UItabs.prototype = {
  init : function() {
  	var self = this;
  	for( var i=0; i<this._navs.length; i++ ){
	  	var active = new this.setActive( self, i );
 			addEventHandler( self._navs[i], self._event, active._setActive );
	  }
  },
  
  setNormal : function() {
  	for( var i=0; i<this._navs.length; i++ ){
	  	$(this._navs[i]).className = this._normal;
	  	$(this._tabs[i]).style.display = 'none';
	  }
  },

  setActive : function( self, index ) { 
  	this._setActive = function() {
  		self.clearTimer();
  		self._timer = setTimeout( function() { 
		  	self.setNormal();  
	  		$(self._tabs[index]).style.display = '';
		    $(self._navs[index]).className = self._active; 
	    }, self._delay );
  	}
	},
  
  clearTimer : function() {
    clearTimeout( this._timer );
  },
  
  getNavs : function() {
  	return $(this._ui_id + '_nav').getElementsByTagName('li');
  },
  
  getTabs : function() {
  	return $(this._ui_id).getElementsByTagName('div');
  }
};


/* Slide */
var Slide = function( options ){
  this._imgs = options.imgs;
  this._urls = options.urls || [];
  this._txts = options.txts || [];
  this._imgSrc = options.imgSrc || 'slide_img';
  this._imgUrl = options.imgUrl || 'slide_url';
  this._imgTxt = options.imgTxt || 'slide_txt';
  this._imgNav = options.imgNav || 'slide_nav';
  this._normal = options.normal || 'slide_normal';
  this._active = options.active || 'slide_active';
  this._speed = options.speed || 5;
  this._delay = options.delay || 5000;
  this._event = options.event || 'click';
	this._effect = options.effect || '';
  this.Init();
};

Slide.prototype = {
	Init : function() {
  	if( !this._imgs ) return;
	  this._index = 1;
	  this._count = this._imgs.length;
//	  switch( this._effect.toLowerCase() ) {
//			case 'some_effect' : 
//				this.Effect = function(){};
//			  break;
//			default :
//				this.Effect = this.Alpha;
//			  break;
//		}
		this.Effect = function(){};
	  this.setNavigate();
  	this.SlideTrans( this._index );
  },
  
  Reset : function() {
		clearInterval( this.slidetimerinit );
		clearInterval( this.slidetimer );
		clearTimeout( this.slidetimeout );
  },

  setSlide : function( self, _index ) {
  	this.slideFunc = function() { self.SlideTrans( _index ); }
	},
  
	setNavigate : function() {
  	var self = this;
  	for( var i=1; i<=this._count; i++ ){
  		var el = document.createElement('a');
  		el.setAttribute( 'id', 'slide_nav_' + i ); 
  		el.setAttribute( 'href', 'javascript:void(0)' ); 
  		el.setAttribute( 'className', this._normal ); 
			el.appendChild( document.createTextNode( i ) );  
  		$( this._imgNav ).appendChild( el );
  		var ss = new this.setSlide( self, i );
  		addEventHandler( $( 'slide_nav_' + i ), self._event, ss.slideFunc );
		}
  },
  
  SlideTrans : function( _index ) {
  	_index = _index || this._index;
  	this.Reset();
		$( this._imgSrc ).src = this._imgs[_index-1];
		$( this._imgUrl ).href = this._urls[_index-1] || '#';
		$( this._imgTxt ).innerHTML = this._txts[_index-1] || '';
		
		for ( var i=1; i<=this._count; i++ )
			$('slide_nav_' + i).className = this._normal;
		$('slide_nav_' + _index).className = this._active;
		_index++;
		_index > this._count ? this._index = 1 : this._index = _index;
		
  	var self = this;
		this.slidetimerinit = setInterval( function(){ self.Effect( 1 ) }, self._speed )
    this.slidetimeout = setTimeout( function(){ self.slidetimer = setInterval( function(){ self.SlideTrans() }, self._speed ) }, self._delay );
	},
  
  Alpha : function( stepUp ) {
		if( browser.msie )
			$(this._imgSrc).filters.alpha.opacity = parseFloat( $(this._imgSrc).filters.alpha.opacity ) + 1 * stepUp;
		$(this._imgSrc).style.opacity = parseFloat( $(this._imgSrc).style.opacity ) + 0.01 * stepUp;
		
		if( parseFloat( $(this._imgSrc).style.opacity ) >= 1 ){
			clearInterval( this.slidetimerinit );
		}
		if( parseFloat( $(this._imgSrc).style.opacity ) <= 0 ){
			this.SlideTrans()
		}
	}
};
