﻿var social_window;

function OpenFacebook()
{
	social_window = window.open("Facebook.aspx", "SocialNetworkWindow", "status=0, menubar=0, toolbar=0, location=0, width=900, height=600");
}

function OpenBebo()
{
	social_window = window.open("Bebo.aspx", "SocialNetworkWindow", "status=0, menubar=0, toolbar=0, location=0, width=800, height=400");
}

function OpenMySpace()
{
	social_window = window.open("MySpace.aspx", "SocialNetworkWindow", "status=0, menubar=0, toolbar=0, location=0, width=800, height=500");
}

function OpenTwitter()
{
	social_window = window.open("Twitter.aspx", "SocialNetworkWindow", "status=0, menubar=0, toolbar=0, location=0, width=800, height=420");
}

function UpdateIdentity(data)
{
	social_window.close();
	ContagionAPI_wrapper.UpdateIdentity(data);
}

function CloseIFrame()
{
	social_window.close();
}

function ShowPopup(id, html, offsetX, offsetY)
{
	if (document.getElementById(id)) { ClosePopup(id); }

	var div = document.createElement('div');
	div.setAttribute('id', id);
	div.style.position = 'absolute';

	div.style.left = findPosX(ContagionAPI_wrapper) + offsetX + 'px';
	div.style.top = findPosY(ContagionAPI_wrapper) + offsetY + 'px';
	div.innerHTML = html;
	document.getElementsByTagName('body')[0].appendChild(div);
}

function findPosX(obj)
{
	var curleft = 0; if (obj.offsetParent) while (1) { curleft += obj.offsetLeft; if (!obj.offsetParent) break; obj = obj.offsetParent; } else if (obj.x) curleft += obj.x; return curleft;
}
function findPosY(obj)
{
	var curtop = 0; if (obj.offsetParent) while (1) { curtop += obj.offsetTop; if (!obj.offsetParent) break; obj = obj.offsetParent; } else if (obj.y) curtop += obj.y; return curtop;
}

function ClosePopup(id)
{
	var div = document.getElementById(id);
	if (div) { div.parentNode.removeChild(div); }
}

//////////////////////////////////////////////////////
/////////////// Static Popup Div /////////////////////
//////////////////////////////////////////////////////

var staticPopupInstances = [];
var currentStaticPopupID = null;

function CreateStaticPopupDiv(id, url, x, y, width, height) {
    staticPopupInstances[id] = new StaticPopupInstance(id, url, x, y, width, height);
}

function ShowStaticPopupDiv(id, itemIdToShow) {
    if (currentStaticPopupID == id) return;
    if (currentStaticPopupID) { staticPopupInstances[currentStaticPopupID].div.style.display = "none"; staticPopupInstances[currentStaticPopupID].iframe.contentWindow.hideShownItem(); }
    staticPopupInstances[id].div.style.display = "block";
    currentStaticPopupID = id;
    
    if (itemIdToShow)
        staticPopupInstances[id].iframe.contentWindow.showItem(itemIdToShow);
}

function HideStaticPopupDiv(id) {
    if (!id)
        id = currentStaticPopupID;
    staticPopupInstances[id].div.style.display = "none";
   ContagionAPI_wrapper.ClosePopup();
    try {
        staticPopupInstances[id].iframe.contentWindow.hideShownItem();
    }
    catch (e) { }
    
}

function StaticPopupInstance(id, url, offsetX, offsetY, width, height) {
    this.div = document.createElement("div");

    this.iframe = document.createElement("iframe");
    this.div.appendChild(this.iframe);
        
    this.div.style.position = "relative";
    this.div.style.display = "none";
    this.div.style.margin = "-672px auto 0 auto";
    this.div.style.padding = "0 33px 0 33px";
    this.div.style.top = offsetY + 'px';
    this.div.style.width = width + "px";
    this.div.style.height = height + "px";
    this.iframe.style.position = "absolute";
    this.iframe.style.left = "34px";
    this.iframe.style.width = width + "px";
    this.iframe.style.height = height + "px";
    this.iframe.frameBorder = "0";

    this.iframe.src = url;

    var close_a = document.createElement("a");
    close_a.innerHTML = "<img src='../images/close.png' alt='' />";
    close_a.style.position = "absolute";
    close_a.style.top = "0px";
    close_a.style.right = "31px";
    close_a.onclick = function() { HideStaticPopupDiv(id); };
    this.div.appendChild(close_a);

    document.body.appendChild(this.div);
}

