// nom de domaine
var wagwire_siteroot = "www.wagwire.com";


function wagwire_get_domain() {
	if (document.domain!="" && document.domain!="undefined") return document.domain;
	else return "http://" + window.location.toString().split("//")[1].split("/")[0] + "/";
}

function wagwire_get_nav_language() {
  var lct="en";
  if (navigator.language) {
    lct=navigator.language.toLowerCase().substring(0, 2);
  } else if (navigator.userLanguage) {
    lct=navigator.userLanguage.toLowerCase().substring(0, 2);
  } else if (navigator.userAgent.indexOf("[")!=-1) {
    var debut=navigator.userAgent.indexOf("[");
    var fin=navigator.userAgent.indexOf("]");
    lct=navigator.userAgent.substring(debut+1, fin).toLowerCase();
  }
  if (lct=="fr") return "fr";
  else return "en";
}

var wagwire_browser_test = {
	ie_test: function() {
        return navigator.userAgent.indexOf("MSIE") >= 0;
    },
    iepre7_test: function() {
        return navigator.userAgent.indexOf("MSIE 6") >= 0 || navigator.userAgent.indexOf("MSIE 5.5") >= 0;
    },
	opera_test: function() {
        return navigator.userAgent.indexOf("Opera") >= 0;
    }
}

// Construction du Wag Mini
function Wagwire(key) {
    this.wagwire_build_app = wagwire_build_app;
    
    var wagmini_div = null;
    
    var key = key;
    
    function wagwire_build_app() {
        if (wagmini_div != null)
            return;

		var doc = window.document;
        var body = wagwire_get_body_element();

        wagmini_div = doc.createElement("div");
        wagmini_div.id = "wagwire_wagmini_div";
        body.appendChild(wagmini_div);
        
        with (wagmini_div.style) {
        	backgroundImage = 'url("http://'+wagwire_siteroot+'/app/recorder/html/resources/images/wag/background.png")';
        	backgroundRepeat = 'no-repeat';
        	backgroundPosition = 'center center';
        	width = "300px";
            height = "300px";
            position = "fixed";
            right = "20px";
            top = "20px";
            textAlign = "center";
            zIndex = "1000000000";
        }
        
		// the close window link
		var close_button = doc.createElement("div");
		if (wagwire_get_nav_language()=="fr") close_button.appendChild(doc.createTextNode("fermer"));
		else close_button.appendChild(doc.createTextNode("close"));
		with (close_button.style) {
	        position = "absolute";
			top = "20px";
			left = "20px";
			fontSize = "14px";
			fontFamily = " \"Helvetica Neue\", Helvetica, Geneva, Arial, sans-serif";
			fontWeight = "bold";
			color = "#aaa";
			cursor = "pointer";
		}
        
        close_button.onclick = close;
        close_button.onmousedown = function() {
			with (close_button.style) {
				color = "#26b1fd";
			}
        }
        close_button.onmouseover = function () {
			with (close_button.style) { 
				color = "#fff";
			}
        }        
        close_button.onmouseout = function () {
			with (close_button.style) {
				color = "#aaa";
			}
        }
        
		// the poweredby link
		var poweredby_button = doc.createElement("a");
    	poweredby_button.setAttribute('href', 'http://'+wagwire_siteroot);
    	poweredby_button.setAttribute('target', '_blank');
		poweredby_button.appendChild(doc.createTextNode("powered by Wagwire"));

		with (poweredby_button.style) {
	        textDecoration = "none";
	        position = "relative";
			top = "250px";
			left = "auto";
			right = "auto";
			fontSize = "11px";
			fontFamily = " \"Helvetica Neue\", Helvetica, Geneva, Arial, sans-serif";
			color = "#aaa";
			cursor = "pointer";
		}
        
        poweredby_button.onmousedown = function() {
			with (poweredby_button.style) {
				color = "#26b1fd";
			}
        }
        poweredby_button.onmouseover = function () {
			with (poweredby_button.style) { 
				color = "#fff";
			}
        }        
        poweredby_button.onmouseout = function () {
			with (poweredby_button.style) {
				color = "#aaa";
			}
        }
        
        // wag mini object
				
		var wagminibeta = '<div style=" position :absolute; top:65px; left:40px;">'
			+ '<objec'+'t '
		    + 'classi'+'d='+'"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" '
		    + 'codebas'+'e='+'"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" '
		    + 'widt'+'h='+'"215" '
		    + 'heigh'+'t='+'"170" '
		    + 'i'+'d='+'"myFlash">'
		
		    + '<para'+'m '
		    + 'nam'+'e='+'"movie" '
		    + 'valu'+'e='+'"http://'+wagwire_siteroot+'/app/recorder/html/gateway/wag/?id='+key+'&siteRoot='+wagwire_siteroot+'" '
		
		    + '<para'+'m '
		    + 'nam'+'e='+'"quality" '
		    + 'valu'+'e='+'"high"/>'
		
		    + '<'+'para'+'m '
		    + 'nam'+'e='+'"bgcolor" '
		    + 'valu'+'e='+'"#ffffff"/>'
		
/*
		    + '<para'+'m '
		    + 'nam'+'e='+'"wmode" '
		    + 'valu'+'e='+'"transparent"/>'
*/
		
		    + '<embe'+'d '
		    + 'sr'+'c='+'"http://'+wagwire_siteroot+'/app/recorder/html/gateway/wag/?id='+key+'&siteRoot='+wagwire_siteroot+'" '
		    + 'qualit'+'y='+'"high" '
		    + 'bgcolo'+'r='+'"#ffffff" '
		    + 'widt'+'h='+'"215" '
		    + 'heigh'+'t='+'"170" '
		    + 'nam'+'e='+'"myFlash" '
		    + 'swLiveConnec'+'t='+'"true" '
/* 		    + 'wmod'+'e='+'"transparent" ' */
		    + 'typ'+'e='+'"application/x-shockwave-flash" '
		    + 'pluginspac'+'e='+'"http://www.macromedia.com/go/getflashplayer">'
		    + '<\/embed>'
		
		    + '<\/objec'+'t>'
		    + '</div>';
		    
		wagmini_div.innerHTML = wagminibeta;

		// attach close button
		wagmini_div.appendChild(close_button);

		// attach poweredby button
		wagmini_div.appendChild(poweredby_button);

		//fix IE and Opera position problem
		fixPosition();
    }
    
	    
    function wagwire_get_body_element() {
        var bodyElement = window.document.body;

        var bodyElements = window.document.getElementsByTagName("body");
        if (bodyElements && bodyElements.length == 1 && bodyElements.item(0))
            bodyElement = bodyElements.item(0);

        return bodyElement;
    }

        

    function close() {
    	if (wagmini_div)
            wagmini_div.parentNode.removeChild(wagmini_div);
    }
    
    // IE has problem with fixed position, so we have to adjust it
    function fixPosition() {
    	if (wagwire_browser_test.opera_test() || (wagwire_browser_test.ie_test() && !(!wagwire_browser_test.iepre7_test() && window.document.compatMode != "BackCompat"))) {
           	wagmini_div.style.position = "absolute";
    		
			window.onresize = _fixPosition;
			window.onscroll = _fixPosition;
			wagwire_get_body_element().onscroll = _fixPosition;
			
			_fixPosition();
		}
    }
    
    function _fixPosition() {
    	if(wagmini_div) {
			wagmini_div.style.top = (wagwire_get_body_element().scrollTop) + "px";
    	}
    }
    
}

