// server details
var server = "www.talentgraphz.com";
var port = "80";
var site = "http://"+server+":"+port
var footerTooltip = "View company details";
var version = "1.0";

// initialisation
window.onload = function () {
    tzWidget.init();
}

// declare widget object
// make it sure, it is not already declared
var tzWidget = window.tzWidget || (function() {
    // flag to keep the initialization status
    var isInitialised = false;
    return {
        /**
         * Set up ground work for pop ups
         * and render graphs for inline
         */
        init : function () {
            // check if initialization already done
            if(isInitialised) {
                if(window.console) console.log("Widget JS already included, skip initialization process.");
                // nothing to do, just return
                return;
            }
            else {
                // set the flag, so it doesn't enters next time
                isInitialised = true;
            }

            /* initialization to be done for the first time*/

            var tzPopupCompanies = tzWidgetUtil.getElementsByClass('tzWidgetPop', null, 'span');
            if(tzPopupCompanies != null && tzPopupCompanies.length > 0) {
                var k=0;
                for(k=0; k<tzPopupCompanies.length;k++) {
                    var tzCompany = tzPopupCompanies[k];
                    var target = (tzCompany.getAttribute('tzTarget')!=null?tzCompany.getAttribute('tzTarget'):'');
                    var companyId = tzCompany.innerHTML;
                    var divId = "div_"+companyId;
                    tzCompany.innerHTML = '<img id="'+companyId+'" name="'+companyId+'" width="16px" height="14px" style="cursor:pointer" alt="talentGraphz Logo" src="'+site+'/imagesEx/favicon.png" onclick="tzWidget.showHidePopup(\''+companyId+'\', \''+divId+'\', \''+target+'\');">';
                    parentCoor = tzWidgetUtil.getCoordinates(document.getElementById(companyId));
                    tzCompany.innerHTML += '<div class="graphWrapper" id="'+divId+'" name="'+divId+'" style="position:absolute;top:'+(parentCoor[0]+20)+';left:'+(parentCoor[1])+';width:505px;height:426px;display:none;visibility:hidden;border: 1px solid #bbbbbb;box-shadow:5px 5px 5px #ccc;-moz-box-shadow:5px 5px 5px #ccc;-webkit-box-shadow:5px 5px 5px #ccc;"/>';
                }
            }

            var tzInlineCompanies = tzWidgetUtil.getElementsByClass('tzWidgetInline', null, 'div');
            if(tzInlineCompanies != null && tzInlineCompanies.length > 0) {
                var p = 0;
                for(p=0; p < tzInlineCompanies.length > 0; p++) {
                    var flashvars = "legend=false";
                    if(tzInlineCompanies[p].getAttribute('tzTarget') != null)
                        flashvars += "&target="+tzInlineCompanies[p].getAttribute('tzTarget');
                    var companyId = tzInlineCompanies[p].innerHTML;
                    flashvars += "&host="+server+"&port="+port+"&companyId="+companyId+"&footerTooltip="+footerTooltip;
                    tzInlineCompanies[p].innerHTML = ''+
                      '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'+
                      'id="Graph" name="Graph" width="100%" height="100%"'+
                      'codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">'+
                      '  <param name="movie" value="'+site+'/swf/tzWidget.swf" />'+
                      '  <param name="quality" value="high" />'+
                      '  <param name="bgcolor" value="#ffffff" />'+
                      '  <param name="BorderStyle" value="1" />'+
                      '  <param name="allowScriptAccess" value="always" />'+
                      '  <param name="FlashVars" value="'+flashvars+'" />'+
                      '  <embed src="'+site+'/swf/tzWidget.swf" quality="high" bgcolor="#ffffff"'+
                      '    width="100%" height="100%" name="Graph" id="Graph" align="middle"'+
                      '    play="true" loop="false" quality="high" border="0px" allowScriptAccess="always" FlashVars="'+flashvars+'"'+
                      '    type="application/x-shockwave-flash"'+
                      '    pluginspage="http://www.adobe.com/go/getflashplayer">'+
                      '  </embed>'+
                      '</object>' ;
                    tzInlineCompanies[p].style.width = '505px';
                    tzInlineCompanies[p].style.height = '426px';
                    tzInlineCompanies[p].style.visibility = 'visible';
                    tzInlineCompanies[p].style.display = 'block';
                    tzInlineCompanies[p].style.border = '1px solid #bbbbbb';
                }
            }
        },

        /**
         * Toggle function to show / hide the pop up
         */
        showHidePopup : function (companyId, containerId, target) {
            var container = document.getElementById(containerId);
            if(container) {
                if(container.style.visibility == 'visible') {
                    container.style.visibility = 'hidden';
                    container.style.display = 'none';
                    container.innerHTML = '';
                }
                else {
                    var graphWrappers = tzWidgetUtil.getElementsByClass('graphWrapper', null, 'div');
                    if(graphWrappers != null && graphWrappers.length > 0) {
                        for(z=0;z<graphWrappers.length;z++) {
                            if(graphWrappers[z].style.visibility == 'visible') {
                                graphWrappers[z].style.visibility = 'hidden';
                                graphWrappers[z].style.display = 'none';
                                graphWrappers[z].innerHTML = '';
                            }
                        }
                    }
                    var flashvars = "legend=false&actionImageTooltip=Close&actionImageSource="+site+"/imagesEx/close.png&actionImageAction=tzWidget.closeGraph";
                    if(target != null && target != '')
                        flashvars += "&target="+target;
                    flashvars += "&host="+server+"&port="+port+"&companyId="+companyId+"&footerTooltip="+footerTooltip;
                    container.innerHTML = ''+
                      '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'+
                      'id="Graph" name="Graph" width="100%" height="100%"'+
                      'codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">'+
                      '  <param name="movie" value="'+site+'/swf/tzWidget.swf" />'+
                      '  <param name="quality" value="high" />'+
                      '  <param name="bgcolor" value="#ffffff" />'+
                      '  <param name="allowScriptAccess" value="always" />'+
                      '  <param name="FlashVars" value="'+flashvars+'" />'+
                      '  <embed src="'+site+'/swf/tzWidget.swf" quality="high" bgcolor="#ffffff"'+
                      '    width="100%" height="100%" name="Graph" id="Graph" align="middle"'+
                      '    play="true" loop="false" quality="high" allowScriptAccess="always" FlashVars="'+flashvars+'"'+
                      '    type="application/x-shockwave-flash"'+
                      '    pluginspage="http://www.adobe.com/go/getflashplayer">'+
                      '  </embed>'+
                      '</object>' ;
                    container.style.visibility = 'visible';
                    container.style.display = 'block';
                }
            }
        },

        /**
         * Close graph
         */
        closeGraph : function () {
            var graphWrappers = tzWidgetUtil.getElementsByClass('graphWrapper', null, 'div');
            if(graphWrappers != null && graphWrappers.length > 0) {
                for(z=0;z<graphWrappers.length;z++) {
                    if(graphWrappers[z].style.visibility == 'visible') {
                        graphWrappers[z].style.visibility = 'hidden';
                        graphWrappers[z].style.display = 'none';
                        graphWrappers[z].innerHTML = '';
                    }
                }
            }
        }
    };
})();

