/********************************************************
For more info: http://www.ibegin.com/blog/p_ajax_feedback_mechanism.html
Created for iBegin.com - local search done right
*********************************************************/

self.onError=null;


var rMargin = 80; // distance from the right area from the browser window (in pixels?)
var bMargin = 60; // distance from the bottom area from the browser window (in pixels?)

var allow_comment = 1; // 0 or 1, this allows users to add comments along with the feedback
var require_click = 1; // 0 or 1, for a viewing the popup on mouse click or on mouse over
var load_on_every_page = 0; // 0 or 1, enable cookie check

//////////// more advanced options
var sliding = 0; // 0 or 1 for an sliding effect
var fade = 1; // 0 or 1 for a fading effect

// comment box distance from the "feedback icon" when clicked or hover, change this if necessary
var        fb_topDistance = -160;
var        fb_leftDistance = -220;

var opacity_interval = 1300; // milliseconds on how the fade increments by 1

var feedback_html = "<img src=\"images/feedback.gif\" style=\"width:48px;height:30px;border:0;\"/>"; // change this if you don't like our image
var notice_html = "<b>Rating...</b>"; // you can change this to an image too! an indicator maybe

// make sure if cookie is set on server: cookie_name and cookie_lifetime must have the same values below
var cookie_set_on_server = 0; // 0 or 1 specifies how we set cookies, on feedback.php or on using our setCookie function
var cookie_name = "_fb__"; // our feedback marker cookie name
var cookie_lifetime = new Date().getTime()+(60*60*24*30);

//// changing values below this part may cause the script not to work properly
var opacity = 0;
var fLoad = 1;

var topPosition = 1000; // set initial values
var leftPosition = 750; // set initial values

var lastScrollX = 0;
var lastScrollY = 0;


/********************************************************
 Make this IE7 Compatible ;)
 http://ajaxian.com/archives/ajax-on-ie-7-check-native-first
*********************************************************/
function createRequestObject() {
        var xmlhttp;
                /*@cc_on
        @if (@_jscript_version>= 5)
                        try {
                                        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                        } catch (e) {
                                        try {
                                                        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                                        } catch (E) {
                                                        xmlhttp = false;
                                        }
                        }
        @else
        xmlhttp = false;
        @end @*/
        if (!xmlhttp && typeof XMLHttpRequest != "undefined") {
                        try {
                                        xmlhttp = new XMLHttpRequest();
                        } catch (e) {
                                        xmlhttp = false;
                        }
        }
        return xmlhttp;
}

var http = createRequestObject();

function initFeedback(elem_wrapper) {

        if(load_on_every_page) {deleteCookie('_fb__');}

        if(getCookie(cookie_name) == null) {

                strFeedback(elem_wrapper);

                // check if we click or not
                if(require_click == 1) {getElem('fb').onclick = showFloaterBox;}else{getElem('fb').onmouseover = showFloaterBox;}

                var docElem = document.documentElement;
                leftPosition = self.innerWidth || (docElem&&docElem.clientWidth) || document.body.clientWidth;
                topPosition = self.innerHeight || (docElem&&docElem.clientHeight) || document.body.clientHeight;

                topPosition -= bMargin;
                leftPosition -= rMargin;

                getElem('floater').style.position = "absolute";
                getElem('floater').style.left = leftPosition+"px";
                getElem('floater').style.display = "block";

                opacity = fade==1?0:100;

                lastScrollX = leftPosition;
                lastScrollY = topPosition;
                action = window.setInterval("moveItem()",1);

        }
}

function popitup(url) {
        newwindow=window.open(url,'name','height=455,width=468');
        if (window.focus) {newwindow.focus()}
        return false;
}

strFeedback = function(elem_wrapper) {
        var strHTML = "<div id=\"floater\" style=\"\">";
        strHTML = strHTML+"<div id=\"fb\"><a href=\"#\" onclick=\"return popitup('feedback.php');\">"+feedback_html+"</a></div>";
        strHTML = strHTML+"</div>";

        getElem(elem_wrapper).innerHTML = strHTML;
}

showFloaterBox = function () {
        var e = getElem('floater');
        if(e.style.display == 'none'){
           e.style.display = '';
                getElem('floater_box').style.top = fb_topDistance+"px";
                getElem('floater_box').style.left = fb_leftDistance+"px";
        } else{e.style.display = 'none';}
}

changeStat = function(elem) {
        getElem('fbstat').innerHTML = elem.title;
        elem.onmouseout = function() {getElem('fbstat').innerHTML = "&nbsp;";}
}
moveItem = function(){
        var docElem = document.documentElement;
        diffY = self.pageYOffset || (docElem&&docElem.scrollTop) || document.body.scrollTop;
        diffX = self.innerWidth || (docElem&&docElem.clientWidth) || document.body.clientWidth;



        if(diffY != lastScrollY) {

                percent = .1 * (diffY - lastScrollY);
                percent = percent >0?Math.ceil(percent):Math.floor(percent);
                if(fLoad != 1) {
                        getElem('floater').style.top = lastScrollY+topPosition+'px';
                }

                lastScrollY = lastScrollY + percent;

                // Fade Out Item here
                fadeOut('floater');


        } else if((diffY == lastScrollY)) {
                if(fLoad == 1) {
                        getElem('floater').style.top = lastScrollY+topPosition+'px';
                        fLoad = 0;
                }
                //show item here Fade In
                fadeIn('floater');
        }

        /*
                Have to make sure it does not go further beyond our center content
                 change the values below to set a limit on up to where we'll gonna bring the marker to the left
                that makes it 870px from the left of the document
        */
        if(diffX != lastScrollX && diffX > 870) {
                diffX -= rMargin;
                getElem('floater').style.left = diffX+"px";
                lastScrollX = diffX;
        }

}


setCookie = function(name,value,days) {
        if (days)        {
                var date = new Date();
                date.setTime(date.getTime()+(days*24*60*60*1000));
                var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/;";
}

getCookie = function(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0)
                return c.substring(nameEQ.length,c.length);
        }
        return null;
}

deleteCookie = function(name) {setCookie(name,"",-1);}

// @note: just say fade in only works properly in FF
fadeIn = function(elemId) {
        var e = getElem(elemId);

        e.style.visibility = 'visible';

        window.setTimeout(function() {

                if (opacity <= 100) {
                        if (e.style.MozOpacity != null) {
                                e.style.MozOpacity = (opacity/100)-.001;
                        }

                        opacity += 1;
                }

        }, opacity_interval);

}

// function does not need a decrement, since the scrolling effect the item produces a fading effect
fadeOut = function(elemId) {
        var e = getElem(elemId);
        if(!sliding) {
                e.style.visibility = 'hidden';
                if(fade) {
                        opacity = 0;
                }
        }

}

getElem = function(elemId) {
        return document.getElementById(elemId);
}