$(document).ready(function(){
	
	$('.text-editor h3.red,.text-editor h3.gray').each(function(){
		code = '<span>' + $(this).html() + '</span>';
		$(this).html(code);
		$(this).css('margin-left','-23px');	
	});

	$('.text-editor li').each(function(){
		code = '<span>' + $(this).html() + '</span>';
		$(this).html(code);
	});
	
	$('.text-editor table').each(function(){
		if ($(this).find('th').length == 0) {
			$(this).find('tr:eq(0) td').addClass('first-row');
		}
	});
	/*
	if ($('.images table').length) {		
		
		$('.top .logo-right').mouseover(function(){
			homeAnimDesign();
		});

		$('.top .logo-left').mouseover(function(){
			homeAnimProduction();
		});
		
		$('.gray-left').mousemove(function(e){ 
			x = (e.originalEvent.layerX || e.originalEvent.offsetX);
			y = (e.originalEvent.layerY || e.originalEvent.offsetY);
			lim_y = (-556/343 * x + 556);
			if (y < lim_y) {
				homeAnimProduction();
			}
		});

		$('.gray-right').mousemove(function(e){ 
			x = 259 - (e.originalEvent.layerX || e.originalEvent.offsetX);
			y = 381 - (e.originalEvent.layerY || e.originalEvent.offsetY);
			lim_y = (-556/343 * x + 410);
			if (y < lim_y) {
				homeAnimDesign();
			}
		});
		
		///$('.logo-wrp-right,.logo-wrp-left').click(function(){
		$('.logo-wrp-right,.logo-wrp-left').hover(function(){
			$(this).addClass('clckd');
			$('.logo-wrp-left,.logo-wrp-right').fadeOut(200);
			$('.top .logo').fadeIn(200,function(){
				if ($('.logo-wrp-left').hasClass('clckd')) {
					homeAnimProduction();
				} else {
					homeAnimDesign();
				}
			});
			return false;
		});
		
		$('.images.design-dub table').hover(function(){
			$('.images.design table').stop(true,false);
			$('.images.design-dub table').stop(true,false);
		},function(){
			homeSlidesGoOnDesign();
		});

		$('.images.production table').hover(function(){
			$('.images.production table').stop(true,false);
		},function(){
			homeSlidesGoOnProduction();
		//	homeSlidesNextDesign();
		});
				
		homeSlidesDuplicate();
		homeSlidesNextProduction();
		homeSlidesNextDesign();
		
		prts = document.location.href.split('#');
		if (prts.length > 1 && prts[1] == 'ofp') {
			$('.logo-wrp-left,.logo-wrp-right').hide();
			$('.top .logo').show();
			homeAnimDesign();
		} else if (prts.length > 1 && prts[1] == 'pp') {
			$('.logo-wrp-left,.logo-wrp-right').hide();
			$('.top .logo').show();
			homeAnimProduction();
		} else {
			window.choice = 'center';
		}
		
	} else {
	
		if ($('.container-line').hasClass('container-line-left')) {
			window.choice = 'design';
		} else {
			window.choice = 'production';
		}
		
		$('.top .logo-right,.top .logo-right-inactive').hover(function(){
			innerAnimDesign();
		});

		$('.top .logo-left,.top .logo-left-inactive').mouseover(function(){
			innerAnimProduction();
		});
		
		$('.top').hover(function(){},function(){
			if (!$('#container').hasClass('site-' + window.choice)) {
				if ($('#container').hasClass('site-production')) {
					if (window.choice == 'animation') {
						window.setTimeout("innerAnimProduction()",1000);
					} else {
						innerAnimProduction();
					}
				} else {
					if (window.choice == 'animation') {
						window.setTimeout("innerAnimDesign()",1000);
					} else {
						innerAnimDesign();
					}
				}
			}
		});
	}
	*/
	
	if ($('.b-main-screen-slides')) {
		
		$('.design-slides').hover(function(){
			$('.design-slides ul').stop(true,false);
		},function(){
			homeSlidesGoOnDesign();
		});

		$('.production-slides').hover(function(){
			$('.production-slides ul').stop(true,false);
		},function(){
			homeSlidesGoOnProduction();
		});

		$('.navigation-left').removeClass('cfix');
		//$('.navigation-left').css('max-width','622px');
		//$('.navigation-right').css('padding-top','0');
		//$('.navigation-right li').css('float','right');
		
		$('.logo,.navigation').fadeIn(200);
	
		if ($('.production-slides').length) {
			homeSlidesDuplicate();
			homeSlidesNextProduction();
			homeSlidesNextDesign();
		}
				
	}
	
	resizingThings();
	
	$(window).resize(function(){
		resizingThings();
	});
	
	if ($('.gallery li').length) {

		$(document).bind('keydown','esc',function (evt){
			$('.overlay,.close,.player').hide();
			$('.container-line').show();
			$('.container-line-dub').hide();
			return false; 
		});
		
		$('.overlay,.close a,.close-player a').live('click',function(){
			$('.overlay,.close,.player').hide();
			$('.container-line').show();
			$('.container-line-dub').hide();
			return false;
		});
		
		$('.see-video').click(function(){
			$('.player param[name="movie"]').attr('value','/player.swf?file=' + $(this).attr('longdesc'));
			$('.player embed').attr('src','/player.swf?file=' + $(this).attr('longdesc'));
			html = '<h2>' + $(this).attr('title') + '</h2>' + "\n";
			html += $(this).nextAll('.description:eq(0)').html();			
			
			$('.container-line').hide();
			$('.container-line-dub').show();

			$('.player .text-editor').html(html);
			
			p = $(this).parent().find('.download-link').text().split('(');
			size = '(' + p[1];
			$('.download-item a').attr('href',$(this).attr('longdesc')).html('<ins>Скачать</ins> ' + size);;
			$('.overlay,.close,.player').show();

			return false;
		});
		
		$('.see-photo').click(function(){
			prts = document.location.href.split('#');
			prts[1] = $(this).attr('href').replace('#','');
			document.location.href = prts.join('#');
			showreelInitSelection();
			showreelSwitchPic();
			$('.overlay,.close,.player').show();
			$('.container-line').hide();
			$('.container-line-dub').show();
			return false;
		});
		
		prts = document.location.href.split('#');
		if (prts.length > 1) {
			$('.gallery li a[href="#' + prts[1] + '"]:eq(0)').trigger('click');
		}
		
		$('.player .left-col img').live('click',function(){
			if ($('.demo-content .selected').parent().next('tr').length) {
				$('.demo-content .selected').parent().next('tr').find('a').trigger('click');
			} else {
				$('.demo-content:visible td:eq(0)').find('a').trigger('click');
			}
			return false;
		});
	}
	
	$('.content .logo-left-inactive').hover(function(){
		$(this).removeClass('logo-left-inactive').addClass('logo-left');
	},function(){
		$(this).addClass('logo-left-inactive').removeClass('logo-left');
	});
	
	$('.content .logo-right-inactive').hover(function(){
		$(this).removeClass('logo-right-inactive').addClass('logo-right');
	},function(){
		$(this).addClass('logo-right-inactive').removeClass('logo-right');
	});
	
	$('.demo-content td').hover(function(){
		$(this).addClass('hover');
	},function(){
		$(this).removeClass('hover');
	});
	
});

