/*
Commonbox Launcher
*/

function showCommonboxWidget(key,lg,params)
{
    //transforme les param item en get
    var url_params = '';
    if (typeof( params ) != "undefined"){
        if (typeof( params.item ) != "undefined"){
        	//pour IE6 et les caractères
        	var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
        	if (typeof( params.item.description ) != "undefined" && IE6){
        		params.item.description = str_replace(['à', 'â', 'ã', 'ä', 'ç', 'è', 'ê', 'ë', 'î', 'ï', 'ô', 'û', 'ü'],['a', 'a', 'a', 'a', 'c', 'e', 'e', 'e', 'i', 'i', 'o', 'u', 'u'],params.item.description);
        	}
        	
            if (typeof( params.item.id ) != "undefined") 				url_params +=  "&shopId=" + 		encodeURIComponent(params.item.id) ;
            if (typeof( params.item.price ) != "undefined") 			url_params +=  "&price=" + 			encodeURIComponent(params.item.price) ;
            if (typeof( params.item.title ) != "undefined") 			url_params +=  "&title=" + 			encodeURIComponent(params.item.title) ;
			if (typeof( params.item.description ) != "undefined") 		url_params +=  "&description=" + 	encodeURIComponent(params.item.description) ;
            if (typeof( params.item.image ) != "undefined") 			url_params +=  "&image=" + 			encodeURIComponent(params.item.image)  ;
            if (typeof( params.item.quantity ) != "undefined") 			url_params +=  "&quantity=" + 		encodeURIComponent(params.item.quantity) ;
            if (typeof( params.item.lastPrice ) != "undefined") 		url_params +=  "&lastPrice=" + 		encodeURIComponent(params.item.lastPrice) ;
            if (typeof( params.item.deliveryPeriod ) != "undefined") 	url_params +=  "&deliveryPeriod=" + encodeURIComponent(params.item.deliveryPeriod) ;
            if (typeof( params.item.deliveryPrice ) != "undefined") 	url_params +=  "&deliveryPrice=" + 	encodeURIComponent(params.item.deliveryPrice) ;
            if (typeof( params.item.link ) != "undefined") 				url_params +=  "&link=" + 			encodeURIComponent(params.item.link) ;
            if (typeof( params.item.urlCheckout ) != "undefined") 		url_params +=  "&urlCheckout=" + 	encodeURIComponent(params.item.urlCheckout)  ;
            if (typeof( params.item.availability ) != "undefined") 		url_params +=  "&availability=" + 	encodeURIComponent(params.item.availability) ;
        }
        if (typeof( params.widget ) != "undefined"){
            if (typeof( params.widget.currency ) != "undefined") 		url_params +=  "&cbox_currency=" + 		params.widget.currency ;
            if (typeof( params.widget.backgroundImg ) != "undefined") 	url_params +=  "&css_backgroundImg=" + 	params.widget.backgroundImg ;
            if (typeof( params.widget.background ) != "undefined") 		url_params +=  "&css_background=" + 	params.widget.background ;
        }
    }
    popup("http://www.commonbox.net/apihtml/?key="+key+"&lg="+lg+url_params,"commonbox",680,600);
}

function str_replace(search, replace, subject) {
    var s = subject;
    var ra = r instanceof Array, sa = s instanceof Array;
    var f = [].concat(search);
    var r = [].concat(replace);
    var i = (s = [].concat(s)).length;
    var j = 0;
    
    while (j = 0, i--) {
        if (s[i]) {
            while (s[i] = (s[i]+'').split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
        }
    }
 
    return sa ? s : s[0];
}

function popup(url, title, width, height)
{
    var numArgs = arguments.length;
    var ht; //max height allowed (computed)
    var windowObj;	// window object that is returned.
    var windowParams = (numArgs > 4) ?  arguments[4] : 'statusbar=no,menubar=no,toolbar=no,scrollbars=yes,resizable=yes,top=0';
    var isOffset = (numArgs > 7) ? arguments[7] : false;
    var offset;

    // {{{ top
    if (numArgs > 5) {
        if (isOffset) {
            offset = (window.screenY) ? window.screenY : self.screenTop;
            offset = (offset) ? offset : 0;
        } else {
            offset = 0;
        }
        offset += arguments[5];
        windowParams += (windowParams) ? ',' : '';
        windowParams += 'top='+offset+',screenY='+offset;
    } else {
        windowParams += 'top=0,screenY=0';
    }
    // }}}
    // {{{ left
    if (numArgs > 6) {
        if (isOffset) {
            offset = (window.screenX) ? window.screenX : self.screenLeft;
            offset = (offset) ? offset : 0;
        } else {
            offset = 0;
        }
        offset += arguments[6];
        windowParams += ',left='+offset+',screenX='+offset;
    }
    // }}}
    // {{{ height
    if (screen.height) { // window
        ht = screen.height;
    } else if (window.document.body.clientHeight) {  // IE
        ht = window.document.body.clientHeight;
    } else if (window.innerHeight) { // Netscape
        ht = window.innerHeight;
    } else if (document.documentElement.clientHeight) { // IE 6+
        ht = document.documentElement.clientHeight;
    } else {
        ht = 580;
    }
    if ((height != 0) && (height > ht)) {
        height = ht;
    }
    // }}}
    // {{{ add height and width to window parameters
    if (height != 0 && width != 0) {
        windowParams += ',height=' + height + ',width=' +width;
    } else if (width != 0) {
        windowParams += ',width=' + width;
    }
    // }}}
	
    windowObj = window.open(url, title, windowParams, false);
    if (windowObj) {
        windowObj.focus(); // bring to front if window already open
    }
    return windowObj;
}
