
var EmailCatcher = function(pseudo, rating, imgSrc, action){
    this.pseudo= pseudo;
    this.rating = rating;
    this.imgSrc = imgSrc;
    if( typeof(action) == 'undefined' ) action = null;
    this.action = action;
}
EmailCatcher.hide = function(){
    window.document.getElementById('emailCatcherContent').innerHTML ="";
}

EmailCatcher.prototype = {
    getElement:function(element){
        return window.document.getElementById(element)
    },
    show: function(){
        this.getElement('emailCatcherContent').innerHTML = this.getElement('emailCatcher').innerHTML;
        var close = this.getElement('emailCatcherContent').getElementsByTagName('a')[0];
        close.onclick = EmailCatcher.hide;
        this.customize();
        window.scrollTo(0,0);
    },
    hide: function(){
        EmailCatcher.hide();
    },
    generateInputsHiddens:function(form){
    	 var queryIndex = form.action.indexOf('?');
    	 var inputsHTML = new String();
    	 if( queryIndex >= 0 ){
             var queryString = form.action.substring(queryIndex+1);
             if( queryString.indexOf('#') >= 0 ){
                 queryString = queryString.substr(0, queryString.indexOf('#'));
             }
             var splittedQuery = queryString.split('&');
             for( index in splittedQuery ){
                 var splittedVar = splittedQuery[index].split('=');
                 var varName = splittedVar[0];
                 var varValue = splittedVar[1] || '';
                 inputsHTML+='<input type="hidden" name="'+varName+'" value="'+varValue+'">';
             }
         }
    	return inputsHTML;
    },
    customize: function(pseudo, rating, imgSrc, action){
        _pseudo = pseudo || this.pseudo;
        _rating = rating || this.rating;
        _imgSrc = imgSrc || this.imgSrc;
        _action = action || this.action;
    
		
        var spanPseudo = this.getElement('emailCatcherContent').getElementsByTagName('span')[2];
        spanPseudo.innerHTML = '<span>'+_pseudo+'</span>';
		
        var img = this.getElement('emailCatcherContent').getElementsByTagName('img')[0];
        img.src= _imgSrc;

        var form = this.getElement('emailCatcherContent').getElementsByTagName('form')[0];
        if( _action ){
            form.action = _action;
        }
        
        this.getElement('moreVars').innerHTML = this.generateInputsHiddens(form);
        
        var spanObject = this.getElement('emailCatcherContent').getElementsByTagName('span')[1];
        var randId= 'obj_'+parseInt(Math.random()*100);
        spanObject.id= randId;
		
		
        var flashvarsEmailCatcher = {
            "rating" : _rating
        };
        var paramsSWF = {
            menu : "false",
            quality : "high",
            AllowScriptAccess : "always",
            wmode: "transparent"
        };
        var attributesSWF={
            'class': 'transparent'
        }
        swfobject.embedSWF("/images/home/common/layer.swf", randId, "160", "120", "9.0.0","/images/common/expressInstall.swf", flashvarsEmailCatcher, paramsSWF, attributesSWF);
				   
        var input = this.getElement('emailCatcherContent').getElementsByTagName('input')[0];

        $(input).one('focus',function(){
            setFocusPosition(this, 0);
        });
        $(input).trigger('focus');
    }
}

/*
   **  Sets the caret (cursor) position of the specified text field.
   **  Valid positions are 0-oField.length.
   */
function setFocusPosition (oField, iPos) {
    // IE Support
	if (document.selection) {
        // Set focus on the element
        oField.focus ();
        // Create empty selection range
        var oSel = document.selection.createRange();
        // Move selection start and end to 0 position
        oSel.moveStart ('character', -oField.value.length);
        // Move selection start and end to desired position
        oSel.moveStart ('character', iPos);
        oSel.moveEnd ('character', 0);
        oSel.select ();
    }
    // Firefox support
    else if (oField.selectionStart || oField.selectionStart == '0') {
        oField.selectionStart = iPos;
        oField.selectionEnd = iPos;
        oField.focus ();
    }
}