/*<![CDATA[*/
/**
* @version $Id: mypms_common.js 53 2007-04-12 09:53:59Z clexus $
* Clexus Private Messaging Component
* @package ClexusPM
* @Copyright (C) 2003 - 2006 Danial Taherzadeh All rights reserved
* @license Released as a commercial component
* @link http://www.clexus.com
**/

function getViewportHeight()
{
    if (window.innerHeight != window.undefined)
    {
        return window.innerHeight;
    }

    if (document.compatMode == 'CSS1Compat')
    {
        return document.documentElement.clientHeight;
    }

    if (document.body)
    {
        return document.body.clientHeight;
    }

    return window.undefined;
}

function getViewportWidth()
{
    if (window.innerWidth != window.undefined)
    {
        return window.innerWidth;
    }

    if (document.compatMode == 'CSS1Compat')
    {
        return document.documentElement.clientWidth;
    }

    if (document.body)
    {
        return document.body.clientWidth;
    }

    return window.undefined;
}

jQuery(document).ready(function()
{
    jQuery('a.cxpm_searchnav').click(function()
    {
        jQuery('#inboxsearch').slideToggle();
        return false;
    });

    jQuery('a.cxpm_delnav').click(function()
    {
        delMessages();
        return false;
    });

    if (jQuery.browser.mozilla)
    {
        jQuery("body").append('<div id="ajaxBusy">&nbsp;&nbsp;' + cpm_loading_msg + '&nbsp;</div>');

        jQuery('#ajaxBusy').css({ backgroundColor: "#CC4444", display: "none",      margin: "0px",
                                  fontFace: "Arial",          paddingLeft: "2px",   paddingRight: "2px",
                                  paddingTop: "2px",          paddingBottom: "2px", position: "absolute",
                                  right: "3px",               top: "3px",           color: "#FFFFFF",
                                  width: "auto",              zIndex: "99999"
        });
    }

    jQuery("#ajaxBusy").ajaxStart(function()
    {
        var thexBody = document.documentElement;

        jQuery(this).css({ top: (parseInt(thexBody.scrollTop, 10) + 3) + "px"
        });

        if (!cpm_disablenotifcpm)
        {
            jQuery(this).show();
        }
    }).ajaxStop(function()
    {
        me = this;

        if (!cpm_disablenotifcpm)
        {
            setTimeout("jQuery(me).hide();", 500);
        }
    });
});

function CheckAll()
{
    var fmobj = document.mutliact;

    for (var i = 0; i < fmobj.elements.length; i++)
    {
        var e = fmobj.elements[i];

        if ((e.name != 'allbox') && (e.type == 'checkbox') && (!e.disabled))
        {
            e.checked = fmobj.allbox.checked;
        }
    }
}

function setStatus(s, n, type)
{
    jQuery('#mypms_topmessage').html("<div class=\"" + type + "\">" + s + "</div>");
    jQuery('#mypms_topmessage').fadeIn("fast");
    setTimeout("jQuery('#mypms_topmessage').fadeOut(\"fast\");", n);
}

Array.prototype.contains = function(element)
{
    for (var i = 0; i < this.length; i++)
    {
        if (this[i] == element)
        {
            return true;
        }
    }

    return false;
};

Array.prototype.walk = function(f)
{
    var a = [], i = this.length;

    while (i--)
    {
        a.push(f(this[i]));
    }

    return a.reverse();
};

function increaseNotesHeight(thisTextarea, add)
{
    newHeight = parseInt(jQuery("#" + thisTextarea).css("height").split("px")[0], 10) + add;
    jQuery("#" + thisTextarea).animate({ height: newHeight
    },                                 "slow");
}

function decreaseNotesHeight(thisTextarea, subtract)
{
    newHeight = parseInt(jQuery("#" + thisTextarea).css("height").split("px")[0], 10) - subtract;
    jQuery("#" + thisTextarea).animate({ height: newHeight
    },                                 "slow");
}

function popup_addContactToList(id, lay, randd, st)
{
    var yurl = mosConfig_live_site + '/index2.php';
    var uparams = { 'option': "com_mypms", "task": "popupmanagecontact", "no_html": 1, "conid": id, "status": st };

    jQuery.ajax({ url: yurl, data: uparams, success: function(req)
    {
        jQuery("#" + lay).html(req);
        setTimeout("cxGetPopupMenu (" + id + ", " + randd + ");", 1000);
    }
    });
}

function popup_manageBlock(id, lay, randd)
{
    var xurl = mosConfig_live_site + '/index2.php';
    var uparams = { 'option': "com_mypms", "task": "popupmanageblock", "no_html": 1, "conid": id };

    jQuery.ajax({ url: xurl, data: uparams, success: function(req)
    {
        jQuery("#" + lay).html(req);
        setTimeout("cxGetPopupMenu (" + id + ", " + randd + ");", 1000);
    }
    });
}

function popup_addContactToFav(id, state, lay, randd)
{
    var xurl = mosConfig_live_site + '/index2.php';
    var uparams = { 'option': "com_mypms", "task": "favcontact", "no_html": 1, "conid": id, "fav": state };

    jQuery.ajax({ url: xurl, data: uparams, success: function(req)
    {
        jQuery("#" + lay).html(req);
        setTimeout("cxGetPopupMenu (" + id + ", " + randd + ");", 1000);
    }
    });
}

