(function ($) {
	var ModelSelector = { // setup local and global aliases for ModelSelector
		active: false,
		init: function (data) {
			this.data = data;
			this.prepDom();
			this.behavior();
			$('#mCarousel').show();
		},
		prepDom: function() {
			if ($.ifixpng) {
				var msImgHeight = $('.msModelImg img').css('height');
				var msImgWidth = $('.msModelImg img').css('width');
				$('div.msModelImg img[src$=.png]').ifixpng().css({height:msImgHeight.replace('px',''),width:msImgWidth.replace('px','')});
			};
			if(this.data.imgType === 'swf'){
				var height = this.data.swfHeight;
				var width = this.data.swfWidth;
				this.swfInit(height, width);
			} 

		},
		behavior: function() {
			var viewLimit;
			var nextButton = this.data.buttonImages.buttonNextHtml;
			var prevButton = this.data.buttonImages.buttonPrevHtml;
			if(this.data.viewLimit === '' || isNaN(this.data.viewLimit) ){
			   viewLimit = 9;
		   }else{
			   viewLimit = this.data.viewLimit
		   }
		//console.log(viewLimit)
		   $('#mCarousel').jcarousel({
				orientation: 'horizontal',
				itemStart: 1,
				itemVisible: viewLimit,
				itemScroll: 1,
				itemWidth: null,
				itemHeight: null,
				scrollAnimation: 1000,
			    autoScroll: 5,
				autoScrollStopOnMouseover: true,
				autoScrollResumeOnMouseout: true,
				wrap: true,
				buttonNextHTML: nextButton,
				buttonPrevHTML: prevButton
			});
			$('#mCarousel dl a').click(function() { window.location = $(this).attr("href"); });
			$('#mCarousel dl').hide().click(function () {return false; });
			$('#mCarousel li').click(function() {
				$(this).siblings('.open').children('dl').slideUp(400, function() {return false; }, 'easeinout');
				$(this).siblings().removeClass('open').end().addClass('open');
				$('dl:hidden',this).slideDown(400, function() { return false; }, 'easeinout');
			});

			$('dd.closeModelOptions').click(function() {
				$(this).parent().slideUp(400,function() {return false; }, 'easeinout');
				$(this).parent().parent().removeClass('open');
			});
			
		},
		swfInit: function(swfArray,height, width){ // may allow hieght and width to be set at some point via prefs
			var i;
			height = (height === undefined || height == '') ? 101 : height;
			width = (width === undefined || width == '')? 175 : width;
			for(i=0; i < swfArray.length; i++){
				$('.msModelImg img').each(function(){
					if($(this).attr('title') === swfArray[i].title){
						$(this).parent().attr('id',swfArray[i].id);
					}
				});
				var hso = new SWFObject(swfArray[i].location,i,width,height,"6,0,65,0","");
				hso.addParam("wmode","transparent");
				hso.addParam("scale","noborder");
				hso.write(swfArray[i].id);
			}

		}
	};
	window.DDC.ModelSelector = ModelSelector;
})(jQuery);





