var $_j = jQuery.noConflict();
var _bar_state=0,_narrow,_wide;
var _url = document.location + "";
var _host = location.host + "";
var _flag_base64 = "/b~~";
var _flag_rot13 = "/r~~";
var array_cookie = new Array();

function serialize( a ) {
    var a_php = "";
    var total = 0;
    for (var key in a)
    {
        ++ total;
        a_php = a_php + "s:" + String(key).length +
        ":\"" + String(key) + "\";s:" +
        String(a[key]).length + ":\"" +
        String(a[key]) + "\";";
    }
    a_php = "a:" + total + ":{" + a_php + "}";
    return a_php;

}



function unserialize ( inp ) { 

    error = 0;
    if (inp == "" || inp.length < 2) {
        errormsg = "input is too short";
        return;
    }
    var val, kret, vret, cval;
    var type = inp.charAt(0);
    var cont = inp.substring(2);
    var size = 0, divpos = 0, endcont = 0, rest = "", next = "";

    switch (type) {
        case "N": // null
        if (inp.charAt(1) != ";") {
            errormsg = "missing ; for null";
        }
        // leave val undefined
        rest = cont;
        break;
        case "b": // boolean
        if (!/[01];/.test(cont.substring(0,2))) {
            errormsg = "value not 0 or 1, or missing ; for boolean";
        }
        val = (cont.charAt(0) == "1");
        rest = cont.substring(1);
        break;
        case "s": // string
        val = "";
        divpos = cont.indexOf(":");
        if (divpos == -1) {
            errormsg = "missing : for string";
            break;
        }
        size = parseInt(cont.substring(0, divpos));
        if (size == 0) {
            if (cont.length - divpos < 4) {
                errormsg = "string is too short";
                break;
            }
            rest = cont.substring(divpos + 4);
            break;
        }
        if ((cont.length - divpos - size) < 4) {
            errormsg = "string is too short";
            break;
        }
        if (cont.substring(divpos + 2 + size, divpos + 4 + size) != "\";") {
            errormsg = "string is too long, or missing \";";
        }
        val = cont.substring(divpos + 2, divpos + 2 + size);
        rest = cont.substring(divpos + 4 + size);
        break;
        case "i": // integer
        case "d": // float
        var dotfound = 0;
        for (var i = 0; i < cont.length; i++) {
            cval = cont.charAt(i);
            if (isNaN(parseInt(cval)) && !(type == "d" && cval == "." && !dotfound++)) {
                endcont = i;
                break;
            }
        }
        if (!endcont || cont.charAt(endcont) != ";") {
            errormsg = "missing or invalid value, or missing ; for int/float";
        }
        val = cont.substring(0, endcont);
        val = (type == "i" ? parseInt(val) : parseFloat(val));
        rest = cont.substring(endcont + 1);
        break;
        case "a": // array
        if (cont.length < 4) {
            errormsg = "array is too short";
            return;
        }
        divpos = cont.indexOf(":", 1);
        if (divpos == -1) {
            errormsg = "missing : for array";
            return;
        }
        size = parseInt(cont.substring(1, divpos - 1));
        cont = cont.substring(divpos + 2);
        val = new Array();
        if (cont.length < 1) {
            errormsg = "array is too short";
            return;
        }
        for (var i = 0; i + 1 < size * 2; i += 2) {
            kret = unserialize(cont, 1);
            if (error || kret[0] == undefined || kret[1] == "") {
                errormsg = "missing or invalid key, or missing value for array";
                return;
            }
            vret = unserialize(kret[1], 1);
            if (error) {
                errormsg = "invalid value for array";
                return;
            }
            val[kret[0]] = vret[0];
            cont = vret[1];
        }
        if (cont.charAt(0) != "}") {
            errormsg = "missing ending }, or too many values for array";
            return;
        }
        rest = cont.substring(1);
        break;
        case "O": // object
        divpos = cont.indexOf(":");
        if (divpos == -1) {
            errormsg = "missing : for object";
            return;
        }
        size = parseInt(cont.substring(0, divpos));
        var objname = cont.substring(divpos + 2, divpos + 2 + size);
        if (cont.substring(divpos + 2 + size, divpos + 4 + size) != "\":") {
            errormsg = "object name is too long, or missing \":";
            return;
        }
        var objprops = unserialize("a:" + cont.substring(divpos + 4 + size), 1);
        if (error) {
            errormsg = "invalid object properties";
            return;
        }
        rest = objprops[1];
        var objout = "function " + objname + "(){";
        for (key in objprops[0]) {
            objout += "" + key + "=objprops[0]['" + key + "'];";
        }
        objout += "}val=new " + objname + "();";
        eval(objout);
        break;
        default:
        errormsg = "invalid input type";
    }
    return (arguments.length == 1 ? val : [val, rest]);
}

