﻿// ==== First some helper functions ====

// Nice, handy strprintf for javascript
function jstrprintf() {
    len = arguments.length;
    if (len == 0) { return; }
    if (len == 1) { return arguments[0]; }
    
    var result;
    var regexstr;
    var replstr;
    var formatstr;
    var re;
    
    result = "";
    regexstr = "";
    replstr = "";
    formatstr = arguments[0];
    
    for (var i=1; i<arguments.length; i++) {
        replstr += String(i+100) + arguments[i]  + String(i + 100);
        regexstr += String(i+100) + "(.*)" + String(i+100);
    }
    re = new RegExp(regexstr);
    var result;
    result = replstr.replace(re, formatstr);
    return result;
}

function AddPx(num) {
    return String(num) + "px";
}

function findParentDiv(obj) {
    while (obj) {
        if (obj.tagName.toUpperCase() == "DIV") {
            return obj;
        }
        
        if (obj.parentElement) {
            obj = obj.parentElement;
        }
        else {
            return null;
        }
    }
    return null;
}

function findParentTagById(obj, parentname) {
    while (obj) {
        if (obj.id.match(parentname)) {
            return obj;
        }
        
        if (obj.parentElement) {
            obj = obj.parentElement;
        }
        else {
            return null;
        }
    }
    return null;
}

// Now for the real thing

var topZ = 100;
var startX;
var startY;
startX = 100;
startY = 100;
nextID = 1;

function CreateDropdownWindow(caption, theWidth, theHeight, canMove, contentSource) {
    var newdiv;
    newdiv = document.createElement("div");
    newdiv.id = "dragTitle" + String(nextID);
    newdiv.className = "divDragTitle";
    newdiv.style.width = theWidth;
    //newdiv.style.height = theHeight;
    newdiv.style.left = AddPx(startX);
    newdiv.style.top = AddPx(startY);
    newdiv.style.zIndex = topZ;
    newdiv.innerHTML = jstrprintf(
        '<table><tr><td>$1</td>' + 
        '<td style="text-align:right">' +
        '<img src="/ihomestatics/cn/scripts/js/dragwins/buttontop.gif" class="divTitleButton" id="dragButton$2" ' + 
        'onmousedown="javascript:toggleContentWin($2)" />' + // min button 
        '<img src="/ihomestatics/cn/scripts/js/dragwins/buttonclose.gif" class="divTitleButton" id="closeButton$2" ' + 
        'onmousedown="javascript:closeWin($2)" />' + // close button
        '</td></tr></table>',
        caption, nextID);
    document.body.appendChild(newdiv);

    var newdiv2;
    newdiv2 = document.createElement("div");
    newdiv2.id = "dragContent" + String(nextID);
    newdiv2.className = "divDragContent";
    newdiv2.style.width = theWidth;
    newdiv2.style.height = theHeight;
    newdiv2.style.left = AddPx(startX);
    newdiv2.style.top = AddPx(startY + 20);
    newdiv2.style.zIndex = topZ;
    if (contentSource) {
        newdiv2.innerHTML = document.getElementById(contentSource).innerHTML;
    }
    
    document.body.appendChild(newdiv2);
    
    // Save away the content DIV into the title DIV for 
    // later access, and vice versa
    newdiv.content = newdiv2;
    newdiv2.titlediv = newdiv;

    //topZ += 1;
    startX += 25;
    startY += 25;
    // If you want you can check when these two are greater than
    // a certain number and then rotate them back to 100,100...
    
    nextID++;
}

function closeWin(id) {
	var title = document.getElementById("dragTitle" + String(id));
	var content = document.getElementById("dragContent" + String(id));
	title.style.display = "none";
	content.style.display = "none";
	if( removeMiniIhome ) {
		removeMiniIhome();
	}
	document.body.removeChild(title);
	document.body.removeChild(content);
}

var winFo;
function broadcast(url,x,y) {
    var content = document.createElement("div");
    content.id = "miniIhome";
    content.style.display = "none";
    document.body.appendChild(content);

	winFo = new SWFObject(url, "miniSwf", "100%", "100%", "7" ,"ffffff");
	winFo.addParam("quality","high");
	winFo.addParam("scale","noscale");
	winFo.addParam("salign","TL");
	winFo.write("miniIhome");

	startX = x||0; // 位置坐标
	startY = y||0;
	// 标题,大小,可移动,显示内容id
    CreateDropdownWindow('梦境家园--电视台', '550px','430px', true,content.id);
	//document.body.removeChild(content);
}
function removeMiniIhome() {
	var elem = document.getElementById("miniIhome");
	if ( elem ) {
		document.body.removeChild(elem);
	}
	// 在此后加其它代码
	sendToActionScript();
}
function moveWin(x,y) {
	var title = document.getElementById("dragTitle" + String(nextID-1));
	var content = document.getElementById("dragContent" + String(nextID-1));
	if (title)
	{
		title.style.left = AddPx(x);
        title.style.top = AddPx(y);
		content.style.left = AddPx(x);
        content.style.top = AddPx(y);
	}
}
function minWin() {
	var title = document.getElementById("dragTitle" + String(nextID-1));
	var content = document.getElementById("dragContent" + String(nextID-1));
	if (title)
	{
		title.style.width = AddPx(0);
        title.style.height = AddPx(0);
		content.style.width = AddPx(0);
        content.style.height = AddPx(0);
	}
	thisMovie("ihome").minWin();
}
function restoreWin() {
	var title = document.getElementById("dragTitle" + String(nextID-1));
	var content = document.getElementById("dragContent" + String(nextID-1));
	if (title)
	{
		title.style.width = "550px";
        title.style.height = "20px";
		content.style.width = "550px";
        content.style.height = "410px";
	}
}
function getTopXY() {
	var elem = document.getElementById("dragTitle" + String(nextID-1));
	if (elem) {
		var strXY = elem.style.left + ':' + elem.style.top;
		strXY = strXY.replace('px','').replace('px','');
		return strXY;
	} else {
		return "0:0";
	}
}
function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
             return window[movieName];
         } else {
             return document[movieName];
    }
}
function sendToActionScript() {
    thisMovie("ihome").notifyClose();
}
function closeMe() {
	closeWin(nextID-1);
}