/**
 * @author mike
 */
$(document).ready(function () {
	
 	var doU = function(u){
		u.find('a').animate({opacity: 1.0}, 500, function(){
			if (u = u.next()){
				doU(u);
			}
		})
	}
	
	$('.fm img:first').addClass('active');
	var h = $('.fm img').attr('src');
	
	
	if($('.fm img').length){
		
		var fmRotate = function (){
				//$el = $('.fm image.active').next();
				//$('.fm img.active').removeClass('active');
				//$el.addClass('active');
				//do transition
				var $active = $('.fm img.active');
				
				var $next = $('.fm img.active').next();
				if (!$next.length){
					$next = $('.fm img:first');
				}
				$active.addClass('last-active');
				
				$next.css({
					opacity: 0.0
				}).addClass('active').animate({
					opacity: 1.0
				}, 1200);
				$active.removeClass('active last-active').animate({
					opacity: 0.0
				}, 1200);
				
			return false;
		};
		
		var ssInt = null;
		var ssTo = null;
		
		fmStopShow = function(){
			clearInterval(ssInt);
			clearTimeout(ssTo);
			ssTo = null;
			ssInt = null;
		}
		fmStartShow = function(l){
			fmStopShow();//clear timers
			if (l == 0) {
				fmSlideShow();
				ssInt = setInterval('fmSlideShow()', 5000);
			}else{
				ssTo = setTimeout('fmStartShow(0)',l);
			}
		}
		fmSlideShow = function(){
			fmRotate();
		}
		
		fmStartShow(5000);
	}
	
	$('#fw > div > div.fm img').not('.active').css({opacity: 0.0});
	
	
	/*product gallery*/
	if($('#pgar').length){
		var gh = $('#pgi img').attr('src');
		$('#pgl div.pg:first').addClass('active');
		$('#pgl div.pg.active ul li a.pgi:first').addClass('active');
		
		$('#pgl div.pg ul li a.pgi').each(function(){
			$('#pgi').append('<img id="b'+$(this).attr('id')+'" src="'+$(this).attr('href')+'" />');
		});
		$('#pgi img:first').addClass('active');
		$('#pgi img').not('.active').css({opacity: 0.0});
		
		$('#pgl div ul li a.pgi').click(function(){
			return pgRotate(this);
		});
		
		
		var pgNext = function(){
			el = $('#pgl div.pg.active ul li a.pgi.active').parent().next().find('a.pgi');
			el = (el && el.length) ?  el : $('#pgl div.pg.active ul li:first a.pgi');
			return pgRotate(el);
		}
		
		$('#pgan').click(function(){
			pgNext();
		});
		$('#pgap').click(function(){
			el = $('#pgl div.pg.active ul li a.pgi.active').parent().prev().find('a.pgi');
			el = (el && el.length) ?  el : $('#pgl div.pg.active ul li:last a.pgi');
			return pgRotate(el);
		});
		
		$('#pgg').change(function(){
			var pgg = $(this).val();
			var pggOld = $('#pgl div.pg.active');
			if ('pg' + pgg != pggOld.attr('id')) {
				stopShow();
				var pggNew = $('#pg'+pgg);
				pggOld.fadeOut('slow',function(){
					$(this).removeClass('active');
				});
				pggNew.hide().addClass('active').fadeIn('slow');
				pgRotate($('#pgl div#pg'+pgg+' ul li:first a.pgi'));
				if ($('#pgar').hasClass('run')) {
					startShow(5000);
				}
			}
		});
		
		var pgRotate = function (el){
			if (gh != $(el).attr('href')) {
				$('#pgl div ul li a.pgi').removeClass('active');
				$(el).addClass('active');
				gh = $(el).attr('href');
				var tx = $(el).next().html();
				//do transition
				var $active = $('#pgi img.active');
				
				var $next = $('#b' + $(el).attr('id'));
				
				$active.addClass('last-active');
				
				$next.css({
					opacity: 0.0
				}).addClass('active').animate({
					opacity: 1.0
				}, 1200);
				$active.removeClass('active last-active').animate({
					opacity: 0.0
				}, 1200);
				$('#pgh1').fadeOut('fast', function(){
					$(this).html(tx).fadeIn('slow');
				});
			}
			return false;
		};
		
		
		$('#pgl').mouseenter(function(){
			$('#pgar').addClass('pause');
		}).mouseleave(function(){
			$('#pgar').removeClass('pause');
			startShow(3000);
		});
		$('#pgh').mouseenter(function(){
			$('#pgar').addClass('pause');
		}).mouseleave(function(){
			$('#pgar').removeClass('pause');
		});
		
		$('#pgar').toggle(function(){
			$(this).removeClass('run');
			stopShow();
		}, function(){
			$(this).addClass('run');
			$('#pgar').removeClass('pause');
			startShow(500);
		});
		
		$('#pga a').mouseenter(function(){
			$('#pgar').addClass('pause');
		}).mouseleave(function(){
			if ($('#pgar').hasClass('run') && $('#pgar').hasClass('pause')) {
				startShow(3000);
			}
			$('#pgar').removeClass('pause');
			
		});
		
		var ssInt = null;
		var ssTo = null;
		
		stopShow = function(){
			clearInterval(ssInt);
			clearTimeout(ssTo);
			ssTo = null;
			ssInt = null;
		}
		startShow = function(l){
			stopShow();//clear timers
			if (l == 0) {
				slideShow();
				ssInt = setInterval('slideShow()', 5000);
			}else{
				ssTo = setTimeout('startShow(0)',l);
			}
		}
		slideShow = function(){
			if ($('#pgar').hasClass('run') && !$('#pgar').hasClass('pause')){
				pgNext();
			}
		}

		var isIE = (navigator.appName == "Microsoft Internet Explorer");
		if (isIE) {
			document.onfocusout = function(){
				stopShow();
				$('#pgar').addClass('pause');
			}
			document.onfocusin = function(){
				if (!ssTo && !ssInt && $('#pgar').hasClass('run')) {
					startShow(3000);
				}
				$('#pgar').removeClass('pause')
			}
		}else{
			$(window).focus(function(){
				if (!ssTo && !ssInt && $('#pgar').hasClass('run')) {
					startShow(3000);
				}
				$('#pgar').removeClass('pause')
			}).blur(function(){
				stopShow();
				$('#pgar').addClass('pause')
			});
		}
		
		startShow(5000);
	}
 });