// CMini version 2.0
// ---------------------------------------------
var CMiniLanguage = {
	FRENCH: 'fr',
	ENGLISH: 'en',
	SPANISH: 'es',	
	JAPANESE: 'ja',	
	GERMAN: 'de',	
	CHINESE: 'zh',	
	RUSSIAN: 'ru',	
	PORTUGUESE: 'pt',	
	ITALIAN: 'it',	
	KOREAN: 'ko'
}
var CMiniClickTo = {
	OVERVIEW: 'public',
	READ: 'view',
	URL: 'url'
}
var CMiniClickTarget = {
	SELF: '_self',
	NEW: '_blank'
}

var CMini = function()
{
	
	this.ViewerUrl = 'http://v.calameo.com/2.0/cmini.swf';
	
	this.BookCode = undefined;
	this.BookName = 'Read it online!';
	
	this.Width		= 240;
	this.Height		= 147;
	
	this.Language	= CMiniLanguage.ENGLISH;
	
	this.ClickTo	= CMiniClickTo.OVERVIEW;
	this.ClickToUrl	= '';
	this.ClickTarget = CMiniClickTarget.NEW;
	
	this.AutoFlip	= 0;
	
	this.Navigation	= true;
	
	this.Page		= 1;
	
	this.AuthID		= '';
	
	this.getHTML = function ()
	{
		var query = 'bkcode=' + this.getBookCode();
		query += '&amp;langid=' + this.getLanguage();
		query += '&amp;clickTo=' + this.getClickTo();
		if ( this.getClickToUrl() != '' ) query += '&amp;clickToUrl=' + this.getClickToUrl();
		query += '&amp;clickTarget=' + this.getClickTarget();
		query += '&amp;autoFlip=' + this.getAutoFlip();
		query += '&amp;showArrows=' + this.getNavigation();
		query += '&amp;page=' + this.getStartPage();

		var html = '<div style="text-align: center;">';
	
		if ( this.BookName != undefined )
		{
			if ( this.AuthID != '' )
			{
				html += '<div style="font-weight: bold;"><a href="http://calameo.com/books/' + this.getBookCode() + '?authid=' + this.getAuthID() + '">' + this.getBookName() + '</a></div>';
			}
			else
			{
				html += '<div style="font-weight: bold;"><a href="http://calameo.com/books/' + this.getBookCode() + '">' + this.getBookName() + '</a></div>';
			}
		}
		
		html += '<div style="padding-top: 8px;">';
		
		html += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + this.getWidth() + '" height="' + this.getHeight() + '">';
		html += '<param name="movie" value="' + this.ViewerUrl + '?' + query + '">';
		html += '<param name="scale" value="noscale" />';
		html += '<param name="loop" value="false" />';
		html += '<param name="salign" value="t" />';
		html += '<param name="allowScriptAccess" value="always" />';
		html += '<param name="wmode" value="transparent" />';
		html += '<embed src="' + this.ViewerUrl + '" type="application/x-shockwave-flash" scale="noscale" allowScriptAccess="always" loop="false" salign="t" wmode="transparent" style="width:' + this.getWidth() + 'px; height:' + this.getHeight() + 'px" flashvars="' + query + '"></embed>';
		html += '</object>';

		/* XHTML compatibility
		html += '<!--[if !IE]> -->';
		html += '<object type="application/x-shockwave-flash" data="' + this.ViewerUrl + '?' + query + '" wmode="transparent" allowScriptAccess="always" width="' + this.getWidth() + '" height="' + this.getHeight() + '">';
		html += '<!-- <![endif]-->';
		html += '<!--[if IE]>';
		html += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="' + this.getWidth() + '" height="' + this.getHeight() + '">';
		html += '<param name="movie" value="' + this.ViewerUrl + '?' + query + '" />';
		html += '<param name="wmode" value="transparent" />';
		html += '<param name="allowScriptAccess" value="always" />';
		html += '<!-->';
		html += '</object>';
		html += '<!-- <![endif]-->';
		*/
		
		html += '</div>';

		switch ( this.getLanguage() )
		{
			case CMiniLanguage.ENGLISH:
				html += '<div style="font-size: 11px;"><a href="http://calameo.com/upload/">Publish at Calam&eacute;o</a> or <a href="http://calameo.com/browse/">browse</a> others.</div>';
				break;
			case CMiniLanguage.FRENCH:
				html += '<div style="font-size: 11px;"><a href="http://calameo.com/upload/">Publiez sur Calam&eacute;o</a> ou <a href="http://calameo.com/browse/">explorez</a> la biblioth&egrave;que.</div>';
				break;
			case CMiniLanguage.SPANISH:
				html += '<div style="font-size: 11px;"><a href="http://calameo.com/upload/">Publish at Calam&eacute;o</a> or <a href="http://calameo.com/browse/">browse</a> others.</div>';
				break;
			case CMiniLanguage.JAPANESE:
				html += '<div style="font-size: 11px;"><a href="http://calameo.com/upload/">Publish at Calam&eacute;o</a> or <a href="http://calameo.com/browse/">browse</a> others.</div>';
				break;
			case CMiniLanguage.GERMAN:
				html += '<div style="font-size: 11px;"><a href="http://calameo.com/upload/">Publish at Calam&eacute;o</a> or <a href="http://calameo.com/browse/">browse</a> others.</div>';
				break;
			case CMiniLanguage.CHINESE:
				html += '<div style="font-size: 11px;"><a href="http://calameo.com/upload/">Publish at Calam&eacute;o</a> or <a href="http://calameo.com/browse/">browse</a> others.</div>';
				break;
			case CMiniLanguage.RUSSIAN:
				html += '<div style="font-size: 11px;"><a href="http://calameo.com/upload/">Publish at Calam&eacute;o</a> or <a href="http://calameo.com/browse/">browse</a> others.</div>';
				break;
			case CMiniLanguage.PORTUGUESE:
				html += '<div style="font-size: 11px;"><a href="http://calameo.com/upload/">Publish at Calam&eacute;o</a> or <a href="http://calameo.com/browse/">browse</a> others.</div>';
				break;
			case CMiniLanguage.ITALIAN:
				html += '<div style="font-size: 11px;"><a href="http://calameo.com/upload/">Publish at Calam&eacute;o</a> or <a href="http://calameo.com/browse/">browse</a> others.</div>';
				break;
			case CMiniLanguage.KOREAN:
				html += '<div style="font-size: 11px;"><a href="http://calameo.com/upload/">Publish at Calam&eacute;o</a> or <a href="http://calameo.com/browse/">browse</a> others.</div>';
				break;
		}

		html += '</div>';

		return html;
	};
	
	this.getJavascript = function ()
	{
		var html = '';
		
		html += '<script type="text/javascript" src="http://calameo.com/cmini.js"></script>\n';
		html += '<script type=\"text/javascript\">\n';
		html += 'var cmini = new CMini();\n';
		html += 'cmini.setLanguage("' + this.getLanguage() + '");\n';
		html += 'cmini.setSize(' + this.getWidth() + ', ' + this.getHeight() + ');\n';
		html += 'cmini.setClick("' + this.getClickTo() + '", "' + this.getClickTarget() + '");\n';
		html += 'cmini.setAutoFlip(' + this.getAutoFlip() + ');\n';
		html += 'cmini.showNavigation(' + this.getNavigation() + ');\n';
		html += 'cmini.setStartPage(' + this.getStartPage() + ');\n';
		html += 'cmini.display(this.getBookCode());\n';
		html += '</script>';

		return html;
	};
	
	this.get = function ( bkcode )
	{
		this.setBookCode( bkcode );
		return this.getHTML();
	};
	
	this.display = function ( bkcode )
	{
		document.write( this.get( bkcode ) );	
	};
	
	this.setBookCode = function ( bkcode )
	{
		this.BookCode = bkcode;
	};
	
	this.getBookCode = function ()
	{
		return this.BookCode;
	};
	
	this.setLanguage = function( langid )
	{
		this.Language = langid;	
	};
	
	this.getLanguage = function ()
	{
		if ( this.Language != CMiniLanguage.ENGLISH && this.Language != CMiniLanguage.FRENCH && this.Language != CMiniLanguage.SPANISH )
		{
			return CMiniLanguage.ENGLISH;			
		}

		return this.Language;
	};
	
	this.setSize = function ( width, height )
	{
		this.Width = width;
		this.Height = height;
	};
	
	this.getWidth = function ()
	{
		return this.Width;
	};
	
	this.getHeight = function ()
	{
		return this.Height;
	};
	
	this.setClick = function(to, target, url)
	{
		this.ClickTo = to;
		this.ClickTarget = target;
		
		if ( url == undefined ) url = '';
		
		this.ClickToUrl = url;
	};
	
	this.getClickTo = function ()
	{
		return this.ClickTo;
	};
	
	this.getClickToUrl = function ()
	{
		return this.ClickToUrl;
	};
	
	this.getClickTarget = function ()
	{
		return this.ClickTarget;
	};
	
	this.setBookName = function ( bookname )
	{
		this.BookName = bookname;	
	};
	
	this.getBookName = function ()
	{
		return this.BookName;
	};
	
	this.setAutoFlip = function ( seconds )
	{
		this.AutoFlip = seconds;	
	};
	
	this.getAutoFlip = function ()
	{
		return this.AutoFlip;
	};
	
	this.showNavigation = function ( navigation )
	{
		this.Navigation = navigation;	
	};
	
	this.getNavigation = function ()
	{
		return ( this.Navigation == true ) ? '1' : '0';
	};
	
	this.setStartPage = function ( page )
	{
		this.Page = page;	
	};
	
	this.getStartPage = function ()
	{
		return this.Page;
	};
	
	this.setAuthID = function ( authid )
	{
		this.AuthID = authid;	
	};
	
	this.getAuthID = function ()
	{
		return this.AuthID;
	};
}

