window.addEvent('domready', function(){
	//showPopupShare();
	showPopup();
	formValid();
});

function formValid(){
	var formContact = $('formContact');
	if(!formContact) return;	
	var contactFormData = [{
		name: 'txtName',
		valid: 'required',	
		message: {
			en: 'username should not be blank',
			vi: 'vui lòng nhập họ và tên'		
		}
	},{
		name: 'txtEmail',
		valid: 'required|email',	
		message: {
			en: 'email should not be blank|please enter valid email',
			vi: 'vui lòng nhập email|vui lòng nhập đúng email'		
		}
	},{
		name: 'txtTel',
		valid: 'required',	
		restrict: '[0-9 ]',
		message: {
			en: 'tel should not be blank',
			vi: 'vui lòng nhập số điện thoại'		
		}
	},/*{
		name: 'txtFile',
		valid: 'image',	
		message: {
			en: 'file should not be blank',
			vi: 'vui lòng chọn đúng kiểu tập tin'		
		}
	},*/{
		name: 'txtCapcha',
		valid: 'required',			
		message: {
			en: 'verify code should not be blank',
			vi: 'vui lòng nhập mã xác nhận'		
		}
	}];
	var frmContactIns = new FormValidator('formContact', contactFormData, {
		language: 'vi',
		ajax: true,
		onSubmit: function(){
			new Request({
				url: 'checkCapcha.php',
				method: 'post',
				data: {
					idCode: $('txtCapcha').value
				},
				onSuccess: function(){
					if(arguments[0] != 1){						
						frmContactIns.showLayer($('txtCapcha'), 'vui lòng nhập mã xác nhận');
						return false;
					}
					formContact.submit();
				}
			}).send();
		}
	});		
}
function showPopupShare(){
	var div = $('content');
	if(!div)	return;
	var ulTag = div.getElement('ul.plusIn');	
	if(!ulTag)	return;
	var liTags = ulTag.getElements('li');
	if(liTags.length < 3 )	return;
	
	var divPopup = $('popupShare');
	if(!divPopup)	return;
	divPopup.inject(document.body);
	
	var timer = null;
	
	liTags[2].removeEvents().addEvents({
		'mouseenter': function(){
			clearTimeout(timer);
			divPopup.setStyles({
				top: liTags[2].getCoordinates().top + liTags[2].getCoordinates().height,
				left: liTags[2].getCoordinates().left
			})
		},
		'mouseleave': function(){
			timer = setTimeout(function(){
				divPopup.setStyle('top', '-15000px');
			}, 300);
		}
	});
	
	divPopup.removeEvents().addEvents({
		'mouseenter': function(){			
			clearTimeout(timer);
		},
		'mouseleave': function(){
			divPopup.setStyle('top', '-15000px');
		}
	});
}

function showPopup(){
	var sdkLink = $('sdkLink');
	if(!sdkLink)	return;
	
	sdkLink.removeEvents().addEvent('click', function(e){
		e.stop();
		new MooLayer('popupSDK');
	});
	
}

var MooLayer = new Class({
    Implements: [Options, Chain],
    options: {
        zIndex: 999,
        opacity: 0.6,
        padding: 0
    },
    initialize: function (selector,popupContentID, ajaxURL) {
        this.selector = $(selector);
		this.url = ajaxURL;
		this.updateDivID = $(popupContentID);
        if (!this.selector) return;
        this.initLayer(selector);
    },
    initLayer: function () {
        var that = this;
        var selector = this.selector;
		if($('overlayLayerPopup')){
			$('overlayLayerPopup').destroy();
		}
        var overlay = new Element('div', {
			'id': 'overlayLayerPopup',            
            'styles': {
                'display': 'block',
                'visibility': 'visible',
                'position': 'absolute',
                'top': 0,
                'left': 0,
                'width': window.getWidth(),
                'height': window.getScrollSize().y,
                'zIndex': that.options.zIndex,
                'backgroundColor': '#000',
                'opacity': that.options.opacity
            }
        }).inject(document.body);
		selector.inject(document.body);      
        selector.setStyles({            
            'position': 'absolute',
            //Browser.Engine.trident4 ? 'absolute' : 'fixed',
            'top': Math.max(0, (window.getHeight() - selector.getCoordinates().height) / 2 - that.options.padding) + window.getScrollTop(),
            'left': (window.getWidth() - selector.getCoordinates().width) / 2,
            'zIndex': that.options.zIndex + 1
        });
        
		var scrollTop = window.getScrollTop();
		
		
		if(this.url){
			new Request.HTML({
				url: this.url,
				method: 'get',		
				update: this.updateDivID,				
				onRequest: function(){		
				},
				onComplete: function(){
					selector.setStyles({
						'top': Math.max(0, (window.getHeight() - selector.getCoordinates().height) / 2 - that.options.padding) + window.getScrollTop(),
						'left': (window.getWidth() - selector.getCoordinates().width) / 2
					});
				}
			}).send();
		}
		
		window.addEvent('scroll',function(){
			if(window.getHeight() < selector.getCoordinates().height){					
				selector.setStyle('position', 'absolute');
				selector.setStyle('top' , scrollTop);	
				return true;
			}
			else {	
				selector.setStyle('position', (Browser.Engine.trident4 ? 'absolute': 'fixed'));
				if(Browser.Engine.trident4){
					selector.setStyle('top', Math.max(0, (window.getHeight() - selector.getCoordinates().height)/2 + (Browser.Engine.trident4 ? window.getScrollTop() : 0) - that.options.padding));
				}
				else{
					selector.setStyle('top', Math.max(0, (window.getHeight() - selector.getCoordinates().height)/2 + (Browser.Engine.trident4 ? window.getScrollTop() : 0) - that.options.padding));
				}
			}
		});
 
        window.addEvent('resize', function (e) {
            selector.setStyles({
                'top': Math.max(0, (window.getHeight() - selector.getCoordinates().height) / 2 - that.options.padding) + window.getScrollTop(),
                'left': (window.getWidth() - selector.getCoordinates().width) / 2
            });
        });

        if (selector.getElement('.btnCancel')) {
            selector.getElement('.btnCancel').removeEvents('click').addEvent('click', function (e) {
                if (e) e.stop();                
                selector.setStyle('top', '-15000px');
                overlay.destroy();
				window.removeEvents('scroll');
				window.removeEvents('resize');
            });
        }
    }
});
