﻿// Use jQuery via jQuery(...)
var SuperZoom = {
    isZoomedIn: false,

    init: function() {
        jQuery('.superzoom-button').click(function() {
       		jQuery(document).scrollTop(0);
            var imagePath = jQuery('.product-image-map img').attr('rel');
            jQuery('.superzoom .image img').attr('src', imagePath).addClass('cursorPlus');
            jQuery('.superzoom .more-views').html(jQuery('.product-info-box .more-views').html());
            jQuery('.superzoom .product-info').html('<div class="price"><span>' + jQuery('.price .regular-price').html().replace(/€/, 'EUR') + '</span></div><div class="name"><span>' + jQuery('h2.product-name .sIFR-alternate').html() + '</span></div>');
            jQuery('.superzoom .scrollable').scrollable();
            jQuery('.superzoom .slider .items a').click(function() {
                jQuery('.superzoom .image img').attr('src', jQuery(this).find('img').attr('rel'));
            });

            jQuery('body').addClass('nonscrollable');

            jQuery('.superzoom').show();
            SuperZoom.repositionImage();
        });

        jQuery('.superzoom .close').click(function() {
            SuperZoom.zoomOut();
            jQuery('body').removeClass('nonscrollable');

            jQuery('.superzoom').hide();
        });

        jQuery('.superzoom .image img').click(function() {
            SuperZoom.zoom();
        });
    },

    zoom: function() {
        if (!this.isZoomedIn) this.zoomIn();
        else this.zoomOut();
    },

    zoomIn: function() {
        if (!this.isZoomedIn) {
        	jQuery('.superzoom .image img').removeClass('cursorPlus').addClass('cursorMinus');
            this.repositionImage(jQuery('.superzoom .image img').width() + 2800);
            // enable image moving
            jQuery('.superzoom').mousemove(function(event) {
                var layer = {
                    width: jQuery(this).width(),
                    height: jQuery(this).height()
                };
                var position = {
                    x: 1 / (layer.width / event.pageX),
                    y: 1 / (layer.height / event.pageY)
                };
                var image = {
                    width: jQuery(this).find('.image img').width(),
                    height: jQuery(this).find('.image img').height()
                };

                jQuery(this).find('.image img').css({
                    top: ((position.y * image.height)) * -1 + 300,
                    left: ((position.x * image.width) - (image.width / 2)) * -.4
                });
            });

            this.isZoomedIn = true;
        }
    },

    zoomOut: function() {
        if (this.isZoomedIn) {
        	jQuery('.superzoom .image img').removeClass('cursorMinus').addClass('cursorPlus');
            this.repositionImage(jQuery('.superzoom .image img').width() - 2800);
            // disable image moving
            jQuery('.superzoom').unbind('mousemove');
            jQuery('.superzoom .image img').css({
                top: 0,
                left: 0
            });

            this.isZoomedIn = false;
        }
    },

    repositionImage: function(newWidth) {
    	sIFR.replace(agbook, {
			selector: '.product-info div.price',
			wmode: 'transparent',
			css: [
				'.sIFR-root { text-transform: uppercase; color: #d80e00; }'
			]
			});
			sIFR.replace(agbook, {
			selector: '.product-info div.name span',
			wmode: 'transparent',
			css: [
				'.sIFR-root { text-transform: uppercase; }'
			]
			});

        if ('undefined' == typeof newWidth) {
            newWidth = jQuery('.superzoom .image img').width();
        }
        else {
            jQuery('.superzoom .image img').width(newWidth);
        }

        jQuery('.superzoom .image').css({
            'width': newWidth,
            'margin-left': newWidth / 2 * -1
        });
    }
};
jQuery(function($){
    jQuery(document).ready(function(){

        jQuery(function($){
            if($('#sendafriendlink').length){
                $('#sendafriendlink').click(function(){
                    var content = '<iframe id="sendfriend-overlay" src="'+$(this).attr('href')+'"></iframe>';
                    $('.product-shop').prepend(content);
                    $('#sendfriend-overlay').hide().load(function(){ 
                        
                        $(this).contents().find('form').submit(function(){
                          $('#sendfriend-overlay').fadeOut();
                        })
                        if($(this).contents().find('#messages_product_view').length){
                            $('.category-head').after($(this).contents().find('#messages_product_view').clone());
                            $(this).remove();
                        }else{
                            $(this).fadeIn();
                        }
                        });
                    return false;
                });
            }
        });

        /* Bildertausch: Hauptnavigation */
        var o;
        jQuery('.quick-access .switch a img').hover(function() {
            o = jQuery(this).attr('src');
            var s = o.slice(-4);
            var f = o.slice(0, -4);
            jQuery(this).attr({
                src: f + '-active' + s
                });
            jQuery(this).show();
        },
        function() {
            jQuery(this).attr({
                src: o
            });
        });
        /* EOF Bildertausch */

        // initialize scrollable
        if(jQuery(".scrollable").length > 0){
            jQuery(".scrollable").scrollable();
        }

        // initialize tabs
        if(jQuery("ul.tabs").length > 0){
            jQuery("ul.tabs").tabs("div.panes > div");
        }

        // initialize accodrion
        if(jQuery(".accordion").length > 0){
            jQuery( ".accordion" ).accordion({
                autoHeight: false
            });
        }

        // Stage
        jQuery(".stage .items a").click(function() {
            var aTag = jQuery(this);
            currentStageItemIndex = aTag.index()+(4*(aTag.parent().index()));
            setStageItem(aTag);
            // when page loads simulate a "click" on the first image
            return false;
        }).filter(":first").click();
        if (jQuery('.stage').length > 0 && typeof animateStage == 'function') {
            stageItemsCount = $('.stage .items a').length;
            jQuery('.stage').hover(function() {
                animateStage('stop');
            },
            function () {
                animateStage('start');
            });
            animateStage('start');
        }

        SuperZoom.init();
    });



    function setStageItem(clickedATag) {
        // see if same thumb is being clicked
        if (clickedATag.hasClass("active")) {
            return;
        }
        // calclulate large image's URL based on the thumbnail URL (flickr specific)
        var imageURL = clickedATag.attr("rel");
        var url = clickedATag.attr('href');
        // get handle to element that wraps the image and make it semi-transparent
        var wrap = $("#image_wrap").show();
        // the large image from www.flickr.com
        var image = new Image();
        // call this function after it's loaded
        image.onload = function() {
            // change the image
            var stageATag = wrap.find('a');
            var currentImage = wrap.find("img");
            var newImage = currentImage.clone();
            newImage.attr("src", imageURL);
            newImage.css({
                display: 'none'
            });

            stageATag.append(newImage);

            newImage.show();
            currentImage.remove();


            stageATag.attr('href', url);
            // activate item
            $(".items a").removeClass("active");
            clickedATag.addClass("active");
        };
        // begin loading the image from www.flickr.com
        image.src = imageURL;
    }



   jQuery('.more-views .item a').live('click', function(){
           var newImgPath = jQuery(this).attr('href');
          
           jQuery('.product-image-map img').attr('src', newImgPath );
            return false;
    });
    jQuery('.superzoom .item a').click(function(){
           var newImgPath = jQuery(this).attr('href');
            jQuery('.product-image-map img').attr('src', newImgPath);
            return false;
    });
});