function isset( mixed_var ) {
    var i = 0, argc = arguments.length, argv = arguments, set=true;
    for (i = 0; i< argc; i++){
        if( argv[i] == undefined ){
            set = false;
            break;
        }
    }
    return set;
}










function str_rot13( str ) {
    return str.replace(/[A-Za-z]/g, function (c) {
        return String.fromCharCode((((c = c.charCodeAt(0)) & 223) - 52) % 26 + (c & 32) + 65);
    });
}

function base64_encode( data ) {
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i=0, enc='';

    do { // pack three octets into four hexets
        o1 = data.charCodeAt(i++);
        o2 = data.charCodeAt(i++);
        o3 = data.charCodeAt(i++);

        bits = o1<<16 | o2<<8 | o3;

        h1 = bits>>18 & 0x3f;
        h2 = bits>>12 & 0x3f;
        h3 = bits>>6 & 0x3f;
        h4 = bits & 0x3f;

        // use hexets to index into b64, and append result to encoded string
        enc += b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
    } while (i < data.length);

    switch( data.length % 3 ){
        case 1:
        enc = enc.slice(0, -2) + '==';
        break;
        case 2:
        enc = enc.slice(0, -1) + '=';
        break;
    }

    return enc;
}

function base64_decode( data ) {
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i=0, enc='';

    do {
        h1 = b64.indexOf(data.charAt(i++));
        h2 = b64.indexOf(data.charAt(i++));
        h3 = b64.indexOf(data.charAt(i++));
        h4 = b64.indexOf(data.charAt(i++));

        bits = h1<<18 | h2<<12 | h3<<6 | h4;

        o1 = bits>>16 & 0xff;
        o2 = bits>>8 & 0xff;
        o3 = bits & 0xff;

        if (h3 == 64)      enc += String.fromCharCode(o1);
        else if (h4 == 64) enc += String.fromCharCode(o1, o2);
        else               enc += String.fromCharCode(o1, o2, o3);
    } while (i < data.length);
    return enc;
}

function _setCookie (name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}
function _getCookie(name) {
    var cookie = " " + document.cookie;
    var search = " " + name + "=";
    var setStr = null;
    var offset = 0;
    var end = 0;
    if (cookie.length > 0) {
        offset = cookie.indexOf(search);
        if (offset != -1) {
            offset += search.length;
            end = cookie.indexOf(";", offset)
            if (end == -1) {
                end = cookie.length;
            }
            setStr = unescape(cookie.substring(offset, end));
        }
    }
    return(setStr);
}

function mirr_resize(){
    var animate_params,new_sign;
    if (_bar_state == 0){
        animate_params = {width: _wide};
        new_sign = "&gt;&lt;";
        _bar_state = 1;
    }else{
        animate_params = {width: 0};
        new_sign = "&lt;&gt;";
        _bar_state = 0;
    }
    $_j("#_penal_td_").animate(
    animate_params,        500,        "linear",
    function(){
        var new_content = $_j("#addr_panel").html();
        $_j("#mirror_resize span").html(new_sign);
    }
    );
}

var encoding = 0;
function _encoding(coding){
    encoding = coding;
}

function mirror_url_submit(){
    var u = $_j("input#mirror_url").attr('value');
    if (u.slice(0,7).toLowerCase() == 'http://'){
        u = u.slice(7);
    }
    if (encoding == 1){
        u = str_rot13(u);
        len = u.length;
        u = "r~~"+len+"/"+u;
    }else if (encoding == 2){
        u = base64_encode(u);
        len = u.length;
        u = "b~~"+len+"/"+u;
    }
    window.location.href='/browser.php/'+u;
    return false;
}

function _rewerite(name,value){
    var cookie = _getCookie("user");
    if (cookie){
        array_cookie = unserialize(cookie);
    }
    array_cookie[name] = value;
    cookie = serialize(array_cookie);
    _setCookie("user", cookie, "/", "/");
}

function _cleaning_cookies(){
    _rewerite("cookies","death");
}

var out_images = 0;
var out_flash = 0;
var out_js = 0;
function _images_cookies(){
    out_images = $_j("input#pr_images").attr("checked");
    if (out_images){
        _rewerite("images", "true");
    }else{
        _rewerite("images", "0");
    }

}

