$(document).ready(function(){
	if ($('#vmarquee').html() != null) initializemarquee();
});

var delayb4scroll=1000
var marqueespeed=1
var pauseit=1 
var copyspeed=marqueespeed
var pausespeed=(pauseit==0)? copyspeed: 0
var actualheight=''

function scrollmarquee(){
	if (parseInt(cross_marquee.style.top)>(actualheight*(-1)+8))
		 cross_marquee.style.top=parseInt(cross_marquee.style.top)-copyspeed+"px"
	else cross_marquee.style.top=parseInt(actualheight)+8+"px";
	
	if (parseInt(cross_marquee_copy.style.top)>(actualheight*(-1)+8))
		 cross_marquee_copy.style.top=parseInt(cross_marquee_copy.style.top)-copyspeed+"px"
	else cross_marquee_copy.style.top=parseInt(actualheight)+8+"px";
}

function initializemarquee(){
	var vmarquee_copy = "<ul id='vmarquee_copy'>"+ $('#vmarquee').html() +"</ul>";
	$('#marqueecontainer').append(vmarquee_copy);
	
	marqueeheight=document.getElementById("marqueecontainer").offsetHeight;
	
	cross_marquee=document.getElementById("vmarquee");
	actualheight=cross_marquee.offsetHeight;	
	cross_marquee.style.top=0;	
	cross_marquee_copy=document.getElementById("vmarquee_copy");	
	cross_marquee_copy.style.top=parseInt(actualheight)+"px";
	
	if (window.opera || navigator.userAgent.indexOf("Netscape/7")!=-1){
		cross_marquee_copy.style.height=marqueeheight+"px";
		cross_marquee_copy.style.overflow="scroll";
		cross_marquee.style.height=marqueeheight+"px";
		cross_marquee.style.overflow="scroll";
		return
	}

setTimeout('lefttime=setInterval("scrollmarquee()",30)', delayb4scroll)
}

function applyNow(){
	$('#signup_info').hide();
	$('#apply').show();
}

/*
 * Lightbox use jQuery library - Require $ 1.2+
 * @Version:	1.0.4

Usage: 	caobox.show('boxID', 'overlayColor', 'overlayOpacity', 'overlayDisable', 'leftPosition', 'topPosition')
Exp:	caobox.show('testID', '000', '50', '', '10', '10')
Arguments:
	divID : [char] Div’s id
	overlayColor : [hex number] Used to define the overlay background color.  ‘000‘ (black) is default
	overlayOpacity : [int 0~100] Used to define the overlay opacity. ‘50‘ is default.
	overlayDisable : ['false'] Used to disable click on overlay to close popup. null is default
	leftPosition : [int] The padding CSS information used in the left position of image box. null mean center is default. [int]<0 is padding-right position
	topPosition : [int] The padding CSS information used in the top position of image box. null mean middle is default. [int]<0 is padding-bottom position
*/

