var cardImagesCount = 0;
var cardImages = ['shooter', 'pro-shooter', 'vip-shooter'];

var winWidth;
var winHeight;
var conHeight;
var curPhoto = 1;

var shotImagesCount = 0;
var shotCounter = -1;
var shotTotal = 0;
var shotTotals = [30, 21, 20, 26, 27, 26, 26];
var shotImages = new Array();
var shotAnimFlag = true;
var shotLoadFlag = true;
var shotPath;
var shotLoaded = false;

$(document).ready(function() {
	ie6Fix();
	ie6MinMaxW();

	inputHelper($('.search-text').addClass('empty'), 'поиск');
	inputHelper($('.sub-text').addClass('empty'), 'Эл.почта');

	if ($('.card').length > 0) {
		var image;
		for (i=0; i<cardImages.length; i++) {
		/*	image = document.getElementById(cardImages[i]);
			image.onload = function () {
				cardImagesCount++;
				checkCardImages();
			};
			image.src = image.src;*/
			image = new Image();
			image.onload = function () {
				cardImagesCount++;
				checkCardImages();
			};
			image.src = $('#'+cardImages[i]).attr('src');
		}
		
	}

	if ($('.popup').length > 0) {
		$('li.rel').click(function() {
			if ($(this).find('.popup').length > 0) {
				$(this).find('.popup').show();
			}
			return false;
		});
		$('body').click(function(){
			$('.popup').hide();
		});
		$('.popup').click(function(e) {
			e.stopPropagation();
		});
	}

	if ($('.popup-form').length > 0) {
		$('.popup-form-show').click(function() {
			var formId = ($(this).attr('id').substr(6));
			$('#'+formId).removeClass('popup-form-sent').show();
			return false;
		});
		$('.popup-form-hide').click(function() {
			$(this).parents('.popup-form').hide();
			return false;
		});
		$('body').click(function() {
			$('.popup-form').hide();
		});
		$('.popup-form').click(function(e) {
			e.stopPropagation();
		});

//		$('#form-feedback .submit input').click(function() {
//			$(this).parents('.popup-form').fadeOut(1000, function() {
//				$(this).addClass('popup-form-sent').fadeIn(1000);
//			});
//			return false;
//		});
	}

	if ($('.photo').length > 0) {
		$('body').append('<div class="overlay"></div><div class="gallery-container"><div class="gallery"><ul></ul></div></div>');
		winWidth = $(window).width();
		winHeight = $(window).height();
		conHeight = $('.container').height();
		if ($.browser.safari) {
			//conHeight += 220;
			//if (winHeight < conHeight) 
			winWidth -= 16;
		}
		$('.overlay').css({
			display:			'none',
			position:			'absolute',
			zIndex:				'99',
			left:				0,
			top:				0,
			backgroundColor:	'#000',
			opacity:			0.8,
			width:				winWidth,
			height:				conHeight
		});
		var conTop = parseInt(parseInt(winHeight - 600) / 2);
		if (conTop < 0) conTop = 0;
		$('.gallery-container').css({
			display:			'none',
			overflow:			'hidden',
			position:			'absolute',
			zIndex:				'100',
			left:				0,
			top:				conTop+'px',
			width:				winWidth+'px',
			height:				600
		});
		$('.gallery').css({
			position:			'absolute',
			zIndex:				'101',
			left:				0,
			top:				0,
			width:				99999
		});
		$('.gallery ul').css({
			listStyle:			'none',
			zoom:				1
		});

		var i = 0;
		$('.photo a').each(function() {
			$(this).attr({id: 'a'+(++i)});
			var item = $('<li>')
				.attr({
					id:					'li'+i
				})
				.css({
					float:				'left',
					zoom:				1,
					marginLeft:			25,
					marginRight:		25,
					width:				800,
					height:				600,
					backgroundColor:	'#fff',
					backgroundImage:	'url("img/loading.gif")',
					backgroundPosition:	'50% 50%',
					backgroundRepeat:	'no-repeat'
				})
				.click(function(e) {
					e.stopPropagation();
					curPhoto = $(this).attr('id');
					$('.gallery').animate({ left: (getPosById($(this).attr('id')))+'px' });
				});
			$('.gallery ul').append(item);

			var image = new Image();
			image.id = 'i'+i;
			image.onload = function () {
				if (parseInt(this.height) > $('.gallery-container').height()) {
					var conTop = parseInt(parseInt(winHeight - this.height) / 2);
					if (conTop < 0) conTop = 0;
					$('.gallery-container').css({
						height:			parseInt(this.height)+'px',
						top:			conTop+'px'
					});
				}
				$('#l'+this.id)
				.append(
					$('<img>')
					.css({
						display:	'none',
						cursor:		'pointer'
					})
					.attr({
						src:		this.src,
						width:		this.width,
						height:		this.height,
						iewidth:	this.width
					})
				)
				.animate({
					width:	this.width+'px',
					height:	this.height+'px'
				}, function() {
					$(this).css({background: 'none'}).find('img').show();
				});
			};
			image.src = $(this).attr('href');
		});
		$('.overlay').click(function() { $('.gallery-container').hide(); $(this).fadeOut(); });
		$('.gallery-container').click(function() { $(this).hide(); $('.overlay').fadeOut(); });
		$('.photo a').click(function() {
			resetOverlay();
			var id = 'li'+$(this).attr('id').substr(1);
			curPhoto = id;
			scroll(0,0);
			$('.overlay').fadeIn(500, function() {
				$('.gallery').css({ left: (getPosById(id))+'px'});
				$('.gallery-container').show();
				$('.gallery').css({ left: (getPosById(id))+'px'});
			});

			return false;
		});
	}

	if ($('.shot-list').length > 0) {
		//$('.container').prepend($('<div>').addClass('debug'));///////
		/*$('.shot-list').append(
			$('<div>')
			.css({width: '100%', height: '620px', position: 'absolute', zIndex: 3, left: '0', top: '80px'})
			.append('<p id="loading"></p>')
		);*/
		/*var loader = spriteAnimation(document.getElementById('loading'), 48, 576, 'backgroundPositionX');
		loader.frameDelay(50);
		loader.start();*/
		var shotRandom = Math.floor(Math.random()*shotTotals.length);
		shotTotal = shotTotals[shotRandom];
		shotPath = 'shots/'+( ((shotRandom+1) < 10)?('0'+(shotRandom+1)):(shotRandom+1) );
		//$('.shot-list').css({background: 'url("'+shotPath+'/'+'00.jpg") 50% 0 no-repeat'});
		$('.shot-list').append(
			$('<div>')
			.attr({id: 'shot0'})
			.css({width: '100%', height: '700px', position: 'absolute', zIndex: 1, left: '0', top: '0', background: 'url("'+shotPath+'/'+'00.jpg") 50% 0 no-repeat'})
		);
		loadShotImage();

		$('.but-pause').click(function() {
			$('#loading').toggle()
			if (shotLoaded) {
				if (shotAnimFlag) {
					shotAnimFlag = false;
					$(this).find('u').html('Начать проигрывание');
					$(this).find('div').addClass('play');
				} else {
					shotAnimFlag = true;
					shotAnimation();
					$(this).find('u').html('Остановить проигрывание');
					$(this).find('div').removeClass('play');
				}
			} else {
				if (shotLoadFlag) {
					shotLoadFlag = false;
					shotAnimFlag = false;
					shotImages = new Array();
					shotImagesCount = 0;
					$(this).find('u').html('Начать загрузку ролика');
					$(this).find('div').addClass('play');
				} else {
					shotLoadFlag = true;
					shotAnimFlag = true;
					$('.shot-list').empty();
					/*.append(
						$('<div>')
						.css({width: '100%', height: '620px', position: 'absolute', zIndex: 3, left: '0', top: '80px'})
						.append('<p id="loading"></p>')
					);*/
					/*var loader = spriteAnimation(document.getElementById('loading'), 48, 576, 'backgroundPositionX');
					loader.frameDelay(50);
					loader.start();*/
					loadShotImage();
					$(this).find('u').html('Остановить загрузку ролика');
					$(this).find('div').removeClass('play');
				}

			}
			return false;
		});
	}
	
	if ($('.contact-lnk').length > 0) {
		$('.contact-lnk').hover(function(){
			$('.map-sm a').addClass('hover');
		}, function() {
			$('.map-sm a').removeClass('hover');
		});
		$('.map-sm a').hover(function(){
			$('.contact-lnk').addClass('contact-lnk-hover');
		}, function() {
			$('.contact-lnk').removeClass('contact-lnk-hover');
		});
	}

	if ($('.iphone-bg').length > 0) {
		iphoneWidth = $('.iphone-pic').width();
		$('.iphone-loading').hide();
		$('.iphone-pic').append($('<div>').addClass('iphone-pic-slideshow'));

		$('.iphone-bg .dots a').each(function() {
			totalSlides++;
			$('.iphone-pic-slideshow').append(
				$('<div>')
					.attr({id: 'idiv-'+totalSlides})
					.addClass('iphone-pic-slide')
					.css({ backgroundImage: 'url("img/loading.gif")' })
			);
			$(this).attr({ id: 'ilink-'+totalSlides });
			var im = new Image();
			im.id = 'ipic-'+totalSlides;
			im.onload = function() {
				$('#idiv-'+this.id.substr(5)).css({ backgroundImage: 'url("'+this.src+'")' });
			}
			im.src = $(this).attr('href');
		});
		$('.iphone-bg .dots #ilink-'+iphoneSlide).parents('li').addClass('sel');

		$('.iphone-pic-slideshow').css({ width: (totalSlides*iphoneWidth)+'px' });

		$('.iphone-bg .dots a').click(function() {
			var slideId = $(this).attr('id').substr(6);
			if (slideId != iphoneSlide) {
				if (!iphoneAnim) iphoneAnimate((slideId > iphoneSlide)?-1:1, slideId, 300);
			}
			return false;
		});
		$('.iphone-pic-slide').click(function() {
			if (!iphoneAnim) iphoneAnimate((iphoneSlide == totalSlides)?1:-1, ((iphoneSlide == totalSlides)?1:iphoneSlide+1), (iphoneSlide == totalSlides)?100:300);
		});


	}
});
var iphoneWidth = 0;
var iphoneSlide = 1, totalSlides = 0;
var iphoneAnim = false;