/////////////////////// G l o b a l   F u n c t i o n s //////////////////////////

/**
 * Navigate to company's page
 */
function focusOn(guid, target) {
    if(target != null && target == 'self') {
        // if window's parent is available, rewrite the lcoation
        if(window.parent != null) {
            window.parent.location = site+"/CompanyDetails.html?guid="+guid;
        }
        else {
            window.location = site+"/CompanyDetails.html?guid="+guid;
        }
    }
    else {
        window.open(site+"/CompanyDetails.html?guid="+guid);
    }
}

/**
 * Navigate to posting's page
 */
function findPostings(companyGuid) {
    window.location = site+"/FindJobs.html?guid="+companyGuid;
}

////////////////////U t i l   F u n c t i o n s//////////////////////////////////

// widget utility class
var tzWidgetUtil = window.tzWidgetUtil || (function() {
    return {
        /**
         * Function to return all elements with the class name
         */
        getElementsByClass : function (searchClass,node,tag) {
            var classElements = new Array();
            if ( node == null )
                node = document;
            if ( tag == null )
                tag = '*';
            var els = node.getElementsByTagName(tag);
            var elsLen = els.length;
            var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
            for (i = 0, j = 0; i < elsLen; i++) {
                if ( pattern.test(els[i].className) ) {
                    classElements[j] = els[i];
                    j++;
                }
            }
            return classElements;
        },

        /**
         * Return top & left coordinates of
         * the specified object
         */
        getCoordinates : function (obj) {
          var leftCoor = 0;
          var topCoor = 0;
          while(obj != null)
          {
            leftCoor += obj.offsetLeft ;
            topCoor += obj.offsetTop ;
            obj = obj.offsetParent ;
          }
          return [topCoor, leftCoor];
        }
    };
})();

