var _siteNotifications = new Array();
var _isNotificationHidden = true;

$(document).ready(function () {
    $('.faq .q').click(function () {
        $(this).next().slideToggle();
    });

    $('.cycle').cycle({
        fx: 'fade',
        speed: 4000,
        pause: 1
    });

    $('.news-cycle').cycle({
        fx: 'turnDown',
        timeout: 10000,
        pause: 1
    });

    $('.list_jackpots .items').cycle({
        sync: false,
        timeout: 4000
    });

    $('.splashtext').cycle({
        fx: 'scrollLeft',
        sync: false,
        timeout: 7000
    });

    $('.list_winners .items').cycle({
        fx: 'scrollUp',
        speedIn: 1000,
        speedOut: 1000,
        delay: -1000
    });

/*        fx: 'scrollUp',
        sync: false,
        timeout: 3000*/

    $.fn.hoverscroll.params = $.extend($.fn.hoverscroll.params, {
        vertical: false,
        width: 900,
        arrows: false
    });

    // Preload large hidden images
    var registerBG = new Image();
    registerBG.src = "/static/gfx/site_slider_bg.png";

    $('.open_register_popup').click(function () {
        toggleRegisterSlide();
    });

    $('.tabs').tabs();

    initHelpTexts();

    if ($(".module_register :input:visible:enabled:first").length > 0) {
        $(".module_register :input:visible:enabled:first").focus();
    }
    else if ($(".module_login_login input").length > 0) {
        $(".module_login_login input")[0].focus();
    }
});

function initHelpTexts() {
    var objArr = $('.field :input');
    for (var i = 0; i < objArr.length; i++) {
		if ($(objArr[i]).parent().hasClass('checkbox')) {
        	$(objArr[i]).focus(function() { $(this).parent().next('.desc').fadeIn('fast') }).blur(function() { $(this).parent().next('.desc').fadeOut('fast') });
		}
        $(objArr[i]).focus(function() { $(this).parent().next('.desc').fadeIn('fast') }).blur(function() { $(this).parent().next('.desc').fadeOut('fast') });
    }
}

function toggleTab(containerId, targetId, tabObj) {
    $('#' + containerId + ' td').removeClass('selected');
    $(tabObj).addClass('selected');
    $('#' + containerId + ' .tab_content').removeClass('active');
    $('#' + targetId).addClass('active');
}

function getElementsByClassName(classname, node) {
    if (!node)
        node = document.getElementsByTagName("body")[0];

    var elements = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var allElements = node.getElementsByTagName("*");

    for (var i = 0, j = allElements.length; i < j; i++) {
        if (re.test(allElements[i].className))
            elements.push(allElements[i]);
    }

    return elements;
}

function getURLParameter(param)
{
  var param = param.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]" + param + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if (results == null)
    return "";
  else
    return results[1];
}

function showMsgHelp(obj) {
    obj.style.display = 'block';
}

function hideMsgHelp(obj) {
    obj.style.display = 'none';
}

function checkForNewWinners() {
    $.getJSON("/livewinners.php?show=true", function (data) {
        var _balancePrefix = data.currency;

        if (_balancePrefix == "EUR") {
            _balancePrefix = "€";
        }
        else if (_balancePrefix == "USD") {
            _balancePrefix = "$";
        }
		 else if (_balancePrefix == "GBP") {
            _balancePrefix = "£";
        }

        showSiteNotification(data.name + " just won " + _balancePrefix + " " + data.win + " playing " + data.game, "money", 5000);
    });
}

function showSiteNotification(text, customClass, timeToShow) {
    if (text != null) {
        _siteNotifications.push(text);
    }
    else {
        if (_siteNotifications.length > 0) {
            _siteNotifications.shift();
        }
    }

    if (_isNotificationHidden) {
        if (_siteNotifications.length > 0) {
            $('#notification-panel .text').html(_siteNotifications[0]);

            if (customClass == "money") {
                $('#notification-panel .text').append("<div class=\"icon\"></div>");
            }

            $('#notification-panel').show().attr("class", customClass).animate({
                top: '65px',
                opacity: 1
            }, 1000, function () {
                setTimeout("hideSiteNotification()", timeToShow);
            });
        }
    }
}

function hideSiteNotification() {
    $('#notification-panel').animate({
        top: "30px",
        opacity: 0
    }, 1000, function () {
        $('#notification-panel').hide();
        _isNotificationHidden = true;
        showSiteNotification(null);
    });
}

function isIE() {
    if (navigator.appName.toUpperCase() == 'MICROSOFT INTERNET EXPLORER') {
        return true;
    }
    else {
        return false;
    }
}

function isNumberKey(evt) {
    var e = evt;
    var charCode;

    if (window.event) { // IE
        charCode = e.keyCode;
    } else if (e.which) { // Safari, Firefox
        charCode = e.which;
    }
    if (charCode > 57 && (charCode < 93 || charCode > 105) && (charCode < 112 || charCode > 146)) {
        return false;
    }
    return true;
}

function isLetterKey(evt) {
    var e = evt;
    var charCode;

    if (window.event) { // IE
        charCode = e.keyCode;
    } else if (e.which) { // Safari, Firefox
        charCode = e.which;
    }
    if ((charCode > 47 && charCode < 58) || (charCode > 95 && charCode < 106)) {
        return false;
    }
    return true;
}

// FIX SO THAT THIS WORKS GREAT IN IE
function printElement(title, content) {
    var _printFrame = document.createElement("IFRAME");

    _printFrame.id = "printingframe" + (Math.random() * 1000);
    _printFrame.style.width = "0px";
    _printFrame.style.height = "0px";
    _printFrame.style.overflow = "hidden";

    document.body.appendChild(_printFrame);

    var _printDoc = _printFrame.contentDocument;

    if (_printDoc == undefined || _printDoc == null) {
        _printDoc = _printFrame.contentWindow.document;
    }

    _printDoc.open();
    _printDoc.write('<html><head><title>' + title + '</title></head><body>' + content + '</body></html>');
    _printDoc.close();

    /*var iframe = document.frames ? document.frames[_printFrame.id] : document.getElementById(_printFrame.id);

    if (isIE()) {
        var iframe = iframe.contentWindow || iframe;
    }*/

    _printFrame.focus();
    _printFrame.print();
}

function roundNumber(value, decimals) {
    return (Math.round((parseFloat(value)) * Math.pow(10, decimals)) / Math.pow(10, decimals)).toFixed(decimals);
}

function setCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

function checkAmountLimits(input, min, max) {
	var value = input.value;
	var ok = value >= min && value <= max;
	
	var parent = $(input).parent().parent();
	
	parent.removeClass(!ok ? 'valid' : 'invalid');
	parent.addClass(ok ? 'valid' : 'invalid');
}

function ccCardTypeOnChange(obj) {
	$('.debit_card_info').css('display', (obj.value == 'mastercard' || obj.value == 'visa') ? 'none' : 'block');
}
