var Browser = navigator.appName;
var Micro = Browser.indexOf("Microsoft");
var IE = false;

if(Micro >= 0) {IE = true;}

var expDays = 10000;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

var thread_coll='0', divs=document.getElementsByTagName('div'), spans=document.getElementsByTagName('span');

cookieValuetext = GetCookie('fcp_prefs2'); // 0 = up ; 1 = stick ; 2 = Closed ; 3 = Right ; 4 = collapse ; 5 = threadlist ; 6 = coll_default ; (status)
if(cookieValuetext == null) {
    cookieValuetext ='0|0|0|1|1|1|0|';
}
cookieValuearray = cookieValuetext.split('|');

if( cookieValuearray[4] == 1 ) {
    thread_coll=1;
}
else {
    thread_coll=0;
}

collapseList = cookieValuearray[7];
collapseArray = collapseList.split(',');

var threadcount = 0;
var threadlist = new Array();
function add_thread(thread) {
    threadlist[threadcount] = thread;
    threadcount++;
}

function coll_init(){
    if(!IE) {
        if(thread_coll==1){
            document.getElementById('boxcheck').style.display='inline';
            document.getElementById('box').style.display='none';
            if (cookieValuearray[6]==1) {
                for(i=0;i<divs.length;i++){
                    if(divs[i].id.match('thread')) divs[i].style.display='none';
                    if(divs[i].id.match('coll')) divs[i].style.display='inline';
                }
                for(i=0;i<spans.length;i++){
                    if(spans[i].id.match('minus')) spans[i].style.display='none';
                    if(spans[i].id.match('plus')) spans[i].style.display='inline';
                }
            }
            else {
                for(i=0;i<divs.length;i++){
                    if(divs[i].id.match('thread')) divs[i].style.display='inline';
                    if(divs[i].id.match('coll')) divs[i].style.display='inline';
                }
                for(i=0;i<spans.length;i++){
                    if(spans[i].id.match('minus')) spans[i].style.display='inline';
                    if(spans[i].id.match('plus')) spans[i].style.display='none';
                }
            }
        }
        if( cookieValuearray[0] == 1 ){
            document.getElementById('up').style.display = 'none';
            document.getElementById('down').style.display = 'inline';
            document.getElementById('title').style.borderBottom = '';
            document.getElementById('content').style.display = 'none';
        }
        if( cookieValuearray[1] == 1 ){
            document.getElementById('stick').style.display = 'none';
            document.getElementById('slide').style.display = 'inline';
            document.getElementById('controlpanel').style.position = 'absolute';
        }
        if( cookieValuearray[3] == 0 ){
            document.getElementById('left').style.display = 'none';
            document.getElementById('right').style.display = 'inline';

            document.getElementById('controlpanel').style.top = "4px";
            document.getElementById('controlpanel').style.left = "4px";
            document.getElementById('controlpanel').style.right = "";
        }
        if( cookieValuearray[5] == 0 ){
            document.getElementById('threadlist').style.display='none';
            document.getElementById('boxchecktl').style.display='none';
            document.getElementById('boxtl').style.display='inline';
        }
        if(thread_coll==1){
            for(j in threadlist) {
                for(i in collapseArray) {
                    if(threadlist[j] == collapseArray[i]) {
                        if (cookieValuearray[6]==0) {
                            document.getElementById('thread' + threadlist[j]).style.display='none';
                            document.getElementById('plus' + threadlist[j]).style.display='inline';
                            document.getElementById('minus' + threadlist[j]).style.display='none';
                        }
                        else {
                            document.getElementById('thread' + threadlist[j]).style.display='inline';
                            document.getElementById('plus' + threadlist[j]).style.display='none';
                            document.getElementById('minus' + threadlist[j]).style.display='inline';
                        }
                    }
                }
            }
        }
        if( cookieValuearray[2] != 1 ) { document.getElementById('controlpanel').style.display='inline'; }
    }
    else {
        if(thread_coll==1){
            if (cookieValuearray[6]==1) {
                for(i=0;i<divs.length;i++){
                    if(divs[i].id.match('thread')) divs[i].style.display='none';
                    if(divs[i].id.match('coll')) divs[i].style.display='inline';
                }
                for(i=0;i<spans.length;i++){
                    if(spans[i].id.match('minus')) spans[i].style.display='none';
                    if(spans[i].id.match('plus')) spans[i].style.display='inline';
                }
            }
            else {
                for(i=0;i<divs.length;i++){
                    if(divs[i].id.match('thread')) divs[i].style.display='inline';
                    if(divs[i].id.match('coll')) divs[i].style.display='inline';
                }
                for(i=0;i<spans.length;i++){
                    if(spans[i].id.match('minus')) spans[i].style.display='inline';
                    if(spans[i].id.match('plus')) spans[i].style.display='none';
                }
            }
            for(threadlist[j] in threadlist) {
                for(coo_id in collapseArray) {
                    if(threadlist[j] == coo_id) {
                        if (cookieValuearray[6]==1) {
                            document.getElementById('thread' + threadlist[j]).style.display='none';
                            document.getElementById('plus' + threadlist[j]).style.display='inline';
                            document.getElementById('minus' + threadlist[j]).style.display='none';
                        }
                        else {
                            document.getElementById('thread' + threadlist[j]).style.display='inline';
                            document.getElementById('plus' + threadlist[j]).style.display='none';
                            document.getElementById('minus' + threadlist[j]).style.display='inline';
                        }
                    }
                }
            }
        }
        document.getElementById('frameHide').style.display='inline';
        document.getElementById('postRequired').style.margin='0.1em';
        document.getElementById('postOptional').style.margin='0.1em';
    }
}

