var sessionName, sessionId;
var webRoot,langCode;
var booUseAjax = true;
var strContent = '';

/**
 * Cookie function
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { 
        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(); 
        }
        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 { 
        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]);
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

/**
 * Check if cookies enabled 
 */
function checkCookies() {
    // Set cookie
	var datetime = new Date();
	datetime = new Date(datetime.getTime()+1000*60*60*24);
	document.cookie = 'testCookies=true; expires='+datetime.toGMTString()+';'; 
	// Read cookie
	var cookie = document.cookie;
	cookiename = cookie.substr(0,cookie.search('='));
	cookiewert = cookie.substr(cookie.search('=')+1,cookie.search(';'));
	if(cookiewert == '') {
		cookiewert = cookie.substr(cookie.search('=')+1,cookie.length);
	} 	
	if (cookiewert) {
		return true;
    }
	return false;
}

/**
 * Set special values for using in javascript
 */
function setValues(arrValues) {
    // Session
    sessionName = arrValues[0];
    sessionId = arrValues[1];
    // Path        
    webRoot = arrValues[2];
    // Language
    langCode = arrValues[3];
    // Content
    strContent = arrValues[4];
}

/**
 * When document full loaded and ready
 */
$(document).ready(function() {
    // Open links in new tab if rel="external"
 	$('[rel=external]').each(function() {
  		$(this).attr('target','_blank');
  	}); 
    // IE fix
    if ($('#divContent').height() > $('#divPage').height()) {
        $('#divPage').height($('#divContent').height());         
    }     
});

/**
 * Count clicks on feed content
 */
function feed_countContentClick(feedId,feedContentId) {
    $.ajax({
        type: 'POST',
        timeout: 3000,
        async: false,
        url: webRoot+'index.php',
        data: 'feed_countContentClick=true&feedId='+feedId+'&feedContentId='+feedContentId,
        success: function(data) {
            return true;
        },
        error: function(data,errorText,errorCode) {
            return false;
        }                
    });
    return true;
}

/**
 * Translate text from a input field to a other
 */ 
function language_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);
        });
    }    
}
