jQuery.noConflict();
jQuery(window).load(function(){
    imagePopupItem.initialize();
    moviePopupItem.initialise();
});

var imagePopupItem=
{
    thumbnailImages:null,
    paddingBottom : 15,

    initialize:function()
    {
        this.showJSVersion();
        this.thumbnailImages=jQuery("#alternateImagesBody").find(".alternateImage");
        this.attachEvents();
        this.setContainerSize();
    },

    showJSVersion:function()
    {
       jQuery(".javascriptEnabled").show();
       jQuery(".closeButton").show();
       jQuery(".chooseColorSwatches").show();
    },

    setContainerSize:function(){
       var parentIframe=jQuery(parent.document).find("#ImageFrame");
       if(parentIframe.length>0){
           var h = jQuery("#imageBody").parents("body.richContent").innerHeight();
           if(h && h != "") parentIframe.css("height", h + "px");
           
           var w = jQuery("#imageBody").innerWidth();
           if(w && w != "") parentIframe.css("width", w + "px");
       }
    },

    
    attachEvents:function()
    {
       var parentThis=this;
       
       jQuery('#zoomInControl').click(function(){
           browserZoomIn(1);
           publishZoomInClickAnalyticsInformation(currentAsin);
       });

       jQuery('#zoomOutControl').click(function(){
           browserZoomOut(1);
           publishZoomOutClickAnalyticsInformation(currentAsin);
       });

       jQuery('#zoomResetControl').click(function(){
           browserZoomReset();
           publishZoomResetClickAnalyticsInformation(currentAsin);
       });
       jQuery('#closeWindow').click(function(){
           window.close();
       });

       this.thumbnailImages.click(function(){
           publishAlternateImageClickAnalyticsInformation(currentAsin);
       });
       this.thumbnailImages.mouseover(function(){
           parentThis.showAltImage(this);
           parentThis.setContainerSize();
       });

       this.thumbnailImages.focus(function(){
           parentThis.showAltImage(this);
           parentThis.setContainerSize();
       });

       this.thumbnailImages.keypress(function(e){
           if(e.which == 13){
               publishAlternateImageClickAnalyticsInformation(currentAsin);
           }
       });

    },

    showAltImage:function(domEle){
        var domElement=jQuery(domEle).parents("li").eq(0);
        if(!domElement || domElement.length < 1) return;
        var key = domElement.attr("key");
        if(!key) return;
        moviePopupItem.hideMovie();
        jQuery(".imageMain").removeClass("imageMain").addClass("imageAlt")
        domElement.addClass("imageMain");
        jQuery(domEle).addClass("imageMain");
        displayImage(key);
    }
}

var moviePopupItem = {
	currentThumbnail: null,
	currentViewHolder: null,
	thumbnailImages: null,

	initialise: function() {
		if (window.moviesJSON) {
			this.thumbnailImages = jQuery('#alternateImagesBody').find('.movieThumbnail');
			this.attachEvents();
		}
	},

	attachEvents: function() {
		var parent = this;

		this.thumbnailImages.mouseover(function() {
			parent.showMovie(this);
		});
	},

	showMovie: function(domElement) {
		if (domElement == this.currentThumbnail)
			return;
		this.currentViewHolder = document.getElementById('productImage');
		if (this.currentViewHolder.style.display == 'none')
			this.currentViewHolder = document.getElementById('zoomViewHolder');
		this.currentViewHolder.style.display = 'none';
		var imageMain = jQuery('#alternateImagesBody').find('.imageMain');
		imageMain.removeClass('imageMain');
		imageMain.addClass('imageAlt');
		this.resetThumbnail();
		domElement.className += ' movieMain';
		var thumbnailImage = domElement.getElementsByTagName('img')[0];
		thumbnailImage.className = 'movieMain';
		this.currentThumbnail = domElement;
		var key = domElement.getAttribute('id');
		var movie = 'http://' + amazonDomain + '/gp/AmznFlashPlayer/standalone.html?mediaObjectId=' + key;
		var movieJSON = moviesJSON[key];
		var width = movieJSON['width'];
		var height = movieJSON['height'];
		movie += '&preset=exact&exactWidth=' + width + '&exactHeight=' + height;
		var controlHeight = (width >= 300) ? 42 : 24;
		
		jQuery('#productVideo').attr('src', movie);
		jQuery('#productVideo').show();
		jQuery('#productVideo').width(width);
		jQuery('#productVideo').height(height + controlHeight);
		/* The Movie Player Widget does not allow free control of its attributes. It uses a table layout that scales incorrectly. The value 24 seems to fit well by experiments. */
	},

	hideMovie: function() {
		if (this.currentThumbnail != null) {
			this.resetThumbnail();
			this.currentThumbnail = null;
		}
		jQuery('#productVideo').hide();
		if (this.currentViewHolder != null) {
			this.currentViewHolder.style.display = 'inline';
			this.currentViewHolder = null;
		}
	},

	resetThumbnail: function() {
		if (this.currentThumbnail != null) {
			this.currentThumbnail.className = this.currentThumbnail.className.replace(/\bmovieMain\b/, '').replace(/\s+$/, '');
			var thumbnailImage = this.currentThumbnail.getElementsByTagName('img')[0];
			if (thumbnailImage != null)
				thumbnailImage.className = '';
		}
	}
}