function resizingThings() {
	t = parseInt(($(window).height() - 700) / 2);
	if (t < 0) {
		t = 0;
	}
	$('.outer').css({paddingTop:t});//,200);

	t = parseInt(($(window).height() - 630) / 2);
	t += 25;
	if (t < 0) {
		t = 0;
	}
	$('.player').css({top:t});//,200);
}

window.showreelQty = new Array();
window.showreelThumbsHeight = new Array();
window.showreelFirstLoadFlag = 0;

function showreelThumbLoaded(gid,img) {
	if (typeof window.showreelQty[gid] == 'undefined') {
		window.showreelQty[gid] = 0;
		window.showreelThumbsHeight[gid] = -5;
	}
	if ($('.demo-content .id-' + gid).css('height') != null) {
		return false;
	}
	window.showreelQty[gid]++;
	window.showreelThumbsHeight[gid] += img.height + 22;
	if (window.showreelQty[gid] == $('.demo-content.id-' + gid + ' tr').length) {
		$('.demo-content.id-' + gid + ' table').css('height',window.showreelThumbsHeight[gid]);
		if ($('.demo-content.id-' + gid + ' table tr').length < 2) {
			return false;
		}
		$('.demo-content.id-' + gid + ' table').animate({marginLeft:0},200,function(){
			$('.player .arrow').css('visibility','visible');
			if ($(this).find('.selected').length) {
				showHideArrows();
			}
		});
		if (window.showreelFirstLoadFlag) {
			return false;
		}
		window.showreelFirstLoadFlag = 1;
		$('.arrow-top').click(function(){
			$('.arrow-bottom').css('visibility','visible');
			mt = parseInt($('.demo-content:visible table').css('margin-top').replace('px',''));
			mt += 150;
			if (mt > -1) {
				mt = 0;
				$('.arrow-top').css('visibility','hidden');
			}
			$('.demo-content:visible table').animate({marginTop: mt},200);
			return false;
		});
		$('.arrow-bottom').click(function(){
			$('.arrow-top').css('visibility','visible');
			mt = parseInt($('.demo-content:visible table').css('margin-top').replace('px',''));
			mt -= 150;
			limit = -1 * parseInt($('.demo-content:visible table').css('height').replace('px','')) + 428;
			if (mt < limit + 1) {
				mt = limit;
				$('.arrow-bottom').css('visibility','hidden');
			}
			$('.demo-content:visible table').animate({marginTop: mt},200);
			return false;
		});
		
		$('.demo-content table td a').click(function(){
			if ($(this).parent().hasClass('selected')) {
				return false;
			}
			$('.demo-content .selected').removeClass('selected');
			$(this).parent().addClass('selected');
			prts = document.location.href.split('#');
			prts[1] = $(this).attr('href').replace('#','');
			document.location.href = prts.join('#');
			showreelSwitchPic();
			return false;
		});
	}
}

