var SF_POPUP_TIMER;
var SF_POPUP_TIMER_DELAY = 1000; // delay in ms before hiding

var sourceUrl = document.location.href;
var pageTitle = document.title;
var sf_popup_vertical_position = 'top';
var SF_POPUP = {
    init: function () {
        SF_POPUP.inject();
        SF_POPUP.browsers();
        SF_POPUP.button(); // flake-it button
        SF_POPUP.autoPosition();
    },
    inject: function () {
        sf_iframe_baseurl =
        'http://shareflake.com/Content/Html/popup.iframe.preview.html?flakeKey=' + flakeKey +
        '&flakeCondId=' + flakeCondId +
        '&sourceUrl=' + sourceUrl +
        '&organizationId=' + organizationId +
        '&title=' + pageTitle +
        '&amount=' + amount +
        '&currency=' + currency;

        $("body").append('<iframe id="sf-iframe" allowtransparency="true" src="' + sf_iframe_baseurl + '&sf_popup_vertical_position=top' + '" frameborder="0" marginheight="0" marginwidth="0" scrolling="no"></iframe>');
    },
    autoPosition: function () {
        $ifr = $("#sf-iframe");
        $ifr_width = parseInt($ifr.css("width"));
        $ifr_height = parseInt($ifr.innerHeight());
        $btn_width = parseInt($("#flake-it-button").css("width"));
        $btn_height = parseInt($("#flake-it-button").css("height"));
        $btn_offset = $("#flake-it-button").offset();

        $pos_x = "left"; // defaults to left
        $pos_y = "top"; // defaults to bottom
        if (($btn_offset.top + $ifr_height) > SF_POPUP.getHeight()) $pos_y = "top"; // bottom?
        ($pos_y == "top") ? $ifr.css("top", ($btn_offset.top - $ifr_height)) : $ifr.css("top", ($btn_offset.top + $btn_height));
        if (($btn_offset.left + $ifr_width) > SF_POPUP.getWidth()) $pos_x = "right"; // right?
        ($pos_x == "left") ? $ifr.css("left", $btn_offset.left) : $ifr.css("left", ($btn_offset.left + $btn_width - $ifr_width));

        $('#sf-iframe').attr('src', $('#sf-iframe').attr('src'));

    },
    button: function () {
        $("#flake-it-button").hover(function (e) {
            SF_POPUP.autoPosition();
            $("#sf-iframe").show();
           
            if (SF_POPUP_TIMER) { clearTimeout(SF_POPUP_TIMER); }
        }, function () {
            SF_POPUP_TIMER = setTimeout("SF_POPUP.buttonHide()", SF_POPUP_TIMER_DELAY);
        });

        $("#sf-iframe").hover(function (e) {
            if (SF_POPUP_TIMER) { clearTimeout(SF_POPUP_TIMER); }
        }, function () {
            SF_POPUP_TIMER = setTimeout("SF_POPUP.buttonHide()", SF_POPUP_TIMER_DELAY);
        });
    },
    buttonHide: function () {
        $("#sf-iframe").hide();
    },
    browsers: function () {
        switch ($.browser.className) {
            case "chrome4":
                $('head').append('<link rel="stylesheet" href="http://shareflake.com/Content/Css/sf.popup.chrome.css" type="text/css" />');
                break;
            case "msie6":
                $('head').append('<link rel="stylesheet" href="http://shareflake.com/Content/Css/sf.popup.ie6.css" type="text/css" />');
                break;
            case "msie7":
                $('head').append('<link rel="stylesheet" href="http://shareflake.com/Content/Css/sf.popup.ie7.css" type="text/css" />');
                break;
            case "msie8":
                $('head').append('<link rel="stylesheet" href="http://shareflake.com/Content/Css/sf.popup.ie8.css" type="text/css" />');
                break;
            default:
                break;
        }

    },
    getHeight: function () {
        var y = 0;
        if (self.innerHeight) {
            y = self.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) {
            y = document.documentElement.clientHeight;
        } else if (document.body) {
            y = document.body.clientHeight;
        }
        return y;
    },
    getWidth: function () {
        var x = 0;
        if (self.innerWidth) {
            x = self.innerWidth;
        } else if (document.documentElement && document.documentElement.clientWidth) {
            x = document.documentElement.clientWidth;
        } else if (document.body) {
            x = document.body.clientWidth;
        }
        return x;
    }
}


function updateFlakeAndTabInfo(provType) {

    var conversionTextBox = document.getElementById("register-box-3-provision-input");
    var conversionValue = 0;

    if (conversionTextBox.value != '') {
        conversionValue = parseInt(conversionTextBox.value);
    }

    if (conversionValue.toString().length != conversionTextBox.value.length && conversionTextBox.value != 0) {
        conversionValue = 'NaN';
    }

    conversionValue = Math.round(conversionValue * 0.8 * 100) / 100;

    if (conversionValue == "") {
        conversionValue = '0';
    }

    //var charityId = $('#txtOrganizations :selected').val();
    
    
    if (provType != null)
    {
        this.previousProvType = provType;
    }
    else
    {
        provType = this.previousProvType;
    }


    $('#sf-tab-organization-provision').html(conversionValue + ' kr');
    $("#sf-tab-organization-link img").attr("src", "http://shareflake.com/Content/Images/Charity/charity-" + charityId + ".png");
    $("#sf-tab-organization-header").html(jQuery("#sf-tab-lblcharity-" + charityId).attr("title"));
    $("#sf-tab-organization-text").html(jQuery("#sf-tab-lblcharity-" + charityId).attr("title"));
    var shareDescription = getShareDescription(provType, conversionValue + ' ' + currency, 'WaterAid');
    $("#sf-tab-text-1").html(shareDescription);
    

    var newSrc = 'http://shareflake.com/Content/Html/popup.iframe.preview.html?flakeKey=' + flakeKey +
        '&flakeCondId=' + flakeCondId +
        '&sourceUrl=' + sourceUrl +
        '&organizationId=' + charityId +
        '&title=' + pageTitle +
        '&amount=' + conversionValue +
        '&currency=' + currency +
        '&provType=' + provType + 
        '&sf_popup_vertical_position=top';

        $('#sf-iframe').attr('src', newSrc);
    


}