var caobox = {		
	fadeEffectDuration : 400,
	fade:"#fade",
	light:"#light",
	boxID:"",
	overlayBgColor:"#000",
	overlayOpacity:"70",
	
	show:function(boxId, overlayBgColor, overlayOpacity, overlayDisable, leftPosition, topPosition)
	{
		// Get layer Background color & opacity
		if (overlayBgColor) this.overlayBgColor = "#"+ overlayBgColor;
		if (overlayOpacity) this.overlayOpacity = overlayOpacity;
		
		// Add layer
		$('body').prepend('<div id="light"></div><div id="fade"></div>');		
		$('#light, #fade').css({'display':'none', 'position':'absolute'});
		$('#light').css({	'zIndex':'10002',
							'height':'auto',
							'overflow':'hidden'});
		$('#fade').css({	'zIndex':'10001',
					   		'left':'0',
							'top':'0',
					   		'backgroundColor': ''+ this.overlayBgColor +'',
					   		'opacity':'.'+ this.overlayOpacity+'',
							'filter':'alpha(opacity='+ this.overlayOpacity +')'
						}).click(	function(){
										if (overlayDisable) return true;
										//else caobox.hide();
									});
	
		// Show layer
		this.boxID='#'+boxId;		
		$('object, embed, select').css('visibility','hidden');
		$('embed').css('display','none');

		
		var autodectect = boxId.substring(boxId.length-3, boxId.length);
		
		// Auto dectect Ajax loading
		if ((autodectect=="tml")||(autodectect=="php")){
			$(this.light).load(boxId, function(){});
		}
		else{
			var inner_boxID = $(this.boxID).html();
			$(this.boxID).clone().appendTo($(this.light));
			/*if (boxId) var filetype = inner_boxID.substring(inner_boxID.length-3, inner_boxID.length);*/
		// FLV		
		//		if (filetype == "flv"){
		//			var embedFLV = '<object height="400" width="420" type="application/x-shockwave-flash" data="'+inner_boxID+'&bufferTime=3&autoStart=false">'
		//				+'<param value="true" name="allowfullscreen"/>'
		//				+'<param value="high" name="quality"/>'
		//				+'<param value="'+inner_boxID+'" name="src"/></object>';		
		//			var full_boxID = "<div id="+ boxId +">"+ embedFLV +"</div>";
		//			$(this.light).append(full_boxID);
		//		}
		//		else{
		//			$(this.boxID).clone().appendTo($(this.light));
		//		}
		}
		
		//$(this.boxID).show();
		//$(this.light).fadeIn('slow', function(){
			 //$(this.light).css({display:'block'}); 
		//});
		$(this.light).fadeIn(900);

		var _top = Math.round((getDocumentSize(3) - $(this.light).height()) / 4);
		
		// Left position, default is center
		if (leftPosition){
			_left = leftPosition;
			if (leftPosition > 0) $(this.light).css({left: _left+ "px"});
			else			
				$(this.light).css({right: Math.abs(_left) + "px"});			
		}
		else{
			//_left = Math.round((getDocumentSize(2) - $(this.light).width()) / 2);
			_left = Math.round((getDocumentSize(2) - 520) / 2);
			$(this.light).css({left: _left+ "px"});
		}		
		
		// Top position, default is middle
		if (topPosition){			
			if (topPosition > 0){
				_top = topPosition;
				$(this.light).css({top: _top+"px"});
			}
			else{
				_top = Math.abs(topPosition);
				$(this.light).css({bottom: _top+"px"});
			}
		}		
		else{
			if(_top < 0) _top = 20;		
			$(this.light).css({top: _top+"px"});
		}
		
		caobox.showAlertLayer();	
		caobox.setFocusElement();
		window.scrollTo(0,0);
		window.onresize=caobox.resizeLayers;
	},
	
	hide:function(){	
		$(this.boxID).remove();
		$(this.fade).fadeOut(this.fadeEffectDuration);
		$(this.light).fadeOut(this.fadeEffectDuration, function(){
			$('object, embed, select').css('visibility','');
			$('embed').css('display','block');
			$('#fade, #light').remove();
		});
	},
	
	showAlertLayer : function(){
		var _height=getDocumentSize(1);
		var _width =getDocumentSize(0);
		
		if(_width < 974) 
			 _width = 974;	
		
		if($(this.boxID).height() > getDocumentSize(1))
			_height=parseInt($(this.boxID).height()) + 65;		
		
		$(this.fade).css({width:_width +'px'});
		$(this.fade).css({height:_height +'px'});	
		//$(this.fade).css({display:'block'}); 
		$(this.fade).fadeIn(900);
	},
	
	resizeLayers : function(){
		if($(this.boxID).height() > getDocumentSize(1)){
			var _height = parseInt($(this.boxID).height());
			var pageHeight = _height;
		}
		else{
			if (getDocumentSize(1) > getDocumentSize(3)){ var pageHeight=getDocumentSize(1)}
			else{ var pageHeight=getDocumentSize(3);}
		};
		
		if(getDocumentSize(0) < 974) $('#fade').css({width:974 + "px"});
		else $('#fade').css({width:getDocumentSize(0) + "px"});
		$('#fade').css({height:pageHeight + 'px'});
		$('#light').css({left: Math.round((getDocumentSize(2) - $('#light').width()) / 2)+"px"});
		
		var _top=Math.round((getDocumentSize(3) - $(this.light).height()) / 2);
		if(_top < 0) _top = 20;
		$(this.light).css({top:_top+"px"});
	},
	
	findFucusElement:function(){
		$("#light").find('object, embed, select').css({'visibility':'visible'});
		$("#light").find('input[@type!=hidden], a, select, textarea').addClass('setFocusAble');			
		return $('.setFocusAble');
	},
	
	setFocusElement:function(){
		var i = 0;
		var cntElement = 0;	
		var allFocusElement = caobox.findFucusElement();
		var len=allFocusElement.length;	
		
		if (len>0){
			for(var temJ = 0; temJ<len-1; temJ++){
				$(allFocusElement[temJ]).attr('rel',temJ+1);
			}
			
			// fix focus on 1st hidden element
			if ( allFocusElement[0].style.display == 'none') 
				allFocusElement[1].focus();
			else
				allFocusElement[0].focus();
	
	
			allFocusElement.focus(function(){ cntElement=this.getAttribute('rel'); });
			$("#light").keydown(function(event){									 
				if(event.shiftKey){
					if(event.keyCode == 9){					
						if (i!=cntElement) i=cntElement;
						i--;			
						if (i == 0) {
							i = len-1;
							allFocusElement[i].focus();
							return false;
						}
					}
				}
				else{
					if(event.keyCode == 9){
						if (i != cntElement) i = cntElement;
						if (i == len-1){
							i=0;
							allFocusElement[0].focus();						
							return false;
						}
						else{
							i++;
						}
					}
				}			
			}); 
		}
	},
	
	unbindClickFace:function(){
		$(this.fade).unbind('click');
	},
	
	init:function()
	{		
		$(document).ready(function(){
			$('.caobox-popup').hide();
			$('body').css({'margin':'0', 'padding':'0'}); // hack browser
			$('img.caobox-popup').css({ 'border':'5px solid #000'});
		})		
	}
};
caobox.init({							
});


