$(document).ready(function(){
		
	 $(".colorbox").colorbox({
			transition:"elastic",
			current: "Screenshot {current} of {total}"
	});
	
	if (!$('#video-head').length) {
		$('#screen_prev').cycle();
	}
	
	$('#video-select,#screenshots-select,#close-video').mouseover(function() {
		$(this).addClass('hover');
	});
	$('#video-select,#screenshots-select,#close-video').mouseout(function() {
		$(this).removeClass('hover');
	});
	$('#video-select').click(function(){
			if (!$('#video-head').hasClass('video')) {
				$('#video-head').addClass('video');
				$('#video_center').show();
				$('#screenshots_center').hide();
				$('#screenshots_text').text('Click on the image to view video');
			}
	});
	$('#screenshots-select').click(function(){
			if ($('#video-head').hasClass('video')) {
				$('#video-head').removeClass('video');
				$('#video_center').hide();
				$('#screenshots_center').show();
				$('#screen_prev').cycle();
				$('#screenshots_text').text('Click on the image to zoom in');
			}
	});
				
	$('.product_award').cycle({
		timeout: 7000
	});

	$("#btn-video").click(function(){
		var swf = this.href;
		openVideo(swf);
		return false;
	});
	
	$("#close-video").click(function(){
		closeVideo();
	});
	
	$('#V_cont').click(function(){
		return false;
	});
	
	$('#V_bg').click(function(){
		closeVideo();
	});
		$window = $(window);
		isIE = !$.support.opacity;
		$overlay = $('#V_overlay');
		$bg = $('#V_bg');
		isIE6 = isIE && !window.XMLHttpRequest;
});

var $window,
	isIE,
	$overlay,
	$bg,
	isIE6;
		
function openVideo(flv) {
		
		$overlay.show();
		$bg.show();
		if (isIE6) {
			$overlay.css({width: $window.width(), height: $window.height(), top: $window.scrollTop(), left: $window.scrollLeft()});
			$bg.css({width: $window.width(), height: $window.height(), top: $window.scrollTop(), left: $window.scrollLeft()});
			
			$window.bind('scroll', function () {
					$overlay.css({width: $window.width(), height: $window.height(), top: $window.scrollTop(), left: $window.scrollLeft()});
					$bg.css({width: $window.width(), height: $window.height(), top: $window.scrollTop(), left: $window.scrollLeft()});
				});
		}
		$(document).bind("keydown", function (e) {
				if (e.keyCode === 27) {
					e.preventDefault();
					closeVideo();
				}
			})
		$bg.show();
		
		if ($("#flash-custom").length) {
			$("#flash-custom div").hide();	
		}
		
		$('#V_cont div.cont').flash(
		  { 
		        src: '/static/video/pl.swf',
		        width: 480,
		        height: 360,
		        flashvars: { way: flv, width: 480, height:360 }
		  },
		  { version: 9 }
		);
		
		//$('#V_cont div.cont').html('<img src = "/static/img/products/fishdom/social/test.jpg" width="480" height="360" />');
		
	};
					
function closeVideo() {
		if ($("#flash-custom").length) {
			$("#flash-custom div").show();	
		}
		 $overlay.hide();
		 $bg.hide();
		 $window.unbind('scroll');
		 $(document).unbind('keydown');
		 $('#V_cont  div.cont').empty();
	}  						

			
function ShowSystReq() {
	if ($('#sys-req').css('display') == 'none') {
		$('#sys-req').css('display', 'block');
    }
    else {
    	$('#sys-req').css('display', 'none');
    }
    return false;
}
			
function openAskEmail(platform, filename) {
	pageTracker._trackPageview('/actions/product/download/'+filename+'/'+platform);
	pageTracker._trackEvent('games', 'download', filename);
	setTimeout(function() {
		location.href = '/download/'+platform+'/'+filename;
	}, 2000);
}

/**
 * Отображает окно для отправки e-mail
 */
function toggleFriendEmail(action) {
	switch (action) {
		case 'show':
			$("#tell-a-friend div.send").removeClass("sent");
			$("#tell-a-friend").modal({
				opacity: 80,
				overlayClose: true
			});
			$("#tell-a-friend input[name='from-name']").focus();
			break;
		case 'hide':
			clearFriendEmail();
			$.modal.close();
			break;
	}
	return false;
}

/**
 * Отправляет e-mail пользователю.
 */