function iphoneAnimate(dir, id, speed) {
	iphoneAnim = true;
	if (iphoneSlide == id) {
		iphoneAnim = false;
	} else {
		if (iphoneSlide == 1 && dir == 1) return;
		if (iphoneSlide == totalSlides && dir == -1) return;
		var curMargin = ((-1)*(iphoneSlide-1))*iphoneWidth;
		$('.iphone-pic-slideshow').animate({ marginLeft: (curMargin+(iphoneWidth*dir))+'px' },
			speed,
			function() {
			$('.iphone-bg .dots li').removeClass('sel');
			iphoneSlide = iphoneSlide-dir;
			$('.iphone-bg .dots #ilink-'+iphoneSlide).parents('li').addClass('sel');
			iphoneAnimate(dir, id, speed);
		});
	}
}



function loadShotImage() {
	var i = shotImagesCount;
	shotImages[i] = new Image();
	shotImages[i].onload = function () {
		checkShotImages();
	};
	shotImages[i].src = shotPath+'/'+((i < 10)?('0'+i):i)+'.jpg';
}
function checkShotImages() {
	//$('.debug').append('<span>'+shotImages[shotImagesCount].src+' ; </span>');///////
	if (shotLoadFlag) {
		shotImagesCount++;
		if (shotImagesCount == shotTotal) {
			shotLoaded = true;
			$('.shot-list').empty();
			$('.shot-list').append(
				$('<div>').attr({id: 'shot0'}).css({width: '100%', height: '700px', position: 'absolute', zIndex: 1, left: '0', top: '0', background: 'url("'+shotImages[0].src+'") 50% 0 no-repeat'})
			);
			if (shotAnimFlag) {
				$('.but-pause u').html('Остановить проигрывание');
				shotAnimation();
			} else {
				$('.but-pause u').html('Начать проигрывание');
			}
		} else {
			if (shotImagesCount >= 3) $('.shot-list #shot'+(shotImagesCount-3)).hide().remove();
			if (shotImagesCount >= 2) {
				$('.shot-list #shot'+(shotImagesCount-2)).css({zIndex: 2});
				$('.shot-list #shot'+(shotImagesCount-2)).fadeOut(500);
				$('.shot-list').append(
					$('<div>')
					.attr({id: 'shot'+(shotImagesCount-1)})
					.css({width: '100%', height: '700px', position: 'absolute', zIndex: 1, left: '0', top: '0', background: 'url("'+shotImages[shotImagesCount-1].src+'") 50% 0 no-repeat'})
				);
			}
			setTimeout('loadShotImage()', 500);
		}
	}
}
function shotAnimation() {
	if (shotAnimFlag) {
		var prevShot = shotCounter;
		if (prevShot >= 0) $('#shot'+prevShot).css({zIndex: 1});

		shotCounter++;

		if (shotCounter >= shotTotal) shotCounter = 0;

		var curShot = shotCounter;
		var nextShot = shotCounter+1;

		if (nextShot >= shotTotal) nextShot = 0;

		$('.shot-list #shot'+curShot).css({zIndex: 2});

		$('.shot-list #shot'+prevShot).hide().remove();

		$('.shot-list #shot'+curShot).fadeOut(500);
		$('.shot-list').append(
			$('<div>')
			.attr({id: 'shot'+nextShot})
			.css({width: '100%', height: '700px', position: 'absolute', zIndex: 1, left: '0', top: '0', background: 'url("'+shotImages[nextShot].src+'") 50% 0 no-repeat'})
		);

		setTimeout('shotAnimation()', 800);
	}
}

