
var t2 = null;
var hStat = 0;
var hID = 'threedcontainer';
var mythreedprofile;
var wmitemid;

function maximizewin(){
  document.getElementById("topA").style.display="none";
  document.getElementById("topB").style.display="";
  document.getElementById("threedcontainer").style.left="20px";
  document.getElementById("threedcontainer").style.top="20px";
  document.getElementById("threedcontainer").style.height=(f_clientHeight()-50)+"px";
  document.getElementById("threedcontainer").style.width=(f_clientHeight()-50)*1.6+"px";
  document.getElementById("silverlightControlHost").style.width=(f_clientHeight()-50)*1.6+"px";
  document.getElementById("silverlightControlHost").style.height=(f_clientHeight()-50)+"px";
}

function minimizewin(){
  document.getElementById("topA").style.display="";
  document.getElementById("topB").style.display="none";
  document.getElementById("threedcontainer").style.left="50px";
  document.getElementById("threedcontainer").style.top="50px";
  document.getElementById("threedcontainer").style.height="500px";
  document.getElementById("threedcontainer").style.width="800px";
  document.getElementById("silverlightControlHost").style.width="800px";
  document.getElementById("silverlightControlHost").style.height="500px";
}

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}


function ViewerProcessReqChange() {
	if (liveSearchReq.readyState == 4) {
		var  res = document.getElementById("slviewer");
		res.style.display = "";
		res.innerHTML = liveSearchReq.responseText;

                var oScript = document.createElement("script");
                                    oScript.src = "http://www.google-analytics.com/urchin.js";
                                document.body.appendChild(oScript);
                _uacct = "UA-611735-1";
                urchinTracker();
	}
}

function ViewerStart(mythreedprofile, wmitemid) {
	if (t2) {
		window.clearTimeout(t2);
	}
        document.getElementById("threedcontainer").style.display = "";
        //document.getElementById("slviewer").style.display="none";
        document.getElementById("slviewer").innerHTML = '<center><img style="margin-top:100px;" src="http://reseller.cartft.com/carpcimages/Silverlight/ajax_loader.gif"></center>';
        document.getElementById("topC").innerHTML = '<a target="blank" href="http://reseller.cartft.com/threed_slviewer_inserter_html?wm_item_id=' + wmitemid + '" style="color:black;">Open HTML5-viewer</a>';
	t2 = window.setTimeout("Viewer(mythreedprofile, wmitemid)",100);
}