function showreelInitSelection() {		
	$('.demo-content .selected').removeClass('selected');
	prts = document.location.href.split('#');
	if (prts.length) {
		num = parseInt(prts[1]);
		if (num > 0) {
			if ($('.demo-content table a[href^="#' + num + '"]').length) {
				$('.demo-content table a[href^="#' + num + '"]').parent().addClass('selected');
			} else {
				$('.demo-content td:eq(0)').addClass('selected');
			}
		} else {
			$('.demo-content td:eq(0)').addClass('selected');
		}
	} else {
		$('.demo-content td:eq(0)').addClass('selected');
	}
	$('.demo-content').hide();
	$('.demo-content .selected').parents('.demo-content').show();
}

function showHideArrows() {
	hide = 0;
	if ($('.demo-content .selected').parents('.demo-content').find('tr').length == 1) {
		hide = 1;
	}
	if (parseInt($('.demo-content .selected').parents('table:eq(0)').css('height').replace('px','')) < 429) {
		hide = 1;
	}
	if (hide) {
		$('.player .arrow').hide();
	} else {
		$('.player .arrow').show();
		$('.player .arrow').css('visibility','visible');
	}
}

function showreelSwitchPic() {
	showHideArrows();
	$('.player .left-col img').css('visibility','hidden');
	id = $.trim($('.demo-content .selected').parents('.demo-content').attr('class').replace('demo-content','').replace('id-',''));
	$lnk = $('.gallery .id-' + id + ' .see-photo:eq(0)');
	html = '<h2>' + $lnk.attr('title') + '</h2>' + "\n";
	html += $lnk.nextAll('.description:eq(0)').html();
	$('.player .text-editor').html(html);
	nsrc = $('.demo-content .selected img').attr('src').replace('80x0xIW','720x550xLH');
	$('.player .left-col img').fadeOut(200,function(){
		html = $('.player .left-col').html().replace($('.player .left-col img').attr('src'),nsrc);
		$('.player .left-col').html(html);
	});
	showreelShift();
}

