﻿function GetXmlHttpObject() {
    var xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

function GetCouponCode(path, CouponID, e) {
    var screenW = screen.width - 20;
    var screenH = screen.height - 120;
    var mouseX = getPosition(e).x + 155;
    var features = "height=" + screenH + ",width=" + (screenW - mouseX) + ",left=" + (mouseX) + ",top=0,resizable=yes,scrollbars=yes";
    var OutForSite = window.open(path + 'Out.ashx?c=1&coupon=' + CouponID, 'OutForSite_' + CouponID, features);
    var elementCode = document.getElementById("Coupon_" + CouponID);      
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert("Your browser does not support AJAX!");
    }
    var url = path + "GetCode.ashx";
    url = url + "?id=" + CouponID;
    url = url + "&ssid=" + Math.random();

    xmlHttp.onreadystatechange = function() {
        elementCode.innerHTML = "<div><img src='"+ path + "Images/loader.gif' alt='' style='margin: 10px 140px;' /></div>";
        if (xmlHttp.readyState == 4) {
            if (xmlHttp.status == 200) {
                elementCode.innerHTML = "<div class='CouponText'>" + xmlHttp.responseText + "</div>";
            }
            else {
                elementCode.innerHTML = "Error: returned status code " + xmlHttp.status + " " + xmlHttp.statusText;
            }
        }
    };
    
    xmlHttp.open("GET", url, true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.send(null);
}

function GetCouponLongDesc(path, CouponID, e) {
    var elementCode = document.getElementById("CouponDesc_" + CouponID);
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert("Your browser does not support AJAX!");
    }
    var url = path + "GetLongDesc.ashx";
    url = url + "?id=" + CouponID;
    url = url + "&ssid=" + Math.random();

    xmlHttp.onreadystatechange = function() {
        elementCode.innerHTML = "<div><img src='" + path + "Images/loader.gif' alt='' style='margin: 10px 140px;' /></div>";
        if (xmlHttp.readyState == 4) {
            if (xmlHttp.status == 200) {
                elementCode.innerHTML = "<p>" + xmlHttp.responseText + "</p>";
            }
            else {
                elementCode.innerHTML = "Error: returned status code " + xmlHttp.status + " " + xmlHttp.statusText;
            }
        }
    };

    xmlHttp.open("GET", url, true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.send(null);
}

function GetCouponList(thisid, path, CouponType, Pager, id, e) {
    var page = Pager + 1;
    var button = '<button type="button" id="' + thisid + '" onclick="GetCouponList(this.id,\'' + path + '\',\'' + CouponType + '\',' + page + ',\'' + id + '\',event)" class="BtnCouponMore">' + $('#' + thisid).text() + '</button>';

    var elementCode = $('#' + thisid).parent().get(0).id;
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert("Your browser does not support AJAX!");
    }
    var url = path + "GetCoupons.ashx";
    url = url + "?ct=" + CouponType;
    url = url + "&p=" + Pager;
    url = url + "&id=" + id;
    url = url + "&ssid=" + Math.random();

    xmlHttp.onreadystatechange = function() {
    $('#' + elementCode).html("<div style='margin-left:295px; margin-bottom:10px;'><img src='" + path + "Images/ajax_loader.gif' alt='' /></div>");
        if (xmlHttp.readyState == 4) {
            if (xmlHttp.status == 200) {
                switch (CouponType) {
                    case "CouponListNew":
                        $('#ctl00_CPH_MainPanel_pnlNewCoupons').append(xmlHttp.responseText);
                        break;
                    case "CouponListEnding":
                        $('#ctl00_CPH_MainPanel_pnlEndingCoupons').append(xmlHttp.responseText);
                        break;
                    case "CouponListTag":
                        $('#ctl00_CPH_MainPanel_pnlCurrentTopCoupons').append(xmlHttp.responseText);
                        break;
                    case "CouponListOffers":
                        $('#ctl00_CPH_MainPanel_pnlCurrentTopOffers').append(xmlHttp.responseText);
                        break;
                    case "CouponListStoreEnding":
                        $('#ctl00_CPH_MainPanel_pnlEndingCoupons').append(xmlHttp.responseText);
                        break;
                    case "CouponListStoreActive":
                        $('#ctl00_CPH_MainPanel_pnlActiveCoupons').append(xmlHttp.responseText);
                        break;
                    case "CouponListStoreUnreliable":
                        $('#ctl00_CPH_MainPanel_pnlUnreliableCoupons').append(xmlHttp.responseText);
                        break;
                }
                $('#' + elementCode).html("");
                if (xmlHttp.responseText != "") {
                    $('#' + elementCode).html(button);
                    init(path);
                }
            }
            else {
                $('#' + elementCode).html("Error: returned status code " + xmlHttp.status + " " + xmlHttp.statusText);
            }
        }
    };

    xmlHttp.open("GET", url, true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.send(null);
}

function getPosition(e) {
    e = e || window.event;
    var cursor = { x: 0, y: 0 };
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    }
    else {
        cursor.x = e.clientX +
            (document.documentElement.scrollLeft ||
            document.body.scrollLeft) -
            document.documentElement.clientLeft;
        cursor.y = e.clientY +
            (document.documentElement.scrollTop ||
            document.body.scrollTop) -
            document.documentElement.clientTop;
    }
    return cursor;
}

function SetCookie(path, CouponID, Vote, isOutFrame) {
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert("Your browser does not support AJAX!");
    }
    var url = path + "SetCookie.ashx";
    url = url + "?id=" + CouponID;
    url = url + "&vote=" + Vote;
    url = url + "&ssid=" + Math.random();

    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            if (xmlHttp.status == 200) {
                if (isOutFrame == 'False') {
                    document.getElementById("YesVote_" + CouponID).style.visibility = 'hidden';
                    document.getElementById("NoVote_" + CouponID).style.visibility = 'hidden';
                    document.getElementById("VoteDesc_" + CouponID).style.visibility = 'hidden';
                } else {
                    document.getElementById("voting").style.visibility = 'hidden';
                }
                if (Vote != "No") { ShowEditModal(path,CouponID); }
            }
        }
    };
    
    xmlHttp.open("GET", url, true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.send(null);
}

