//dojo.require("dijit.layout.TabContainer");
//dojo.require("dijit.layout.ContentPane");
//dojo.require("dijit.layout.BorderContainer");
dojo.require("dojo.parser");
//dojo.require("dijit.Tooltip");
dojo.require("dijit.form.TextBox");
//dojo.require("dijit.form.CheckBox");            //required for RadioButton
//dojo.require("dijit.form.RadioButton");
//dojo.require("dijit.form.FilteringSelect"); 
//dojo.require("dojox.form.Uploader");

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

function decode(str){
    return Encoder.htmlDecode(str);
}
function encode(str){
    return Encoder.htmlEncode(str);
}

function cancel(){
    hatch();
    var o = document.getElementById('overlay');
    var i = document.getElementById('seed_layer');
    i.style.visibility = 'hidden';
    o.style.visibility = 'hidden';
    o.style.width = "0px;";
    o.style.height = "0px;";
    o.innerHTML = '';
}

function enableForm(id){
    var container = dojo.query('#' + id);
    dojo.query('input', container).forEach(
      function(inputElem){
        inputElem.disabled = 'enabled';
      }
    )        
}

function hatch(f){
    var over = document.getElementById('overlay');
    if(f) {
        var over = document.getElementById('overlay');
        var vs = dojo.window.getBox();
        over.style.position = 'absolute';
        over.style.width = vs.w + 'px';
        over.style.height=getDocHeight() + 'px';
        over.style.visibility = 'visible';
        over.style.left = '0px';
        over.style.top = '0px';
        return;
    }
    over.style.visible = false;
    over.style.height = '0px';
    over.style.width = '0px';
    
}


function seed(board){
    hatch(1);
    
    var over = document.getElementById('overlay');
    over.innerHTML = "<div class='seed_layer' id='seed_layer'></div>";

    var vs = dojo.window.getBox();
    over.style.width = vs.w + 'px';
    over.style.height=getDocHeight() + 'px';
    over.style.visibility = 'visible';
    var html = "<form id='seedForm' method='post' action='/Local/seed/seed.php'><input type='hidden' name='board' value='" + board + "'>";
    html = html + "<div style='color:#993233;text-align:center;font-size:1.2em'>Clip an Article to Bullrabbit.com <a href='javascript:cancel()'><img style='float:right' src='/Local/seed/images/x.png' title='Cancel'></a></div><br>";
    html = html + "<table>";
    html = html + "<tr>";
    html = html + "<th title='Copy the URL and Paste Here'>Paste Link:</th><td><input dojoType='dijit.form.TextBox' id='link' name='link' style='width:280px' value=''><a href='javascript:checkUrl()' title='Check this URL'><img src='/Local/seed/images/go.png' style='vertical-align:middle;padding-left:4px;padding-bottom:2px;'></a></td>";
    html = html + "</tr>";
    html = html + "<tr>";
    html = html + "<th>Source:</th><td><input dojoType='dijit.form.TextBox' id='source' name='source' style='width:280px' disabled></td>";
    html = html + "</tr>";
    html = html + "<tr>";
    html = html + "<th>Article Title:</th><td><input dojoType='dijit.form.TextBox' id='title' name='title' style='width:280px' disabled></td>";
    html = html + "</tr>";
    html = html + "<tr>";
    html = html + "<th>Article Image:</th><td><input dojoType='dijit.form.TextBox' id='image' name='image' style='width:280px' disabled></td>";
    html = html + "</tr>";
    html = html + "<tr>";
    html = html + "<th>Excerpt:</th><td><textarea dojoType='dijit.form.TextArea' id='excerpt' name='excerpt' style='width:280px;height:120px' disabled></textarea></td>";
    html = html + "</tr>";
    html = html + "<tr>";
    html = html + "<th>Your Comment:</th><td><textarea dojoType='dijit.form.TextArea' id='comment' name='comment' style='width:280px;height:120px' disabled></textarea></td>";
    html = html + "</tr>";
    html = html + "</table>";
    html = html + "<div style='width:100%;text-align:center'><br><a href='javascript:go()'><img src='/Local/seed/images/cliparticle.jpg' title='Create Post on Bullrabbit.com'></a></div>";
    html = html + "</form><div id='imgpop' name='imgpop' class='imgpop'></div>";
    //html = html + "<div style='text-align:center'>&copy; 2011 Bullrabbit.com ALL RIGHTS RESERVED</div>";
    
    var obj = document.getElementById('seed_layer');
    dw = 480;
    dh = 440;
    obj.style.width = dw + 'px';
    obj.style.height = dh + 'px';
    obj.style.background = 'white';
    obj.style.border = 'solid 2px';
    obj.style.padding = '2.5% 2.5%';
    obj.style.position = 'absolute';

    
    obj.style.left = vs.l + (vs.w/2 - dw/2) + 'px';
    obj.style.top = vs.t + (vs.h/2 - dh/2) + 'px';
    //obj.style.marginLeft = vs.w/2 - 250;
    //obj.style.marginTop = ;
    obj.innerHTML = html;
   
    obj.style.visibility = 'visible';
    
}