function coll_switch(){
    if(thread_coll==0){
        document.getElementById('boxcheck').style.display='inline';
        document.getElementById('box').style.display='none';
        thread_coll=1;
        if (cookieValuearray[6]==1) {
            for(i=0;i<divs.length;i++){
                if(divs[i].id.match('thread')) divs[i].style.display='none';
                if(divs[i].id.match('coll')) divs[i].style.display='inline';
            }
            for(i=0;i<spans.length;i++){
                if(spans[i].id.match('minus')) spans[i].style.display='none';
                if(spans[i].id.match('plus')) spans[i].style.display='inline';
            }
        }
        else {
            for(i=0;i<divs.length;i++){
                if(divs[i].id.match('thread')) divs[i].style.display='inline';
                if(divs[i].id.match('coll')) divs[i].style.display='inline';
            }

            for(i=0;i<spans.length;i++){
                if(spans[i].id.match('minus')) spans[i].style.display='inline';
                if(spans[i].id.match('plus')) spans[i].style.display='none';
            }
        }

        cookieValuearray[4]=1;
        cookieValuetext = cookieValuearray.join('|');
        SetCookie('fcp_prefs2', cookieValuetext, exp);

        for(threadlist[j] in threadlist) {
            for(coo_id in collapseArray) {
                if(threadlist[j] == coo_id) {
                    if (cookieValuearray[6]==1) {
                        document.getElementById('thread' + threadlist[j]).style.display='none';
                        document.getElementById('plus' + threadlist[j]).style.display='inline';
                        document.getElementById('minus' + threadlist[j]).style.display='none';
                    }
                    else {
                        document.getElementById('thread' + threadlist[j]).style.display='inline';
                        document.getElementById('plus' + threadlist[j]).style.display='none';
                        document.getElementById('minus' + threadlist[j]).style.display='inline';
                    }
                }
            }
        }
    }
    else {
        document.getElementById('box').style.display='inline';
        document.getElementById('boxcheck').style.display='none';
        thread_coll=0;
        for(i=0;i<divs.length;i++){
            if(divs[i].id.match('thread')) divs[i].style.display='inline';
            if(divs[i].id.match('coll')) divs[i].style.display='none';
        }
        cookieValuearray[4]=0;
        cookieValuetext = cookieValuearray.join('|');
        SetCookie('fcp_prefs2', cookieValuetext, exp);
    }
}

