﻿function Contactlist(parentID, myobjectname, cmdshowid) {
    this.mytimer = "";
    this._myobjectname = myobjectname
    this._parentID = "#" + parentID;
    this._theholder = jQuery(this._parentID)
    this._contactlistbuttonclicked = false;
    this._contactpicker = "";
    this._cmdshowid = "#" + cmdshowid;
    this._cmdshow = jQuery(this._cmdshowid)
    this.makevisible = function() {
        this._contactlistbuttonclicked = true;
        window.clearTimeout(this.mytimer);
	this.resize();
    }
    this.nomakevisible = function() {
        this._contactlistbuttonclicked = false;
    }
    this.showcontactlist = function() {
        this._contactlistbuttonclicked = true;

        window.clearTimeout(this.mytimer);
        var theholder = this._theholder
        if (theholder) {
            theholder.show();
        }
	this.resize();
    }
    this.hidecontactlist = function() {
        if (this._contactlistbuttonclicked == false) {
            var theholder = this._theholder
            if (theholder.is(':visible')) {
                theholder.hide();
            }
        }
        else {
            this._contactlistbuttonclicked = false;
        }
    }
    this.resize = function(){
	var innerwidth = this._theholder.find("td.inner").children().css("width").replace("px","")*1;
	var box4w = this._theholder.find("td.box_4").css("width").replace("px","")*1;
	var box5w = this._theholder.find("td.box_5").css("width").replace("px","")*1;
	
        var offset = this._cmdshow.offset();
        var wWidth = jQuery(document).width();
        var wHeight = jQuery(document).height();
        var oWidth = innerwidth+box4w+box5w;
        var oHeight = this._theholder.outerHeight();
        var left = offset.left;
        var top = offset.top
	var bottnheight = this._cmdshow.outerHeight();
	this._theholder.css("width",innerwidth+box4w+box5w)
	if (jQuery.browser.msie){wWidth -= 21;wHeight -= 21}
	
	//this._theholder.find(".telco_logos").html(wWidth)
	
        while (left + oWidth > wWidth && left > 0) {
            left = left - 1
        }
	while (left < 0 ) {
            left = left + 1
        }
        if (top + oHeight > wHeight) {
           top = top - oHeight
        }
        while (top + oHeight  > wHeight && top > 0) {
            top = top - 1
        }
        this._theholder.css("left", left);
        this._theholder.css("top", top);
    }
    this.init = function() {
        this._theholder.bind("click", { obj: this }, function(event) {
            var theinstance = event.data.obj
            theinstance.makevisible();
        })
        this._cmdshow.bind("click", { obj: this }, function(event) {
            var theinstance = event.data.obj
            theinstance.showcontactlist();
        })
        jQuery("body").bind("click", { obj: this }, function(event) {
            var theinstance = event.data.obj
            //  alert(theinstance._myobjectname)
            theinstance.mytimer = window.setTimeout(theinstance._myobjectname + ".hidecontactlist()", 1000)
        })
        jQuery(this._parentID + " select").bind("click", { obj: this }, function(event) {
            var theinstance = event.data.obj
            theinstance.makevisible();
        })
        this._contactpicker = jQuery(this._parentID + " .contactpicker");
        var my_options = this._contactpicker.find("option");
        my_options.sort(function(a, b) {
            if (a.text > b.text) return 1;
            else if (a.text < b.text) return -1;
            else return 0
        })
        this._contactpicker.empty().append(my_options);
        this._contactpicker.bind("change", { obj: this }, function(event) {
            var theobject = event.data.obj
            theobject.makevisible();
            var theselectedvalue = theobject._contactpicker.val();
            theobject._theholder.find(".singlecontact").hide();
            theobject._theholder.find("." + theselectedvalue).show();
        })
        var firstoption = this._contactpicker.find("option:first")
        firstoption.attr('selected', 'selected');
        this._contactpicker.change();
	this.resize();
    }
}