var isMozilla;
var objDiv = null;
var originalDivHTML = "";
var DivID = "";
var over = false;
var isUseMeebo = true;

function showHelpDiv(divId, title){
    var w, h, l, t;
    w = 250;
    h = 500;
    l = window.screen.availWidth / 5;
    t = window.screen.availHeight / 7;
    displayFloatingDiv(divId, title, w, h, l, t);
}

function showBlogDiv(divId, title){
    var w, h, l, t;
    w = 200;
    h = 200;
    l = 50;
    t = 50;
    displayFloatingDiv(divId, title, w, h, l, t);
}

function getMeeboCode(){
    return '<div>' +
    '<table style="width: 30%; text-align: left; margin-left: auto; margin-right: auto;" border="0" cellpadding="0" cellspacing="0">' +
    '<tbody>' +
    '<tr><td align="center">' +
    '<embed palette="background" src="http://widget.meebo.com/mm.swf?vkepPZNHZM" type="application/x-shockwave-flash" width="200" height="250"></embed>' +
    '</td></tr>' +
    '<tr><td align="justify" style="background-color: white; text-align: justify; font-size: 10px; font-family: Arial; color: green;">' +
    'Please provide your EmailID before starting any conversation, so that we can assist you later and serve better.' +
    '</td></tr>' +
    '<hr>' +
    '<tr><td>' +
    '<div class="contactus">Contact us:</div>' +
    '<ul>' +
    '<li class="contactus"><b>Address: </b>KITSL UG-9, A Block,<br>Omaxe Gurgaon Mall, Sohna Road, Gurgaon<br>Haryana (122 018)</li>' +
    '<li class="contactus"><b>Phone: </b>+91 - 124 - 4251249</li>' +
    '<li class="contactus"><b>Email: </b><a class="mn" href="mailto:support@gooseberry.in">support@gooseberry.in</a></li>' +
    '<li class="contactus"><b>By SMS: </b>Send a sms to <b>56070</b><br>in the following format: <i>"KITS&lt;space&gt;support&lt;space&gt;your message"</i></li>' +
    '</ul>' +
    '</td></tr>' +
    '</tbody>' +
    '</table>' +
    '</div>';
}

function displayFloatingDiv(divId, title, width, height, left, top){
    DivID = divId;
    
    document.getElementById(divId).style.width = width + 'px';
    //    document.getElementById(divId).style.height = height + 'px';
    
    var addHeader;
    if (isUseMeebo) 
        originalDivHTML = getMeeboCode();
    else 
        originalDivHTML = document.getElementById(divId).innerHTML;
    
    //    changeHeaderHere
    addHeader = '<table style="width:' + width + 'px" class="floatingHeader">' +
    '<tr><td ondblclick="void(0);" onmouseover="over=true;" onmouseout="over=false;" style="cursor:move;height:18px">' +
    title +
    '</td>' +
    '<td style="width:18px" align="right"><a href="javascript:hiddenFloatingDiv(\'' +
    divId +
    '\');void(0);">' +
    '<img alt="Close..." title="Close..." src="images/close.jpg" border="0"></a></td></tr></table>';
    
    document.getElementById(divId).innerHTML = addHeader + originalDivHTML;
    
    document.getElementById(divId).className = 'helpdiv';
    //    setLocation(left, top);
    document.getElementById(DivID).style.left = 'auto';
    document.getElementById(DivID).style.top = 'auto';
    
    document.getElementById(divId).style.visibility = "visible";
}

function setLocation(left, top){
    document.getElementById(DivID).style.left = left + 'px';
    document.getElementById(DivID).style.top = top + 'px';
}

function hiddenFloatingDiv(divId){
    document.getElementById(divId).innerHTML = originalDivHTML;
    document.getElementById(divId).style.visibility = 'hidden';
    DivID = "";
}

function MouseDown(e){
    if (over) {
        if (isMozilla) {
            objDiv = document.getElementById(DivID);
            X = e.layerX;
            Y = e.layerY;
            return false;
        }
        else {
            objDiv = document.getElementById(DivID);
            objDiv = objDiv.style;
            X = event.offsetX;
            Y = event.offsetY;
        }
    }
}

function MouseMove(e){
    if (objDiv) {
        if (isMozilla) {
            objDiv.style.top = (e.pageY - Y) + 'px';
            objDiv.style.left = (e.pageX - X) + 'px';
            return false;
        }
        else {
            objDiv.pixelLeft = event.clientX - X + document.body.scrollLeft;
            objDiv.pixelTop = event.clientY - Y + document.body.scrollTop;
            return false;
        }
    }
}

function MouseUp(){
    objDiv = null;
}

function init(){
    isMozilla = (document.all) ? 0 : 1;
    
    if (isMozilla) {
        document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
    }
    
    document.onmousedown = MouseDown;
    document.onmousemove = MouseMove;
    document.onmouseup = MouseUp;
    prepareInputsForHint();
}

init();

function prepareInputsForHint(){
    prepareDiv(document.getElementsByTagName("input"));
    prepareDiv(document.getElementsByTagName("textarea"));
    prepareDiv(document.getElementsByTagName("select"));
}

function prepareDiv(inputs){
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].parentNode.getElementsByTagName("div")[0]) {
            inputs[i].onfocus = function(){
                this.parentNode.getElementsByTagName("div")[0].style.display = "inline";
            }
            inputs[i].onblur = function(){
                this.parentNode.getElementsByTagName("div")[0].style.display = "none";
            }
        }
    }
}
