﻿var popup_list = [];
jQuery.fn.outerHTML = function() {
    return jQuery('<div></div>').append(this.eq(0).clone()).html();
};

function _default(val, defaultVal) {
    return val === undefined || val === null ? defaultVal : val;
}

function rtrim(str, pattern) {
    return str.replace(pattern + "$", "");
}

function toggleImage(obj_id, on_over, on_out) {
    var obj = jQuery("img[id='" + obj_id + "']").get(0);
    if (obj && obj.src) {
        obj.onmouseover = function() { this.src = on_over; }
        obj.onmouseout = function() { this.src = on_out; }
    }
}
function preload_images(img_array, base_path) {
    base_path = base_path || "";
    if (img_array && img_array.push) {
        var img;
        for (var i = 0; i < img_array.length; i++) {
            if (img_array[i] == null || img_array[i] == "") continue;
            img = new Image();
            img.src = base_path + img_array[i];
        }
    }
}
function OpenPopup(url, width, height) {
    var features = "toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes,width=" + width + ",height=" + height + "";
    window.open(url, "", features);
}
function showRCPopup(div_id, url, o_w, o_h) {
    var d = jQuery("#popup_format");
    var target = jQuery("#" + div_id);
    var l = 0;
    var _h = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
    try {
        w = parseInt(target.get(0).style.width.replace("px", ""), 10) || o_w || 600;
        h = parseInt(target.get(0).style.height.replace("px", ""), 10) || o_h || 300;
    } catch (e) { }

    w += 86;
    h += 66;
    if (h > _h) {
        l = 10;
    } else {
        l = (_h - h) / 2;
    }
    //alert(l + " - " + h + " - " + w + " - " + _h);
    //Checking if we can find scroll position:
    var _top = document.body.scrollTop
          || window.pageYOffset
          || (document.body.parentElement
              ? document.body.parentElement.scrollTop
              : 0
              );
    var _html = target.outerHTML()
        .replace('PARAM NAME="Play" VALUE="-1"', 'PARAM NAME="Play" VALUE="0"')
        .replace('NAME="FlashVars" VALUE=""', 'NAME="FlashVars" VALUE="autoplay=t&enablecallbacks=t"')
        ;
    //alert(_html);
    jQuery(".popup_content_div", d).html(_html);
    jQuery(".popup_content_div #" + div_id, d).show();
    showPopup("popup_format", w, null, l + _top);
    if (url) {
        var ifrm = jQuery(".popup_content_div #IFramePopup iframe").eq(0).css({ height: o_h }).get(0);
        if (ifrm) {
            ifrm.src = url;
        }
    }
}

function closeRCPopup() {
    var func = arguments[0];
    jQuery("#popup_format .popup_content_div").html("");
    removePopup();
    if (func && typeof window[func] == 'function') {
        window[func](Array.prototype.splice.call(arguments, 0, 1));
    }
}
function openMiniPopup(div_id, layout_div, content_div, par, pointer_pos) {
    jQuery(document.body).unbind("click", bodyClick);
    var src = jQuery(div_id).eq(0);
    var dest = jQuery(layout_div).eq(0);
    var dest_content_div = jQuery(content_div, dest).eq(0);
    if (src && dest && dest_content_div) {
        dest_content_div.html("");
        dest_content_div.html(src.html());
        var pos = jQuery(par).position();
        var _offset_w = Math.round((jQuery(document).width() - 1000) / 2);
        var _offset_h = 30;
        if (jQuery(".MainContentDiv1 .MiniPopup1").length > 0) {
            _offset_w = 0;
            _offset_h = 10;
        }
        pointer_pos = pointer_pos || 1;
        var x_offset = 0,
        y_offset = 0;
        jQuery(".MiniPopup1 .tc, .MiniPopup1 .bc").html("");
        var m_pos = jQuery(".MainContentDiv1").position(),
        m_w = jQuery(".MainContentDiv1").width(),
        m_h = jQuery(".MainContentDiv1").height();
        if (pointer_pos == 1 || pointer_pos == 2) {
            jQuery(".MiniPopup1 .tc").eq(0).css({ textAlign: pointer_pos == 1 ? "left" : "right" }).html("<img src='images/new_images/blue_mini_pointer_1.png' />")
            dest.css({ left: pos.left + _offset_w, top: pos.top + _offset_h });
        } else {
            jQuery(".MiniPopup1 .bc").eq(0).css({ textAlign: pointer_pos == 4 ? "left" : "right" }).html("<img src='images/new_images/blue_mini_pointer_2.png' />")
            //dest.css({ left: pos.left + _offset_w, top: pos.top + _offset_h - dest.height() - 20 });
            dest.css({ left: pos.left + _offset_w, top: pos.top + _offset_h - dest.height() - 20 });
        }
        var d_pos = { left: parseInt(dest.css("left"), 10), top: parseInt(dest.css("top"), 10) },
        d_w = dest.width(),
        d_h = dest.height();
        if (d_pos.left < 0) {
            dest.css({ left: 20 });
        }
        if (d_pos.left + d_w > m_w) {
            dest.css({ left: m_w - 40 - d_w });
        }

        dest.show();
        setTimeout(function() {
            jQuery(document.body).bind("click", bodyClick);
            jQuery(".MiniPopup1:visible").unbind("click").bind("click", function(event) {
                event.stopPropagation();
            });
        }, 10);
        var close_link = jQuery("a.closeLink", dest).eq(0);
        close_link.unbind("click").bind("click", function() {
            jQuery(document.body).unbind("click");
            dest_content_div.html("");
            dest.hide();
        });
    }
}
function bodyClick(event) {
    jQuery(".MiniPopup1:visible a.closeLink").click();
    jQuery(document.body).unbind("click", bodyClick);
}