// Get ducument size
// @param int 0 ~ 3
// @return array [width of current page] [height of current page] [width of window] [height of window]
function getDocumentSize(val){
	var xScroll,yScroll,value;
	
	if(window.innerHeight&&window.scrollMaxY){
		xScroll=window.innerWidth+window.scrollMaxX;
		yScroll=window.innerHeight+window.scrollMaxY;
	}
	else if(document.body.scrollHeight>document.body.offsetHeight){
		xScroll=document.body.scrollWidth;
		yScroll=document.body.scrollHeight;
		}
		else{
			xScroll=document.body.offsetWidth;
			yScroll=document.body.offsetHeight;
		}
	
	var windowWidth,windowHeight;
	
	if(self.innerHeight){
		if(document.documentElement.clientWidth){
			windowWidth=document.documentElement.clientWidth;
		}
		else{
			windowWidth=self.innerWidth;
		}
		windowHeight=self.innerHeight;
	}
	else if(document.documentElement&&document.documentElement.clientHeight){
			windowWidth=document.documentElement.clientWidth;
			windowHeight=document.documentElement.clientHeight;
		}
		else if(document.body){
			windowWidth=document.body.clientWidth;
			windowHeight=document.body.clientHeight;
		}
	
	if(yScroll<windowHeight){
		pageHeight=windowHeight;
	}
	else{
		pageHeight=yScroll
	}
	
	if(xScroll<windowWidth){
		pageWidth=xScroll
	}
	else{
		pageWidth=windowWidth
	}
	
	arrayPageSize=new Array(pageWidth, pageHeight, windowWidth, windowHeight);
	return arrayPageSize[val];
};