//////////////////////////////////////////////////////
/////////////// Animated Popup Div ///////////////////
//////////////////////////////////////////////////////

var animatedPopupInstances = [];

function CreateAnimatedPopup(id, startX, startY, endX, endY, startWidth, startHeight, endWidth, endHeight, duration) {
    animatedPopupInstances[id] = new AnimatedPopupInstance(id, startX, startY, endX, endY, startWidth, startHeight, endWidth, endHeight, duration);
}

function OpenAnimatedPopup(id, url) {
    animatedPopupInstances[id].loadUrl(url);
    animatedPopupInstances[id].startOpen();
}

function HideAnimatedPopup(id) {
    animatedPopupInstances[id].startClose();
}

function AnimatedPopupInstance(id, startX, startY, endX, endY, startWidth, startHeight, endWidth, endHeight, duration) {
    var tick_length = 20;

    var div = document.getElementById(id);
    if (!div) {
        div = document.createElement("div");
        document.body.appendChild(div);
    }
    var iframe = document.createElement("iframe");
    div.appendChild(iframe);

    var close_a = document.createElement("a");
    close_a.innerHTML = "Close";
    close_a.style.position = "absolute";
    close_a.style.top = "0px";
    close_a.style.right = "0px";
    close_a.onclick = function() { HideAnimatedPopup(id); };
    div.appendChild(close_a);

    div.style.position = "absolute";
    div.style.overflow = "hidden";
    div.style.top = startY + "px";
    div.style.left = startX + "px";
    div.style.width = startWidth + "px";
    div.style.height = startHeight + "px";
    iframe.style.width = endWidth + "px";
    iframe.style.height = endHeight + "px";
    iframe.frameBorder = "0";

    var steps = duration/tick_length;

    var x_step = (endX - startX) / steps;
    var y_step = (endY - startY) / steps;
    var w_step = (endWidth - startWidth) / steps;
    var h_step = (endHeight - startHeight) / steps;
    
    var x = startX;
    var y = startY;
    var width = startWidth;
    var height = startHeight;
    var tick_count = 0;

    var closing = false;
    var opening = false;

    this.loadUrl = function(url) {
        iframe.src = url;
    }

    this.startOpen = function() {
        if (opening)
            return;

        if (closing) {
            closing = false;
            tick_count = steps - Math.abs((endY - parseInt(div.style.top.substring(0, div.style.top.length - 2))) / y_step);
        }
        else 
            tick_count = 0;

        closing = false;
        opening = true;

        this.openTick();
    }

    this.startClose = function() {
        if (closing)
            return;

        if (opening) {
            opening = false;
            tick_count = steps - Math.abs((startY - parseInt(div.style.top.substring(0, div.style.top.length - 2))) / y_step);
        }
        else 
            tick_count = 0;
        
        closing = true;
        opening = false;
        this.closeTick();
    }

    this.openTick = function() {
        tick_count++;
        if (tick_count >= steps) {
            x = endX;
            y = endY;
            div.style.top = endY + "px";
            div.style.left = endX + "px";
            div.style.height = endHeight + "px";
            div.style.width = endWidth + "px";
        }
        else if (opening) {
            x += x_step; div.style.left = x + "px";
            y += y_step; div.style.top = y + "px";
            width += w_step; div.style.width = width + "px";
            height += h_step; div.style.height = height + "px";
            setTimeout('animatedPopupInstances["'+id+'"].openTick()', tick_length)
        }
    }

    this.closeTick = function() {
        tick_count++;
        if (tick_count >= steps) {
            x = startX;
            y = startY;
            div.style.top = startY + "px";
            div.style.left = startX + "px";
            div.style.height = startHeight + "px";
            div.style.width = startWidth + "px";
        }
        else if (closing) {
            x -= x_step; div.style.left = x + "px";
            y -= y_step; div.style.top = y + "px";
            width -= w_step; div.style.width = width + "px";
            height -= h_step; div.style.height = height + "px";
            setTimeout('animatedPopupInstances["'+id+'"].closeTick()', tick_length)
        }
    }
}