function coll_switch_ie(){
    if(thread_coll==0){
        thread_coll=1;
        if (cookieValuearray[6]==1) {
            for(i=0;i<divs.length;i++){
                if(divs[i].id.match('thread')) divs[i].style.display='none';
                if(divs[i].id.match('coll')) divs[i].style.display='inline';
            }
            for(i=0;i<spans.length;i++){
                if(spans[i].id.match('minus')) spans[i].style.display='none';
                if(spans[i].id.match('plus')) spans[i].style.display='inline';
            }
        }
        else {
            for(i=0;i<divs.length;i++){
                if(divs[i].id.match('thread')) divs[i].style.display='inline';
                if(divs[i].id.match('coll')) divs[i].style.display='inline';
            }
            for(i=0;i<spans.length;i++){
                if(spans[i].id.match('minus')) spans[i].style.display='inline';
                if(spans[i].id.match('plus')) spans[i].style.display='none';
            }
        }

        cookieValuearray[4]=1;
        cookieValuetext = cookieValuearray.join('|');
        SetCookie('fcp_prefs2', cookieValuetext, exp);

        for(threadlist[j] in threadlist) {
            for(coo_id in collapseArray) {
                if(threadlist[j] == coo_id) {
                    if (cookieValuearray[6]==1) {
                        document.getElementById('thread' + threadlist[j]).style.display='none';
                        document.getElementById('plus' + threadlist[j]).style.display='inline';
                        document.getElementById('minus' + threadlist[j]).style.display='none';
                    }
                    else {
                        document.getElementById('thread' + threadlist[j]).style.display='inline';
                        document.getElementById('plus' + threadlist[j]).style.display='none';
                        document.getElementById('minus' + threadlist[j]).style.display='inline';
                    }
                }
            }
        }
    }
    else {
        thread_coll=0;
        for(i=0;i<divs.length;i++){
            if(divs[i].id.match('thread')) divs[i].style.display='inline';
            if(divs[i].id.match('coll')) divs[i].style.display='none';
        }
        cookieValuearray[4]=0;
        cookieValuetext = cookieValuearray.join('|');
        SetCookie('fcp_prefs2', cookieValuetext, exp);
    }
}

function add_thread_id(thread) {
    for(x in collapseArray) {
        if(thread==collapseArray[x]) return;
    }

    if(collapseArray.length > 99) collapseArray.pop();
    collapseArray.unshift(thread);

    cookieValuearray[7] = collapseArray.join();
    cookieValuetext = cookieValuearray.join('|');
    SetCookie('fcp_prefs2', cookieValuetext, exp);
}

function remove_thread_id(thread) {
    for(x in collapseArray) {
        if(thread==collapseArray[x]) {
            collapseArray.splice(x,1);
            break;
        }
    }

    cookieValuearray[7] = collapseArray.join();
    cookieValuetext = cookieValuearray.join('|');
    SetCookie('fcp_prefs2', cookieValuetext, exp);
}


function switch_thread(thread) {
    if(document.getElementById('thread' + thread).style.display=='inline'){
        document.getElementById('thread' + thread).style.display='none';
        document.getElementById('plus' + thread).style.display='inline';
        document.getElementById('minus' + thread).style.display='none';
        if(cookieValuearray[6]==0) {
            add_thread_id(thread);
        }
        else {
            remove_thread_id(thread);
        }
    }
    else {
        document.getElementById('thread' + thread).style.display='inline';
        document.getElementById('minus' + thread).style.display='inline';
        document.getElementById('plus' + thread).style.display='none';
        if(cookieValuearray[6]==1) {
            add_thread_id(thread);
        }
        else {
            remove_thread_id(thread);
        }
    }
}

function switch_subjectlist(){
    if(document.getElementById('subjectlist').style.display=='inline'){
        document.getElementById('subjectlist').style.display='none';
        document.getElementById('boxchecktl').style.display='none';
        document.getElementById('boxtl').style.display='inline';
        cookieValuearray[5]=0;
        cookieValuetext = cookieValuearray.join('|');
        SetCookie('fcp_prefs2', cookieValuetext, exp);
    }
    else {
        document.getElementById('subjectlist').style.display='inline';
        document.getElementById('boxchecktl').style.display='inline';
        document.getElementById('boxtl').style.display='none';
        cookieValuearray[5]=1;
        cookieValuetext = cookieValuearray.join('|');
        SetCookie('fcp_prefs2', cookieValuetext, exp);
    }
}

function close_panel()
{
    if (
    confirm("Are you sure to close the Fchan-Control-Panel? You will only be able to restore it by clearing your cookies!")
    ) {
        document.getElementById('controlpanel').style.display='none';
        cookieValuearray[2]=1;
        cookieValuetext = cookieValuearray.join('|');
        SetCookie('fcp_prefs2', cookieValuetext, exp);
    }
}

function fcp_left()
{
    document.getElementById('left').style.display = 'none';
    document.getElementById('right').style.display = 'inline';

    document.getElementById('controlpanel').style.top = "4px";
    document.getElementById('controlpanel').style.left = "4px";
    document.getElementById('controlpanel').style.right = "";
    cookieValuearray[3]=0;
    cookieValuetext = cookieValuearray.join('|');
    SetCookie('fcp_prefs2', cookieValuetext, exp);
};