function _flash_cookies(){
    out_flash = $_j("input#pr_flash").attr("checked");
    if (out_flash){
        _rewerite("flash", "true");
    }else{
        _rewerite("flash", "0");
    }
}

function _js_cookies(){
    out_js = $_j("input#pr_js").attr("checked");
    if (out_js){
        _rewerite("js", "true");
    }else{
        _rewerite("js", "0");
    }
}

function decoding_url(surl,_flag_length,_flag_){
    surl = surl.substr(_flag_length);
    var _pos_ = surl.indexOf("/");
    var _int = surl.substr(0,_pos_);
    _pos_ = _int.length + 1;
    _int = parseInt(_int);
    var _decod_ = surl.substr(_pos_,_int);
    var _nocod_ = surl.substr((_pos_+_int));
    if (_flag_ == _flag_base64){
        _decod_ = base64_decode(_decod_);
    }else if (_flag_ == _flag_rot13){
        _decod_ = str_rot13(_decod_);
    }
    if (_url.substr(0,5) =='https'){
        return 'https://' + _decod_ + _nocod_;
    }else{
        return 'http://'  + _decod_ + _nocod_;
    }

}

function _show_current_url(){
    var _flag_length = _flag_base64.length;
    var _pos_ = _url.indexOf(_host) + _host.length;
    var surl = _url.substr(_pos_);
    var _nev_url;
    if (surl.substr(0,_flag_length) == _flag_base64){
        _url = decoding_url(surl,_flag_length,_flag_base64);
    }else if(surl.substr(0,_flag_length) == _flag_rot13){
        _url = decoding_url(surl,_flag_length,_flag_rot13);
    }else{
        surl= surl.substr(1);
        _pos_ = surl.indexOf("/");
        surl = surl.substr(_pos_);
        if (surl.substr(0,_flag_length) == _flag_base64){
            _nev_url = decoding_url(surl,_flag_length,_flag_base64);
        }else if(surl.substr(0,_flag_length) == _flag_rot13){
            _nev_url = decoding_url(surl,_flag_length,_flag_rot13);
        }
        if (_nev_url != _url){
            $_j("#mirror_url").attr("value",_nev_url);
        }
    }
}

function _default_form(){
    var cookie = _getCookie("user");
    if (cookie){
        array_cookie = unserialize(cookie);
    }
    if (isset(array_cookie["flash"])){
        var _flash = array_cookie["flash"];
    }else{
        var _flash = "";
    }
    if (isset(array_cookie["js"])){
        var _js = array_cookie["js"];
    }else{
        var _js = "";
    }
    if (isset(array_cookie["images"])){
        var _images = array_cookie["images"];
    }else{
        var _images = "";
    }
    if (isset(array_cookie["opacity"])){
        var _opacity = array_cookie["opacity"];
    }else{
        var _opacity = "";
    }

    if (_flash == "true") {
        jQuery("input#pr_flash").attr("checked",true);
    }
    if (_js == "true"){
        $_j("#pr_js").attr("checked",true);
    }
    if (_images == "true"){
        $_j("#pr_images").attr("checked",true);
    }
    if (_opacity){
        _transp(_opacity)
    }
}

function _cleaning_url(){
    $_j("#mirror_url").attr("value","http://");
}

function _transp(percent){
    if (percent < 0 || percent > 60){
        percent = 0;
    }

    for (i=0; i <= percent; i++){
        $_j("#transp" + i).css( "background-color", "black" );
    }
    for (i = 60; i > percent; i--){
        $_j("#transp" + i).css( "background-color", "#d2d2d2" );
    }
    $_j("span#transp_percent").html(percent+"");
    $_j("table#mirror_banner").css("opacity",1-percent/100);
    _rewerite("opacity", percent);
}

// Ajax
function _load_baner_text(){
    $_j.post('/getbaner.php', {
        host: _host
    },
    _onAjaxSuccess);
}

function _onAjaxSuccess(data){
    $_j("#mirr_ad").html(data);
}

jQuery(document).ready(function($){
    $('body').append(_mirror_panel);
    _wide = $("#mirror_banner").width();
    _narrow = $("#mirr_ad").width()+20;
    $("#mirror_banner").width(_narrow).draggable();
    $('#mirror_form').submit(mirror_url_submit);
    $('#mirror_resize').click(mirr_resize);
    _show_current_url();
    _default_form();
    setTimeout(function(){_load_baner_text()}, 1000);
});