function populate(data){
    document.getElementById('overlay').style.cursor = 'auto';
    
    var title = dojo.byId('title');
    var excerpt = dojo.byId('excerpt');
    var image = dojo.byId('image');
    var source = dojo.byId('source');
    var comment = dojo.byId('comment');
    var excerpt = dojo.byId('excerpt');
    
    if(data.og !== null){
        if('title' in data.og)
            title.value = decode(data.og.title);
        if('site_name' in data.og)
            source.value = decode(data.og['site_name']);
        if('description' in data.og)
            excerpt.value = decode(data.og.description);
        if('image' in data.og)
            selectImage(data.og.image);
    }

    if(data.meta !== null){
        if('title' in data.meta)
            title.value = decode(data.meta.title);
        if ('author' in data.meta)
            source.value += ' (' + decode(data.meta.author) + ')';
        if ('description' in data.meta )
            excerpt.value = decode(data.meta.description);
        if('image' in data.meta)
            selectImage(data.meta.image);
    }
    
    
    source.disabled = false;
    title.disabled = false;
    image.disabled = false;
    comment.disabled = false;
    excerpt.disabled = false;
    
}

function selectImage(url){
    document.getElementById('image').value = url;
    return;
    
    var pop = document.getElementById('imgpop');
    pop.style.width = '240px';
    pop.style.height= '210px';
    html = "<h3 style='color:#993233;text-align:center;font-size:1.2em'>Image Found</h3><div align='center'><img src='" + url + "' width='100'></div>";
    html = html + "<div align='center'><img src='/images/useimage.jpg'>";
    html = html + "<div align='center'><img src='/images/donotuse.jpg'>";
    pop.innerHTML = html;
    pop.style.visibility = 'visible';
    pop.style.top = '130px';
    pop.style.left = '140px';
    pop.style.position = 'absolute';
    pop.style.zindex = 2500;
}

var sentFlag = 0;
function go(){
    document.getElementById('overlay').style.cursor = 'wait';
    var e = document.getElementById('seed_layer');
    e.style.visibility = 'hidden';
    e = document.getElementById('title');
    e.value = encode(e.value);
    e = document.getElementById('excerpt');
    e.value = encode(e.value);
    e = document.getElementById('comment');
    e.value = encode(e.value);
    
    var xhrArgs = {
        form: dojo.byId('seedForm'),
        handleAs: "text",
        load: function(data,form){
            cancel();
            window.location = '/index.php';
        },
        error: function(data){
            alert("Error posting: " + data);
        }
    };
    dojo.xhrPost(xhrArgs);
    
}

function checkUrl(){
    
    document.getElementById('overlay').style.cursor = 'wait';
    var xhrArgs = {
        form: dojo.byId('seedForm'),
        handleAs: "json",
        load: function(data,form){
            populate(data);
        },
        error: function(data){
            alert("error:" + data);
        }
    };
    dojo.xhrPost(xhrArgs);
}
    