function Viewer(mythreedprofile, wmitemid) {
	if (window.XMLHttpRequest) {
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
        liveSearchReq.onreadystatechange = ViewerProcessReqChange;
        if ((navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1) || (navigator.userAgent.indexOf('iPad') != -1)) {  
    	        liveSearchReq.open("GET", "http://reseller.cartft.com/threed_slviewer_inserter?wm_item_id=" + wmitemid + "&my_threed_profile=" + mythreedprofile + "&iphone=1&r=" + Math.floor(Math.random()*50000), true );
        } else {
   	        liveSearchReq.open("GET", "http://reseller.cartft.com/threed_slviewer_inserter?wm_item_id=" + wmitemid + "&my_threed_profile=" + mythreedprofile + "&r=" + Math.floor(Math.random()*50000), true );
        }
	liveSearchReq.send(null);
}

function StartHelp(mythreedprofile, wmitemid) {
    if (hStat == 0) {
        onOff(hID);
        hStat = 1;
        init();
        ViewerStart(mythreedprofile, wmitemid);
    }
}

function onOff(id) {
    obj = (document.getElementById ? document.getElementById(id) : (document.all ? document.all[id] : (document.layers ? document.layers[id] : null)));
    obj.style.visibility = (obj.style.visibility == 'visible' ? 'hidden' : 'visible');
}

function EndHelp(id) {
    hStat = 0;
    hID = 'threedcontainer';
    obj = (document.getElementById ? document.getElementById(id) : (document.all ? document.all[id] : (document.layers ? document.layers[id] : null)));
    obj.style.visibility = 'hidden';

    var  res = document.getElementById("silverlightControlHost");
    res.innerHTML = "";

    document.getElementById("topA").style.display="";
    document.getElementById("topB").style.display="none";

    document.getElementById("threedcontainer").style.left="50px";
    document.getElementById("threedcontainer").style.top="50px";
    document.getElementById("threedcontainer").style.height="500px";
    document.getElementById("threedcontainer").style.width="800px";
    //document.getElementById("silverlightControlHost").style.width="800px";
    //document.getElementById("silverlightControlHost").style.height="500px";
}




  var objDrag = null;     // Element, über dem Maus bewegt wurde

  var mouseX   = 0;       // X-Koordinate der Maus
  var mouseY   = 0;       // Y-Koordinate der Maus

  var offX = 0;           // X-Offset der Maus zur linken oberen Ecke des Elements
  var offY = 0;           // Y-Offset der Maus zur linken oberen Ecke des Elements

  // Browserweiche
  IE = document.all&&!window.opera;
  DOM = document.getElementById&&!IE;

  // Initialisierungs-Funktion
  function init(){
    // Initialisierung der Überwachung der Events
    document.onmousemove = doDrag;  // Bei Mausbewegung die Fkt. doDrag aufrufen
    document.onmouseup = stopDrag;  // Bei Loslassen der Maustaste die Fkt. stopDrag aufrufen
  }

  // Wird aufgerufen, wenn die Maus über einer Box gedrückt wird
  function startDrag(objElem) {
    // Objekt der globalen Variabel zuweisen -> hierdurch wird Bewegung möglich
    objDrag = objElem;

    // Offsets im zu bewegenden Element ermitteln
    offX = mouseX - objDrag.offsetLeft;
    offY = mouseY - objDrag.offsetTop;
  }

  // Wird ausgeführt, wenn die Maus bewegt wird
  function doDrag(ereignis) {
    // Aktuelle Mauskoordinaten bei Mausbewegung ermitteln
    mouseX = (IE) ? window.event.clientX : ereignis.pageX;
    mouseY = (IE) ? window.event.clientY : ereignis.pageY;

    // Wurde die Maus über einem Element gedrück, erfolgt eine Bewegung
    if (objDrag != null) {
      // Element neue Koordinaten zuweisen
      objDrag.style.left = (mouseX - offX) + "px";
      objDrag.style.top = (mouseY - offY) + "px";

      // Position in Statusleiste ausgeben
      window.status = "Box-Position: " + objDrag.style.left + ", " + objDrag.style.top;
    }
  }

  // Wird ausgeführt, wenn die Maustaste losgelassen wird
  function stopDrag(ereignis) {
    // Objekt löschen -> beim Bewegen der Maus wird Element nicht mehr verschoben
    objDrag = null;
  }

// Silverlight Funktionen
        function onSilverlightError(sender, args) {
            var appSource = "";
            if (sender != null && sender != 0) {
              appSource = sender.getHost().Source;
            }
            
            var errorType = args.ErrorType;
            var iErrorCode = args.ErrorCode;

            if (errorType == "ImageError" || errorType == "MediaError") {
              return;
            }

            var errMsg = "Unhandled Error in Silverlight Application " +  appSource + "\n" ;

            errMsg += "Code: "+ iErrorCode + "    \n";
            errMsg += "Category: " + errorType + "       \n";
            errMsg += "Message: " + args.ErrorMessage + "     \n";

            if (errorType == "ParserError") {
                errMsg += "File: " + args.xamlFile + "     \n";
                errMsg += "Line: " + args.lineNumber + "     \n";
                errMsg += "Position: " + args.charPosition + "     \n";
            }
            else if (errorType == "RuntimeError") {           
                if (args.lineNumber != 0) {
                    errMsg += "Line: " + args.lineNumber + "     \n";
                    errMsg += "Position: " +  args.charPosition + "     \n";
                }
                errMsg += "MethodName: " + args.methodName + "     \n";
            }

            throw new Error(errMsg);
        }

