﻿// JScript File

var tcplayAd_url = 'http://www.twochop.com';
//tcplayAd_url = 'http://localhost:12121';

var tcplayb11 = {
    createPlayButton: function (ptyid, pid, pdata, btype) {
        var vuniqueId = Math.random() * Math.random();
        vuniqueId = (vuniqueId + '').replace('.', '_');
        //vuniqueId = 'ad' + vuniqueId;
        var vGamename = 'TwoChop game'; // '{Game name}';
        var vPlaycount = '0'; //'{Count}';
        if (btype == '2') {
            if (ptyid == '0') {
                document.write("<div style='width: 265px; height: 81px;cursor:pointer; background: url(\"" + tcplayAd_url + "/Content/Images/homeplayBtn.png\") no-repeat;' ");
                document.write("	onclick='javascript:tcplayb11.showGame(\"" + ptyid + "\",\"" + pid + "\",\"" + pdata + "\")'></div> ");
            }
            else if ((ptyid == '1') || (ptyid == '2')) {

                document.write("<div style='width:265px;margin:2px;line-height:81px;height:81px;'>");
                document.write("<table cellpadding='0' cellspacing='0' style='padding:0px;margin:0px;border:0px solid black;width:265px;'>");
                document.write("<tr>");
                document.write("<td style='width:265px;padding:0px;margin:0px;border:0px solid blue;'><div name='playbtnid" + vuniqueId + "' id='playbtnid" + vuniqueId + "' title='Click here to play " + vGamename + "' style='background:url(\"" + tcplayAd_url + "/Content/Images/homeplayBtn.png\");width:265px;height:81px;color:#575757;cursor:pointer;'");
                document.write(" onclick='javascript:tcplayb11.showGame(\"" + ptyid + "\",\"" + pid + "\",\"" + pdata + "\")' ></div>");
                document.write("</td>");
                document.write("<td name='playbtncount" + vuniqueId + "' id='playbtncount" + vuniqueId + "' style='vertical-align:middle;padding:0px;margin:0px;border:0px solid blue;width:33px;height:20px;background:url(\"" + tcplayAd_url + "/Content/Images/bubble2.png\") no-repeat;font-family:helvetica,Arial;font-size:11px;text-align:center;color:#6fbcdc;font-weight:bold;display:none;'>" + vPlaycount + "</td>");
                document.write("</tr>");
                document.write("</table>");
                document.write("</div>");
            }

        }
        else {
            if (ptyid == '0') {
                document.write("<div style='width: 150px; height: 38px;cursor:pointer; background: url(\"" + tcplayAd_url + "/Content/Images/tc_playButtonB.png\") no-repeat;' ");
                document.write("	onclick='javascript:tcplayb11.showGame(\"" + ptyid + "\",\"" + pid + "\",\"" + pdata + "\")'></div> ");
            }
            else if ((ptyid == '1') || (ptyid == '2')) {

                document.write("<div style='width:98px;margin:2px;line-height:20px;height:20px;'>");
                document.write("<table cellpadding='0' cellspacing='0' style='padding:0px;margin:0px;border:0px solid black;width:98px;'>");
                document.write("<tr>");
                document.write("<td style='width:65px;padding:0px;margin:0px;border:0px solid blue;'><div name='playbtnid" + vuniqueId + "' id='playbtnid" + vuniqueId + "' title='Click here to play " + vGamename + "' style='background:url(\"" + tcplayAd_url + "/Content/Images/tc_playButtonB11.png\");width:65px;height:20px;color:#575757;cursor:pointer;'");
                document.write(" onclick='javascript:tcplayb11.showGame(\"" + ptyid + "\",\"" + pid + "\",\"" + pdata + "\")' ></div>");
                document.write("</td>");
                document.write("<td name='playbtncount" + vuniqueId + "' id='playbtncount" + vuniqueId + "' style='vertical-align:middle;padding:0px;margin:0px;border:0px solid blue;width:33px;height:20px;background:url(\"" + tcplayAd_url + "/Content/Images/bubble2.png\") no-repeat;font-family:helvetica,Arial;font-size:11px;text-align:center;color:#6fbcdc;font-weight:bold;'>" + vPlaycount + "</td>");
                document.write("</tr>");
                document.write("</table>");
                document.write("</div>");
            }

        }
        (function () {

            // Localize jQuery variable
            var tcjQuery;

            //Load jQuery if not present
            //if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.6.4') {
            if (window.jQuery === undefined) {
                var script_tag = document.createElement('script');
                script_tag.setAttribute("type", "text/javascript");
                script_tag.setAttribute("src", "http://code.jquery.com/jquery-1.6.4.js");
                script_tag.onload = scriptLoadHandler;
                script_tag.onreadystatechange = function () { // Same thing but for IE
                    if (this.readyState == 'complete' || this.readyState == 'loaded') {
                        scriptLoadHandler();
                    }
                };
                // Try to find the head, otherwise default to the documentElement
                (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
            } else {
                // The jQuery version on the window is the one we want to use
                tcjQuery = window.jQuery.noConflict();
                main();
            }

            //Called once jQuery has loaded
            function scriptLoadHandler() {
                // Restore $ and window.jQuery to their previous values and store the
                // new jQuery in our local jQuery variable
                //jQuery = window.jQuery.noConflict(true);
                tcjQuery = window.jQuery.noConflict();
                // Call our main function
                main();
            }

            //Our main function
            function main() {
                tcjQuery(document).ready(function ($) {
                    $('iframe').each(function () {
                        var addurl;

                        if ($(this).attr("src")) {
                            if ($(this).attr("src").split('?').length > 1) {
                                var url = $(this).attr("src").split('?');

                                //check for twochop widget iframe if not pass wmode
                                if (url != tcplayAd_url + "/blank.htm") {
                                    $(this).attr("src", url[0] + "?wmode=transparent&" + url[1]);
                                }
                            }
                            else {
                                var url = $(this).attr("src");

                                //check for twochop widget iframe if not pass wmode
                                if (url != tcplayAd_url + "/blank.htm") {
                                    //$(this).attr("src", url + "?wmode=transparent");
                                }
                            }
                        }
                    });

                    $.getJSON("http://leaderboard.twochop.com/PlayStats/getbuttonclicks/" + pid + "?callback=?", function (playbtndata) {
                        document.getElementById('playbtnid' + vuniqueId).title = 'Click here to play ' + playbtndata.activityname;
                        document.getElementById('playbtncount' + vuniqueId).innerHTML = playbtndata.clicks;
                    });
                });
            }

        })(); // We call our anonymous function immediately

        if (!document.getElementById('dv2CPlyAdCntr')) {

            var dv2CPlyAdCntr = document.createElement('div');
            dv2CPlyAdCntr.id = 'dv2CPlyAdCntr';
            dv2CPlyAdCntr.style.background = 'url("' + tcplayAd_url + '/Content/Images/W11/bgb11.png")';
            dv2CPlyAdCntr.style.position = 'absolute';
            dv2CPlyAdCntr.style.top = '0px';
            dv2CPlyAdCntr.style.left = '0px';
            dv2CPlyAdCntr.style.width = '890px'; // document.body.scrollWidth;
            dv2CPlyAdCntr.style.height = '740px'; // '650px'; // document.body.scrollHeight;
            dv2CPlyAdCntr.style.zIndex = '99993';
            dv2CPlyAdCntr.style.display = 'none';
            //dv2CPlyAdCntr.style.border = '1px solid red';
            
            document.body.insertBefore(dv2CPlyAdCntr, document.body.firstChild);  ///new 

            /*
            var dv2CPlyCntrAdd = document.createElement('div');
            dv2CPlyCntrAdd.id = 'dv2CPlyCntrAdd';
            //dv2CPlyCntrAdd.style.background = 'url("' + tcplayAd_url + '/Content/Images/W11/bg.png")';
            dv2CPlyCntrAdd.style.position = 'absolute';
            dv2CPlyCntrAdd.style.top = '625px';
            dv2CPlyCntrAdd.style.left = '0px';
            dv2CPlyCntrAdd.style.width = '890px'; // document.body.scrollWidth;
            dv2CPlyCntrAdd.style.height = '105px'; // '650px'; // document.body.scrollHeight;
            dv2CPlyCntrAdd.style.zIndex = '99993';
            //dv2CPlyCntrAdd.style.border = '1px solid red';
            dv2CPlyCntrAdd.style.display = 'none';
            document.body.insertBefore(dv2CPlyCntrAdd, document.body.firstChild);
            document.body.insertBefore(dv2CPlyAdCntr, dv2CPlyCntrAdd);
*/
            var htmlString = '';

            htmlString += '<iframe id="fr2CPlyAd" src="' + tcplayAd_url + '/blank.htm" style="display:none;z-index:99997;position:absolute;left:85px;top:78px;width:720px;height:595px;border:0px;" frameborder="0" allowtransparency="true"></iframe>';

            htmlString += '<img id="img2CPlyCls" src="' + tcplayAd_url + '/Content/Images/W11/btn-close.png" style="z-index:99999;position:absolute;left:800px;top:60px;border:0px;cursor:pointer;" onclick="tcplayb11.hideGame();" />';

            dv2CPlyAdCntr.innerHTML = htmlString;
            
            //dv2CPlyCntrAdd.innerHTML = '<iframe scrolling="no" id="fr2CPlyAdd" src="' + tcplayAd_url + '/blank.htm" style="display:none;z-index:99997;position:absolute;left:81px;top:8px;width:728px;height:90px;border:0px solid black;" frameborder="0"></iframe>';

            var tcautoplay = getQuerystring('tcautoplay');
            if (tcautoplay != null && tcautoplay != "") {

                tcplayb11.showGame(ptyid, pid, pdata);
            }
        }
    }
    ,
    showGame: function (ptyid, pid, pdata) {
        if (document.getElementById('dv2CPlyAdCntr').style.display != 'block') {
            var sTop = document.documentElement.scrollTop || document.body.scrollTop;
            var sLeft = document.documentElement.scrollLeft || document.body.scrollLeft;

            // Test Code
            var svr = '';
            if (document.getElementById('tx2CSvr')) {
                svr = encodeURIComponent(document.getElementById('tx2CSvr').value);
            }
            // Test Code


            if (ptyid == '0') {
                document.getElementById('fr2CPlyAd').contentWindow.location.replace(tcplayAd_url + '/Home/wgtPlay?eiid=' + pid + '&customdata=' + pdata);
                //document.getElementById('fr2CPlyAdd').contentWindow.location.replace(tcplayAd_url + '/Home/wgtPlay?eiid=' + pid + '&customdata=' + pdata);
            }
            else if (ptyid == '1') {
                var referrerURL = encodeURIComponent(window.location.href);
                var referrerTitle = encodeURIComponent(document.title);
                document.getElementById('fr2CPlyAd').contentWindow.location.replace(tcplayAd_url + '/Home/wgtplayadb11?idtype=' + ptyid + '&id=' + pid + '&customdata=' + pdata + '&svr=' + svr + '&rt=' + referrerTitle + '&ru=' + referrerURL);
                //document.getElementById('fr2CPlyAdd').contentWindow.location.replace(tcplayAd_url + '/Home/wgtPlayAddb11?idtype=' + ptyid + '&id=' + pid + '&customdata=' + pdata + '&svr=' + svr);
            }
            document.getElementById('dv2CPlyAdCntr').style.left = sLeft + ((document.body.clientWidth / 2) - 445) + 'px';
            //document.getElementById('dv2CPlyCntrAdd').style.left = sLeft + ((document.body.clientWidth / 2) - 445) + 'px';
            document.getElementById('dv2CPlyAdCntr').style.top = (sTop + 50) + 'px'; // sTop + ((document.body.clientHeight / 2) - 325) + 'px';
            //document.getElementById('dv2CPlyCntrAdd').style.top = (sTop + 625) + 'px'; // sTop + ((document.body.clientHeight / 2) - 325) + 'px';

            document.getElementById('dv2CPlyAdCntr').style.display = 'block';
            //document.getElementById('dv2CPlyCntrAdd').style.display = 'block';
            document.getElementById('fr2CPlyAd').style.display = 'block';
            //document.getElementById('fr2CPlyAdd').style.display = 'block';
        }
    }
    ,
    hideGame: function () {
        document.getElementById('dv2CPlyAdCntr').style.display = 'none';
        //document.getElementById('dv2CPlyCntrAdd').style.display = 'none';
        document.getElementById('fr2CPlyAd').style.display = 'none';
        //document.getElementById('fr2CPlyAdd').style.display = 'none';
        document.getElementById('fr2CPlyAd').contentWindow.location.replace(tcplayAd_url + '/blank.htm');
        //document.getElementById('fr2CPlyAdd').contentWindow.location.replace(tcplayAd_url + '/blank.htm');
    }
}

function getQuerystring(key) {
    var default_;
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}
