var help_table;

function print_preview() {

    var tab = get_active_tab();
    var html = tab.element_.html();
    $('#printHTML').val(html);
    $('#printForm').get(0).submit();
}

function make_drop_down(options, callback, top, left) {
    var t = document.createElement("TABLE");
    var body = document.createElement("TBODY");
    t.appendChild(body);

    for(var i=0; i<options.length; ++i) {
        var tr = document.createElement("TR");
        var td = document.createElement("TD");
        $(td).text(options[i]);
        $(td).css({padding: "5px 10px", cursor: 'hand'});
        $(td).css({cursor: 'pointer'});
        $(td).css({fontSize: '12px'});

        tr.appendChild(td);
        body.appendChild(tr);

        (function (myidx) {
            $(td).click(function (e) {
                callback(myidx);
                t.parentNode.removeChild(t);
            });
        })(i);

        $(td).mouseover(function (e) {
            $(this).css({'background': '#DCEBFD'});
        });
        $(td).mouseout(function (e) {
            $(this).css({'background': '#fafbfb'});
        });
    }

    $(t).css({
        position: 'absolute', 
        border: '1px solid #DDE9F9',
        background: '#fafbfb',
        top: top,
        left: left
    });

    document.body.appendChild(t);
    return t;
}

function select_help(i) {
    help_table = null;
    var orginal_src = $('#helpspan').attr('src');
    var new_src = orginal_src.replace(/icons([a-zA-Z\-]*)/,"icons");
    $('#helpspan').attr('src',new_src);
    if (i==0) {
        var lang = getLANG().toLowerCase();
        window.location.href = './'+lang+'/demo-editor.html';
    }
    if (i == 1) {
        var lang = getLANG().toLowerCase();
        var url = './'+lang+'/help.html';
        window.open(url);
    }
}

function show_help() {
    var p = $('#helpspan').position();
    if(help_table && help_table.style.display != "none"){
        help_table.parentNode.removeChild(help_table);
        var orginal_src1 = $('#helpspan').attr('src');
        var new_src1 = orginal_src1.replace(/icons([a-zA-Z\-]*)/,"icons");
        $('#helpspan').attr('src', new_src1);
        help_table = null;
    }
    else {
        help_table = make_drop_down(['Interactive Demo', 'Correcting Words'], select_help, p.top + 25, p.left - 90);    
        $(help_table).attr("id", "help_table");
		    show_popup("#help_table", function() {
					if (help_table) {
						var orginal_src1 = $('#helpspan').attr('src');
						var new_src1 = orginal_src1.replace(/icons([a-zA-Z\-]*)/,"icons");
						$('#helpspan').attr('src', new_src1);
						$(help_table).remove();
						help_table = null;
					}
					});
        var orginal_src = $('#helpspan').attr('src');
        var new_src = orginal_src.replace(/icons([a-zA-Z\-]*)/,"icons-pushed");
        $('#helpspan').attr('src', new_src);
    }
};

// Setting, reading and erasing cookie
function setCookie(key,value,p) {
    var t = new Date();
    var cookieDate = new Date(t.getFullYear()+1, t.getMonth(), t.getDate());
    if (!p) {
		document.cookie = key + "=" + escape(value) + ";expires=" + cookieDate.toGMTString() + "; path=/";
	}
	else {
		document.cookie = key + "=" + escape(value) + ";expires=" + cookieDate.toGMTString() + "; path="+p.toLowerCase();
		
	}
}

function readCookie(key) {
    var cookie = document.cookie;

    var first = cookie.indexOf(key + "=");

    // cookie exists
    if (first >= 0) {
        var str = cookie.substring(first, cookie.length);
        var last = str.indexOf(";");

        // if last cookie
        if (last < 0) last = str.length;

        // get cookie value
        str = str.substring(0, last).split("=");
        return unescape(str[1]);
    } else {
        return null;
    }
}

function eraseCookie(key) {
    var cookieDate = new Date(1970);
    document.cookie = key + "= ; expires=" + cookieDate.toGMTString() + "; path=/";
}

function changeCookie() {
    eraseCookie();
    if (navigator.cookieEnabled) {
        setCookie("lang", __lang__);
    }
}

function getCurrentLangCookieValue() {
    var defaultLang = 'Hindi';
    if (navigator.cookieEnabled) {
        lang = readCookie("lang");
        if ( lang != null ) {
            return lang;
        }
    }
    return "Hindi";
}

function continue_demo(){
    window.location.href = '../'+$('#demo_langlist').val().toLowerCase()+'/demo-editor2.html';
}

function replay_animation(){
    var swf_obj = document.getElementById('animation');
    if(swf_obj&&!swf_obj.IsPlaying())
        swf_obj.Play();
    window.setTimeout('replay_animation()',1000);        
}