function fcp_right()
{
    document.getElementById('left').style.display = 'inline';
    document.getElementById('right').style.display = 'none';

    document.getElementById('controlpanel').style.top = "4px";
    document.getElementById('controlpanel').style.left = "";
    document.getElementById('controlpanel').style.right = "4px";
    cookieValuearray[3]=1;
    cookieValuetext = cookieValuearray.join('|');
    SetCookie('fcp_prefs2', cookieValuetext, exp);
};

function fcp_up()
{
    document.getElementById('up').style.display = 'none';
    document.getElementById('down').style.display = 'inline';
    document.getElementById('title').style.borderBottom = '';
    document.getElementById('content').style.display = 'none';
    cookieValuearray[0]=1;
    cookieValuetext = cookieValuearray.join('|');
    SetCookie('fcp_prefs2', cookieValuetext, exp);
};

function fcp_down()
{
    document.getElementById('down').style.display = 'none';
    document.getElementById('up').style.display = 'inline';
    document.getElementById('content').style.display = '';
    document.getElementById('title').style.borderBottom = '1px solid slategray';
    cookieValuearray[0]=0;
    cookieValuetext = cookieValuearray.join('|');
    SetCookie('fcp_prefs2', cookieValuetext, exp);
};

function collapseall() {
    for(i=0;i<divs.length;i++){
        if(divs[i].id.match('thread')) divs[i].style.display='none';
    }
    for(i=0;i<spans.length;i++){
        if(spans[i].id.match('minus')) spans[i].style.display='none';
        if(spans[i].id.match('plus')) spans[i].style.display='inline';
    }

    cookieValuearray[6] = 1;
    cookieValuearray[7] = '';
    cookieValuetext = cookieValuearray.join('|');
    SetCookie('fcp_prefs2', cookieValuetext, exp);
}

function expandall() {
    for(i=0;i<divs.length;i++){
        if(divs[i].id.match('thread')) divs[i].style.display='inline';
    }
    for(i=0;i<spans.length;i++){
        if(spans[i].id.match('minus')) spans[i].style.display='inline';
        if(spans[i].id.match('plus')) spans[i].style.display='none';
    }

    cookieValuearray[6] = 0;
    cookieValuearray[7] = '';
    cookieValuetext = cookieValuearray.join('|');
    SetCookie('fcp_prefs2', cookieValuetext, exp);
}

function check_new(board,num)
{
    nums = GetCookie('nums');
    if(nums == null) { nums ='0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0'; }
    numsarr = nums.split('|');
    if (num > numsarr[board]) { document.getElementById('num_' + board).style.color = 'red'; }
    numsarr[board]=num;
    nums = numsarr.join('|');
    SetCookie('nums', nums, exp);
}

function fcp_stick()
{
    document.getElementById('stick').style.display = 'none';
    document.getElementById('slide').style.display = 'inline';
    document.getElementById('controlpanel').style.position = 'absolute';
    cookieValuearray[1]=1;
    cookieValuetext = cookieValuearray.join('|');
    SetCookie('fcp_prefs2', cookieValuetext, exp);
};

function fcp_slide()
{
    document.getElementById('slide').style.display = 'none';
    document.getElementById('stick').style.display = 'inline';
    document.getElementById('controlpanel').style.position = 'fixed';
    cookieValuearray[1]=0;
    cookieValuetext = cookieValuearray.join('|');
    SetCookie('fcp_prefs2', cookieValuetext, exp);
};

function zoomIn(caller, whichpic, width, height) {
	var image = document.getElementById(whichpic);
    var link = image.parentNode;
    
    var thumbnail = image.src;
    var full_img = link.href;
    var tnwidth = image.width;
    var tnheight = image.height;
    
    image.width = width;
    image.height = height;
    image.src = link.href;
    link.setAttribute('onclick', "zoomOut('"+whichpic+"','"+thumbnail+"',"+tnwidth+","+tnheight+",'"+caller.id+"');return false;");
    caller.setAttribute('onclick', "zoomOut('"+whichpic+"','"+thumbnail+"',"+tnwidth+","+tnheight+",'"+caller.id+"');return false;");
    return false;
}

function zoomOut(whichpic, thumbnail, width, height, button) {
	var image = document.getElementById(whichpic);
	var caller = document.getElementById(button);
    var link = image.parentNode;
    caller.setAttribute('onclick', "zoomIn(this,'"+whichpic+"',"+image.width+","+image.height+"); return false;");
    link.setAttribute('onclick', "");
    image.src = thumbnail;
    image.width = width;
    image.height = height;
    return true;
}