﻿function ReadyFunctions() {

    $(".map-hover a").hover(function () {
        $(".map-hover a span").animate({ opacity: "show", top: "88" }, "slow");
    }, function () {
        $(".map-hover a span").animate({ opacity: "hide", top: "78" }, "fast");
    });

    $('#nav li').hover(
	function () {
	    //show its submenu
	    $('ul', this).slideDown(150);

	},
	function () {
	    //hide its submenu
	    $('ul', this).slideUp(100);
	}
	);

    $("#tabs").tabs();
    $(".advancedPanel").hide();
    $("#showAdvanced").click(function () {
        $(".advancedPanel").slideToggle("slow");
    });

    $('#features1').jshowoff({ speed: 5000, links: false, controls: false, effect: 'fade', hoverPause:false });
    $('#features2').jshowoff({ speed: 5000, links: false, controls: false, effect: 'fade', hoverPause: false });
    $('#features3').jshowoff({ speed: 5000, links: false, controls: false, effect: 'fade', hoverPause: false });

    $("#aspnetForm").validate({ onsubmit: false });
    $('.validationGroup .causesValidation').click(function (evt) {
        var $group = $(this).parents('.validationGroup');
        var isValid = true;
        var $firstitem;

        $group.find(':input').each(function (i, item) {
            if (!$(item).valid()) {
                if (i == 0)
                    $firstitem = item;
                isValid = false;
            }
        });

        if (!isValid) {
            evt.preventDefault();
        }
    });

    $('.validationCombo .causesValidationCombo').click(function (evt) {
        var $group = $(this).parents('.validationCombo');
        var isValid = true;
        var $firstitem;

        $group.find(':input').each(function (i, item) {
            if (!$(item).valid()) {
                if (i == 0)
                    $firstitem = item;
                isValid = false;
            }
        });

        if (!isValid) {
            evt.preventDefault();
        }
    });

    $(".thumb").thumbs();
    var imgArr = [];
    function setGallery() {
        $("#pikame li").each(function (i) {
            var img = $(this).find('a').attr('href');
            var title = $(this).find('img').attr('alt');
            imgArr[i] = {
                'href': img,
                'title': title
            }
            $(this).find('a').attr("href", "javascript:;");
            $(this).find('img').attr("title", i);
        });
        //console.log(imgArr);
    }
    setGallery();

    function showGallery(num) {
        $.fancybox(imgArr,
    {
        'index': num,
        'type': 'image',
        'titlePosition':'outside',
		'overlayColor': '#000',
		'overlayOpacity': 0.9,
        'overlayShow': true
    });
    }

    function updateImage(self) {
        var num = self.active.attr("title");
        //console.log(num, imgArray);
        self.caption.find("a").click(function(){showGallery(num)}); // Uncomment to activate popup on any links in caption
        self.anchor.click(function () { showGallery(num) });
    }
    if(imgArr.length>1)
        $("#pikame").PikaChoose({ autoPlay: true, animationFinished: updateImage, transition: [0] });
    else $("#pikame").PikaChoose({ autoPlay: false, animationFinished: updateImage, transition: [0] });

	$("#pikame").jcarousel({					
		initCallback: function(carousel) 
		{
			$(carousel.list).find('img').click(function() {
					var clicked = parseInt($(this).parents('.jcarousel-item').attr('jcarouselindex'));
					var last = ($(this).parents('ul').find('li:last').index() == clicked-1) ? true : false;
					if(!last){
						clicked = (clicked-2<=0) ? 0 : clicked-2;
					}
				carousel.scroll(clicked);
			});
		}
    });

};

function DoJqueryCheck() {

};

function ValAndMask() {
    $('.postalCode').mask("99-999");
    $('.nip').mask("9999999999");
    $('.pesel').mask("99999999999");
    $('.regon').mask("999999999");
    $('.phone').mask("+49 99 9999999");
    $('.mobile').mask("+49 999999999");
    $('.numbers').ForceNumericOnly();
}

$(document).ready(function () {
    ValAndMask();
    ReadyFunctions();
});

$(document).AjaxReady(function () {
    ValAndMask();
    ReadyFunctions();
});

var Page_IsValid = true;
var execute = false;
function SetConfirmation(buttonId, message, causesValidation) {
    //Unbind click before bind
    //(This is needed when using UpdatePanels)
    $("#" + buttonId)
		.unbind('click')
		.click(function (e) {
		    //We keep the original click event so
		    //it can be executed as a callback
		    var target = $(e.target);

		    //Check if the control is a button or a link
		    if (target.attr("href")) {
		        callback = function () { execute = true; window.location.href = target.attr("href"); };
		    }
		    else {
		        callback = function () { execute = true; target.click(); };
		    }

		    //Register popup and yes callback
		    //We take into account causes validation button property
		    //And if the page is currently valid
		    if (!causesValidation || Page_IsValid)
		        confirmPopup(message, callback);

		    //If execute is true, it means that it was set by the yes callback
		    //and so we should return true in order to not interfer with the form submission
		    var result = execute;
		    execute = false;
		    return result;
		});
}

jQuery.fn.ForceNumericOnly =
function () {
    return this.each(function () {
        $(this).keydown(function (e) {
            var key = e.charCode || e.keyCode || 0;
            // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
            return (
				key == 8 ||
				key == 9 ||
				key == 46 ||
				(key >= 37 && key <= 40) ||
				(key >= 48 && key <= 57) ||
				(key >= 96 && key <= 105));
        })
    })
};

function confirmPopup(message, callbackYes) {
    $('#confirm').modal({
        close: false,
        overlayId: 'confirmModalOverlay',
        containerId: 'confirmModalContainer',
        onShow: function (dialog) {
            dialog.data.find('.message').append(message);

            // if the user clicks "yes"
            dialog.data.find('.yes').click(function () {
                // call the callback
                if ($.isFunction(callbackYes)) {
                    callbackYes.apply();
                }
                // close the dialog
                $.modal.close();
            });
            window.focus();
        }
    });
}

