$(function () {
	
	//////////////////////////////////////////////////////////////
	// Popup menus
	//////////////////////////////////////////////////////////////
	
	$('.dropdown.no-align').dropdown({
		sub: '.popup',
        interval: 10,
        timeout: 300/*,
        onClose: function () { Form.clearErrors(this.$sub.find('form')); }*/
	});

    $('.dropdown.left-align').dropdown({
		sub: '.popup',
        interval: 10,
        timeout: 300,
        /*onClose: function () { Form.clearErrors(this.$sub.find('form')); },*/
		position: {
			my: 'left top',
			at: 'left bottom',
			offset: '0 0'
		}
	});

    $('.dropdown.right-align').dropdown({
		sub: '.popup',
        interval: 10,
        timeout: 300,
        /*onClose: function () { Form.clearErrors(this.$sub.find('form')); },*/
		position: {
			my: 'right top',
			at: 'right bottom',
			offset: '0 0'
		}
	});
	
	//////////////////////////////////////////////////////////////
	// Tooltips
	//////////////////////////////////////////////////////////////
	
	$('a[rel="tooltip"]').qtip({
      	content: {
         	text: function(api) {
				return $(this).children('.tooltip-content').first().clone().show();
			}, 
         	title: {
				text: function(api) {
					return $(this).attr('title');
				}
			}
      	},
      	position: {
      		my: 'top left', 
      		at: 'bottom center', 
         	viewport: $(window)
      	},
      	show: {
         	event: 'click mouseenter'
      	},
      	hide: {
		  	event: 'click mouseleave'
		},
     	solo: true, 
     	style: {
			classes: 'ui-tooltip-dark tooltip'
		}
   	});

    $('.formfield.error')
        .not(':has(input[type="checkbox"], input[type="radio"])')
        .each(function () {

        var $this = $(this);
        var $target = $this.find('input, textarea, select');

        $this.qtip({
            content: {
                text: function(api) {
                    return $(this).find('.message').first().text();
                }
            },
            position: {
                target: $target,
                my: 'left top',
                at: 'center right',
                viewport: $(window)
            },
            show: {
                target: $target,
                event: 'focus'
            },
            hide: {
                target: $target,
                event: 'blur'
            },
            solo: true,
            style: {
                classes: 'ui-tooltip-dark tooltip error'
            }
        });
   	});

    $('.formfield.error')
        .has('input[type="checkbox"], input[type="radio"]')
        .each(function () {

        var $this = $(this);

        $this.qtip({
            content: {
                text: function(api) {
                    return $(this).find('.message').first().text();
                }
            },
            position: {
                my: 'left top',
                at: 'center right',
                viewport: $(window)
            },
            show: {
                event: 'mouseover'
            },
            hide: {
                event: 'mouseout'
            },
            solo: true,
            style: {
                classes: 'ui-tooltip-light ui-tooltip-shadow tooltip error'
            }
        });
   	});

    // Hide form error spans
    $('.formfield.error').find('.message').hide();
    
   	//////////////////////////////////////////////////////////////
	// In-Field labels
	//////////////////////////////////////////////////////////////
	
	$(".formfield.in-field-labels label, form.in-field-labels .formfield label").inFieldLabels({ fadeOpacity: 0.3 });
	
	// Fix: with plain inFieldLabels plugin the labels are still selectable, this is not desirable.
	$(".formfield.in-field-labels label, form.in-field-labels .formfield label").mousedown(function() { $(this).closest('.formrow').find('input').focus(); return false; });

    //////////////////////////////////////////////////////////////
	// Overlays
	//////////////////////////////////////////////////////////////

    var triggers = $(".overlay-trigger").overlay({
        fixed: true,
        top: '20%',
        close: '.close-overlay',
        mask: {
            color: '#fff',
            loadSpeed: 200,
            opacity: 0.5
        },
        onBeforeLoad: function (e) {
            window.console.log(this);
            Form.clearErrors(this.getOverlay().find('form'));
        },
        onLoad: function (e) {
            this.getOverlay().find('input').first().focus();
        }
    });

    //////////////////////////////////////////////////////////////
	// Override some links with AJAX functionality
	//////////////////////////////////////////////////////////////

    /*$('#login-form').submit(function (e) {
        var loadingTimeout = window.setTimeout(function () {
            $(document.body).showLoading({
                overlayZIndex: 10000,
                indicatorZIndex: 10001,
                beforeShow: function (a) {
                    a.overlay.css({ position: 'fixed' });
                    a.indicator.css({ position: 'fixed' });
                }
            });
        }, 100);
        Form.ajaxSubmit({
            form: this,
            url: '?controller=ajax.Login&action=process',
            complete: function () {
                window.clearTimeout(loadingTimeout);
                $(document.body).hideLoading();
            }
        });
        return e.preventDefault();
    });*/

    /*$('#logout-link').click(function (e) {
        var loadingTimeout = window.setTimeout(function () {
            $(document.body).showLoading({
                beforeShow: function (a) {
                    a.overlay.css({ position: 'fixed' });
                    a.indicator.css({ position: 'fixed' });
                }
            });
        }, 100);
        Ajax.request({
            url: '?controller=ajax.Logoff&action=process',
            complete: function () {
                window.clearTimeout(loadingTimeout);
                $(document.body).hideLoading();
            }
        });
        return e.preventDefault();
    });*/

    //////////////////////////////////////////////////////////////
	// Init cloud-zoom
	//////////////////////////////////////////////////////////////

    $('.zoom').jqzoom({
        zoomType: 'innerzoom',
        zoomWidth: 'auto',
        zoomHeight: 'auto',
        preloadImages: true,
        showEffect: 'show',
        hideEffect: 'hide',
        title: false,
        activate: 'mousedown',
        deactivate: 'click mouseleave'
    });
    
    $('.scope-link').each(function () {
        var $that = $(this);
        var $rel = $('#' + $that.attr('rel')).find('img');
        $that.bind('click', function (e) { return e.preventDefault(); });
        $that.bind('mousedown mousemove', function (e) {
            $rel.trigger(e);
            return e.preventDefault();
        });
    });

    //////////////////////////////////////////////////////////////
	// Product listing
	//////////////////////////////////////////////////////////////

    /*$('.product-listing .product-cell .info').hide();

    $('.product-listing .product-cell').hover(
        function (e) {
            $(this).find('.info').css({ bottom: '-55px' }).show().stop().animate({ top: '0px' });
        },
        function (e) {
            $(this).find('.info').stop().animate({ bottom: '-55px' });
        }
    );*/

    //////////////////////////////////////////////////////////////
	// Cart
	//////////////////////////////////////////////////////////////

    var $cartBox = $('#cart-info-box');

    if ($cartBox.length > 0) {

        var $content = $('#content');
        var cartBoxOrigin = $cartBox.offset();
        var cartBoxTopSpacing = 20;
        var cartBoxBottomSpacing = 80;

        $(document).scroll(function (e) {
            var scrollPos = $(window).scrollTop();
            var cartBoxHeight = $cartBox.outerHeight();
            var contentHeight = $content.outerHeight();
            var contentOrigin = $content.offset();
            if (scrollPos > cartBoxOrigin.top - cartBoxTopSpacing) {
                var cartBoxBottom = scrollPos + cartBoxTopSpacing + cartBoxHeight;
                var parentBottom = contentOrigin.top + contentHeight;
                if (cartBoxBottom < (parentBottom - cartBoxBottomSpacing)) {
                    $cartBox.css({
                        position: 'fixed',
                        top: cartBoxTopSpacing + 'px',
                        width: $cartBox.parent().width() + 'px'
                    });
                } else {
                    var top = (parentBottom - contentOrigin.top - (cartBoxBottomSpacing + cartBoxHeight));
                    $cartBox.css({
                        position: 'absolute',
                        top: top + 'px',
                        width: $cartBox.parent().width() + 'px'
                    });
                }
            } else {
                $cartBox.css({ position: 'static', width: '100%' });
            }
        });

        $(window).resize(function (e) {
            if ($cartBox.css('position') != 'static') {
                $cartBox.css({ width: $cartBox.parent().width() + 'px' });
            }
        });

    }

});

