jQuery.fn.center = function() {
    return this.each(function () {
        var t = $(this);

        t.css({
            position:   'fixed', 
            left:       '50%', 
            top:        '50%',
        }).css({
            marginLeft: '-' + (t.outerWidth() / 2) + 'px', 
            marginTop:  '-' + (t.outerHeight() / 2) + 'px'
        });
    });
};

$().ready(function () {

	$('#laundryAdvice a.close').click(function(evt) {
		evt.preventDefault();
	   $('#laundryAdvice').fadeOut(250);
	});
	
	$('#showLaundryAdvice').click(function(evt) {
		evt.preventDefault();
	   $('#laundryAdvice').fadeIn(0, function() {
            $(this).center();
       });
	});
	
	$('#sizeGuideWomen a.close').click(function(evt) {
		evt.preventDefault();
	   $('#sizeGuideWomen').fadeOut(250);
	});
	
	$('#showSizeGuideWomen').click(function(evt) {
		evt.preventDefault();
	   $('#sizeGuideWomen').fadeIn(0, function() {
            $(this).center();
       });
	});

	$('#sizeGuideMen a.close').click(function(evt) {
		evt.preventDefault();
	   $('#sizeGuideMen').fadeOut(250);
	});
	
	$('#showSizeGuideMen').click(function(evt) {
		evt.preventDefault();
	   $('#sizeGuideMen').fadeIn(0, function() {
            $(this).center();
       });
	});

/*
 *  Switching main image on product pages
 */

    function prevImg() {
        var activeImg = $('.imageThumb.active');
        var activeImgSrc = $('.imageThumb.active').attr('src');

        if(activeImgSrc == $('.imageThumb').first().attr('src')) {
            var prevImg = $('.imageThumb').last();
            var prevImgSrc = $('.imageThumb').last().attr('src');
        } else {
            var prevImg = $('.imageThumb.active').prev();
            var prevImgSrc = $('.imageThumb.active').prev().attr('src');
        }

        $(activeImg).removeClass('active');
        $(prevImg).addClass('active');

        $('#mainImage').fadeOut(150,function() {
            $('#mainImage').attr('src', prevImgSrc);
            $('#mainImage').fadeIn();
        });
    }

    function nextImg() {
        var activeImg = $('.imageThumb.active');
        var activeImgSrc = $('.imageThumb.active').attr('src');

        if(activeImgSrc == $('.imageThumb').last().attr('src')) {
            var nextImg = $('.imageThumb').first();
            var nextImgSrc = $('.imageThumb').first().attr('src');
        } else {
            var nextImg = $('.imageThumb.active').next();
            var nextImgSrc = $('.imageThumb.active').next().attr('src');
        }

        $(activeImg).removeClass('active');
        $(nextImg).addClass('active');

        $('#mainImage').fadeOut(150,function() {
            $('#mainImage').attr('src', nextImgSrc);
            $('#mainImage').fadeIn();
        });
    }

    $(function() {
        $('#productButtonPrev, #productButtonNext').hover(function() {
            $(this).fadeTo(200, 0.5);
        },
        
        function() {
            $(this).fadeTo(200, 1);
        });
    });

    $('#productButtonPrev').click(function() {
        prevImg();
    });

    $('#productButtonNext').click(function() {
        nextImg();
    });

/*
 *  /Switching main image on product pages
 */

/*
 *  Image overlay on product pages
 */

    $('#mainImage').click(function() {
        var bigImgSrc = $('#mainImage').attr('src').replace('Photo', 'oPhoto');

        $('#imgOverlayContainer').fadeIn(200, function() {
            $('#imgOverlayContainer').width($(window).width());
            $('#imgOverlayContainer').height($(window).height());

            $('#imgOverlay').width($(window).width() * 0.8);
            $('#imgOverlay').height('auto');

            $('#imgOverlay').css({
                'top': 0,
                'left': $(window).width() * 0.1
            });

            $('#imgOverlay').attr('src', bigImgSrc);
            
            $('body').addClass('noOverflow');
        });
    });

    $(window).resize(function() {
        $('#imgOverlayContainer').width($(window).width());
        $('#imgOverlayContainer').height($(window).height());

        $('#imgOverlay').width($(window).width() * 0.8);
        $('#imgOverlay').height('auto');

        $('#imgOverlay').css({
            'top': 0,
            'left': $(window).width() * 0.1
        });
    });

    $('#imgOverlayContainer').mousemove(function (e) {
        var containerWidth = $(this).width();
        var containerHeight = $(this).height();
        var imgWidth = $('#imgOverlay').width();
        var imgHeight = $('#imgOverlay').height();

        var posx = 0;
        var posy = 0;
        if (!e) var e = window.event;
        if (e.pageX || e.pageY)     {
            posx = e.pageX;
            posy = e.pageY;
        }
        else if (e.clientX || e.clientY)    {
            posx = e.clientX + document.body.scrollLeft
                + document.documentElement.scrollLeft;
            posy = e.clientY + document.body.scrollTop
                + document.documentElement.scrollTop;
        }

        $('#imgOverlay').css('left', (containerWidth - imgWidth) * posx / containerWidth);
        $('#imgOverlay').css('top', (containerHeight - imgHeight) * posy / containerHeight);
    });

    $('#imgOverlay').click(function() {
        $('#imgOverlayContainer').fadeOut(200, function() {
	        $('body').removeClass('noOverflow');

            $('#imgOverlay').attr('src', '#');

            $('#imgOverlay').css({
                'top': 0,
                'left': 0
            });

            $('#imgOverlayContainer, #imgOverlay').width(0).height(0);
        });
    });

/*
 *  /Image overlay on product pages
 */

	$('#n_women a.maincat').hover(function() {
        $('#categoryNav a.maincat').removeClass('active');
        $(this).addClass('active');
        $('#n_accessories_sub').hide();
    	$('#n_men_sub').hide();
    	$('#n_women_sub').show();
	});

	$('#n_men a.maincat').hover(function() {
        $('#categoryNav a.maincat').removeClass('active');
        $(this).addClass('active');
        $('#n_accessories_sub').hide();
    	$('#n_women_sub').hide();
    	$('#n_men_sub').show();
	});

	$('#n_accessories a.maincat').hover(function() {
        $('#categoryNav a.maincat').removeClass('active');
        $(this).addClass('active');
        $('#n_men_sub').hide();
    	$('#n_women_sub').hide();
    	$('#n_accessories_sub').show();
	});
	
	
	$('#main').mouseover(function() {
        $('#categoryNav a.maincat').removeClass('active');
        if($('body').attr('id') != 'b_men') {
            $('#n_men_sub').hide();
        }
        if($('body').attr('id') != 'b_women') {
        	$('#n_women_sub').hide();
        }
        if($('body').attr('id') != 'b_accessories') {
        	$('#n_accessories_sub').hide();
        }
	});
    
    /*
    function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt)
    {
        var idx = carousel.index(i, mycarousel_itemList.length);
        carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[idx - 1]));
    }
    
    function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt)
    {
        carousel.remove(i);
    }
    function mycarousel_getItemHTML(item)
    {
        return item.li;
    }
 
    jQuery('#models.two').jcarousel({
    	animation:1000,
    	scroll:2,
        wrap: 'circular',
        itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
        itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback}
    });
    
    jQuery('#models.three').jcarousel({
    	animation:1000,
    	scroll:3,
        wrap: 'circular',
        itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
        itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback}
    });
    
    jQuery('#startpagescroller').jcarousel({
    	animation:2000,
    	scroll:1,
        wrap: 'circular',
        auto: 2,
        itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
        itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback}
    });
    */

});

/** Add a giftcard to the current cart */
function addGiftCard(sessid) {
	var url = "/~lecolena/views/popup-addgiftcard.php?"+sessid;
	var giftcard_window = window.open(url,'name','height=250,width=400');
	if (window.focus) { giftcard_window.focus(); }
}