function ShowEditModal(path, ExpanseID) {
    var frame = $get('IframeEdit');
    frame.src = path + "EditExpanse.aspx?id=" + ExpanseID;
    $find('EditModalPopup').show();
}

function On_Cancel() {
     var frame = $get('IframeEdit');
    frame.src = "";
}

function ShowContactModal(path) {
    var framecontact = $get('IframeContact');
    framecontact.src = path + "ContactExpanse.aspx";
    $find('EditModalContactPopup').show();
}

function On_CancelContact() {
    var framecontact = $get('IframeContact');
    framecontact.src = "";
}

function init(path) {
    var clip = null;
    var pid = '';
    var iid = '';
    var cid = '';
    var cpnid = '';
    var txt = '';

    // setup single ZeroClipboard object for all our elements
    clip = new ZeroClipboard.Client();
    clip.setHandCursor(true);

    // assign a common mouseover function for all elements using jQuery
    $('div.CCodePanel').mouseover(function() {
        // set the clip text to our innerHTML
        //clip.setText(this.innerHTML);
        pid = this.id;
        iid = pid.replace('p', 'i');
        cid = pid.replace('p', 'c');

        //code = $(this).children('.CCode');
        //txt = code.html();
        txt = $(this).text();
        clip.setText(txt);

        // reposition the movie over our element
        // or create it if this is the first time
        if (clip.div) {
            clip.receiveEvent('mouseout', null);
            clip.reposition(this);
        }
        else clip.glue(this);

        // gotta force these events due to the Flash movie
        // moving all around.  This insures the CSS effects
        // are properly updated.
        clip.receiveEvent('mouseover', null);
    });

    clip.addEventListener('complete', function(client, text) {
        cpnid = pid.replace('p', '');
        //alert(pid + " - " + cid + " - " + " - " + iid + " - " + txt + " - " + cpnid);
        //go out
        var OutForSite = window.open(path + 'Out.ashx?c=1&coupon=' + cpnid, 'OutForSite_' + cpnid);
    });

    clip.addEventListener('mouseout', function(client, text) {
        $('#' + iid).css('visibility', 'hidden');
        $('#' + cid).css('background-color', '#FDEDB4');
    });

    clip.addEventListener('mouseover', function(client, text) {
        $('div.CCodeInfo').css('visibility', 'hidden');
        $('#' + iid).css('visibility', 'visible');
        $('#' + cid).css('background-color', '#FCE48E');
    });
    
    fbinit(path);

    $('.CouponShareImg').hover(function() {
        $(this).children('div').slideToggle('slow');
    });
}

function fbinit(path) {
    $('.share_button').click(function(e) {
        e.preventDefault();
        switch (this.id.substr(0, 2)) {
            case 'tw':
                twcid = "id=" + this.id.replace('tw_', '');
                twsid = "ssid=" + Math.random();
                params = twcid + "&" + twsid;

                var features = "height=400,width=600,left=0,top=0,resizable=yes,scrollbars=yes";
                var wTweetThis = window.open('', 'wTweetThis_' + twcid, features);

                $.getJSON(path + "gettwshareinfo.ashx?" + params, function(data) {
                    $.getJSON("http://api.bitly.com/v3/shorten?login=darkice74&apiKey=R_84e76c16bc7a3f58c4d3e1bd4d347827&longUrl=" + encodeURIComponent(data.link) + "&format=json", function(bitly) {
                        var strTweet = data.tweet;
                        if (strTweet.length > (140 - (bitly.data.url.length + 1))) {
                            strTweet = strTweet.substr(0, (140 - (bitly.data.url.length + 4))) + "..."
                        }
                        wTweetThis.window.location.href = "http://twitter.com/home?status=" + encodeURIComponent(strTweet + " " + bitly.data.url);
                        //var features = "height=400,width=600,left=0,top=0,resizable=yes,scrollbars=yes";
                        //var wTweetThis = window.open('http://twitter.com/home?status=' + encodeURIComponent(strTweet + " " + bitly.data.url), 'wTweetThis_' + twcid, features);
                    });
                });
                break;
            default:
                fbcid = "id=" + this.id.replace('fb_', '');
                fbsid = "ssid=" + Math.random();
                params = fbcid + "&" + fbsid;

                //var features = "height=600,width=800,left=0,top=0,resizable=yes,scrollbars=yes";
                //var wShareThis = window.open('', 'wShareThis' + fbcid, features);
                var windowURL = window.location.href;

                $.getJSON(path + "getfbshareinfo.ashx?" + params, function(data) {
                    window.location.href = "http://www.facebook.com/dialog/feed?" +
                                                      "app_id=171114509575567&" +
                                                      "link=" + data.link + "&" +
                                                      "picture=" + data.picture + "&" +
                                                      "name=" + encodeURIComponent(data.name) + "&" +
                                                      "caption=" + encodeURIComponent(data.caption) + "&" +
                                                      "description=" + encodeURIComponent(data.description) + "&" +
                                                      "message=" + encodeURIComponent(data.message) + "&" +
                                                      "redirect_uri=" + windowURL;
                    /*FB.ui(
                    {
                    method: 'feed',
                    display: 'iframe',
                    name: data.name,
                    link: data.link,
                    picture: data.picture,
                    caption: data.caption,
                    description: data.description,
                    message: data.message
                    });*/
                });
        }
    });
}