// Backward compatibility with cMini v1.0
// ---------------------------------------------
var _cBKCODE = undefined;
var _cLANGID = "EN";
var _cWIDTH = 256;
var _cHEIGHT = 175;
var _cCLICKTO = undefined;
var _cCLICKTARGET = "_self";
var _cCLICKTEXT = "";
var _cAUTOFLIP = undefined;
var _cSHOWARROWS = false;
var _cPAGE = undefined;

var Url = {

    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

function getCMiniCode(url,bkcode,langid,width,height,clickTo,clickTarget,clickText,autoFlip,showArrows,page)
{
	var _cHTML = '';
	
	if ( url && bkcode )
	{
		var _cQUERY = 'bkcode=' + bkcode + '&langid=' + langid;
		
		if ( clickTo != undefined && clickTo )								_cQUERY += '&clickTo='+Url.encode(clickTo);
		if ( clickTarget != undefined && clickTarget )						_cQUERY += '&clickTarget='+Url.encode(clickTarget);
		//if ( clickText != undefined )										_cQUERY += '&clickText='+Url.encode(clickText);
		if ( autoFlip != undefined && autoFlip )							_cQUERY += '&autoFlip='+Math.abs(Math.round(autoFlip));
		if ( showArrows != undefined && showArrows != "0" )					_cQUERY += '&showArrows=1';
		if ( page != undefined && page )									_cQUERY += '&page='+Math.abs(Math.round(page));
	
		if ( clickText != undefined )
		{
			_cHTML += '<div style="text-align: center;"><a href="http://calameo.com/books/' + bkcode + '" style="font-size: 11px;">' + clickText + '</a></div>';
		}
		
		_cHTML += '<div style="padding-top: 4px;">';
		_cHTML += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + width + '" height="' + height + '">';
		_cHTML += '<param name="movie" value="' + url + '?' + _cQUERY + '">';
		_cHTML += '<param name="scale" value="noscale" />';
		_cHTML += '<param name="loop" value="false" />';
		_cHTML += '<param name="salign" value="t" />';
		_cHTML += '<param name="allowScriptAccess" value="never" />';
		_cHTML += '<param name="wmode" value="transparent" />';
		_cHTML += '<embed src="' + url + '" type="application/x-shockwave-flash" scale="noscale" allowScriptAccess="always" loop="false" salign="t" wmode="transparent" style="width:' + width + 'px; height:' + height + 'px" flashvars="' + _cQUERY + '"></embed>';
		_cHTML += '</object>';
		
		_cHTML += '</div>';
	}
	return _cHTML;
}

function getCMiniJSCode(bkcode,langid,width,height,clickTo,clickTarget,clickText,autoFlip,showArrows,page)
{
	var _cHTML = '';
	
	if ( bkcode )
	{
		_cHTML += '<script type="text/javascript" src="http://www.calameo.com/cmini.js"></script>\n';
		_cHTML += '<script type=\"text/javascript\">\n';
		_cHTML += '_cBKCODE=\''+bkcode+'\';\n';
		_cHTML += '_cLANGID=\''+langid+'\';\n';
		_cHTML += '_cWIDTH=\''+width+'\';\n';
		_cHTML += '_cHEIGHT=\''+height+'\';\n';
		if ( clickTo != undefined ) _cHTML += '_cCLICKTO=\''+escape(clickTo)+'\';\n';
		if ( clickTarget != undefined && clickTarget != '' ) _cHTML += '_cCLICKTARGET=\''+escape(clickTarget)+'\';\n';
		if ( clickText != undefined && clickText ) _cHTML += '_cCLICKTEXT=\''+clickText+'\';\n';
		if ( autoFlip != undefined && autoFlip ) _cHTML += '_cAUTOFLIP=\''+Math.abs(Math.round(autoFlip))+'\';\n';
		if ( showArrows != undefined && showArrows ) _cHTML += '_cSHOWARROWS=true;\n';
		if ( page != undefined && page ) _cHTML += '_cPAGE=\''+Math.abs(Math.round(page))+'\';\n';
		_cHTML += 'displayCMini();\n';
		_cHTML += '</script>';
	}
	return _cHTML;
}

function getCMini()
{
	var _cURL = "http://v.calameo.com/2.0/cmini.swf";
	return getCMiniCode(_cURL, _cBKCODE, _cLANGID, _cWIDTH, _cHEIGHT, _cCLICKTO, _cCLICKTARGET, _cCLICKTEXT, _cAUTOFLIP, _cSHOWARROWS, _cPAGE);
}

function displayCMini(){
	document.write( getCMini() );
}

/* add document.getElementById if needed (mobile IE < 5) */
if (!document.getElementById && document.all) { document.getElementById = function(id) { return document.all[id]; }}