var utils = {
    baseHref : '',
    ieVersion : -1,
    ios : false,
    init : function() {
        this.getIEVersion();
        this.getIOS();
        this.getBaseHref();
        /* $('a.popupLink').bind('click', function(e) {
            utils.popupWindow($(this).attr('href'), $(this).attr('title').replace(/[^A-z]/ig, ''), 'width=600, height=800, scrollbars');
            e.preventDefault();
        }); */
    },
    getIEVersion : function () {
        var rv = -1;
        if (navigator.appName === 'Microsoft Internet Explorer') {
            var ua = navigator.userAgent;
            var re = new RegExp("MSIE ([0-9]{1,}[\\.0-9]{0,})");
            if (re.exec(ua) !== null) {
                rv = parseFloat(RegExp.$1);
            }
        }
        this.ieVersion = rv;
    },
    getIOS : function() {
        var agent=navigator.userAgent.toLowerCase();
        var is_iphone = ((agent.indexOf('iphone')!=-1));
        var is_ipad = ((agent.indexOf('ipad')!=-1));
        var is_ipod = ((agent.indexOf('ipod')!=-1));
        if(is_iphone || is_ipad || is_ipod) {
            this.ios = true;
        }
    },
    countrySelector : {
        init: function() {
            $('.country-other').hide();
            if($('#country').val() === 'AU' || $('#country').val() === '') {
                $('.country-au').show();
                $('.country-au select').removeAttr('disabled');
                $('.country-other').hide();
                $('.country-other input').attr('disabled', 'disabled');
            } else {
                $('.country-au').hide();
                $('.country-au select').attr('disabled', 'disabled');
                $('.country-other').show();
                $('.country-other input').removeAttr('disabled');
            }
            this.enableCountryControls();
        },
        enableCountryControls : function() {
            $('#country').change(function() {
                if($(this).val() === 'AU') {
                    $('.country-au').show();
                    $('.country-au select').removeAttr('disabled');
                    $('.country-other').hide();
                    $('.country-other input').attr('disabled', 'disabled');
                } else {
                    $('.country-au').hide();
                    $('.country-au select').attr('disabled', 'disabled');
                    $('.country-other').show();
                    $('.country-other input').removeAttr('disabled');
                }
            });
        }
    },
    popupWindow : function (url, name, attributes) {
        // Append popup to the URL if it does not already exist
        var queryStringExists = false;
        if (url.match(/\?/)) {
            queryStringExists = true;
        }

        if (queryStringExists) {
            // Only append if we haven't already got the parameter popup defined
            if (!url.match(/(\?|&)popup=/)) {
                url = utils.baseHref + url + '&popup=true';
            }
        } else {
            url = utils.baseHref + url + '?popup=true';
        }

        var newWindow = window.open(url, name.replace(/[^A-z]/ig, ''), attributes);
        newWindow.focus();
    },
    getBaseHref : function () {
        var baseTags = document.getElementsByTagName('base');
        if (baseTags !== undefined && baseTags.length > 0) {
            this.baseHref = baseTags[0].href;
        }
    },
    decjubaOverlay : {
        init : function () {
            $('a.popup-iframe-page:not([data-decjuba-overlay])').click(function(e) {
                e.preventDefault();
                utils.decjubaOverlay.getContent($(this).attr('href'));
            }).attr('data-decjuba-overlay', 'true');
        },
        getContent : function (url) {
            if(url.indexOf('http://') == -1) {
                url = utils.baseHref + url;
            }
            $.get(url+'?popup=true', function(data) {
                utils.decjubaOverlay.showOverlay(data);
            });
        },
        showOverlay: function(html) {
            $('body').prepend('<div class="popup-overlay">&nbsp;</div><div class="popup-container  popup-iframe-container"><div class="popup-wrapper"><div class="popup-window"><a href="#" id="popup-close">Close</a><div class="popup-window-content">' + html + '</div></div></div><div class="overlay-close-pane"></div></div>');
            $('.popup-overlay').css({display: 'block'});
            $('.popup-container').css({display: 'block'});
            windowHeight = $(window).height();
            totalHeight = $('#page').height();
            if (windowHeight < totalHeight) {
                totalHeight = totalHeight + 44;
            }
            $('.popup-wrapper').css({top : $(window).scrollTop() + 50});
            $('.popup-overlay, .popup-container').css({height: totalHeight + 'px'});
            $('.popup-overlay').animate({opacity: .7}, 300, "swing", function() {
                $('.popup-container').animate({opacity: 1}, 300, "swing", function() {
                    $('.popup-window-content, #popup-close, .overlay-close-pane').click(function(e) {
                        e.preventDefault();
                        MainHolderBgResize.checkResize();
                        utils.decjubaOverlay.closeOverlay();
                    });
                });
            });
        },
        closeOverlay: function() {
            $('.popup-container').animate({opacity: 0}, 200, "swing", function() {
                $('.popup-overlay').animate({opacity: 0}, 200, "swing", function() {
                    $('.popup-overlay').remove();
                    $('.popup-container').remove();
                });
            });
        }
    },
    readCookie : function(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;
    }
};


