;(function($){
	/*  Variables  */
	var container = null;
	var allImgs = '', alllis = '', containerStr = '';
	
	var element = this;
	var _bgStretcherPause = false;
	var _bgStretcherTm = null;
	
	$.fn.bgStretcher = function(settings){
		settings = $.extend({}, $.fn.bgStretcher.defaults, settings);
		$.fn.bgStretcher.settings = settings;
		
		function _build(){
			if(!settings.images.length){ return; }
			
			_genHtml();
			
			containerStr = '#' + settings.imageContainer;
			container = $(containerStr);
			allImgs = '#' + settings.imageContainer + ' img';
			alllis = '#' + settings.imageContainer + ' li';
			
			$(alllis).hide();
			$(alllis + ':first').show().addClass('bgs-current');
			
			if(!container.length){ return; }
			$(window).resize(_resize);
			
			if(settings.slideShow && $(allImgs).length > 1){
				_bgStretcherTm = setTimeout('$.fn.bgStretcher.slideShow()', settings.nextSlideDelay);
			}
			_resize();
		};
		
		function _resize(){
			var winW = $(window).width();
			var winH = $(window).height();
			var imgW = 0, imgH = 0;

			//	Update container's height
			container.width(winW);
			container.height(winH);
			
			//	Non-proportional resize
			if(!settings.resizeProportionally){
				imgW = winW;
				imgH = winH;
			} else {
				var initW = settings.imageWidth, initH = settings.imageHeight;
				var ratio = initH / initW;
				
				imgW = winW;
				imgH = winW * ratio;
				
				if(imgH < winH){
					imgH = winH;
					imgW = imgH / ratio;
				}
			}
			
			//	Apply new size for images
			if(!settings.resizeAnimate){
				$(allImgs).width(imgW).height(imgH);
			} else {
				$(allImgs).animate({width: imgW, height: imgH}, 'normal');
			}
		};
		
		function _genHtml(){
			var code = '<div id="' + settings.imageContainer + '" class="bgstretcher"><ul>'; 

			//2012-02-15 jackie修改。
			var bgImages_sum = settings.images.length;
			var result = ''
			for(var j=0; j<bgImages_sum; j++)
			{
				var rand = Math.random();
				var temp_1 = rand * bgImages_sum;
				var temp_2 = Math.floor(temp_1);
				if(temp_2 < bgImages_sum)
				{
					result = temp_2;
					break;
				}
			}  
			if(typeof(settings.ids[result])!='undefined'){
				code += '<li id="'+settings.ids[result]+'"><img src="' + settings.images[result] + '" alt="" /></li>';
			}else{
				code += '<li><img src="' + settings.images[result] + '" alt="" /></li>';
			}
			//2012-02-15 jackie修改。

			/**	 2012-02-15 jackie修改。
			for(i = 0; i < settings.images.length; i++){
				if(typeof(settings.ids[i])!='undefined'){
					code += '<li id="'+settings.ids[i]+'"><img src="' + settings.images[i] + '" alt="" /></li>';
				}else{
					code += '<li><img src="' + settings.images[i] + '" alt="" /></li>';
				}
			}
			*/
			code += '</ul></div>';
			$(code).appendTo('body');
			if(typeof(settings.ids[0])!='undefined'){
				getProductRecord(settings.ids[0]);
			}
		};
		
		/*  Start bgStretcher  */
		_build();
	};
	
	$.fn.bgStretcher.play = function(){
       _bgStretcherPause = false;
       $.fn.bgStretcher._clearTimeout();
       $.fn.bgStretcher.slideShow();
       
	};
	
	$.fn.bgStretcher._clearTimeout = function(){
       if(_bgStretcherTm != null){
           clearTimeout(_bgStretcherTm);
           _bgStretcherTm = null;
       }
	}
	
	$.fn.bgStretcher.pause = function(){
	   _bgStretcherPause = true;
	   $.fn.bgStretcher._clearTimeout();
	};
	
	$.fn.bgStretcher.slideShow = function(){
		var current = $(containerStr + ' li.bgs-current');
		var next = current.next();

		if(!next.length){
			next = $(containerStr + ' li:first');
		}
		
		if(next.attr('id').length>0){
			getProductRecord(next.attr('id'));
		}
				
		$(containerStr + ' li').removeClass('bgs-current');
		next.addClass('bgs-current');
		
		next.fadeIn( $.fn.bgStretcher.settings.slideShowSpeed );
		
		current.fadeOut( $.fn.bgStretcher.settings.slideShowSpeed );
		
		if(!_bgStretcherPause){
		  _bgStretcherTm = setTimeout('$.fn.bgStretcher.slideShow()', $.fn.bgStretcher.settings.nextSlideDelay);
		}
	};
	
	/*  Default Settings  */
	$.fn.bgStretcher.defaults = {
		imageContainer:             'bgstretcher',
		resizeProportionally:       true,
		resizeAnimate:              false,
		images:                     [],
		ids:                     [],
		imageWidth:                 1440,
		imageHeight:                768,
		nextSlideDelay:             5000,
		slideShowSpeed:             'normal',
		slideShow:                  true
	};
	$.fn.bgStretcher.settings = {};
})(jQuery);

function getProductRecord(id){
	url="index.php?app=product&act=getProRecord&id="+id;
	jQuery.get(url,'',function(data){
		var rs=$.evalJSON(data);
		$("#product_texture").html(rs['product_texture']);
		$("#product_price").html(rs['product_price']);
		$("#product_specification").html(rs['product_specification']);
		$("#orderLink").attr("href","index.php?app=product&act=order&id="+rs['id']);
		//alert(data);
	});
}


/******导航*******/
var DDSPEED = 10;
var DDTIMER = 10;

// main function to handle the mouse events //
function ddMenu(id,d){
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  clearInterval(c.timer);
  if(d == 1){
    clearTimeout(h.timer);
    if(c.maxh && c.maxh <= c.offsetHeight){return}
    else if(!c.maxh){
      c.style.top = '-' + c.offsetHeight + 'px';
      c.style.display = 'block';
      c.style.height = 'auto';
      c.maxh = c.offsetHeight;
      c.style.height = '0px';
    }
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }else{
    h.timer = setTimeout(function(){ddCollapse(c)},10);
  }
}

// collapse the menu //
function ddCollapse(c){
  c.timer = setInterval(function(){ddSlide(c,-1)},DDTIMER);
}

// cancel the collapse if a user rolls over the dropdown cent //
function cancelHide(id){
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  clearTimeout(h.timer);
  clearInterval(c.timer);
  if(c.offsetHeight < c.maxh){
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }
}

// incrementally expand/contract the dropdown and change the opacity //
function ddSlide(c,d){
  var currh = c.offsetHeight;
  var dist;
  if(d == 1){
    dist = (Math.round((c.maxh - currh) / DDSPEED));
  }else{
    dist = (Math.round(currh / DDSPEED));
  }
  if(dist <= 1 && d == 1){
    dist = 1;
  }
  c.style.top = parseInt(c.style.top.replace('px','')) - parseInt(dist * d) + 'px';
  c.style.height = currh + (dist * d) + 'px';
  c.style.opacity = currh / c.maxh;
  c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')';
  if((currh < 2 && d != 1) || (currh > (c.maxh - 2) && d == 1)){
    clearInterval(c.timer);
  }
}