// Prépare l'affichage du Wag Mini
function wagwire_build_app (key) {
/* 	alert(key); */
	wagwire = new Wagwire(key);
    wagwire.wagwire_build_app();
}

// Construction du bouton du Wag Mini
function wagwire_build_button (key) {
		document.write('<objec'+'t ');
		
		document.write('classi'+'d='+'"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ');
		document.write('codebas'+'e='+'"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" ');
		document.write('widt'+'h='+'"170" ');
		document.write('heigh'+'t='+'"65" ');
		document.write('i'+'d='+'"myFlash">');
		
		document.write('<para'+'m ');
		document.write('nam'+'e='+'"allowScriptAccess" ');
		document.write('valu'+'e='+'"always"/>');
		
		document.write('<para'+'m ');
		document.write('nam'+'e='+'"movie" ');
		document.write('valu'+'e='+'"http://'+wagwire_siteroot+'/app/mini/html/gateway/?id='+key+'&siteRoot='+wagwire_siteroot+'"/>');
		
		document.write('<para'+'m ');
		document.write('nam'+'e='+'"quality" ');
		document.write('valu'+'e='+'"high"/>');
		
		document.write('<para'+'m ');
		document.write('nam'+'e='+'"bgcolor" ');
		document.write('valu'+'e='+'"#ffffff"/>');
		
		document.write('<para'+'m ');
		document.write('nam'+'e='+'"wmode" ');
		document.write('valu'+'e='+'"transparent"/>');
		
		document.write('<embe'+'d ');
		document.write('sr'+'c='+'"http://'+wagwire_siteroot+'/app/mini/html/gateway/?id='+key+'&siteRoot='+wagwire_siteroot+'" ');
		document.write('qualit'+'y='+'"high" ');
		document.write('bgcolo'+'r='+'"#ffffff" ');
		document.write('widt'+'h='+'"170" ');
		document.write('heigh'+'t='+'"65" ');
		document.write('nam'+'e='+'"myFlash" ');
 		document.write('allowScriptAcces'+'s='+'"always" ');
 		document.write('swLiveConnec'+'t='+'"true" ');
		document.write('wmod'+'e='+'"transparent" ');
		document.write('typ'+'e='+'"application/x-shockwave-flash" ');
		document.write('pluginspac'+'e='+'"http://www.macromedia.com/go/getflashplayer"/>');
		
		document.write('</objec'+'t>');

}

// Affichage du bouton du Wag Mini
function wagwire_display_wag_mini(key) {
	wagwire_build_button (key);
}
