var cl = cl || {};
var uri = location.href.parseUri();
var URL_DOMAIN = uri.protocol + "://" + uri.host;

var SpinnerHtml = '<span class="spinnerText"><img src="/images/global/ajax-loading.gif" width="14" height="14"> Loading ...</span>';

var BehaviorRollover = 'def_rollover';

NO_USER_NOTIFY = 0;

// Create an image object for the livechat button.
// We want to do this asap because this image sometimes takes a while to load
// due to the logic and roundtrip time to query the liveperson server.
// I put this in this file due to its dependence on the above uri object.

// removed per bug 45928 - JDD

// var liveChatButtonImageObj = new Image();
// liveChatButtonImageObj.src = uri.protocol+'://service.liveperson.net/hc/24631554/?cmd=repstate&site=24631554&channel=web&&ver=1&imageUrl='+URL_DOMAIN+'/images/global/livepersonSm&skill=Clinique';
// liveChatButtonImageObj.name="hcIcon";
// liveChatButtonImageObj.style.border="0";

// Load any livechat links with the button above
var loadLiveChatButtons = function() {
	$$('a.livechat-image-tag').each(function(el){
		if ( el.empty() ) {
			var img = new Image();
			img.src = liveChatButtonImageObj.src;
			img.name = liveChatButtonImageObj.name;
			img.style.border = liveChatButtonImageObj.style.border;
			el.update(img);
		}
	});
}

cl.showErrors = function(errorObjectsArray, errListNode) {
    var errListItemNodes = errListNode.select("li");
    errListItemNodes.each(function(li){
        li.hide();
    })
    errorObjectsArray.each(function(errObj){
        var errKey = errObj.key;
        var regEx = new RegExp(errKey);
        // try to find LI whose ID matches the error key 
        var errListItemNode = errListItemNodes.find(function(node) {
            return regEx.test(node.id);
        });
        if (errListItemNode) {
            errListItemNode.show();
        } else {
            var newLiNode = new Element("li").insert(errObj.text);
            errListNode.insert(newLiNode);
        }
    });
    errListNode.show();
};


// CL JP toCurrency (overrides definition)
Object.extend( Number.prototype, {
    toCurrency: function(symb, after) {
        num = this.toFixed(0);
        if (after) {
            return num+symb;
        } else {
            return symb+num;
        }
    }
});

/* old ELC front-end interface compatibility */
var el = $;
Element.addMethods({
    addBehavior: function(e,b){
        $(e).className = b;
    }
});

/* why was this moved here?? this lives/lived in definition/functions CM*/

// old 'whenEl()'
var onElement = function(element, callback, duration, attempts) {
    var count =  0;
    var duration = duration || .5;
    var attempts = attempts || 100;
    new PeriodicalExecuter(function(pe){
        count++;
        if (count >= attempts) { pe.stop(); }
        if ($(element)) {
            pe.stop();
            callback($(element));
            callback = function(){};
        }
    }, duration);
};

var onObject = function(element, callback, duration, attempts) {
    var count = 0;
    var duration = duration || .5;
    var attempts = attempts || 100;
    var obj = undefined;
    new PeriodicalExecuter(function(pe){
        count++;
        if (count >= attempts) { pe.stop(); }
        try {
            obj = eval(object);
        } catch(e) {
            // try again
        }
        if (obj) {
            pe.stop();
            callback(obj);
            callback = function(){};
        }
    }, duration);
};

// miscellaneous utilities
function generateGuid(){
    var result, i, j;
    result = '';
    for(j=0; j<32; j++) {
        if (j == 8 || j == 12 || j==16 || j==20)
            result = result + '-';
        i = Math.floor(Math.random()*16).toString(16).toUpperCase();
        result = result + i;
    }
    return result;
}

// Added this so we can check for boolean settings in html-template files.
// That's cuz this gives an error if no value is set: obj.checked = ( #{VAL} ? true : false )

// Why not toBool() method off the extended String object??? Think OO!!! '#{VAL}'.toBool() - CM

function CheckBooleanVar(val) {
	if ( val ) return true;
	return false;	
}

flashHandlerCallbacks = {};

function flashHandlerNamer ( flash_id, event_name ) {
    return flash_id.toLowerCase() + ':' + event_name.toLowerCase();
}

function addFlashHandlerCallback ( id, evt, func ) {
    flashHandlerCallbacks[ flashHandlerNamer ( id, evt  ) ] = func ( id, evt );
}

flashToJavascript = function ( flash_id, event_name ) {
    var idEvt = flashHandlerNamer ( flash_id, event_name );
    var handler = flashHandlerCallbacks[ idEvt ];
    if ( typeof handler == 'function' ) {
        handler( flash_id, event_name );
        return true;
    } else { return false; }
};

/* FLASH TEST FUNCTIONS

    addFlashHandlerCallback ( 'test', 'test', 
        function ( id, evt ) {
            alert ( flashHandlerNamer ( id, evt ) + ' is found.' ) });
    flashToJavascript ('test', 'test');

END TEST FUNCTIONS */