function cxGetPopupMenu(id, randd)
{
    var yurl = mosConfig_live_site + '/index2.php';
    jQuery.ajax({ url: yurl,
                  data: { "option": "com_mypms", "task": "getpopupmenu", "no_html": 1, "conid": id, "rand": randd
    },            success: function(request)
    {
        jQuery("#cxpopupcontent_" + randd).html(request);
        return;
    }
    });
}

var id = null;
var settings = null;
jQuery.fn.cxnotifier = function(options)
{
    var me = this;
    this.settings = { closeLink: 'none', animation: 'fade', height: '50'
    };

    if (options)
    {
        jQuery.extend(this.settings, options);
    }

    id = this.attr("id");
    settings = this.settings;
    jQuery(this).css('padding', '0').css('height', this.settings.height + 'px').css('margin', '0');
    jQuery('html').css('padding', '0 0 ' + ( this.settings.height * 1 + 50 ) + 'px 0');
/*
    if (jQuery.browser.mozilla) {
    jQuery('html').css('padding', '0 0 ' + (this.settings.height + 5) + 'px 0');
    }
*/
    if ( typeof document.body.style.maxHeight != "undefined" ) {
        jQuery(this).css('position', 'fixed').css('bottom', '0').css('left', '0');
    }
    /*if (typeof document.body.style.maxHeight == "undefined")
    {
        jQuery('html').css('overflow', 'hidden');

        if (jQuery.browser.mozilla) {
            jQuery('body').css('margin', '0').css('height', '100%').css('overflow', 'auto');
        }

        jQuery(this).css('position', 'absolute');
    }
    else
    {
        jQuery(this).css('position', 'fixed');
    }
*/
    jQuery(this).hide().slideDown("slow");

    jQuery("#cxnotifier .notifclose,#cxnotifier a#dismiss").click(function()
    {
        var xurl = mosConfig_live_site + '/index2.php?option=com_mypms&task=flagntfmsgs&no_html=1';
        jQuery.ajax({ url: xurl, type: "POST", data: { ntfyids: cxids
        }
        });

        jQuery.closecxnotifier();
        return false;
    });

    return me;
};

jQuery.closecxnotifier = function()
{
    this.cxnotifier = jQuery('#' + id);
    jQuery(this.cxnotifier).slideUp("slow");
    jQuery("#gotmail").remove();
    jQuery('html').css('padding', '0');
    jQuery('body').css('overflow', 'visible'); // Change IE6 hack back

};

jQuery(document).ready(function()
{
    jQuery('.windowOpen').click(function()
    {
        jQuery(".cxwindow").hide();

        me = this;
        var randd = jQuery(me).attr("id").split("__")[1].split("_")[1];
        var id = jQuery(me).attr("id").split("__")[1].split("_")[0];

        if (jQuery('#window__' + jQuery(me).attr("id").split("__")[1]).css('display') == 'none')
        {
            var fullHeight = getViewportHeight();
            var fullWidth = getViewportWidth();
            var theBody = document.documentElement;

            var scTop = parseInt(theBody.scrollTop, 10);
            var scLeft = parseInt(theBody.scrollLeft, 10);
            var wind = '#window__' + jQuery(me).attr("id").split("__")[1];
            jQuery(wind).css("top", (scTop + fullHeight / 2 - 300 / 2) + "px").css("left", (scLeft + fullWidth / 2 - 400
                                                                                               / 2) + "px");

            cxGetPopupMenu(id, randd);
            jQuery(wind).animate({ opacity: 'show'
            },                   500);
        }

        this.blur();

        return false;
    });

    jQuery('.windowClose').click(function()
    {
        me2 = this;
        var cid = (jQuery(me2).parents(".cxwindow").attr("id").split("__")[1]);
        jQuery('#window__' + cid).hide();
    });
});

function cxpopupWindow(mypage, myname, w, h, scroll)
{
    var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
    winprops = 'height=' + h + ',width=' + w + ',top=' + wint + ',left=' + winl + ',scrollbars=' + scroll
                   + ',resizable';
    wins = document.open(mypage, myname, winprops);
    /*    if (parseInt(navigator.appVersion) >= 4) { wins.window.focus(); }*/
    if (!wins)
    {
        alert("Popup window could not be initiated. Please disable your popup blockers.")
    }

    return wins;
}

function makeIMlinks(cxwin)
{
    console.log(jQuery("a").not(".cxalertlink"));
    jQuery("a").not(".cxalertlink").unbind("click").click(function()
    {
        var cxlink = jQuery(this).attr("href");
        cxlink = cxlink.replace(/index.php/g, "index2.php");
        var cxlinka = "CPM" + Math.floor(Math.random() * 500);
        cxpopupWindow(cxlink, cxlinka, 600, 480, 0);
        return false;
    });
}

jQuery.fn.cxAlertSound = function()
{
    if (cpm_enable_sound)
    {
        jQuery(this).after('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="0" height="0" id="gotmail" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="'+ mosConfig_live_site+ '/components/com_mypms/sounds/gotmail.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="'+ mosConfig_live_site + '/components/com_mypms/sounds/gotmail.swf" quality="high" bgcolor="#ffffff" width="1" height="1" name="gotmail" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>');
    }

    return this;
};
/*]]>*/