function sendFriendEmail() {
	pageTracker._trackPageview('/actions/product/tell-a-friend/send');
	var elBlock = $("#tell-a-friend");
	
	// Проверяем имя
	var name = $("input[name='from-name']", elBlock).val();
	if (name == "") {
		errorFriendEmail('from-name');
		return true;
	}
	
	// Проверяем email
	var email = $("input[name='from-email']", elBlock).val();
	if (!checkEmail(email)) {
		errorFriendEmail('from-email');
		return true;
	}
	
	// Проверяем список друзей
	var friends = $("input[name='to-email']", elBlock).val();
	var emails = friends.split(",");
	for (id in emails) {
		if (!checkEmail(emails[id])) {
			errorFriendEmail('to-email');
			return true;
		}
	}
	
	// Проверяем сообщение
	var message = $("textarea[name='message']", elBlock).val();
	if (message == "") {
		errorFriendEmail('message');
		return true;
	}
	
	// Проверяем код
	var code = $("input[name='code']", elBlock).val();
	if (code == "") {
		errorFriendEmail('code');
		return true;
	}
	
	var product = $("input[name='product']", elBlock).val();
	var platform = $("input[name='platform']", elBlock).val();
	$(":input", elBlock).attr("disabled", "disabled");
	
	// Отправляем запрос
	$.ajax({
		type: 'POST',
		url: '/data/tell-a-friend.php',
		data: {
			name: name,
			email: email,
			friends: friends,
			message: message,
			code: code,
			product: product,
			platform: platform
		},
		dataType: 'json',
		success: function(data) {
			$("#tell-a-friend :input").removeAttr("disabled");
			if (data.status == 'error') {
				pageTracker._trackPageview('/actions/product/tell-a-friend/error/'+data.position);
				errorFriendEmail(data.position);
			} else {
				pageTracker._trackPageview('/actions/product/tell-a-friend/ok');
				$("#tell-a-friend div.send").addClass("sent");
				clearFriendEmail();
				setTimeout(function() {
					toggleFriendEmail('hide');
				}, 3000);
			}
		},
		error: function(obj, error, data) {
			$("#tell-a-friend :input").removeAttr("disabled");
			errorFriendEmail('default');
		}
	});
}

/**
 * Выводит сообщение об ошибке при отправке e-mail
 */
function errorFriendEmail(position) {
	// Опреляем позицию для сообщения
	switch (position) {
		case 'from-name':
			var left = 416;
			var top = 104;
			var message = 'Please enter your name';
			break;
		case 'from-email':
			var left = 416;
			var top = 131;
			var message = 'Please make sure your e-mail is correct';
			break;
		case 'to-email':
			var left = 400;
			var top = 173;
			var message = 'Please make sure the e-mail is correct';
			break;
		case 'message':
			var left = 456;
			var top = 362;
			var message = 'Please enter your message';
			break;
		case 'code':
			var left = 296;
			var top = 548;
			var message = 'The characters you entered do not match the image';
			break;
		default:
			var left = 182;
			var top = 558;
			var message = 'Please make sure your information is corerct';
	}
	
	var elError = $("#tell-a-friend div.error");
	$("p", elError).html(message);
	elError.css({
		'left': left+'px',
		'top': top+'px'
	}).show();
}

/**
 * Очищает поля в форме отправки email
 */
function clearFriendEmail() {
	$("#tell-a-friend form")[0].reset();
}

/**
 * Обновляет каптчу
 */
function refreshCode() {
	var rand = Math.floor(Math.random()*1000000);
	$("#tell-a-friend img.code").attr("src", "/data/friend-code.jpg?r="+rand);
}

/**
 * Подписывается на рассылку iPhone
 */
function subscribeIphone() {
	var email = $("#iphone-subscribe input[name='email']");
	var status = $("#iphone-subscribe span.status");
	
	if (email.val() == '') {
		status.html('Please enter your e-mail address').addClass('error');
		return;
	}
	if (!checkEmail(email.val())) {
		status.html('Please make sure your e-mail is correct').addClass('error');
		return;
	}
	
	$.getJSON(
		'/data/phplist.php?action=subscribe', 
		{
			email: email.val(), 
			list: 'iphone', 
			confirm: true
		},
		function(data) {
			if (data.status != 'ok') {
				status.html('Please make sure your e-mail is correct').addClass('error');
			} else {
				$("#iphone-subscribe h3").html('Thank you!');
				$("#iphone-subscribe span.status").hide();
				$("#iphone-subscribe span.input").hide();
				$("#iphone-subscribe input.join").hide();
				$("#iphone-subscribe span.thanks").css('display', 'block');
				$("#iphone-subscribe p").addClass('ok');
				$("#iphone-subscribe div.round-block-bottom").removeClass('round-block-bottom').addClass('round-block-bottom-ok');
			}
		}
	);
}

/**
 * Facebook Share
 */
function fbs_click() {
	u = location.protocol+'//'+location.host+location.pathname+'?ref=fb';
	t = document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
} 
