/**
 * When document full loaded and ready
 */
$(document).ready(function() {
    // Set copy and paste field for the generated address 
    setGeneratedAddress();
    // Modified exernal links
 	$('[rel=external]').each(function() {
  		$(this).attr('target','_blank');
  	});           
});

/**
 * Cookie function
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

/**
 * Set copy and paste field for the generated address
 */
function setGeneratedAddress() {
    if (($('#divLoginGenerate').css('display') != 'block') && $('#strLoginAlias').val()) {
        $('#divLoginGenerate').fadeIn('slow');   
    } 
    if (!$('#strLoginAlias').val()) {
        $('#divLoginGenerate').fadeOut('slow');   
    }
    if ($('#strGeneratedAddress').length && $('#strLoginAlias').length && $('#intLoginDomainId').length) {
        strAddress = $('#strLoginAlias').val()+'@'+$('#intLoginDomainId option:selected').text();
        if (strAddress.match(/ \(PW\)/gi)) {
            $('#divPasswordInput').fadeIn('normal');
        } else {
            $('#divPasswordInput').fadeOut('normal');
        }
        strAddress = strAddress.replace(/ \(PW\)/,'');
        $('#strGeneratedAddress').val(strAddress);
    }        
}

/**
 * Generate a random email alias
 */
function getRandomAddress() {
	var arrCo      = new Array("b","c","d","f","g","h","j","k","l","m","n","p","r","s","t","v","w","x","y","z","1","2","3","4","5","6","7","8","9");
 	var arrVo      = new Array("a","e","i","o","u");
 	var intC,intV;
    var strRetVal  = '';
 	for (var i=0; i<5; i++) {
        intC = Math.ceil(Math.random() * 1000) % 20;
        intV = Math.ceil(Math.random() * 1000) % 5;
        strRetVal+= arrCo[intC] + arrVo[intV];
	}
    $('#strLoginAlias').val(strRetVal); 
    setGeneratedAddress();
}

/**
 * Translate text from a input field to a other
 */ 
function translateInput(strFromLanguage,strToLanguage,strFromInputId,strToInputId) {
    var strText = $('#'+strFromInputId).val();
    var tmpInputFrom = document.getElementById(strFromInputId);
    var intInputFromLen = tmpInputFrom.value.length;
    var intInputFromStart = tmpInputFrom.selectionStart;
    var intInputFromEnd = tmpInputFrom.selectionEnd;
    var strSelectedText = tmpInputFrom.value.substring(intInputFromStart, intInputFromEnd);
    if (strSelectedText) {
        strText = strSelectedText;
    }
    if (strText) {
        google.language.translate(strText,strFromLanguage,strToLanguage, function(result) {
            $('#'+strToInputId).val($('#'+strToInputId).val()+result.translation);
        });
    }    
}

/**
 * Select all/read/unread/none in inbox
 */
function inboxSelectMessages(type) {
    $("input[name='chkMessage[]']").each(function(index) {
        if (type == 'all') {
            $(this).attr('checked', true);
        }
        if (type == 'none') {
            $(this).attr('checked', false);
        }
        if (type == 'read') {
            if ($(this).attr('rel') == 'read') {
                $(this).attr('checked', true);
            }
            if ($(this).attr('rel') == 'unread') {
                $(this).attr('checked', false);
            }
        }
        if (type == 'unread') {
            if ($(this).attr('rel') == 'unread') {
                $(this).attr('checked', true);
            }
            if ($(this).attr('rel') == 'read') {
                $(this).attr('checked', false);
            }
        }
  });
}

/**
 * Get window size
 */
function getWindowSize() {
    var myWidth = 0, myHeight = 0;
 
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    return [ myWidth, myHeight ];
}

/**
 * PopUnder function
 * by Frank Burian, http://burian.appfield.net
 * @param string - strUrl Url to popUnder content
 * @param string - strTitle Window title
 * @param int - intWidth Width of popUnder window
 * @param int - intHeight Height of popUnder window
 * @param string - strCookieName Name of reload block cookie
 */
function popUnder(strUrl,strTitle,intWidth,intHeight,strCookieName) {
    // Get cookie for reload block
    var strCookieValue = document.cookie;
    var strPattern = eval('/'+strCookieName+'=true/i');
    // Check cookie
    if (!strCookieValue.match(strPattern)) {
        // Set cookie for reload block
        var objDate = new Date();
        objDate = new Date(objDate.getTime()+1000*60*15); // Reload block for 1 hour
        document.cookie = strCookieName+'=true; expires='+objDate.toGMTString()+';';         
        // Set position
    	var intLeft = (screen.width) ? (screen.width-intWidth)/2 : 100;
        var intTop  = (screen.height) ? (screen.height-intHeight)/2 : 100;
        // PopUnder parameter
    	var strParameter = 'width='+intWidth+',height='+intHeight+',top='+intTop+',left='+intLeft+',scrollbars=no,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no,dependent=no';
    	var objWindow = window.open(strUrl,'',strParameter);
        // Set popUnder in the background 
    	objWindow.blur();
        window.focus();
    }
}  