function showreelShift() {
	if (isNaN(parseInt($('.demo-content .selected').parents('table:eq(0)').css('height').replace('px','').replace('auto','')))) {
		window.setTimeout('showreelShift()',200);
		return false;
	}
	if(parseInt($('.demo-content .selected').parents('table:eq(0)').css('height').replace('px','')) < 428) {
		return false;
	}
	window.showreelThumbsShift = 214 - parseInt($('.demo-content .selected img')[0].height / 2);
	$('.demo-content .selected').parent().prevAll('tr').each(function(){
		window.showreelThumbsShift -= $(this).find('img')[0].height + 22;
	});
	mt = window.showreelThumbsShift;
	if (mt > -1) {
		mt = 0;
		$('.arrow-top').css('visibility','hidden');
	}
	
	limit = -1 * parseInt($('.demo-content .selected').parents('table:eq(0)').css('height').replace('px','')) + 428;
	if (mt < limit + 1) {
		mt = limit;
		$('.arrow-bottom').css('visibility','hidden');
	}
	$('.demo-content .selected').parents('table:eq(0)').animate({marginTop: mt},200);	
}

function showreelPicLoaded() {
	h = $('.player .left-col img')[0].height;
	$('.player .left-col img').css({marginTop: (275 - h / 2)}).css('visibility','visible').fadeIn(400);
}

function shiftBgLine(mychoice,animation) {
	l = parseInt(($(window).width() - 940) / 2);
	if (mychoice == 'design') {
		l -= 180;
	}
	if (mychoice == 'production') {
		l += 20;
	}
	if (animation) {
		$('.container-line').animate({marginLeft:l},450);
	} else {
		$('.container-line').css({marginLeft:l});
	}
}

function homeSlidesDuplicate() {
	
	$('.b-main-screen-slides').each(function(){
		$(this).find('li').clone().insertAfter($(this).find('li:last'));
		$(this).find('li:last').addClass('last');
		$(this).find('ul').css('width',($(this).find('ul li').length * 125) + 'px');
	});
	
}

function homeSlidesNextProduction() {
	t = parseInt($('.production-slides ul').css('width').replace('px','') / 2);	
	time = t * 12.8;
	$('.production-slides ul').animate({left:'-' + t + 'px'},time,'linear',function(){
		$(this).css('left',0);
		homeSlidesNextProduction();
	});
}

function homeSlidesNextDesign() {
	t = parseInt($('.design-slides ul').css('width').replace('px','') / 2);	
	time = t * 12.8;
	$('.design-slides ul').animate({left:'-' + t + 'px'},time,'linear',function(){
		$(this).css('left',0);
		homeSlidesNextDesign();
	});
}

function homeSlidesGoOnProduction() {
	t = parseInt($('.production-slides ul').css('width').replace('px','') / 2);
	tt = t + parseInt($('.production-slides ul').css('left').replace('px',''));
	time = tt * 12.8;
	$('.production-slides ul').animate({left:'-' + t + 'px'},time,'linear',function(){
		$(this).css('left',0);
		homeSlidesNextProduction();
	});
	
}

function homeSlidesGoOnDesign() {
	t = parseInt($('.design-slides ul').css('width').replace('px','') / 2);
	tt = t + parseInt($('.design-slides ul').css('left').replace('px',''));
	time = tt * 12.8;
	$('.design-slides ul').animate({left:'-' + t + 'px'},time,'linear',function(){
		$(this).css('left',0);
		homeSlidesNextDesign();
	});
}