function checkCardImages() {
	if (cardImagesCount == 3) {
		$('#'+cardImages[0]).parents('.card-img').animate({left: (parseInt((($('.card').width())/100)*46)-242)+'px'}, function() {
			$('#'+cardImages[1]).parents('.card-img').animate({left: (parseInt((($('.card').width())/100)*46)-242)+'px'}, function() {
				$('#'+cardImages[2]).parents('.card-img').animate({left: (parseInt((($('.card').width())/100)*46)-242)+'px'});
				$('#'+cardImages[2]).parents('.card-img').find('.card-sh').fadeOut(1000);
			});
			$('#'+cardImages[1]).parents('.card-img').find('.card-sh').fadeOut(1000);
		});
		$('#'+cardImages[0]).parents('.card-img').find('.card-sh').fadeOut(1000);
	}
}


$(window).bind('resize', function() {
	ie6Fix();

	if ($('.card').length > 0) {
		$('.card img').css({left: (parseInt((($('.card').width())/100)*46)-242)+'px'});
	}

	resetOverlay();
});

function resetOverlay() {
	if ($('.photo').length > 0) {
		winWidth = $(window).width();
		winHeight = $(window).height();
		conHeight = $('.container').height();
		if ($.browser.safari) {
			//conHeight += 220;
			//if (winHeight < conHeight) 
			winWidth -= 16;
		}
		if ($('.gallery').height() > conHeight) conHeight = $('.gallery').height();
		$('.overlay').css({
			width:				winWidth,
			height:				conHeight
		});
		var conTop = parseInt(parseInt(winHeight - 600) / 2);
		if (conTop < 0) conTop = 0;
		$('.gallery-container').css({
			top:				conTop+'px',
			width:				winWidth+'px'
		});
		$('.gallery').css({ left: (getPosById(curPhoto))+'px'});
	}
}

