/**
 * @author mike
 */
$(document).ready(function () {
	
	$('select#products_jump').change(function(){
		document.location = $(this).val();
	});
	
 	//$('#bfi0').addClass('active');
	var doU = function(u){
		u.find('a').animate({opacity: 1.0}, 500, function(){
			if (u = u.next()){
				doU(u);
			}
		})
	}
	
	$('#fw > div:first').addClass('on').siblings().addClass('off');
	$('#fw').find('div.fs ul li a').css({opacity: 0.0});
	doU($('#fw div.on div.fs ul li:first'));
	
	$('.fm img').addClass('active');
	var h = $('.fm img').attr('src');
	
	$('.fs a').each(function(){
		$(this).parents('div.fs').siblings('.fm')
			.append('<img id="b'+$(this).attr('id')+'" src="'+$(this).attr('href')+'" />');
	});
	
	$('#fw > div > div.fm img').not('.active').css({opacity: 0.0});
	
	$('div.lc').css({backgroundPosition: '60px bottom'});
	
	$('#fw > div > div.fs a').click(function(){
		if (h != $(this).attr('href')) {
			var x = $(this).siblings('.lc');
			h = $(this).attr('href');
			
			$('#fw > div > div.fs a').removeClass('active');
			$(this).addClass('active');
			
			//do transition
			var $active = $('#fw div.on .fm img.active');
			
			var $next = $('#b' + $(this).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);
			
			p = $(this).position();
			l = p.left + 30;
			x.css({
				backgroundPosition: l+'px bottom',
				opacity: 0.0
			}).show().animate({
				opacity: 1.0
			}, 1200);
		
		}else {
			
			$(this).siblings('.lc').css({
				opacity: 0.0
			}).show().animate({
				opacity: 1.0
			}, 600);
		}
		return false;
	}).mouseout(function(){
		$(this).siblings('.lc').stop().fadeOut('normal');
	}).mouseenter(function(){
		$(this).parents('ul').find('li div.lc').fadeOut('normal');
	});
	
	
	$('#fr li a').click(function(){
		var t = $(this).attr('id');
		var $target = $('#f'+t);
		if ($target.hasClass('off')){
			$target.find('div.fm img:first').each(function(){
				h = $(this).attr('src');
				$(this).addClass('active').css({opacity: 1.0});
				$(this).siblings().removeClass('active').css({opacity: 0.0});
			});
			$target.find('div.fs ul li a').css({opacity: 0.0}).show();
			$target.css({opacity: 0.0})
					.removeClass('off')
			        .addClass('on')
			        .animate({opacity: 1.0}, 1200, function(){
						doU($target.find('div.fs ul li:first'));
					});
			$target.siblings().find('div.fs ul li a').hide();
			$target.siblings().removeClass('on').animate({opacity: 0.0}, 1200, function(){
				$(this).addClass('off');
			});
		}else{
			$target.find('div.fm img:first').each(function(){
				h = $(this).attr('src');
				//$(this).siblings().removeClass('active').css({opacity: 0.0});
				$('#fw > div > div.fs a').removeClass('active');
				$('div.lc').hide('normal');
				//do transition
				var $active = $('#fw div.on .fm img.active');
				
				//var $next = $('#b' + $(this).attr('id'));
				var $next = $(this);
				
				$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;
	});
	
	/*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);
	}
 });