function homeAnimProduction() {
	if (window.choice == 'production' || window.choice == 'animation' ) {
		return false;
	}
	$('.top .logo:hidden').fadeIn(200);
	shiftBgLine('production',true);
	window.choice = 'animation';
	$('.navigation-right').fadeOut(100,function(){
		$('.container-line').animate({backgrounddPosition:'-135px 0'},350,function(){
			$('.navigation-left').fadeIn(100);
		});
	});
	$('.images.design-dub').hide();
	$('.images.design').fadeOut(50,function(){
		//$('.images table').css('left',0);
	});
	$('.title-design').fadeOut(50,function(){
		$('.gray-left').fadeOut(150);		
		$('a.production').fadeOut(100,function(){
			$('.content-line').animate({left:'589px'},400,function(){
				$('.gray-right').fadeIn(100);
				$('a.design').fadeIn(200,function(){
					$('.title-production,.images.production').fadeIn(250,function(){
						window.choice = 'production';
					});
				});
			});
		});
	});
	$('.logo-right').removeClass('logo-right').addClass('logo-right-inactive');
	$('.logo-left-inactive').removeClass('logo-left-inactive').addClass('logo-left');
}

function homeAnimDesign() {
	if (window.choice == 'design' || window.choice == 'animation') {
		return false;
	}
	$('.top .logo:hidden').fadeIn(200);
	shiftBgLine('design',true);
	window.choice = 'animation';
	$('.navigation-left').fadeOut(100,function(){
		$('.container-line').animate({backgrounddPosition:'-330px 0'},350,function(){
			$('.navigation-right').fadeIn(100);
		});
	});
	$('.images.production').fadeOut(50,function(){
		//$('.images table').css('left',0);
	});
	$('.title-production').fadeOut(50,function(){
		$('.gray-right').fadeOut(150);		
		$('a.design').fadeOut(100,function(){
			$('.content-line').animate({left:0},400,function(){
				$('.gray-left').fadeIn(150);
				$('a.production').fadeIn(200,function(){
					$('.images.design-dub').show();
					$('.title-design,.images.design').fadeIn(250,function(){
						window.choice = 'design';
					});
				});
			});
		});		
	});
	$('.logo-left').removeClass('logo-left').addClass('logo-left-inactive');
	$('.logo-right-inactive').removeClass('logo-right-inactive').addClass('logo-right');
}

function innerAnimProduction() {
	if (window.choice == 'production' || window.choice == 'animation' ) {
		return false;
	}
	$('.top .logo:hidden').fadeIn(200);
	shiftBgLine('production',true);
	window.choice = 'animation';
	$('.navigation-right').fadeOut(100,function(){
		$('.container-line').animate({backgrounddPosition:'-135px 0'},50,function(){
			$('.navigation-left').fadeIn(100);
			window.choice = 'production';
		});
	});
	$('.logo-left-inactive').removeClass('logo-left-inactive').addClass('logo-left');
	$('.logo-right').removeClass('logo-right').addClass('logo-right-inactive');
}

function innerAnimDesign() {
	if (window.choice == 'design' || window.choice == 'animation') {
		return false;
	}
	$('.top .logo:hidden').fadeIn(200);
	shiftBgLine('design',true);
	window.choice = 'animation';
	$('.navigation-left').fadeOut(100,function(){
		$('.container-line').animate({backgrounddPosition:'-330px 0'},50,function(){
			$('.navigation-right').fadeIn(100);
			window.choice = 'design';
		});
	});
	$('.logo-left').addClass('logo-left-inactive').removeClass('logo-left');
	$('.logo-right-inactive').addClass('logo-right').removeClass('logo-right-inactive');
}

function showreelThumbLoaded2($img) {
	
	h = $img[0].height;
	d = (100 - h) / 2;
	$img.hide().css('visibility','visible').css('margin-top',d + 'px').fadeIn(200);
	
}