function getPosById(id) {
	var left = 0, total = 0;
	$('.gallery li').each(function() {
		if ($(this).attr('id') == id) { left = total; return true; }
		var imW = parseInt($(this).find('img').attr('width'));
		if (isNaN(imW)) parseInt($(this).find('img').attr('iewidth'));
		if (isNaN(imW)) imW = 800;
		total += imW + 50;
	});
	var imW = parseInt($('#'+id).find('img').attr('width'));
	if (isNaN(imW)) parseInt($('#'+id).find('img').attr('iewidth'));
	if (isNaN(imW)) imW = 800;
	return parseInt((winWidth - imW + 50) / 2) - left;
}

var ie6Timer;
function ie6Fix() {
	if ($.browser.msie && $.browser.version < 7) {
		$('.sidebar').hide().show();
		clearTimeout(ie6Timer);
		ie6Timer = setTimeout('ie6MinMaxW()', 100);
	}
	if ($.browser.msie && $.browser.version == 7) {
		$('.price-table').css({ width: ($('.container').width()*65/100)+'px' });
		$('.table-head').css({ width: 'auto' });
		$('.table-row').css({ width: 'auto' });
		$('.table-head').css({ width: $('.price-table').width()+'px' });
		$('.table-row').css({ width: $('.price-table').width()+'px' });
		
		$('.list').css({ width: ($('.container').width()*65/100)+'px' });
		$('.list-head').css({ width: 'auto' });
		$('.list-row').css({ width: 'auto' });
		$('.list-head').css({ width: $('.list').width()+'px' });
		$('.list-row').css({ width: $('.list').width()+'px' });
		
		$('.video-block').css({ width: ($('.container').width()*65/100)+'px' });
		$('.video-block span').css({ width: 'auto' });
		$('.video-block ul').css({ width: 'auto' });
		$('.video-block span').css({ width: parseInt($('.video-block').width()/10)+'px' });
		$('.video-block ul').css({ width: parseInt(($('.video-block').width()/10)*9)+'px' });
		
	}
}
function ie6MinMaxW() {
	if ($.browser.msie && $.browser.version < 7)  {
		w = (($(window).width()<988)?'988px':($(window).width()>1450?'1450px':"100%"));
		$('.container').css({width: w});
		$('.footer').css({width: w});
	}
}

function inputIn(obj, text) {
   if ($(obj).val() == text) {
      $(obj).val('');
      $(obj).removeClass('empty');
   }
}
function inputOut (obj, text) {
   if ($(obj).val() == '' || $(obj).val() == text) {
      $(obj).val(text);
      $(obj).addClass('empty');
   }
}
function inputHelper(obj, text) {
   $(obj)
      .bind ('focus', function () {
         inputIn (this, text);
      })
      .bind ('blur', function () {
         inputOut (this, text);
      });
	inputOut(obj, text);
}