﻿var appPath;

function resolveUrl(url) {
    return url.replace("~/", appPath);
}

/**
 * @author Alexandre Magno
 * @desc Center a element with jQuery
 * @version 1.0
 * @example
 * $("element").center({
 *
 * 		vertical: true,
 *      horizontal: true
 *
 * });
 * @obs With no arguments, the default is above
 * @license free
 * @param bool vertical, bool horizontal
 * @contribution Paulo Radichi
 *
 */
jQuery.fn.center = function(params) {

		var options = {

			vertical: true,
			horizontal: true

		}
		op = jQuery.extend(options, params);

   return this.each(function(){

		//initializing variables
		var $self = jQuery(this);
		//get the dimensions using dimensions plugin
		var width = $self.width();
		var height = $self.height();
		//get the paddings
		var paddingTop = parseInt($self.css("padding-top"));
		var paddingBottom = parseInt($self.css("padding-bottom"));

		//get the borders
		var borderTop = parseBorderWidth($self.css("border-top-width")); // parseInt();
		var borderBottom = parseBorderWidth($self.css("border-bottom-width")); // parseInt();

		//get the media of padding and borders
		var mediaBorder = (borderTop+borderBottom)/2;
		var mediaPadding = (paddingTop+paddingBottom)/2;

		//get the type of positioning
		var positionType = $self.parent().css("position");
		// get the half minus of width and height
		var halfWidth = (width/2)*(-1);
		var halfHeight = ((height/2)*(-1))-mediaPadding-mediaBorder;
		// initializing the css properties
		var cssProp = {
			position: 'absolute'
		};

		if(op.vertical) {
			cssProp.height = height;
			cssProp.top = '50%';			
			cssProp.marginTop = halfHeight;
		}
		if(op.horizontal) {
			cssProp.width = width;
			cssProp.left = '50%';
			cssProp.marginLeft = halfWidth;
		}
		//check the current position
		if(positionType == 'static') {
			$self.parent().css("position","relative");
		}
		//aplying the css
		$self.css(cssProp);

   });
};

function parseBorderWidth(stringValue)
{
    var w = stringValue;
    if ( w instanceof Number )
    {
        return parseFloat(w);
    }
    // just guessing here
    switch(w)
    {
        case "thin":
            return 1;
        case "medium":
            return 2;
        case "thick":
            return 3;
    }
    return 0;
}

//jQuery.fn.center = function () {
//    this.css("position","fixed");
////    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
////    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
//    this.css("top", ( $(window).height() - this.height() ) / 2 + "px");
//    this.css("left", ( $(window).width() - this.width() ) / 2 + "px");
//    return this;
//}

$(function() {

});

function openBingoGame(url) {
    window.open(resolveUrl(url), '_blank', 'top=100, left=100, height=500, width=700, status=no, menubar=no, resizable=no, scrollbars=no, toolbar=no, location=no, directories=no');
}

function openRecoverPassword(url) {
    window.open(resolveUrl(url), '_blank', 'top=100, left=100, height=230, width=300, status=no, menubar=no, resizable=no, scrollbars=no, toolbar=no, location=no, directories=no');
}

function showErrorMessage(message) {
    alert(message);
}

$.fn.showProgressAnim = function()
{
    $("#loadingDiv").center();
    this.block( { message : $("#loadingDiv") } );
}

$.fn.hideProgressAnim = function()
{
    this.unblock();
}

function showProgressAnim()
{
    $("#loadingDiv").center();
    $.blockUI( { message : $("#loadingDiv") } );
}

function showProgressAnim()
{
    $("#loadingDiv").center();
    $.blockUI( { message : $("#loadingDiv") } );
}

function hideProgressAnim()
{
    $.unblockUI();
}


//function captureSubmit()
//{
//    $('input', this).attr('disabled', 'disabled');
//    $('select', this).attr('disabled', 'disabled');
//    $('textarea', this).attr('disabled', 'disabled');     
//    $.blockUI( { message : $("#loadingDiv") } );
//    setTimeout(function() {
//        $('input', this).attr('disabled', '');
//        $('select', this).attr('disabled', '');
//        $('textarea', this).attr('disabled', '');     
//        $.unblockUI();
//    }, 1000);
//    return true;               
//}

function openBonusWindow(url)
{
// 'BonusInfo.aspx?ApplicationBonusID=<%# Eval("ApplicationBonusID") %>'
    window.open(url, '_blank','top=100, left=100, height=250, width=700, status=no, menubar=no, resizable=no, scrollbars=yes, toolbar=no, location=no, directories=no');

}

