﻿var ProductHelper;

(function ($) {

	ProductHelper = {
		product_content_container: null,
		photo: null,
		photo_caption: null,
		thumbnails: null,
		swf_container: null,
		scroller: null,
		
		selected_thumbnail_index: null,

		config: {
			div: '<div />',
		
			product_content_container_id: 'product-content',
			photo_container_id: 'product-image',
			photo_caption_id: 'product-caption',
			thumbnail_container_id: 'product-thumbnails',
			swf_container: 'product-image-flash',
			scroll_frame_id: 'scroll-frame',
			scroll_view_id: 'scroll-view',
			
			active_class: 'active',
			
			scroller: {
				scroll: 4,
				animation: 'fast',
				easing: 'swing',
				wrap: null,
				itemFirstInCallback: null,
				itemFirstOutCallback: null
			}
		},

		setObjects: function () {
			this.product_content_container = $('#' + this.config.product_content_container_id);
			this.photo = $('#' + this.config.photo_container_id + ' img');
			this.photo_caption = $('#' + this.config.photo_caption_id);
			this.thumbnail_container = $('#' + this.config.thumbnail_container_id);
			this.thumbnails = $('#' + this.config.thumbnail_container_id + ' li');
			this.swf_container = $(this.config.div).attr('id', 'product-image-flash');
		},

		initialize: function () {
			this.setObjects();
			this.attachEvents();
			this.loadDependencies();
			
			this.createScroller();
		},
		
		loadDependencies: function () {
			var self = this;
			$.getUniqueScript('/pages/script/jquery/jquery.plugin.ui.min.js', function () { 
				$.preloadImages($('li a', ProductHelper.thumbnail_container), 'href');
				ProductHelper.createTabs();
			});
			
			if (this.thumbnails.hasClass('mt_productshotdemo')) {
				$.getUniqueScript('/pages/script/jquery/jquery.plugin.flash.min.js', function () { 
					$('#' + self.config.thumbnail_container_id + ' li').eq(0).click();
				});
			}
		},
		
		attachEvents: function () {
			this.thumbnails.click(function (e) { ProductHelper.changePhoto(e, this); });
			
			$('#instructions h3').click(function () {
				$('#instructions > div').slideToggle('slow');
			});
		},
		
		changePhoto: function (e, obj) {
			e.preventDefault();
			var $obj = $(obj);
			
			if (!$obj.hasClass(this.config.active_class)) {
				var obj_link = $('a', $obj);

				if ($obj.hasClass('mt_productshotdemo')) {
					var swf = $('#' + this.config.swf_container);
					
					if (swf.length < 1) { 
						$('#' + this.config.photo_container_id).
							prepend(this.swf_container);
					}	
					this.photo.hide();
					$('#' + this.config.swf_container).flash({ 
						src: obj_link.attr('rel'),
						width: 243,
						height: 243,
						wmode: 'transparent'
					});
				} else {
					this.swf_container.empty();
					$('#' + this.config.swf_container).remove();

					var photo =  $('#' + this.config.photo_container_id + ' img');
					if (photo.length < 1) { 
						$('#' + this.config.photo_container_id).
							append(this.photo);
					}
					this.photo.show();
					this.photo.attr('src', obj_link.attr('href'));
				}
			
				this.photo_caption.text(obj_link.attr('title'));
				
				$('#' + this.config.thumbnail_container_id + ' .' + this.config.active_class).removeClass(this.config.active_class);
				$obj.addClass(this.config.active_class);
			}
		},
		
		createTabs: function () {
			if (this.product_content_container.length > 0) {
				var $tabs = $('> ul a', this.product_content_container);
				var disabled_tabs = new Array();
				var default_tab = 0;
				
				$tabs.each(function (i) {
					var $obj = $(this);
					var $panel = $($obj.attr('href'));
					
					if ($.trim($panel.html()) == '') {
						disabled_tabs.push(i);
					} else {
						if ($obj.hasClass('default')) {
							default_tab = i;
						}
					}
				});
			
				this.product_content_container.tabs({ selected: default_tab, disabled: disabled_tabs });
			}
		},
		
		createScroller: function () {
			this.scroller = $('#' + this.config.scroll_frame_id + ' li');
		
			if (this.scroller.length < 1) {
				this.scroller.hide();
			} else {
				$.getUniqueScript('/pages/script/jquery/jquery.plugin.jcarousel.js', function () {
					ProductHelper.config.scroller.size = ProductHelper.scroller.length;
					$('#' + ProductHelper.config.scroll_view_id).jcarousel(ProductHelper.config.scroller).css({ 'position': 'inherit' });
				});
			}
		}
	}

	$(document).ready(function () {
		ProductHelper.initialize();	
	});


}) (jQuery);

/* function postbuynowform(myform, windowname)
{
var buywin = window.open('', windowname, 'location,scrollbars,menubar,resizable,toolbar,width=782,height=400');
myform.target=windowname;
myform.submit();
} */
