﻿//$(function() {
//    $('#GetStart').click(function() {
//        var blind = '<div id="Blinds">&nbsp;</div><div id="AppPop"><div id="LightboxL">&nbsp;</div><div id="LightBoxContent">fff</div><div id="LightboxR">&nbsp;</div></div>';
//        $('#form1').html($('#form1').html() + blind);
//        //    $('#AppPop').corner("bevel");
//    });
//});

(function($) {
    $.fn.lightbox = function(options) {
        var opts = $.extend({}, $.fn.lightbox.defaults, options);
        return initialize();

        function initialize() {
            $('#overlay').remove();
            $('#lightbox').remove();
            opts.inprogress = false;

            // if jsonData, build the imageArray from data provided in JSON format
            if (opts.jsonData && opts.jsonData.length > 0) {
                var parser = opts.jsonDataParser ? opts.jsonDataParser : $.fn.lightbox.parseJsonData;
                opts.imageArray = [];
                opts.imageArray = parser(opts.jsonData);
            }

            var outerImage = '<div id="outerImageContainer"><div id="DataContainer"><div id="SignUpHead">' + opts.lightboxTitle + '<img src="/images/HelpIcon/action_delete.png" alt="close" id="closedLightBox"/> </div>' + opts.lightBoxInnerHtml + '</div></div>';

            var string = '<div id="overlay"></div><div id="lightbox">' + outerImage + '</div>';


            $("body").append(string);
            //            $('#yearSel').html(yearOption());
            //            $('#monthSel').html(monthOption());

            //            $.GET('/Countries', function(data) {
            //                var countries = Betting.deserialize(data);
            //                var countriesOption = '<option value=0>Countries</option>';
            //                $.each(countries, function(cIndex, cObject) {
            //                    countriesOption += '<option value=' + cObject.ID + '>' + cObject.Name + '</option>';
            //                });
            //                $('#countrySel').html(countriesOption);
            //            });

            $("#closedLightBox").click(function() { end(); return false; });
            $('#outerImageContainer').width(opts.widthCurrent).height(opts.heightCurrent);
            // $('#imageDataContainer').width(opts.widthCurrent);
            if (!opts.imageClickClose) {
                $("#lightboxImage").click(function() { return false; });
                $("#hoverNav").click(function() { return false; });
            }
            start();
        };
        function getPageSize() {
            var jqueryPageSize = new Array($(document).width(), $(document).height(), $(window).width(), $(window).height());
            return jqueryPageSize;
        };

        function getPageScroll() {
            var xScroll, yScroll;

            if (self.pageYOffset) {
                yScroll = self.pageYOffset;
                xScroll = self.pageXOffset;
            } else if (document.documentElement && document.documentElement.scrollTop) {  // Explorer 6 Strict
                yScroll = document.documentElement.scrollTop;
                xScroll = document.documentElement.scrollLeft;
            } else if (document.body) {// all other Explorers
                yScroll = document.body.scrollTop;
                xScroll = document.body.scrollLeft;
            }

            var arrayPageScroll = new Array(xScroll, yScroll);
            return arrayPageScroll;
        };

        function pause(ms) {
            var date = new Date();
            var curDate = null;
            do { curDate = new Date(); }
            while (curDate - date < ms);
        };

        function start() {
            //            $("select, embed, object").hide();
            var arrayPageSize = getPageSize();
            $("#overlay").hide().css({ width: '100%', height: arrayPageSize[1] + 'px', opacity: opts.overlayOpacity }).fadeIn(500, function() {
                $('#outerImageContainer').fadeIn();
            });
            // calculate top and left offset for the lightbox


            var arrayPageScroll = getPageScroll();
            //            var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
            var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 3);
            var lightboxLeft = arrayPageScroll[0];
            $('#lightbox').css({ top: lightboxTop + 'px', left: lightboxLeft + 'px' }).show();

            resizeImageContainer(opts.resizeWidthTo, opts.resizeHeightTo);
        };

        function end() {
            //            disableKeyboardNav();
            $('#lightbox').hide();
            $('#overlay').fadeOut();
            $('select, object, embed').show();
        };

        function preloadNeighborImages() {
            if (opts.loopImages && opts.imageArray.length > 1) {
                preloadNextImage = new Image();
                preloadNextImage.src = opts.imageArray[(opts.activeImage == (opts.imageArray.length - 1)) ? 0 : opts.activeImage + 1][0]

                preloadPrevImage = new Image();
                preloadPrevImage.src = opts.imageArray[(opts.activeImage == 0) ? (opts.imageArray.length - 1) : opts.activeImage - 1][0]
            } else {
                if ((opts.imageArray.length - 1) > opts.activeImage) {
                    preloadNextImage = new Image();
                    preloadNextImage.src = opts.imageArray[opts.activeImage + 1][0];
                }
                if (opts.activeImage > 0) {
                    preloadPrevImage = new Image();
                    preloadPrevImage.src = opts.imageArray[opts.activeImage - 1][0];
                }
            }
        };

        function resizeImageContainer(imgWidth, imgHeight) {
            // get current width and height
            //            opts.widthCurrent = $("#outerImageContainer").outerWidth();
            //            opts.heightCurrent = $("#outerImageContainer").outerHeight();

            // get new width and height
            var widthNew = Math.max(350, imgWidth + (opts.borderSize * 2));
            var heightNew = (imgHeight + (opts.borderSize * 2));

            // scalars based on change from old to new
            opts.xScale = (widthNew / opts.widthCurrent) * 100;
            opts.yScale = (heightNew / opts.heightCurrent) * 100;

            // calculate size difference between new and old image, and resize if necessary
            wDiff = opts.widthCurrent - widthNew;
            hDiff = opts.heightCurrent - heightNew;

            $('#DataContainer').animate({ width: 260 }, opts.resizeSpeed, 'linear');
            $('#outerImageContainer').animate({ width: 260 }, opts.resizeSpeed, 'linear', function() {
                $('#outerImageContainer').animate({ height: 260 }, opts.resizeSpeed, 'linear', function() {
                    //                    showImage();
                });
            });

            //            // if new and old image are same size and no scaling transition is necessary,
            //            // do a quick pause to prevent image flicker.
            //            if ((hDiff == 0) && (wDiff == 0)) {
            //                if (jQuery.browser.msie) { pause(250); } else { pause(100); }
            //            }

            //            $('#prevLink').height(imgHeight);
            //            $('#nextLink').height(imgHeight);
        };
        function yearOption() {
            var yOption = '<option value="0">year</option>';
            var d = new Date();

            for (var yIndex = d.getFullYear(); yIndex > (d.getFullYear() - 100); yIndex--) {
                yOption += '<option value=' + (yIndex) + '>' + yIndex + '</option>';
            }
            return yOption;
        };
        function monthOption() {
            var mOption = '<option value="0">month</option>';
            var mIndex = 1;
            while (mIndex <= 12) {
                mOption += '<option value=' + mIndex + '>' + mName(mIndex) + '</option>'
                mIndex++;
            }
            return mOption;
        }
        function mName(mindex) {

            switch (mindex) {
                case 1:
                    return "January";
                case 2:
                    return "February";
                case 3:
                    return "March";
                case 4:
                    return "April";
                case 5:
                    return "May";
                case 6:
                    return "June";
                case 7:
                    return "July";
                case 8:
                    return "August";
                case 9:
                    return "September";
                case 10:
                    return "October";
                case 11:
                    return "November";
                case 12:
                    return "December";
                default:
                    alert('Get month name error');
            }
        }
    };
    $.fn.lightbox.parseJsonData = function(data) {
        var imageArray = [];

        $.each(data, function() {
            imageArray.push(new Array(this.url, this.title));
        });

        return imageArray;
    };
    $.fn.lightbox.changeMY = function() {
        var m = $('#monthSel').val();
        var y = $('#yearSel').val();
        if ((y != 0) && (m != 0)) {
            $('#daySel').html($.getDays(y, m));
        }
    };
    $.autoStart = function() {
        $.fn.lightbox.initialize();
        $.fn.lightbox.start(this);
        return false;
    };
    $.fn.lightbox.defaults = {
        fileLoadingImage: 'images/loading.gif',
        fileBottomNavCloseImage: 'images/closelabel.gif',
        lightboxTitle: '',
        overlayOpacity: 0.1,
        borderSize: 10,
        imageArray: new Array,
        activeImage: null,
        inprogress: false,
        resizeSpeed: 350,
        widthCurrent: 250,
        heightCurrent: 250,
        resizeWidthTo: 260,
        resizeHeightTo: 260,
        xScale: 1,
        yScale: 1,
        displayTitle: true,
        navbarOnTop: false,
        slideNavBar: false, // slide nav bar up/down between image resizing transitions
        navBarSlideSpeed: 350,
        displayHelp: false,
        strings: {
            help: ' \u2190 / P - previous image\u00a0\u00a0\u00a0\u00a0\u2192 / N - next image\u00a0\u00a0\u00a0\u00a0ESC / X - close image gallery',
            prevLinkTitle: 'previous image',
            nextLinkTitle: 'next image',
            prevLinkText: '&laquo; Previous',
            nextLinkText: 'Next &raquo;',
            closeTitle: 'close image gallery',
            image: 'Image ',
            of: ' of '
        },
        fitToScreen: false, 	// resize images if they are bigger than window
        disableNavbarLinks: false,
        loopImages: false,
        imageClickClose: true,
        jsonData: null,
        jsonDataParser: null,
        lightBoxInnerHtml: ''
    };
    $.isEmail = function emailValidator1(elem, helperMsg) {
       
        var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
        if (elem.val().match(emailExp)) {
            return true;
        }
        else {
            //            elem.focus();
            return false;
        }
    };
})(jQuery);