function bindWithdrawalPopupButton(limit)
{
    $("#wizardDiv input[type=submit].nav-button").bind("click", function()
    {
        var amount = $("#wizardDiv input[type=text].amount-input").val();
        
        try
        {
            amount = parseFloat(amount);
//            alert("amount: " + amount + " limit: " + limit);            
        }
        catch(e)
        {
            return true;
        }
        
        if ( amount >= limit )
        {        
            bindWithdrawalPopupCloseButton();
            $(".popupDiv").center();                        
            $.blockUI( {
                    message: $(".popupDiv")
                }
            );  
            return false;
        }
        else
        {
            return true;
        }
    });
}

function bindWithdrawalPopupCloseButton()
{
    $(".popupDiv input[type=submit].close-notice-button").bind("click", function()
    {
        $(".popupDiv input.notice-read-button").unbind("click");                                
        $(".popupDiv input.close-notice-button").unbind("click");                                
        $("#wizardDiv input.nav-button").unbind("click");

        $.unblockUI( { onUnblock: function() {
            $("#wizardDiv input[type=submit].nav-button").click();
        } } );
                
    });

    $(".popupDiv a.notice-read-button").bind("click", function() 
    {
        $(".popupDiv input.notice-read-button").unbind("click");                                
        $(".popupDiv input.close-notice-button").unbind("click");                                
        $("#wizardDiv input.nav-button").unbind("click");
         
        $.ajax({
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            url: resolveUrl("~/Services/UserService.asmx/ReadWithdrawalMessage"),
            success: function(o) {
//                bindWithdrawalPopupButton();            
                $.unblockUI( { onUnblock: function() {
                    $("#wizardDiv input[type=submit].nav-button").click();                                
                } } );
            },
            error: function(r, s) {
                bindWithdrawalPopupButton();
                $.unblockUI( { onUnblock: function() {
                    alert("An error has occured. Please try again");                                
                } } );
            }
        });                                    
    });
}

function checkRakeUploadStatus()
{
    $.ajax({
        type: 'POST',
        url: resolveUrl("~/Public/Services/RakeService.asmx"),
        success: function(o) {
            var x = eval(o);
            alert(x);            
        },
        error: function(r, s) {
            alert("An error occured");
        }
    });
}

/*
 * Will find specified form elements beneath the selected object (normally a form) and post their values to the url specified in the options map.
 * 
 */
$.fn.postFormValues = function(options, formElementNames) {
    if (options.data == null) {
        options.data = {};
    }
    for (var i = 0; i < formElementNames.length; i++) {
        options.data[formElementNames[i]] = this.find("[name='" + formElementNames[i] + "']").val();
    }

    $.ajax(options);
}

$.fn.postFormValuesWithAliases = function(options, formElementNamesAndAliases) {
    if (options.data == null) {
        options.data = {};
    }

    for (var i = 0; i < formElementNamesAndAliases.length; i++) {
        var nameAndAlias = formElementNamesAndAliases[i];
        var elements = this.find("[name='" + nameAndAlias.name + "']");
        elements.each(function() {
            var value = $.fieldValue(this, true);
            if (value != null) {
                options.data[nameAndAlias.alias] = value;
//                alert(value);
            }
            // .val();

        });

    }

    $.ajax(options);
}

$.fn.validateRequiredInput = function(validCallback, invalidCallback)
{
    var value = this.val(); // find("[name='" + formElementName + "']").val();
    if ( value == null || value.length == 0 )
    {
        invalidCallback();
        return false;
    }
    validCallback();
    return true;
}

//$.fn.formValidator = function() {
//{
//    validate : function() {
//        var success = true;
//        for (var i = 0; i < validators.length; i++) {
//            var validator = validators[i];
//            success = validators.validate() && success;
//        }
//        return success;
//    }
//}

$.fn.initAjaxForm = function(destinationSelector)
{
    var mc = $(destinationSelector);
   
    var options = {
        target: mc
    };

    $(this).ajaxForm(options);
}

//$.fn.postFormValues = function(options, formElementNames) // formSelector, formActionUrl, names, onSuccess, onError)
//{
//    var data = { };
//    for(var i = 0; i < formElementNames.length; i++)
//    {
//        data[formElementNames[i]] = this.find("[name='" + formElementNames[i] + "']").val();
//    }
//    
//    options.data = data;
//    
//    $.ajax(options);        
//}

function startUpdatingRemoteFrameHeight(baseUrl, frameID)
{
    var currentHeight = "";
    $(this).everyTime(200, function() {
        var newHeight = $("body").outerHeight();
        if ( currentHeight != newHeight )
        {
            try
            {
                // "http://www.noiq.com:8080/CallbackFrame.html
                $("#callbackFrame").attr("src", baseUrl + "?h=" + newHeight + "&f=" + frameID);                
            }
            catch(e)
            {
            }
            currentHeight = newHeight;
        }
     });
}