function reloadParent() {
    var par = window.opener;
    if (par) {
        par.location.href = par.location.href;
    }
}

function newRCBBox() {
    var str = "\
			<table cellspacing=0 cellpadding=0 class='rcb_box'>\
			<tr>\
				<td class='rcb_tl'></td>\
				<td class='rcb_tc'></td>\
				<td class='rcb_tr'></td>\
			</tr>\
			<tr>\
				<td class='rcb_ml'></td>\
				<td class='rcb_mc'>\
					<div id='rcb_mc_contents'>\
					</div>\
				</td>\
				<td class='rcb_mr'></td>\
			</tr>\
			<tr>\
				<td class='rcb_bl'></td>\
				<td class='rcb_bc'></td>\
				<td class='rcb_br'></td>\
			</tr>\
			</table>\
				";
    var d = document.createElement("div");
    d.className = "rcb_box_container";
    d.id = "RCBBox" + getCountOfBoxes();
    d.innerHTML = str;
    return d;
}
function createRCBPopup(param_object) {
    var po = param_object || {};
    po.div_id = po.div_id || "";
    po.url = po.url || "";
    po.width = isNaN(parseInt(po.width, 10)) ? 600 : parseInt(po.width, 10);
    po.height = isNaN(parseInt(po.height, 10)) ? -1 : parseInt(po.height, 10);
    po.scroll = po.scroll || false;
    var d = newRCBBox();
    if(po.closeButton === true){
        jQuery(".rcb_tr", d).html("<a href='javascript:;' onclick='closeRCBPopup(this);'><img src='/images/new_images/top_right_close.png' class='overlay_close' /></a>");
    }
    var c = d.getElementsByTagName("div")[0];
    c.style.width = po.width + "px";
    if (po.height > 0) {
        c.style.height = po.height + "px";
        c.style.overflowY = "scroll";
    }
    if (po.div_id !== "") {
        applyOverlay();
        var e = document.getElementById(po.div_id);
        if (e) e = e.cloneNode(true);
        if (e && po.url !== "") {
            var ifrm = e.getElementsByTagName("iframe")[0];
            ifrm.src = po.url;
        }
        e.style.display = "block";
        c.appendChild(e);
        po.width += 24;
        po.height += 24;
        document.getElementsByTagName("body")[0].appendChild(d);
        positionPopup(d, po.width, po.height);
        adjustOverlay(d);
    }
    jQuery(window).unbind("resize").bind("resize", function() {
        positionPopup(d, po.width, po.height);
        adjustOverlay();
    });
}
var gOverlay = null;
function positionPopup(div, w, h) {
    //alert(w + " - " + h);
    var s = getSize(1);
    var _s = getSize();
    var _s_top = jQuery(document).scrollTop();
    var posX = parseInt(jQuery(document).scrollLeft() + (s.w - w) / 2, 10) + "px";
    var _posY = (s.h - jQuery(div).height())/2;
    _posY = _posY > 10 ? _posY : 10;
    _posY = _s_top + _posY + h > _s.h ? 10 : _posY;
    var posY = parseInt(_s_top + _posY, 10) + "px";
    jQuery(div).css({width: w, height: h, left: posX, top: posY});
}
function adjustOverlay(div) {
    if (gOverlay && gOverlay.style && gOverlay.style.display == "block") {
        var s = getSize();
        jQuery(gOverlay).css({ width: s.w, height: s.h });
    }
}

function applyOverlay() {
    if (gOverlay == null) {
        gOverlay = document.createElement("div");
        gOverlay.className = "overlay_cover";
        document.getElementsByTagName("body")[0].appendChild(gOverlay);
        var s = getSize();
        jQuery(gOverlay).css({ width: s.w, height: s.h });
    }
    gOverlay.style.display = "block";
}

function createNormalRCBPopup(div_id, width, height) {
    createRCBPopup({ div_id: div_id, width: width, height: height });
}
function createIFrameRCBPopup(div_id, url, width, height) {
    createRCBPopup({ div_id: div_id, url: url, width: width, height: height });
}
function closeRCBPopup(obj) {
    var pn = obj.parentNode;
    while (pn) {
        if (pn.nodeName === "TABLE" && pn.className === "rcb_box") {
            document.getElementsByTagName("body")[0].removeChild(pn.parentNode);
            jQuery(window).unbind("resize");
            gOverlay.style.display = "none";
            break;
        }
        pn = pn.parentNode;
    }
}
function getCountOfBoxes() {
    var cnt = 0;
    var d = document.getElementsByTagName("body")[0].childNodes;
    for (var i = 0; i < d.length; i++) {
        if (d[i].nodeType === 1 && d[i].className === "rcb_box_container") {
            cnt++;
        }
    }
    return cnt;
}

function getSize(visible) {
    visible = visible || 0;
    var o = {};
    if (visible) {
        o = {
            w: window.document.documentElement.clientWidth,
            h: window.document.documentElement.clientHeight
        };
    } else {
        o = {
            w: Math.max(window.document.documentElement.scrollWidth, window.document.documentElement.clientWidth),
            h: Math.max(window.document.documentElement.scrollHeight, window.document.documentElement.clientHeight)
        };
    }
    return o;
}